Skip to content
Snippets Groups Projects
customPopup.go 2.38 KiB
Newer Older
  • Learn to ignore specific revisions
  • Bastien DUMONT's avatar
    Bastien DUMONT committed
    package models
    
    import (
    	"encoding/json"
    	"errors"
    	"log"
    	"net/http"
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    
    	"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"`
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    }
    
    // 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
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    
    	dh.sqlClient.Save(&updatedCustomPopup)
    
    	w.Header().Set("Content-Type", "application/json")
    	json.NewEncoder(w).Encode(customPopup)
    	log.Printf("| updated customPopup | %v", r.RemoteAddr)
    }