-
Notifications
You must be signed in to change notification settings - Fork 3
/
certificates_of_deposit_operations.go
122 lines (104 loc) · 4.13 KB
/
certificates_of_deposit_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) getAllCertificatesOfDepositOperations(c echo.Context) error {
log.Debug("[API] Retrieving all certificates of deposit operations")
result, err := s.db.GetAll(wallet.CertificateOfDeposit{})
if err != nil {
errMsg := fmt.Sprintf("Error on retrieve certificates of deposit operations: %v", err)
return logAndReturnError(c, errMsg)
}
return c.JSON(http.StatusOK, result)
}
// getCertificateOfDepositOperationByID godoc
// @Summary Get get certificate of deposit operation by ID
// @Description get certificate of deposit operation data
// @Accept json
// @Produce json
// @Success 200 {object} wallet.CertificateOfDeposit
// @Failure 404 {object} api.ErrorMessage
// @Failure 500 {object} api.ErrorMessage
// @Router /certificates-of-deposit/operations/{id} [get]
// @Param id path string true "Operation id"
func (s *server) getCertificateOfDepositOperationByID(c echo.Context) error {
id := c.Param("id")
log.Debugf("[API] Retrieving certificate of deposit operation with id: %s", id)
result := &wallet.CertificateOfDeposit{}
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("Certificate of deposit operation '%s' not found", id)
return c.JSON(http.StatusNotFound, errorMessage(errMsg))
}
return c.JSON(http.StatusOK, result)
}
// insertCertificateOfDepositOperation godoc
// @Summary Insert some certificate of deposit operation
// @Description insert new certificate of deposit operation
// @Accept json
// @Produce json
// @Success 200 {object} interface{}
// @Failure 422 {object} api.ErrorMessage
// @Failure 500 {object} api.ErrorMessage
// @Router /certificates-of-deposit/operations [post]
func (s *server) insertCertificateOfDepositOperation(c echo.Context) error {
log.Debugf("[API] Inserting certificate of deposit operation")
data := wallet.NewCertificateOfDeposit()
if err := c.Bind(data); err != nil {
errMsg := fmt.Sprintf("Error on bind certificate of deposit: %v", err)
return logAndReturnError(c, errMsg)
}
if err := c.Validate(data); err != nil {
errMsg := fmt.Sprintf("Error on validate certificate of deposit: %v", err)
return c.JSON(http.StatusUnprocessableEntity, errorMessage(errMsg))
}
result, err := s.db.Create(data)
if err != nil {
errMsg := fmt.Sprintf("Error on insert certificate of deposit: %v", err)
return logAndReturnError(c, errMsg)
}
return c.JSON(http.StatusOK, result)
}
// updateCertificateOfDepositOperationByID godoc
// @Summary Update some certificate of deposit operation
// @Description update new certificate of deposit 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 /certificates-of-deposit/operations/{id} [put]
// @Param id path string true "Operation id"
func (s *server) updateCertificateOfDepositOperationByID(c echo.Context) error {
id := c.Param("id")
log.Debugf("[API] Updating certificate of deposit operation with id %s", id)
data := wallet.NewCertificateOfDeposit()
if err := c.Bind(data); err != nil {
errMsg := fmt.Sprintf("Error on bind certificate of deposit: %v", err)
return logAndReturnError(c, errMsg)
}
if err := c.Validate(data); err != nil {
errMsg := fmt.Sprintf("Error on validate certificate of deposit: %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 certificate of deposit: %v", err)
return logAndReturnError(c, errMsg)
}
if result.MatchedCount != 0 {
return c.JSON(http.StatusOK, result)
}
errMsg := fmt.Sprintf("Certificate of deposit operation '%s' not found", id)
return c.JSON(http.StatusNotFound, errorMessage(errMsg))
}