-
Notifications
You must be signed in to change notification settings - Fork 3
/
ficfi_operations.go
122 lines (104 loc) · 3.58 KB
/
ficfi_operations.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
// Copyright (c) 2020, Marcelo Jorge Vieira (https://github.com/mfinancecombr)
// Licensed under the BSD 3-Clause License
package api
import (
"fmt"
"net/http"
"github.com/labstack/echo/v4"
"github.com/mfinancecombr/finance-wallet-api/wallet"
log "github.com/sirupsen/logrus"
)
func (s *server) getAllFICFIOperations(c echo.Context) error {
log.Debug("[API] Retrieving all FICFI operations")
result, err := s.db.GetAll(wallet.FICFI{})
if err != nil {
errMsg := fmt.Sprintf("Error on retrieve FICFI operations: %v", err)
return logAndReturnError(c, errMsg)
}
return c.JSON(http.StatusOK, result)
}
// getFICFIOperationByID godoc
// @Summary Get FICFI operation by ID
// @Description get FICFI operation data
// @Accept json
// @Produce json
// @Success 200 {object} wallet.FICFI
// @Failure 404 {object} api.ErrorMessage
// @Failure 500 {object} api.ErrorMessage
// @Router /ficfi/operations/{id} [get]
// @Param id path string true "Operation id"
func (s *server) getFICFIOperationByID(c echo.Context) error {
id := c.Param("id")
log.Debugf("[API] Retrieving FICFI operation with id: %s", id)
result := &wallet.FICFI{}
if err := s.db.Get(id, result); err != nil {
errMsg := fmt.Sprintf("Error on retrieve '%s' operations: %v", id, err)
return logAndReturnError(c, errMsg)
}
if result == nil {
errMsg := fmt.Sprintf("FICFI operation '%s' not found", id)
return c.JSON(http.StatusNotFound, errorMessage(errMsg))
}
return c.JSON(http.StatusOK, result)
}
// insertFICFIOperation godoc
// @Summary Insert some FICFI operation
// @Description insert new FICFI operation
// @Accept json
// @Produce json
// @Success 200 {object} interface{}
// @Failure 422 {object} api.ErrorMessage
// @Failure 500 {object} api.ErrorMessage
// @Router /ficfi/operations [post]
func (s *server) insertFICFIOperation(c echo.Context) error {
log.Debugf("[API] Inserting FICFI operation")
data := wallet.NewFICFI()
if err := c.Bind(data); err != nil {
errMsg := fmt.Sprintf("Error on bind FICFI: %v", err)
return logAndReturnError(c, errMsg)
}
if err := c.Validate(data); err != nil {
errMsg := fmt.Sprintf("Error on validate FICFI: %v", err)
return c.JSON(http.StatusUnprocessableEntity, errorMessage(errMsg))
}
result, err := s.db.Create(data)
if err != nil {
errMsg := fmt.Sprintf("Error on insert FICFI: %v", err)
return logAndReturnError(c, errMsg)
}
return c.JSON(http.StatusOK, result)
}
// updateFICFIOperationByID godoc
// @Summary Update some FICFI operation
// @Description update new FICFI operation
// @Accept json
// @Produce json
// @Success 200 {object} interface{}
// @Failure 404 {object} api.ErrorMessage
// @Failure 422 {object} api.ErrorMessage
// @Failure 500 {object} api.ErrorMessage
// @Router /ficfi/operations/{id} [put]
// @Param id path string true "Operation id"
func (s *server) updateFICFIOperationByID(c echo.Context) error {
id := c.Param("id")
log.Debugf("[API] Updating FICFI operation with id %s", id)
data := wallet.NewFICFI()
if err := c.Bind(data); err != nil {
errMsg := fmt.Sprintf("Error on bind FICFI: %v", err)
return logAndReturnError(c, errMsg)
}
if err := c.Validate(data); err != nil {
errMsg := fmt.Sprintf("Error on validate FICFI: %v", err)
return c.JSON(http.StatusUnprocessableEntity, errorMessage(errMsg))
}
result, err := s.db.Update(id, data)
if err != nil {
errMsg := fmt.Sprintf("Error on update FICFI: %v", err)
return logAndReturnError(c, errMsg)
}
if result.MatchedCount != 0 {
return c.JSON(http.StatusOK, result)
}
errMsg := fmt.Sprintf("FICFI operation '%s' not found", id)
return c.JSON(http.StatusNotFound, errorMessage(errMsg))
}