-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcategory.go
94 lines (78 loc) · 2.77 KB
/
category.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
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"
)
// CategoryController implements the category resource.
type CategoryController struct {
*goa.Controller
}
// NewCategoryController creates a category controller.
func NewCategoryController(service *goa.Service) *CategoryController {
return &CategoryController{Controller: service.NewController("CategoryController")}
}
// List runs the list action.
func (c *CategoryController) List(ctx *app.ListCategoryContext) error {
// CategoryController_List: start_implement
dataStore := &dal.DataStore{}
dataStore.GetSession()
// Close the session
defer dataStore.Close()
dc := dal.NewDalCategory(dataStore)
categories, err := dc.FetchAll()
if err != nil {
ctx.ResponseData.Service.LogError("InternalServerError", "req_id", middleware.ContextRequestID(ctx), "ctrl", "Category", "action", "List", ctx.RequestData.Request.Method, ctx.RequestData.Request.URL, "databaseError", err.Error())
return ctx.InternalServerError()
}
res := make(app.GwentapiCategoryCollection, len(*categories))
lastModified := time.Time{}
for i, category := range *categories {
c, _ := factory.CreateCategory(&category)
if lastModified.Before(category.Last_Modified) {
lastModified = category.Last_Modified
}
res[i] = c
}
// CategoryController_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 *CategoryController) Show(ctx *app.ShowCategoryContext) error {
// CategoryController_Show: start_implement
dataStore := &dal.DataStore{}
dataStore.GetSession()
// Close the session
defer dataStore.Close()
dc := dal.NewDalCategory(dataStore)
uuid, err := helpers.DecodeUUID(ctx.CategoryID)
if err != nil {
return ctx.NotFound()
}
category, 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", "Category", "action", "Show", ctx.RequestData.Request.Method, ctx.RequestData.Request.URL, "databaseError", err.Error())
return ctx.InternalServerError()
}
// CategoryController_Show: end_implement
res, _ := factory.CreateCategory(category)
helpers.LastModified(ctx.ResponseData, category.Last_Modified)
if ctx.IfModifiedSince != nil {
if !helpers.IsModified(*ctx.IfModifiedSince, category.Last_Modified) {
return ctx.NotModified()
}
}
return ctx.OK(res)
}