This repository has been archived by the owner on May 21, 2024. It is now read-only.
/
song_cntrl.go
116 lines (105 loc) · 2.78 KB
/
song_cntrl.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
package controller
import (
"fmt"
"net/http"
"github.com/labstack/echo/v4"
"github.com/typical-go/typical-rest-server/internal/app/data_access/mysqldb"
"github.com/typical-go/typical-rest-server/internal/app/domain/mymusic/service"
"github.com/typical-go/typical-rest-server/pkg/cachekit"
"github.com/typical-go/typical-rest-server/pkg/echokit"
"go.uber.org/dig"
)
type (
// SongCntrl is controller to book entity
SongCntrl struct {
dig.In
Cache *cachekit.Store
Svc service.SongSvc
}
)
var _ echokit.Router = (*SongCntrl)(nil)
// SetRoute to define API Route
func (c *SongCntrl) SetRoute(e echokit.Server) {
e.GET("/songs", c.Find, c.Cache.Middleware)
e.GET("/songs/:id", c.FindOne, c.Cache.Middleware)
e.HEAD("/songs/:id", c.FindOne, c.Cache.Middleware)
e.POST("/songs", c.Create)
e.PUT("/songs/:id", c.Update)
e.PATCH("/songs/:id", c.Patch)
e.DELETE("/songs/:id", c.Delete)
}
// Create book
func (c *SongCntrl) Create(ec echo.Context) (err error) {
var book mysqldb.Song
if err = ec.Bind(&book); err != nil {
return err
}
ctx := ec.Request().Context()
newSong, err := c.Svc.Create(ctx, &book)
if err != nil {
return echokit.HTTPError(err)
}
ec.Response().Header().Set(echo.HeaderLocation, fmt.Sprintf("/songs/%d", newSong.ID))
return ec.JSON(http.StatusCreated, newSong)
}
// Find songs
func (c *SongCntrl) Find(ec echo.Context) (err error) {
var req service.FindReq
if err := ec.Bind(&req); err != nil {
return err
}
ctx := ec.Request().Context()
songs, err := c.Svc.Find(ctx, &req)
if err != nil {
return echokit.HTTPError(err)
}
return ec.JSON(http.StatusOK, songs)
}
// FindOne book
func (c *SongCntrl) FindOne(ec echo.Context) error {
ctx := ec.Request().Context()
id := ec.Param("id")
book, err := c.Svc.FindOne(ctx, id)
if err != nil {
return echokit.HTTPError(err)
}
return ec.JSON(http.StatusOK, book)
}
// Delete book
func (c *SongCntrl) Delete(ec echo.Context) (err error) {
if err = c.Svc.Delete(
ec.Request().Context(),
ec.Param("id"),
); err != nil {
return echokit.HTTPError(err)
}
return ec.NoContent(http.StatusNoContent)
}
// Update book
func (c *SongCntrl) Update(ec echo.Context) (err error) {
var book mysqldb.Song
if err = ec.Bind(&book); err != nil {
return err
}
ctx := ec.Request().Context()
paramID := ec.Param("id")
updatedSong, err := c.Svc.Update(ctx, paramID, &book)
if err != nil {
return echokit.HTTPError(err)
}
return ec.JSON(http.StatusOK, updatedSong)
}
// Patch book
func (c *SongCntrl) Patch(ec echo.Context) (err error) {
var book mysqldb.Song
if err = ec.Bind(&book); err != nil {
return err
}
ctx := ec.Request().Context()
paramID := ec.Param("id")
patchedSong, err := c.Svc.Patch(ctx, paramID, &book)
if err != nil {
return echokit.HTTPError(err)
}
return ec.JSON(http.StatusOK, patchedSong)
}