Skip to content
Snippets Groups Projects
monthlyInfo.go 4.87 KiB
Newer Older
  • Learn to ignore specific revisions
  • package models
    
    import (
    	"encoding/json"
    	"log"
    	"net/http"
    
    
    	"forge.grandlyon.com/web-et-numerique/factory/llle_project/backoffice-server/internal/common"
    
    )
    
    type MonthlyInfo struct {
    	Year  int    `json:"year"`
    	Month int    `json:"month"`
    	Info  string `json:"info"`
    
    	Image string `json:"image"`
    
    }
    
    // GetAllMonthlyInfo godoc
    
    //	@Summary		List all monthlyInfo
    //	@Description	Get details of all monthlyInfo
    //	@Tags			monthlyInfo
    //	@Produce		json
    //	@Success		200	{array}	MonthlyInfo
    
    //	@Router			/api/animator/monthlyInfo [get]
    
    func (dh *DataHandler) GetAllMonthlyInfo(w http.ResponseWriter, r *http.Request) {
    	var monthlyInfo []MonthlyInfo
    
    	dh.sqlClient.Find(&monthlyInfo)
    
    
    	w.Header().Set("Content-Type", "application/json")
    	json.NewEncoder(w).Encode(monthlyInfo)
    	log.Printf("| get all monthlyInfo | %v", r.RemoteAddr)
    }
    
    // GetSingleMonthlyInfo godoc
    
    //	@Summary		Get details of a specific monthlyInfo
    //	@Description	Get details of a specific monthlyInfo
    //	@Tags			monthlyInfo
    //	@Produce		json
    //	@Success		200		{object}	MonthlyInfo
    //	@Failure		404		{string}	string	"Not found"
    //	@Param			year	path		int		true	"Year of the monthlyInfo"
    //	@Param			month	path		int		true	"Month of the monthlyInfo"
    
    //	@Router			/api/animator/monthlyInfo/{year}/{month} [get]
    
    func (dh *DataHandler) GetSingleMonthlyInfo(w http.ResponseWriter, r *http.Request) {
    	year, month, err := common.YearMonthFromRequest(r)
    	if err != nil {
    		http.Error(w, err.Error(), http.StatusBadRequest)
    
    	}
    
    	w.Header().Set("Content-Type", "application/json")
    
    	var monthlyInfo MonthlyInfo
    
    	err = dh.sqlClient.Where("year = ? AND month = ?", year, month).First(&monthlyInfo).Error
    
    	if err != nil {
    		// If not found, answer "not found"
    		w.WriteHeader(http.StatusNotFound)
    		return
    	}
    	json.NewEncoder(w).Encode(monthlyInfo)
    }
    
    // SaveMonthlyInfo godoc
    
    //	@Summary		Create/update a specific monthlyInfo' content
    //	@Description	Create/update a specific monthlyInfo' content
    //	@Tags			monthlyInfo
    //	@Accept			json
    //	@Produce		json
    //	@Success		200			{object}	MonthlyInfo	"Updated successfully"
    //	@Success		201			{object}	MonthlyInfo	"Created successfully"
    //	@Failure		400			{string}	string		"Bad Request"
    //	@Failure		500			{string}	string		"Internal server error"
    //	@Param			monthlyInfo	body		MonthlyInfo	true	"MonthlyInfo to create/update with new content"
    
    //	@Router			/api/animator/monthlyInfo [put]
    
    func (dh *DataHandler) SaveMonthlyInfo(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 monthlyInfo MonthlyInfo
    	err := decoder.Decode(&monthlyInfo)
    	if err != nil {
    		w.WriteHeader(http.StatusInternalServerError)
    		return
    	}
    
    	// Check if this monthlyInfo exists
    
    	err = dh.sqlClient.Where("year = ? AND month = ?", monthlyInfo.Year, monthlyInfo.Month).First(&MonthlyInfo{}).Error
    
    
    	if err != nil {
    		// Create a monthlyInfo
    
    		err = dh.sqlClient.Create(&MonthlyInfo{
    
    			Year:  monthlyInfo.Year,
    			Month: monthlyInfo.Month,
    			Info:  monthlyInfo.Info,
    			Image: monthlyInfo.Image,
    		}).Error
    
    		if err != nil {
    			w.WriteHeader(http.StatusInternalServerError)
    			return
    		}
    		w.WriteHeader(http.StatusCreated)
    		json.NewEncoder(w).Encode(monthlyInfo)
    
    		log.Printf("| new monthlyInfo | year : %d month : %d | %v", monthlyInfo.Year, monthlyInfo.Month, r.RemoteAddr)
    		return
    
    	} else {
    		// Update info
    
    		err = dh.sqlClient.Model(&MonthlyInfo{}).Where("year = ? AND month = ?", monthlyInfo.Year, monthlyInfo.Month).Update("info", monthlyInfo.Info).Error
    
    		if err != nil {
    			w.WriteHeader(http.StatusInternalServerError)
    			return
    		}
    
    
    		// Update info
    
    		err = dh.sqlClient.Model(&MonthlyInfo{}).Where("year = ? AND month = ?", monthlyInfo.Year, monthlyInfo.Month).Update("image", monthlyInfo.Image).Error
    
    		if err != nil {
    			w.WriteHeader(http.StatusInternalServerError)
    			return
    		}
    
    
    		json.NewEncoder(w).Encode(monthlyInfo)
    		log.Printf("| updated monthlyInfo | year : %d month : %d | %v", monthlyInfo.Year, monthlyInfo.Month, r.RemoteAddr)
    	}
    }
    
    // DeleteMonthlyInfo godoc
    
    //	@Summary		Delete a specific monthlyInfo
    //	@Description	Delete a specific monthlyInfo
    //	@Tags			monthlyInfo
    //	@Produce		json
    //	@Success		200		{string}	string	"successful delete"
    //	@Failure		404		{string}	string	"Not found"
    //	@Param			year	path		int		true	"Year of the monthlyInfo"
    //	@Param			month	path		int		true	"Month of the monthlyInfo"
    
    //	@Router			/api/animator/monthlyInfo/{year}/{month} [delete]
    
    func (dh *DataHandler) DeleteMonthlyInfo(w http.ResponseWriter, r *http.Request) {
    	year, month, err := common.YearMonthFromRequest(r)
    	if err != nil {
    		http.Error(w, err.Error(), http.StatusBadRequest)
    
    	err = dh.sqlClient.Where("year = ? AND month = ?", year, month).Delete(&MonthlyInfo{}).Error
    
    	if err != nil {
    		http.Error(w, err.Error(), http.StatusNotFound)
    		return
    	}
    
    	w.WriteHeader(http.StatusOK)
    	w.Write([]byte("successful delete"))
    }