101 lines
2.8 KiB
Go
101 lines
2.8 KiB
Go
package postgres
|
|
|
|
import (
|
|
"database/sql"
|
|
|
|
"gitlab.mareshq.com/hq/backoffice/backoffice-api/pkg/training"
|
|
)
|
|
|
|
type DateRepository struct {
|
|
db *sql.DB
|
|
}
|
|
|
|
func NewDateRepository(db *sql.DB) *DateRepository {
|
|
return &DateRepository{db: db}
|
|
}
|
|
|
|
func (r *DateRepository) Get(id training.DateID) (*training.Date, error) {
|
|
var d training.Date
|
|
err := r.db.QueryRow("SELECT * FROM date WHERE id = $1", id).Scan(&d.ID, &d.Date, &d.TrainingID, &d.StartTime, &d.Days, &d.Price, &d.IsOnline, &d.Location, &d.Address, &d.Capacity)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &d, nil
|
|
}
|
|
|
|
func (r *DateRepository) FindAll() ([]training.Date, error) {
|
|
rows, err := r.db.Query("SELECT * FROM date")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var dates []training.Date
|
|
for rows.Next() {
|
|
var d training.Date
|
|
err := rows.Scan(&d.ID, &d.Date, &d.TrainingID, &d.StartTime, &d.Days, &d.Price, &d.IsOnline, &d.Location, &d.Address, &d.Capacity)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
dates = append(dates, d)
|
|
}
|
|
|
|
return dates, nil
|
|
}
|
|
|
|
func (r *DateRepository) FindAllForTraining(id training.ID) ([]training.Date, error) {
|
|
rows, err := r.db.Query("SELECT * FROM training.dates WHERE td.training_id = $1 ORDER BY date DESC", id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var dates []training.Date
|
|
for rows.Next() {
|
|
var d training.Date
|
|
err := rows.Scan(&d.ID, &d.Date)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
dates = append(dates, d)
|
|
}
|
|
|
|
return dates, nil
|
|
}
|
|
|
|
func (r *DateRepository) Create(d *training.Date) error {
|
|
d.ID = training.NewDateID()
|
|
_, err := r.db.Exec("INSERT INTO date (id, date, training_id, start_time, days, price, is_online, location, address, capacity) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)", d.ID, d.Date, d.TrainingID, d.StartTime, d.Days, d.Price, d.IsOnline, d.Location, d.Address, d.Capacity)
|
|
return err
|
|
}
|
|
|
|
func (r *DateRepository) Update(d *training.Date) error {
|
|
_, err := r.db.Exec("UPDATE date SET date = $1, start_time $2, days = $3, price = $4, is_online = $5, location = $6, address = $7, capacity = $8 WHERE id = $2", d.Date, d.StartTime, d.Days, d.Price, d.IsOnline, d.Location, d.Address, d.Capacity, d.ID)
|
|
return err
|
|
}
|
|
|
|
func (r *DateRepository) Delete(id training.DateID) error {
|
|
_, err := r.db.Exec("DELETE FROM date WHERE id = $1", id)
|
|
return err
|
|
}
|
|
|
|
func (r *DateRepository) IsFull(id training.DateID) (bool, error) {
|
|
var d training.Date
|
|
err := r.db.QueryRow("SELECT * FROM date WHERE id = $1", id).Scan(&d.ID, &d.Date, &d.TrainingID, &d.StartTime, &d.Days, &d.Price, &d.IsOnline, &d.Location, &d.Address, &d.Capacity)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
var count int8
|
|
err = r.db.QueryRow("SELECT COUNT(*) FROM attendee WHERE date_id = $1", id).Scan(&count)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
return count >= d.Capacity, nil
|
|
}
|