Newer
Older
package models
import (
"encoding/json"
"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"
type Base struct {
ID string `gorm:"primary_key;"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `sql:"index"`
}
func (b *Base) BeforeCreate(tx *gorm.DB) (err error) {
b.ID = uuid.NewString()
return
}
type SgeConsent struct {
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
Address string `json:"address"`
PostalCode string `json:"postalCode"`
City string `json:"city"`
SafetyBoarding bool `json:"safetyOnBoarding"`
InseeCode string `json:"inseeCode"`
EndDate time.Time `json:"endDate"`
ServiceID int `json:"serviceID,omitempty"`
// This type is only used for Swagger documentation
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"`
Address string `json:"address"`
PointID int `json:"pointID"`
PostalCode string `json:"postalCode"`
City string `json:"city"`
SafetyBoarding bool `json:"safetyOnBoarding"`
InseeCode string `json:"inseeCode"`
EndDate time.Time `json:"endDate"`
ServiceID int `json:"serviceID,omitempty"`
}
ServiceID int `json:"serviceID"`
}
// @Summary Get details of a specific consent
// @Description Get details of a specific consent
// @Tags consent
// @Produce json
// @Failure 404 {string} string "Not found"
// @Param id path int true "ID of the consent"
// @Router /api/sge/consent/{id} [get]
func (dh *DataHandler) GetSgeConsentById(w http.ResponseWriter, r *http.Request) {
id, err := common.IdFromRequest(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
err = dh.sqlClient.First(&consent, "id = ?", id).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
http.Error(w, "consent not found", http.StatusNotFound)
return
}
http.Error(w, "error while finding consent", http.StatusInternalServerError)
w.Header().Set(constants.ContentType, constants.Json)
json.NewEncoder(w).Encode(consent)
log.Printf("| get consent | name : %v | %v", consent.Lastname, r.RemoteAddr)
}
// @Summary Create a new consent
// @Description Create a new consent
// @Tags consent
// @Produce json
// @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/sge/consent [post]
func (dh *DataHandler) PostSgeConsent(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)
err := decoder.Decode(&consent)
if err != nil {
http.Error(w, "couldn't parse body", http.StatusInternalServerError)
log.Println(err.Error())
return
}
consent.EndDate = time.Now().AddDate(1, 0, 0)
// 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)
}
// @Summary Update a consent, giving it a serviceID
// @Description Update a consent, giving it a serviceID
// @Tags consent
// @Produce json
// @Failure 400 {string} string "invalid service id"
// @Failure 404 {string} string "couldn't find consent"
// @Failure 500 {string} string "couldn't parse body"
// @Param id body UpdateConsentBody true "service ID"
// @Router /api/sge/consent [put]
func (dh *DataHandler) UpdateSgeConsent(w http.ResponseWriter, r *http.Request) {
if r.Body == http.NoBody {
http.Error(w, "request body is empty", http.StatusBadRequest)
return
}
id, err := common.IdFromRequest(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Find consent
err = dh.sqlClient.First(&consent, "id = ?", id).Error
http.Error(w, "couldn't find consent", http.StatusNotFound)
log.Println(err.Error())
return
}
// Get service ID
decoder := json.NewDecoder(r.Body)
err = decoder.Decode(&body)
if err != nil {
http.Error(w, "couldn't parse body", http.StatusInternalServerError)
return
}
if body.ServiceID <= 0 {
http.Error(w, "invalid service id", http.StatusBadRequest)
return
}
// Update and save consent
consent.ServiceID = body.ServiceID
err = dh.sqlClient.Save(&consent).Error
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Println(err.Error())
return
}
w.Header().Set(constants.ContentType, constants.Json)
json.NewEncoder(w).Encode(consent)
log.Printf("| updated consent | name : %v | serviceID : %v | %v", consent.Lastname, consent.ServiceID, r.RemoteAddr)
}
// @Summary Delete a specific consent
// @Description Delete a specific consent
// @Tags consent
// @Produce json
// @Failure 404 {string} string "Not found"
// @Failure 500 {string} string "Not found"
// @Param id path int true "ID of the consent"
// @Router /api/sge/consent/{id} [delete]
func (dh *DataHandler) DeleteSgeConsentById(w http.ResponseWriter, r *http.Request) {
id, err := common.IdFromRequest(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
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)
// @Summary Search for consents
// @Description Search for consents based on the pointID
// @Tags consent
// @Produce json
// @Failure 400 {string} string "Not found"
// @Param search query string true "pointID to search"
// @Router /api/admin/sge/consent [get]
func (dh *DataHandler) SearchSgeConsent(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
}
dh.sqlClient.Unscoped().Model(SgeConsent{}).Where("point_id LIKE ?", "%"+search+"%").Count(&totalRows)
dh.sqlClient.Unscoped().Order("created_at desc").Offset(offset).Limit(limit).Where("point_id LIKE ?", "%"+search+"%").Find(&consents)
TotalRows int64 `json:"totalRows"`
Rows []SgeConsent `json:"rows"`
}
pagination.TotalRows = totalRows
pagination.Rows = consents
w.Header().Set(constants.ContentType, constants.Json)
log.Printf("| get all consents | limit : %d | page : %d | %v", limit, page, r.RemoteAddr)