2023-06-20 14:10:46 +00:00
package holiday
import (
"github.com/google/uuid"
"github.com/jmoiron/sqlx"
"strconv"
"strings"
"time"
)
2023-08-05 19:34:09 +00:00
type HolidayService struct {
2023-06-20 14:10:46 +00:00
DB * sqlx . DB
}
2023-08-05 19:34:09 +00:00
func ( s * HolidayService ) Find ( search Search , paging Paging ) ( [ ] Holiday , error ) {
2023-06-20 14:10:46 +00:00
var holidays [ ] Holiday
var err error
if search . Date != nil {
2023-08-05 15:50:40 +00:00
holidays , err = s . findByDate ( * search . Date , search . IsStateHoliday ( ) , search . IsReligiousHoliday ( ) , search . Country )
2023-06-20 14:10:46 +00:00
} else if search . RangeStart != nil || search . RangeEnd != nil {
2023-08-05 15:50:40 +00:00
holidays , err = s . findForRange ( getDateOrDefault ( search . RangeStart , 1000 ) , getDateOrDefault ( search . RangeEnd , 3000 ) , search . IsStateHoliday ( ) , search . IsReligiousHoliday ( ) , search . Country )
2023-06-20 14:10:46 +00:00
} else if search . Year != nil {
2023-08-05 15:50:40 +00:00
holidays , err = s . findByYear ( * search . Year , search . IsStateHoliday ( ) , search . IsReligiousHoliday ( ) , search . Country )
2023-06-20 14:10:46 +00:00
} else {
2023-08-05 15:50:40 +00:00
holidays , err = s . find ( search . IsStateHoliday ( ) , search . IsReligiousHoliday ( ) , search . Country )
2023-06-20 14:10:46 +00:00
}
if err != nil {
return nil , err
}
return s . paginate ( holidays , paging ) , err
}
2023-08-05 19:34:09 +00:00
func ( s * HolidayService ) FindById ( id uuid . UUID ) ( Holiday , error ) {
2023-06-20 14:10:46 +00:00
var holiday Holiday
return holiday , s . DB . Get ( & holiday , ` SELECT * FROM "holiday" WHERE "id" = $1; ` , id )
}
2023-08-05 19:34:09 +00:00
func ( s * HolidayService ) findByDate ( date time . Time , isState * bool , isReligious * bool , country string ) ( [ ] Holiday , error ) {
2023-06-20 14:10:46 +00:00
var holidays [ ] Holiday
return holidays , s . DB . Select ( & holidays , ` SELECT * FROM "holiday" WHERE "date" = $1 AND country = $2 ` + s . filter ( isState , isReligious ) + ";" , date , country )
}
2023-08-05 19:34:09 +00:00
func ( s * HolidayService ) findForRange ( rangeStart time . Time , rangeEnd time . Time , isState * bool , isReligious * bool , country string ) ( [ ] Holiday , error ) {
2023-06-20 14:10:46 +00:00
var holidays [ ] Holiday
return holidays , s . DB . Select ( & holidays , ` SELECT * FROM "holiday" WHERE "date" BETWEEN $1 AND $2 AND country = $3 ` + s . filter ( isState , isReligious ) + ";" , rangeStart , rangeEnd , country )
}
2023-08-05 19:34:09 +00:00
func ( s * HolidayService ) findByYear ( year int , isState * bool , isReligious * bool , country string ) ( [ ] Holiday , error ) {
2023-06-20 14:10:46 +00:00
var holidays [ ] Holiday
return holidays , s . DB . Select ( & holidays , ` SELECT * FROM "holiday" WHERE extract(year from "date") = $1 AND country = $2 ` + s . filter ( isState , isReligious ) + ";" , year , country )
}
2023-08-05 19:34:09 +00:00
func ( s * HolidayService ) find ( isState * bool , isReligious * bool , country string ) ( [ ] Holiday , error ) {
2023-06-20 14:10:46 +00:00
var holidays [ ] Holiday
return holidays , s . DB . Select ( & holidays , ` SELECT * FROM "holiday" WHERE country = $1 ` + s . filter ( isState , isReligious ) + ";" , country )
}
2023-08-05 19:34:09 +00:00
func ( s * HolidayService ) paginate ( holidays [ ] Holiday , paging Paging ) [ ] Holiday {
2023-06-20 14:10:46 +00:00
start := paging . Page * paging . PageSize
end := ( paging . Page + 1 ) * paging . PageSize
if end < len ( holidays ) {
return holidays [ start : end ]
} else if start < len ( holidays ) {
return holidays [ start : ]
} else {
return [ ] Holiday { }
}
}
2023-08-05 19:34:09 +00:00
func ( s * HolidayService ) filter ( isState * bool , isReligious * bool ) string {
2023-06-20 14:10:46 +00:00
var filters [ ] string
if isState != nil {
filters = append ( filters , "is_state = " + strconv . FormatBool ( * isState ) )
}
if isReligious != nil {
filters = append ( filters , "is_religious = " + strconv . FormatBool ( * isReligious ) )
}
if len ( filters ) > 0 {
return " AND " + strings . Join ( filters , " AND " )
} else {
return ""
}
}
2023-08-05 19:34:09 +00:00
func ( s * HolidayService ) Update ( holiday Holiday ) ( Holiday , error ) {
2023-06-20 14:10:46 +00:00
_ , err := s . DB . Exec ( ` UPDATE holiday SET "country" = $1, "name" = $2, "description" = $3, "date" = $4, "is_state"=$5, "is_religious"=$6 WHERE "id" = $7 ` ,
& holiday . Country , & holiday . Name , & holiday . Description , & holiday . Date , & holiday . IsStateHoliday , & holiday . IsReligiousHoliday , & holiday . Id ,
)
return holiday , err
}
2023-08-05 19:34:09 +00:00
func ( s * HolidayService ) Create ( holiday Holiday ) ( Holiday , error ) {
2023-06-20 14:10:46 +00:00
holiday . Id = uuid . Must ( uuid . NewRandom ( ) )
_ , err := s . DB . Exec ( ` INSERT INTO holiday (id, country, name, description, date, is_state, is_religious) values ($1, $2, $3, $4, $5, $6, $7) ` ,
& holiday . Id , & holiday . Country , & holiday . Name , & holiday . Description , & holiday . Date , & holiday . IsStateHoliday , & holiday . IsReligiousHoliday ,
)
return holiday , err
}
2023-08-05 19:34:09 +00:00
func ( s * HolidayService ) Delete ( id uuid . UUID ) error {
2023-06-20 14:10:46 +00:00
_ , err := s . DB . Exec ( ` DELETE FROM holiday WHERE "id" = $1 ` , & id )
return err
}
func getDateOrDefault ( date * time . Time , year int ) time . Time {
if date == nil {
return time . Date ( year , 1 , 1 , 0 , 0 , 0 , 0 , time . UTC )
} else {
return * date
}
}