This repository has been archived by the owner on Jul 19, 2020. It is now read-only.
/
locale_cntrl.go
111 lines (101 loc) · 3.23 KB
/
locale_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
package controller
import (
"fmt"
"net/http"
"strconv"
"github.com/labstack/echo"
"github.com/hotstone-seo/hotstone-server/app/repository"
"github.com/hotstone-seo/hotstone-server/app/service"
"go.uber.org/dig"
"gopkg.in/go-playground/validator.v9"
)
// LocaleCntrl is controller to locale entity
type LocaleCntrl struct {
dig.In
service.LocaleService
}
// Route to define API Route
func (c *LocaleCntrl) Route(e *echo.Echo) {
e.GET("locales", c.Find)
e.POST("locales", c.Create)
e.GET("locales/:id", c.FindOne)
e.PUT("locales", c.Update)
e.DELETE("locales/:id", c.Delete)
}
// Create locale
func (c *LocaleCntrl) Create(ctx echo.Context) (err error) {
var locale repository.Locale
var lastInsertID int64
ctx0 := ctx.Request().Context()
if err = ctx.Bind(&locale); err != nil {
return err
}
if err = validator.New().Struct(locale); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
if lastInsertID, err = c.LocaleService.Insert(ctx0, locale); err != nil {
return echo.NewHTTPError(http.StatusUnprocessableEntity, err.Error())
}
return ctx.JSON(http.StatusCreated, GeneralResponse{
Message: fmt.Sprintf("Success insert new locale #%d", lastInsertID),
})
}
// Find of locale
func (c *LocaleCntrl) Find(ctx echo.Context) (err error) {
var locales []*repository.Locale
ctx0 := ctx.Request().Context()
if locales, err = c.LocaleService.Find(ctx0); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return ctx.JSON(http.StatusOK, locales)
}
// FindOne locale
func (c *LocaleCntrl) FindOne(ctx echo.Context) (err error) {
var id int64
var locale *repository.Locale
ctx0 := ctx.Request().Context()
if id, err = strconv.ParseInt(ctx.Param("id"), 10, 64); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID")
}
if locale, err = c.LocaleService.FindOne(ctx0, id); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
if locale == nil {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Locale#%d not found", id))
}
return ctx.JSON(http.StatusOK, locale)
}
// Delete locale
func (c *LocaleCntrl) Delete(ctx echo.Context) (err error) {
var id int64
ctx0 := ctx.Request().Context()
if id, err = strconv.ParseInt(ctx.Param("id"), 10, 64); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID")
}
if err = c.LocaleService.Delete(ctx0, id); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return ctx.JSON(http.StatusOK, GeneralResponse{
Message: fmt.Sprintf("Success delete locale #%d", id),
})
}
// Update locale
func (c *LocaleCntrl) Update(ctx echo.Context) (err error) {
var locale repository.Locale
ctx0 := ctx.Request().Context()
if err = ctx.Bind(&locale); err != nil {
return err
}
if locale.ID <= 0 {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID")
}
if err = validator.New().Struct(locale); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
if err = c.LocaleService.Update(ctx0, locale); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return ctx.JSON(http.StatusOK, GeneralResponse{
Message: fmt.Sprintf("Success update locale #%d", locale.ID),
})
}