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