41 lines
1.1 KiB
Go
41 lines
1.1 KiB
Go
|
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
|
||
|
}
|