Added dynamic support for countries
This commit is contained in:
1 parent
381444a838
commit
47b9939ead
17 files changed
+296
-69
No files matched your search
@@ -0,0 +1,40 @@
|
||||
package holiday
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
type CountryService struct {
|
||||
DB *sqlx.DB
|
||||
}
|
||||
|
||||
func (s *CountryService) FindById(id uuid.UUID) (Country, error) {
|
||||
var country Country
|
||||
return country, s.DB.Get(&country, `SELECT * FROM "country" WHERE "id" = $1;`, id)
|
||||
}
|
||||
|
||||
func (s *CountryService) Find() ([]Country, error) {
|
||||
var countries []Country
|
||||
return countries, s.DB.Select(&countries, `SELECT * FROM "country";`)
|
||||
}
|
||||
|
||||
func (s *CountryService) Update(country Country) (Country, error) {
|
||||
_, err := s.DB.Exec(`UPDATE country SET "iso_name" = $2, "name" = $3 WHERE "id" = $1`,
|
||||
&country.Id, &country.IsoName, &country.Name,
|
||||
)
|
||||
return country, err
|
||||
}
|
||||
|
||||
func (s *CountryService) Create(country Country) (Country, error) {
|
||||
country.Id = uuid.Must(uuid.NewRandom())
|
||||
_, err := s.DB.Exec(`INSERT INTO country (id, iso_name, name) values ($1, $2, $3)`,
|
||||
&country.Id, &country.IsoName, &country.Name,
|
||||
)
|
||||
return country, err
|
||||
}
|
||||
|
||||
func (s *CountryService) Delete(id uuid.UUID) error {
|
||||
_, err := s.DB.Exec(`DELETE FROM country WHERE "id" = $1`, &id)
|
||||
return err
|
||||
}
|
||||
@@ -8,11 +8,11 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
type HolidayService struct {
|
||||
DB *sqlx.DB
|
||||
}
|
||||
|
||||
func (s *Service) Find(search Search, paging Paging) ([]Holiday, error) {
|
||||
func (s *HolidayService) Find(search Search, paging Paging) ([]Holiday, error) {
|
||||
var holidays []Holiday
|
||||
var err error
|
||||
if search.Date != nil {
|
||||
@@ -30,32 +30,32 @@ func (s *Service) Find(search Search, paging Paging) ([]Holiday, error) {
|
||||
return s.paginate(holidays, paging), err
|
||||
}
|
||||
|
||||
func (s *Service) FindById(id uuid.UUID) (Holiday, error) {
|
||||
func (s *HolidayService) FindById(id uuid.UUID) (Holiday, error) {
|
||||
var holiday Holiday
|
||||
return holiday, s.DB.Get(&holiday, `SELECT * FROM "holiday" WHERE "id" = $1;`, id)
|
||||
}
|
||||
|
||||
func (s *Service) findByDate(date time.Time, isState *bool, isReligious *bool, country string) ([]Holiday, error) {
|
||||
func (s *HolidayService) findByDate(date time.Time, isState *bool, isReligious *bool, country string) ([]Holiday, error) {
|
||||
var holidays []Holiday
|
||||
return holidays, s.DB.Select(&holidays, `SELECT * FROM "holiday" WHERE "date" = $1 AND country = $2 `+s.filter(isState, isReligious)+";", date, country)
|
||||
}
|
||||
|
||||
func (s *Service) findForRange(rangeStart time.Time, rangeEnd time.Time, isState *bool, isReligious *bool, country string) ([]Holiday, error) {
|
||||
func (s *HolidayService) findForRange(rangeStart time.Time, rangeEnd time.Time, isState *bool, isReligious *bool, country string) ([]Holiday, error) {
|
||||
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)
|
||||
}
|
||||
|
||||
func (s *Service) findByYear(year int, isState *bool, isReligious *bool, country string) ([]Holiday, error) {
|
||||
func (s *HolidayService) findByYear(year int, isState *bool, isReligious *bool, country string) ([]Holiday, error) {
|
||||
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)
|
||||
}
|
||||
|
||||
func (s *Service) find(isState *bool, isReligious *bool, country string) ([]Holiday, error) {
|
||||
func (s *HolidayService) find(isState *bool, isReligious *bool, country string) ([]Holiday, error) {
|
||||
var holidays []Holiday
|
||||
return holidays, s.DB.Select(&holidays, `SELECT * FROM "holiday" WHERE country = $1 `+s.filter(isState, isReligious)+";", country)
|
||||
}
|
||||
|
||||
func (s *Service) paginate(holidays []Holiday, paging Paging) []Holiday {
|
||||
func (s *HolidayService) paginate(holidays []Holiday, paging Paging) []Holiday {
|
||||
start := paging.Page * paging.PageSize
|
||||
end := (paging.Page + 1) * paging.PageSize
|
||||
if end < len(holidays) {
|
||||
@@ -67,7 +67,7 @@ func (s *Service) paginate(holidays []Holiday, paging Paging) []Holiday {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) filter(isState *bool, isReligious *bool) string {
|
||||
func (s *HolidayService) filter(isState *bool, isReligious *bool) string {
|
||||
var filters []string
|
||||
if isState != nil {
|
||||
filters = append(filters, "is_state = "+strconv.FormatBool(*isState))
|
||||
@@ -82,14 +82,14 @@ func (s *Service) filter(isState *bool, isReligious *bool) string {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) Update(holiday Holiday) (Holiday, error) {
|
||||
func (s *HolidayService) Update(holiday Holiday) (Holiday, error) {
|
||||
_, 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
|
||||
}
|
||||
|
||||
func (s *Service) Create(holiday Holiday) (Holiday, error) {
|
||||
func (s *HolidayService) Create(holiday Holiday) (Holiday, error) {
|
||||
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,
|
||||
@@ -97,7 +97,7 @@ func (s *Service) Create(holiday Holiday) (Holiday, error) {
|
||||
return holiday, err
|
||||
}
|
||||
|
||||
func (s *Service) Delete(id uuid.UUID) error {
|
||||
func (s *HolidayService) Delete(id uuid.UUID) error {
|
||||
_, err := s.DB.Exec(`DELETE FROM holiday WHERE "id" = $1`, &id)
|
||||
return err
|
||||
}
|
||||
@@ -15,6 +15,12 @@ type Holiday struct {
|
||||
IsReligiousHoliday bool `db:"is_religious"`
|
||||
}
|
||||
|
||||
type Country struct {
|
||||
Id uuid.UUID `db:"id"`
|
||||
IsoName string `db:"iso_name"`
|
||||
Name string `db:"name"`
|
||||
}
|
||||
|
||||
type Paging struct {
|
||||
PageSize int `form:"page_size" binging:"min=0"`
|
||||
Page int `form:"page" binging:"min=0"`
|
||||
|
||||
Reference in new issue
Block a user