Skip to content

Commit 56c8bec

Browse files
author
Dorian Stoll
committedMar 19, 2017
featured.go
1 parent bf004ff commit 56c8bec

File tree

4 files changed

+196
-0
lines changed

4 files changed

+196
-0
lines changed
 

‎src/SpaceDock/objects/featured.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
SpaceDock Backend
3+
API Backend for the SpaceDock infrastructure to host modfiles for various games
4+
5+
SpaceDock Backend is licensed under the Terms of the MIT License.
6+
Copyright (c) 2017 Dorian Stoll (StollD), RockyTV
7+
*/
8+
9+
package objects
10+
11+
import (
12+
"SpaceDock"
13+
)
14+
15+
type Featured struct {
16+
Model
17+
18+
Mod Mod `json:"-" spacedock:"lock"`
19+
ModID uint `json:"mod" spacedock:"lock"`
20+
}
21+
22+
func (s *Featured) AfterFind() {
23+
if SpaceDock.DBRecursion == 2 {
24+
return
25+
}
26+
isRoot := SpaceDock.DBRecursion == 0
27+
SpaceDock.DBRecursion += 1
28+
SpaceDock.Database.Model(s).Related(&(s.Mod), "Mod")
29+
if isRoot {
30+
SpaceDock.DBRecursion = 0
31+
}
32+
}
33+
34+
func NewFeatured(mod Mod) *Featured {
35+
f := &Featured{
36+
Mod: mod,
37+
ModID: mod.ID,
38+
}
39+
f.Meta = "{}"
40+
return f
41+
}

