Skip to content
Snippets Groups Projects
customPopup.go 2.6 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
    
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    	"forge.grandlyon.com/web-et-numerique/factory/llle_project/backoffice-server/internal/constants"
    
    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"`
    
    	Image        string    `json:"image"`
    
    	Description  string    `json:"description"`
    	EndDate      time.Time `json:"endDate"`
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    }
    
    // GetCustomPopup godoc
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    //
    
    //	@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]
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    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
    	}
    
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    	w.Header().Set(constants.ContentType, constants.Json)
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    	json.NewEncoder(w).Encode(popupInfo)
    	log.Printf("| get customPopup | %v", r.RemoteAddr)
    }
    
    // SaveCustomPopup godoc
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    //
    
    //	@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]
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    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
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    	updatedCustomPopup.Title = customPopup.Title
    	updatedCustomPopup.Description = customPopup.Description
    
    	updatedCustomPopup.EndDate = customPopup.EndDate
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    
    	dh.sqlClient.Save(&updatedCustomPopup)
    
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    	w.Header().Set(constants.ContentType, constants.Json)
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    	json.NewEncoder(w).Encode(customPopup)
    	log.Printf("| updated customPopup | %v", r.RemoteAddr)
    }