Newer
Older
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package models
import (
"encoding/json"
"errors"
"log"
"net/http"
"time"
"forge.grandlyon.com/web-et-numerique/factory/llle_project/backoffice-server/internal/common"
"forge.grandlyon.com/web-et-numerique/factory/llle_project/backoffice-server/internal/constants"
"gorm.io/gorm"
)
type GrdfConsent struct {
Base
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
Pce string `json:"pce"`
PostalCode string `json:"postalCode"`
EndDate time.Time `json:"endDate"`
}
// This type is only used for Swagger documentation
type GrdfConsentSwagger struct {
ID int `json:"ID"`
CreatedAt time.Time `json:"CreatedAt"`
UpdatedAt time.Time `json:"UpdatedAt"`
DeletedAt time.Time `json:"DeletedAt"`
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
Pce string `json:"pce"`
PostalCode string `json:"postalCode"`
EndDate time.Time `json:"endDate"`
}
// GetConsentById godoc
//
// @Summary Get details of a specific consent
// @Description Get details of a specific consent
// @Tags consent
// @Produce json
// @Success 200 {object} GrdfConsentSwagger
// @Failure 404 {string} string "Not found"
// @Param id path int true "ID of the consent"
// @Router /api/grdf/consent/{id} [get]
func (dh *DataHandler) GetGrdfConsentById(w http.ResponseWriter, r *http.Request) {
id, err := common.IdFromRequest(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var consent GrdfConsent
err = dh.sqlClient.First(&consent, "id = ?", id).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
http.Error(w, "consent not found", http.StatusNotFound)
return
}
http.Error(w, "error while finding consent", http.StatusInternalServerError)
return
}
w.Header().Set(constants.ContentType, constants.Json)
json.NewEncoder(w).Encode(consent)
log.Printf("| get consent | name : %v | %v", consent.Lastname, r.RemoteAddr)
}
// PostConsent godoc
//
// @Summary Create a new consent
// @Description Create a new consent
// @Tags consent
// @Produce json
// @Success 201 {object} GrdfConsentSwagger
// @Failure 400 {string} string "Bad request"
// @Failure 500 {string} string "couldn't create consent"
// @Param id path int true "ID of the consent"
// @Router /api/grdf/consent [post]
func (dh *DataHandler) PostGrdfConsent(w http.ResponseWriter, r *http.Request) {
if r.Body == http.NoBody {
http.Error(w, "request body is empty", http.StatusBadRequest)
return
}
decoder := json.NewDecoder(r.Body)
var consent GrdfConsent
err := decoder.Decode(&consent)
if err != nil {
http.Error(w, "couldn't parse body", http.StatusInternalServerError)
log.Println(err.Error())
return
}
// Create a consent in SQL
err = dh.sqlClient.Create(&consent).Error
if err != nil {
http.Error(w, "couldn't create consent", http.StatusInternalServerError)
log.Println(err.Error())
return
}
w.Header().Set(constants.ContentType, constants.Json)
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(consent)
log.Printf("| new consent | name : %v | %v", consent.Lastname, r.RemoteAddr)
}
// DeleteConsentById godoc
//
// @Summary Delete a specific consent
// @Description Delete a specific consent
// @Tags consent
// @Produce json
// @Success 200 {object} GrdfConsentSwagger
// @Failure 404 {string} string "Not found"
// @Failure 500 {string} string "Not found"
// @Param id path int true "ID of the consent"
// @Router /api/grdf/consent/{id} [delete]
func (dh *DataHandler) DeleteGrdfConsentById(w http.ResponseWriter, r *http.Request) {
id, err := common.IdFromRequest(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var consent = GrdfConsent{}
err = dh.sqlClient.First(&consent, "id = ?", id).Error
if err != nil {
http.Error(w, "couldn't find consent", http.StatusInternalServerError)
log.Println(err.Error())
return
}
// Update and save consent in MySQL
consent.EndDate = time.Now()
err = dh.sqlClient.Save(&consent).Error
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Println(err.Error())
return
}
dh.sqlClient.Delete(&consent)
log.Printf("| deleted consent | id : %v | %v", id, r.RemoteAddr)
}
// SearchConsent godoc
//
// @Summary Search for consents
// @Description Search for consents based on the pointID
// @Tags consent
// @Produce json
// @Success 200 {array} GrdfConsentSwagger
// @Failure 400 {string} string "Not found"
// @Param search query string true "pointID to search"
// @Router /api/admin/consent [get]
func (dh *DataHandler) SearchGrdfConsent(w http.ResponseWriter, r *http.Request) {
search := r.URL.Query().Get("search")
page, limit, err := common.PageLimitFromRequest(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var totalRows int64
dh.sqlClient.Unscoped().Model(GrdfConsent{}).Where("pce LIKE ?", "%"+search+"%").Count(&totalRows)
offset := page * limit
var consents []GrdfConsent
dh.sqlClient.Unscoped().Order("created_at desc").Offset(offset).Limit(limit).Where("pce LIKE ?", "%"+search+"%").Find(&consents)
var pagination struct {
TotalRows int64 `json:"totalRows"`
Rows []GrdfConsent `json:"rows"`
}
pagination.TotalRows = totalRows
pagination.Rows = consents
w.Header().Set(constants.ContentType, constants.Json)
json.NewEncoder(w).Encode(pagination)
log.Printf("| get all consents | limit : %d | page : %d | %v", limit, page, r.RemoteAddr)
}