-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrarity.go
95 lines (78 loc) · 2.7 KB
/
rarity.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"github.com/GwentAPI/gwentapi/app"
"github.com/GwentAPI/gwentapi/dataLayer/dal"
"github.com/GwentAPI/gwentapi/dataLayer/factory"
"github.com/GwentAPI/gwentapi/helpers"
"github.com/goadesign/goa"
"github.com/goadesign/goa/middleware"
"time"
)
// RarityController implements the rarity resource.
type RarityController struct {
*goa.Controller
}
// NewRarityController creates a rarity controller.
func NewRarityController(service *goa.Service) *RarityController {
return &RarityController{Controller: service.NewController("RarityController")}
}
// List runs the list action.
func (c *RarityController) List(ctx *app.ListRarityContext) error {
// RarityController_List: start_implement
dataStore := &dal.DataStore{}
dataStore.GetSession()
// Close the session
defer dataStore.Close()
dc := dal.NewDalRarity(dataStore)
rarities, err := dc.FetchAll()
if err != nil {
ctx.ResponseData.Service.LogError("InternalServerError", "req_id", middleware.ContextRequestID(ctx), "ctrl", "Rarity", "action", "List", ctx.RequestData.Request.Method, ctx.RequestData.Request.URL, "databaseError", err.Error())
return ctx.InternalServerError()
}
res := make(app.GwentapiRarityCollection, len(*rarities))
lastModified := time.Time{}
for i, rarity := range *rarities {
r, _ := factory.CreateRarity(&rarity)
if lastModified.Before(rarity.Last_Modified) {
lastModified = rarity.Last_Modified
}
res[i] = r
}
// RarityController_List: end_implement
helpers.LastModified(ctx.ResponseData, lastModified)
if ctx.IfModifiedSince != nil {
if !helpers.IsModified(*ctx.IfModifiedSince, lastModified) {
return ctx.NotModified()
}
}
return ctx.OK(res)
}
// Show runs the show action.
func (c *RarityController) Show(ctx *app.ShowRarityContext) error {
// RarityController_Show: start_implement
dataStore := &dal.DataStore{}
dataStore.GetSession()
// Close the session
defer dataStore.Close()
dc := dal.NewDalRarity(dataStore)
uuid, err := helpers.DecodeUUID(ctx.RarityID)
if err != nil {
return ctx.NotFound()
}
rarity, err := dc.Fetch(uuid)
if helpers.IsNotFoundError(err) {
return ctx.NotFound()
} else if err != nil {
ctx.ResponseData.Service.LogError("InternalServerError", "req_id", middleware.ContextRequestID(ctx), "ctrl", "Rarity", "action", "Show", ctx.RequestData.Request.Method, ctx.RequestData.Request.URL, "databaseError", err.Error())
return ctx.InternalServerError()
}
// RarityController_Show: end_implement
res, _ := factory.CreateRarity(rarity)
helpers.LastModified(ctx.ResponseData, rarity.Last_Modified)
if ctx.IfModifiedSince != nil {
if !helpers.IsModified(*ctx.IfModifiedSince, rarity.Last_Modified) {
return ctx.NotModified()
}
}
return ctx.OK(res)
}