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