-
Notifications
You must be signed in to change notification settings - Fork 0
/
task_controller.go
126 lines (103 loc) · 3.35 KB
/
task_controller.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package controller
import (
"github.com/minipkg/ozzo_routing"
"github.com/pkg/errors"
"github.com/yaruz/app/internal/pkg/apperror"
"github.com/minipkg/log"
"github.com/minipkg/ozzo_routing/errorshandler"
"github.com/minipkg/pagination"
"github.com/yaruz/app/internal/domain/task"
routing "github.com/go-ozzo/ozzo-routing/v2"
)
type modelController struct {
Logger log.Logger
Service task.IService
}
// RegisterHandlers sets up the routing of the HTTP handlers.
//
// GET /api/tasks/ - список всех моделей
// GET /api/task/{ID} - детали модели
func RegisterTaskHandlers(r *routing.RouteGroup, logger log.Logger, service task.IService) {
c := modelController{
Logger: logger,
Service: service,
}
r.Get("/tasks", c.list)
r.Get("/tasksp", c.listp) // try with pagination
r.Get(`/task/<id>`, c.get)
}
// get method is for getting a one entity by ID
func (c modelController) get(ctx *routing.Context) error {
id, err := ozzo_routing.ParseUintParam(ctx, "id")
if err != nil {
return errorshandler.BadRequest("ID is required to be uint")
}
entity, err := c.Service.Get(ctx.Request.Context(), id)
if err != nil {
if err == apperror.ErrNotFound {
c.Logger.With(ctx.Request.Context()).Info(err)
return errorshandler.NotFound("not found")
}
c.Logger.With(ctx.Request.Context()).Error(err)
return errorshandler.InternalServerError("")
}
return ctx.Write(entity)
}
// list method is for a getting a list of all entities
func (c modelController) list(ctx *routing.Context) error {
/*where := c.Service.NewEntity()
err := ozzo_routing.ParseQueryParamsIntoStruct(ctx, where)
if err != nil {
errors.Wrapf(apperror.ErrBadRequest, err.Error())
}
cond := selection_condition.SelectionCondition{
Where: where,
}*/
st := c.Service.NewEntity()
cond, err := ozzo_routing.ParseQueryParams(ctx, st)
if err != nil {
errors.Wrapf(apperror.ErrBadRequest, err.Error())
}
items, err := c.Service.Query(ctx.Request.Context(), cond)
if err != nil {
if err == apperror.ErrNotFound {
c.Logger.With(ctx.Request.Context()).Info(err)
return errorshandler.NotFound("")
}
c.Logger.With(ctx.Request.Context()).Error(err)
return errorshandler.InternalServerError("")
}
return ctx.Write(items)
}
// list method is for a getting a list of all entities
func (c modelController) listp(ctx *routing.Context) error {
struc := c.Service.NewEntity()
cond, err := ozzo_routing.ParseQueryParams(ctx, struc)
if err != nil {
errors.Wrapf(apperror.ErrBadRequest, err.Error())
}
count, err := c.Service.Count(ctx.Request.Context(), cond)
if err != nil {
if err == apperror.ErrNotFound {
c.Logger.With(ctx.Request.Context()).Info(err)
return errorshandler.NotFound("")
}
c.Logger.With(ctx.Request.Context()).Error(err)
return errorshandler.InternalServerError("")
}
pages := pagination.NewFromRequest(ctx.Request, int(count))
cond.Limit = uint(pages.Limit())
cond.Offset = uint(pages.Offset())
ctx.Response.Header().Add("pages", pages.BuildLinkHeader("/modelsp", pages.PerPage))
items, err := c.Service.Query(ctx.Request.Context(), cond)
if err != nil {
if err == apperror.ErrNotFound {
c.Logger.With(ctx.Request.Context()).Info(err)
return errorshandler.NotFound("")
}
c.Logger.With(ctx.Request.Context()).Error(err)
return errorshandler.InternalServerError("")
}
pages.Items = items
return ctx.Write(pages)
}