holiday-api/handlers.go

118 lines
3.3 KiB
Go
Raw Normal View History

2023-06-20 14:10:46 +00:00
package main
import (
2023-12-31 12:35:29 +00:00
"bytes"
"encoding/csv"
2023-06-20 14:10:46 +00:00
"encoding/xml"
"fmt"
2023-06-23 17:31:02 +00:00
"github.com/gin-gonic/gin"
2023-06-20 14:10:46 +00:00
"github.com/google/uuid"
"holiday-api/holiday"
"net/http"
2023-12-31 12:35:29 +00:00
"strconv"
2023-06-20 14:10:46 +00:00
"time"
)
2023-08-05 19:34:09 +00:00
func getHolidays(service holiday.HolidayService) gin.HandlerFunc {
2023-06-23 17:31:02 +00:00
return func(c *gin.Context) {
paging := holiday.Paging{PageSize: 50}
if err := c.ShouldBindQuery(&paging); err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
2023-06-20 14:10:46 +00:00
}
2023-06-23 17:31:02 +00:00
search := holiday.Search{}
if err := c.ShouldBindQuery(&search); err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
2023-06-20 14:10:46 +00:00
}
2023-06-23 17:31:02 +00:00
2023-06-20 14:10:46 +00:00
holidays, err := service.Find(search, paging)
if err != nil {
2023-12-31 12:35:29 +00:00
render(c, http.StatusNotFound, ErrorResponse{Created: time.Now(), Message: "failed fetching holidays"}, nil)
2023-06-20 14:10:46 +00:00
} else {
2023-12-31 12:35:29 +00:00
render(c, http.StatusOK, mapHolidays(holidays), search.Type)
2023-06-20 14:10:46 +00:00
}
}
}
type DateResponse struct{ time.Time }
func (d DateResponse) MarshalJSON() ([]byte, error) {
return []byte("\"" + d.String() + "\""), nil
}
func (d DateResponse) String() string {
return fmt.Sprintf(`%04d-%02d-%02d`, d.Year(), d.Month(), d.Day())
}
type ErrorResponse struct {
XMLName xml.Name `json:"-" xml:"Error"`
Created time.Time `json:"created" xml:"created"`
Message string `json:"message" xml:"message"`
Code int `json:"-" xml:"-"`
}
type HolidayResponse struct {
XMLName xml.Name `json:"-" xml:"Holidays"`
Holidays []HolidayItemResponse `json:"holidays"`
}
2023-12-31 12:35:29 +00:00
func (h HolidayResponse) CSV() []byte {
buffer := bytes.Buffer{}
csvWriter := csv.NewWriter(&buffer)
csvWriter.Write([]string{"id", "date", "name", "description", "isStateHoliday", "isReligiousHoliday"})
for _, item := range h.Holidays {
csvWriter.Write([]string{item.Id.String(), item.Date.String(), item.Name, item.Description, strconv.FormatBool(item.IsStateHoliday), strconv.FormatBool(item.IsReligiousHoliday)})
}
csvWriter.Flush()
return buffer.Bytes()
}
2023-06-20 14:10:46 +00:00
type HolidayItemResponse struct {
XMLName xml.Name `json:"-" xml:"Holiday"`
Id uuid.UUID `json:"id" xml:"id,attr"`
Date DateResponse `json:"date" xml:"date,attr"`
Name string `json:"name" xml:"name"`
Description string `json:"description" xml:"description,omitempty"`
IsStateHoliday bool `json:"isStateHoliday" xml:"isState,attr"`
IsReligiousHoliday bool `json:"isReligiousHoliday" xml:"isReligious,attr"`
}
type HolidaySingleResponse struct {
Id *uuid.UUID
Country string
Date DateResponse
Name string
Description string
IsStateHoliday bool
IsReligiousHoliday bool
}
type HolidaySingleRequest struct {
Id *uuid.UUID
Country string
Date DateResponse
Name string
Description string
IsStateHoliday bool
IsReligiousHoliday bool
}
func mapHolidays(holidays []holiday.Holiday) HolidayResponse {
var response = make([]HolidayItemResponse, 0, len(holidays))
for _, h := range holidays {
response = append(response, HolidayItemResponse{
Id: h.Id,
Date: DateResponse{h.Date},
Name: h.Name,
Description: h.Description,
IsStateHoliday: h.IsStateHoliday,
IsReligiousHoliday: h.IsReligiousHoliday,
})
}
return HolidayResponse{
Holidays: response,
}
}