-
Notifications
You must be signed in to change notification settings - Fork 0
/
order_handler.go
50 lines (38 loc) · 931 Bytes
/
order_handler.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
package handler
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/sandronister/mongo-go/internal/usecase"
)
type OrderHandler struct {
orderUseCase *usecase.OrderUseCase
}
func NewOrderHandler(orderUseCase *usecase.OrderUseCase) *OrderHandler {
return &OrderHandler{
orderUseCase: orderUseCase,
}
}
func (o *OrderHandler) Create(c *gin.Context) {
var orderDTO usecase.OrderInputDTO
if err := c.ShouldBindJSON(&orderDTO); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error()})
return
}
output, err := o.orderUseCase.Save(orderDTO)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error()})
return
}
c.JSON(http.StatusCreated, output)
}
func (o *OrderHandler) List(c *gin.Context) {
output, err := o.orderUseCase.List()
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error()})
return
}
c.JSON(http.StatusOK, output)
}