Newer
Older
package models
import (
"encoding/json"
"errors"
"log"
"net/http"
"gorm.io/gorm"
)
type CustomPopup struct {
ID uint `gorm:"<-:create"`
PopupEnabled bool `json:"popupEnabled"`
Title string `json:"title"`
Description string `json:"description"`
EndDate time.Time `json:"endDate"`
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
}
// GetCustomPopup godoc
// @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("Content-Type", "application/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/admin/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.Title = customPopup.Title
updatedCustomPopup.Description = customPopup.Description
updatedCustomPopup.EndDate = customPopup.EndDate
dh.sqlClient.Save(&updatedCustomPopup)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(customPopup)
log.Printf("| updated customPopup | %v", r.RemoteAddr)
}