1
0
Fork 0

feat: add postgres-backed repositories

This commit is contained in:
Vojtěch Mareš 2024-04-28 15:25:33 +02:00
parent a2d47a4b3e
commit c340623721
Signed by: vojtech.mares
GPG key ID: C6827B976F17240D
4 changed files with 324 additions and 0 deletions

View file

@ -0,0 +1,102 @@
package postgres
import (
"database/sql"
"gitlab.mareshq.com/hq/backoffice/backoffice-api/pkg/training"
)
type AttendeeRepository struct {
db *sql.DB
}
func NewAttendeeRepository(db *sql.DB) *AttendeeRepository {
return &AttendeeRepository{db: db}
}
func (r *AttendeeRepository) Find(id training.AttendeeID) (*training.Attendee, error) {
var a training.Attendee
err := r.db.QueryRow("SELECT * FROM attendee WHERE id = $1", id).Scan(&a.ID, &a.Name, &a.DateID, &a.Email, &a.Company, &a.Role, &a.IsStudent, &a.HasAttended, &a.HasPaid)
if err != nil {
return nil, err
}
return &a, nil
}
func (r *AttendeeRepository) FindAll() ([]training.Attendee, error) {
rows, err := r.db.Query("SELECT * FROM attendee")
if err != nil {
return nil, err
}
defer rows.Close()
var attendees []training.Attendee
for rows.Next() {
var a training.Attendee
err := rows.Scan(&a.ID, &a.Name, &a.DateID, &a.Email, &a.Company, &a.Role, &a.IsStudent, &a.HasAttended, &a.HasPaid)
if err != nil {
return nil, err
}
attendees = append(attendees, a)
}
return attendees, nil
}
func (r *AttendeeRepository) FindAllForDate(dateID training.DateID) ([]training.Attendee, error) {
rows, err := r.db.Query("SELECT * FROM attendee WHERE date_id = $1", dateID)
if err != nil {
return nil, err
}
defer rows.Close()
var attendees []training.Attendee
for rows.Next() {
var a training.Attendee
err := rows.Scan(&a.ID, &a.Name, &a.DateID, &a.Email, &a.Company, &a.Role, &a.IsStudent, &a.HasAttended, &a.HasPaid)
if err != nil {
return nil, err
}
attendees = append(attendees, a)
}
return attendees, nil
}
func (r *AttendeeRepository) CountForDate(dateID training.DateID) (int, error) {
var count int
err := r.db.QueryRow("SELECT COUNT(*) FROM attendee WHERE date_id = $1", dateID).Scan(&count)
if err != nil {
return 0, err
}
return count, nil
}
func (r *AttendeeRepository) Save(a *training.Attendee) error {
_, err := r.db.Exec("INSERT INTO attendee (id, date_id, name, email, company, role, is_student, has_attended, has_paid) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)", a.ID, a.DateID, a.Name, a.Email, a.Company, a.Role, a.IsStudent, a.HasAttended, a.HasPaid)
return err
}
func (r *AttendeeRepository) Update(a *training.Attendee) error {
_, err := r.db.Exec("UPDATE attendee SET name = $1, email = $2, company = $3, role = $4, is_student = $5, has_attended = $6, has_paid = $7 WHERE id = $8", a.Name, a.Email, a.Company, a.Role, a.IsStudent, a.HasAttended, a.HasPaid, a.ID)
return err
}
func (r *AttendeeRepository) UpdateAttendance(id training.AttendeeID, hasAttended bool) error {
_, err := r.db.Exec("UPDATE attendee SET has_attended = $1 WHERE id = $2", hasAttended, id)
return err
}
func (r *AttendeeRepository) UpdatePayment(id training.AttendeeID, hasPaid bool) error {
_, err := r.db.Exec("UPDATE attendee SET has_paid = $1 WHERE id = $2", hasPaid, id)
return err
}
func (r *AttendeeRepository) Delete(id training.AttendeeID) error {
_, err := r.db.Exec("DELETE FROM attendee WHERE id = $1", id)
return err
}

View file

@ -0,0 +1,100 @@
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) Save(d *training.Date) error {
_, 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
}

