/
forum.go
87 lines (72 loc) · 2.12 KB
/
forum.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
package controllers
import (
"context"
"github.com/labstack/echo/v4"
"github.com/rinatkh/db_forum/internal/model/dto"
"github.com/rinatkh/db_forum/internal/service"
"github.com/sirupsen/logrus"
)
type ForumController struct {
log *logrus.Entry
registry *service.Registry
}
func (c *ForumController) CreateForum(ctx echo.Context) error {
request := new(dto.CreateForumRequest)
if err := ctx.Bind(request); err != nil {
c.log.Errorf("Bind error: %s", err)
return err
}
response, err := c.registry.ForumService.CreateForum(context.Background(), request)
if err != nil {
return err
}
return ctx.JSON(response.Code, response.Data)
}
func (c *ForumController) GetForumUsers(ctx echo.Context) error {
request := new(dto.GetForumUsersRequest)
if err := ctx.Bind(request); err != nil {
c.log.Errorf("Bind error: %s", err)
return err
}
request.Slug = ctx.Param("slug")
if request.Limit < -1 || request.Limit == 0 {
request.Limit = 100
}
response, err := c.registry.ForumService.GetForumUsers(context.Background(), request)
if err != nil {
return err
}
return ctx.JSON(response.Code, response.Data)
}
func (c *ForumController) GetForum(ctx echo.Context) error {
request := new(dto.GetForumRequest)
if err := ctx.Bind(request); err != nil {
c.log.Errorf("Bind error: %s", err)
return err
}
request.Slug = ctx.Param("slug")
response, err := c.registry.ForumService.GetForum(context.Background(), request)
if err != nil {
return err
}
return ctx.JSON(response.Code, response.Data)
}
func (c *ForumController) GetForumThreads(ctx echo.Context) error {
request := new(dto.GetForumThreadsRequest)
if err := ctx.Bind(request); err != nil {
c.log.Errorf("Bind error: %s", err)
return err
}
request.Slug = ctx.Param("slug")
if request.Limit < -1 || request.Limit == 0 {
request.Limit = 100
}
response, err := c.registry.ForumService.GetForumThreads(context.Background(), request)
if err != nil {
return err
}
return ctx.JSON(response.Code, response.Data)
}
func NewForumController(log *logrus.Entry, registry *service.Registry) *ForumController {
return &ForumController{log: log, registry: registry}
}