Newer
Older
package models
import (
"encoding/json"
"errors"
"log"
"net/http"
"forge.grandlyon.com/web-et-numerique/factory/llle_project/backoffice-server/internal/constants"
ID uint `gorm:"<-:create"`
PopupEnabled bool `json:"popupEnabled"`
Title string `json:"title"`
Description string `json:"description"`
EndDate time.Time `json:"endDate"`
// @Summary Give status of custom popup
// @Description Give status of custom poup
// @Tags customPopup
// @Produce json
// @Success 200 {object} CustomPopup
// @Failure 404 {string} string "Not found"
// @Router /api/common/customPopup [get]
func (dh *DataHandler) GetCustomPopup(w http.ResponseWriter, r *http.Request) {
var popupInfo CustomPopup
err := dh.sqlClient.First(&popupInfo).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
http.Error(w, "custom popup status not found", http.StatusNotFound)
return
}
w.Header().Set(constants.ContentType, constants.Json)
json.NewEncoder(w).Encode(popupInfo)
log.Printf("| get customPopup | %v", r.RemoteAddr)
}
// SaveCustomPopup godoc
// @Summary Update custom popup content
// @Description Update custom popup content
// @Tags customPopup
// @Accept json
// @Produce json
// @Success 200 {object} CustomPopup "Updated successfully"
// @Failure 400 {string} string "Bad Request"
// @Failure 500 {string} string "Internal server error"
// @Param customPopup body CustomPopup true "CustomPopup to create/update with new content"
// @Router /api/animator/customPopup [put]
func (dh *DataHandler) SaveCustomPopup(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 customPopup CustomPopup
err := decoder.Decode(&customPopup)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
var updatedCustomPopup CustomPopup
err = dh.sqlClient.First(&updatedCustomPopup).Error
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
updatedCustomPopup.PopupEnabled = customPopup.PopupEnabled
updatedCustomPopup.Image = customPopup.Image
updatedCustomPopup.Title = customPopup.Title
updatedCustomPopup.Description = customPopup.Description
updatedCustomPopup.EndDate = customPopup.EndDate
w.Header().Set(constants.ContentType, constants.Json)
json.NewEncoder(w).Encode(customPopup)
log.Printf("| updated customPopup | %v", r.RemoteAddr)
}