‎src/SpaceDock/objects/init.go

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import "SpaceDock"
1515
*/
1616
func init() {
1717
SpaceDock.CreateTable(&Ability{})
18+
SpaceDock.CreateTable(&Featured{})
1819
SpaceDock.CreateTable(&Game{})
1920
SpaceDock.CreateTable(&GameVersion{})
2021
SpaceDock.CreateTable(&Mod{})

‎src/SpaceDock/routes/featured.go

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
SpaceDock Backend
3+
API Backend for the SpaceDock infrastructure to host modfiles for various games
4+
5+
SpaceDock Backend is licensed under the Terms of the MIT License.
6+
Copyright (c) 2017 Dorian Stoll (StollD), RockyTV
7+
*/
8+
9+
package routes
10+
11+
import (
12+
"gopkg.in/kataras/iris.v6"
13+
"SpaceDock/objects"
14+
"SpaceDock"
15+
"SpaceDock/utils"
16+
"github.com/spf13/cast"
17+
"SpaceDock/middleware"
18+
)
19+
20+
/*
21+
Registers the routes for the featured section
22+
*/
23+
func FeaturedRegister() {
24+
Register(GET, "/api/mods/featured", list_featured)
25+
Register(GET, "/api/mods/featured/:gameshort", list_featured_game)
26+
Register(POST, "/api/mods/featured/:gameshort",
27+
middleware.NeedsPermission("mods-feature", true, "gameshort"),
28+
add_featured,
29+
)
30+
Register(DELETE, "/api/mods/featured/:gameshort",
31+
middleware.NeedsPermission("mods-feature", true, "gameshort"),
32+
remove_featured,
33+
)
34+
}
35+
36+
/*
37+
Path: /api/mods/featured
38+
Method: GET
39+
Description: Returns a list of featured mods.
40+
*/
41+
func list_featured(ctx *iris.Context) {
42+
var featured []objects.Featured
43+
SpaceDock.Database.Find(&featured)
44+
output := make([]map[string]interface{}, len(featured))
45+
for i,element := range featured {
46+
output[i] = utils.ToMap(element)
47+
}
48+
utils.WriteJSON(ctx, iris.StatusOK, iris.Map{"error": false, "count": len(output), "data": output})
49+
}
50+
51+
/*
52+
Path: /api/mods/featured/:gameshort
53+
Method: GET
54+
Description: Returns a list of featured mods for a specific game.
55+
*/
56+
func list_featured_game(ctx *iris.Context) {
57+
gameshort := ctx.GetString("gameshort")
58+
59+
// Check if the game exists
60+
game := &objects.Game{}
61+
SpaceDock.Database.Where("short = ?", gameshort).First(game)
62+
if game.Short != gameshort {
63+
utils.WriteJSON(ctx, iris.StatusNotFound, utils.Error("The gameshort is invalid.").Code(2125))
64+
return
65+
}
66+
67+
var featured []objects.Featured
68+
SpaceDock.Database.Find(&featured)
69+
output := []map[string]interface{}{}
70+
for _,element := range featured {
71+
if element.Mod.Game.Short == gameshort {
72+
output = append(output, utils.ToMap(element))
73+
}
74+
}
75+
utils.WriteJSON(ctx, iris.StatusOK, iris.Map{"error": false, "count": len(output), "data": output})
76+
}
77+
78+
/*
79+
Path: /api/mods/featured/:gameshort
80+
Method: POST
81+
Description: Features a mod for this game. Required fields: modid
82+
Abilities: mods-feature
83+
*/
84+
func add_featured(ctx *iris.Context) {
85+
// Get the mod
86+
gameshort := ctx.GetString("gameshort")
87+
modid := cast.ToUint(utils.GetJSON(ctx, "modid"))
88+
89+
// Get the mod
90+
mod := &objects.Mod{}
91+
SpaceDock.Database.Where("id = ?", modid).First(mod)
92+
if mod.ID != modid {
93+
utils.WriteJSON(ctx, iris.StatusNotFound, utils.Error("The modid is invalid.").Code(2130))
94+
return
95+
} else if mod.Game.Short != gameshort {
96+
utils.WriteJSON(ctx, iris.StatusBadRequest, utils.Error("The gameshort is invalid.").Code(2125))
97+
return
98+
} else if !mod.Published {
99+
utils.WriteJSON(ctx, iris.StatusBadRequest, utils.Error("The mod must be published first.").Code(3022))
100+
return
101+
}
102+
103+
// Check if the mod is already featured
104+
feature := &objects.Featured{}
105+
SpaceDock.Database.Where("mod_id = ?", modid).First(feature)
106+
if feature.ModID == modid {
107+
utils.WriteJSON(ctx, iris.StatusBadRequest, utils.Error("The mod is already featured").Code(3015))
108+
return
109+
}
110+
111+
// Everything is fine, lets feature the mod
112+
feature = objects.NewFeatured(*mod)
113+
SpaceDock.Database.Save(feature)
114+
utils.WriteJSON(ctx, iris.StatusOK, iris.Map{"error": false, "count": 1, "data": utils.ToMap(feature)})
115+
}
116+
117+
/*
118+
Path: /api/mods/featured/:gameshort
119+
Method: DELETE
120+
Description: Unfeatures a mod for this game. Required fields: modid
121+
Abilities: mods-feature
122+
*/
123+
func remove_featured(ctx *iris.Context) {
124+
// Get the mod
125+
gameshort := ctx.GetString("gameshort")
126+
modid := cast.ToUint(utils.GetJSON(ctx, "modid"))
127+
128+
// Get the mod
129+
mod := &objects.Mod{}
130+
SpaceDock.Database.Where("id = ?", modid).First(mod)
131+
if mod.ID != modid {
132+
utils.WriteJSON(ctx, iris.StatusNotFound, utils.Error("The modid is invalid.").Code(2130))
133+
return
134+
} else if mod.Game.Short != gameshort {
135+
utils.WriteJSON(ctx, iris.StatusBadRequest, utils.Error("The gameshort is invalid.").Code(2125))
136+
return
137+
} else if !mod.Published {
138+
utils.WriteJSON(ctx, iris.StatusBadRequest, utils.Error("The mod must be published first.").Code(3022))
139+
return
140+
}
141+
142+
// Check if the mod is already featured
143+
feature := &objects.Featured{}
144+
SpaceDock.Database.Where("mod_id = ?", modid).First(feature)
145+
if feature.ModID != modid {
146+
utils.WriteJSON(ctx, iris.StatusBadRequest, utils.Error("This mod isn't featured.").Code(3015))
147+
return
148+
}
149+
150+
// Everything is fine, lets remove the feature
151+
SpaceDock.Database.Delete(feature)
152+
utils.WriteJSON(ctx, iris.StatusOK, iris.Map{"error": false})
153+
}

‎src/SpaceDock/routes/init.go

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func init() {
2929
AccessRegister()
3030
AccountsRegister()
3131
AdminRegister()
32+
FeaturedRegister()
3233
GameRegister()
3334
GeneralRegister()
3435
PublisherRegister()

0 commit comments

Comments
 (0)
Please sign in to comment.