72 lines
2.1 KiB
Go
72 lines
2.1 KiB
Go
package holiday
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/csv"
|
|
"encoding/xml"
|
|
"fmt"
|
|
"github.com/google/uuid"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
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"`
|
|
}
|
|
|
|
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()
|
|
}
|
|
|
|
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"`
|
|
}
|
|
|
|
func ToResponse(holidays []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,
|
|
}
|
|
}
|