View file

@ -0,0 +1,61 @@
package postgres
import (
"database/sql"
"gitlab.mareshq.com/hq/backoffice/backoffice-api/pkg/training"
)
type FeedbackRepository struct {
db *sql.DB
}
func NewFeedbackRepository(db *sql.DB) *FeedbackRepository {
return &FeedbackRepository{db: db}
}
func (r *FeedbackRepository) Get(id training.FeedbackID) (*training.Feedback, error) {
var f training.Feedback
err := r.db.QueryRow("SELECT * FROM feedback WHERE id = $1", id).Scan(&f.ID, &f.AttendeeID, &f.Rating, &f.Comment, &f.IsAnonymous, &f.IsSharingAllowed)
if err != nil {
return nil, err
}
return &f, nil
}
func (r *FeedbackRepository) FindAll() ([]training.Feedback, error) {
rows, err := r.db.Query("SELECT * FROM feedback")
if err != nil {
return nil, err
}
defer rows.Close()
var feedbacks []training.Feedback
for rows.Next() {
var f training.Feedback
err := rows.Scan(&f.ID, &f.AttendeeID, &f.Rating, &f.Comment, &f.IsAnonymous, &f.IsSharingAllowed)
if err != nil {
return nil, err
}
feedbacks = append(feedbacks, f)
}
return feedbacks, nil
}
func (r *FeedbackRepository) Save(f *training.Feedback) error {
_, err := r.db.Exec("INSERT INTO feedback (id, attendee_id, rating, comment, is_anonymous, is_sharing_allowed) VALUES ($1, $2, $3, $4, $5, $6)", f.ID, f.AttendeeID, f.Rating, f.Comment, f.IsAnonymous, f.IsSharingAllowed)
return err
}
func (r *FeedbackRepository) Update(f *training.Feedback) error {
_, err := r.db.Exec("UPDATE feedback SET attendee_id = $2, rating = $3, comment = $4, is_anonymous = $5, is_sharing_allowed = $6 WHERE id = $1", f.ID, f.AttendeeID, f.Rating, f.Comment, f.IsAnonymous, f.IsSharingAllowed)
return err
}
func (r *FeedbackRepository) Delete(id training.FeedbackID) error {
_, err := r.db.Exec("DELETE FROM feedback WHERE id = $1", id)
return err
}

View file

@ -0,0 +1,61 @@
package postgres
import (
"database/sql"
"gitlab.mareshq.com/hq/backoffice/backoffice-api/pkg/training"
)
type TrainingRepository struct {
db *sql.DB
}
func NewTrainingRepository(db *sql.DB) *TrainingRepository {
return &TrainingRepository{db: db}
}
func (r *TrainingRepository) Save(t *training.Training) error {
_, err := r.db.Exec("INSERT INTO training (id, name, days, description, price) VALUES ($1, $2, $3)", t.ID, t.Name, t.Days, t.Description, t.Price)
return err
}
func (r *TrainingRepository) Get(id training.ID) (*training.Training, error) {
var t training.Training
err := r.db.QueryRow("SELECT * FROM training WHERE id = $1", id).Scan(&t.ID, &t.Name, &t.Days, &t.Description, &t.Price)
if err != nil {
return nil, err
}
return &t, nil
}
func (r *TrainingRepository) Update(t *training.Training) error {
_, err := r.db.Exec("UPDATE training SET name = $1, days = $2, description = $3, price = $4 WHERE id = $5", t.Name, t.Days, t.Description, t.Price, t.ID)
return err
}
func (r *TrainingRepository) Delete(id training.ID) error {
_, err := r.db.Exec("DELETE FROM training WHERE id = $1", id)
return err
}
func (r *TrainingRepository) FindAll() ([]training.Training, error) {
rows, err := r.db.Query("SELECT * FROM training")
if err != nil {
return nil, err
}
defer rows.Close()
var trainings []training.Training
for rows.Next() {
var t training.Training
err := rows.Scan(&t.ID, &t.Name, &t.Days, &t.Description, &t.Price)
if err != nil {
return nil, err
}
trainings = append(trainings, t)
}
return trainings, nil
}