2022-08-29 20:58:19 +00:00
|
|
|
package repositories
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2022-09-21 20:24:08 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2022-09-20 10:21:01 +00:00
|
|
|
"officeHours/models"
|
2022-08-29 20:58:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type RoomRepo struct {
|
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRoomRepo(db *sql.DB) *RoomRepo {
|
|
|
|
return &RoomRepo{
|
|
|
|
db: db,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RoomRepo) FindByName(name string) (models.Room, error) {
|
|
|
|
row := r.db.QueryRow("SELECT * FROM room WHERE name=?", name)
|
|
|
|
var room models.Room
|
|
|
|
if err := row.Scan(&room.Name, &room.MaxOccupy); err != nil {
|
|
|
|
return models.Room{}, err
|
|
|
|
}
|
|
|
|
return room, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RoomRepo) FindById(id int) (models.Room, error) {
|
|
|
|
row := r.db.QueryRow("SELECT * FROM room WHERE id=?", id)
|
|
|
|
var room models.Room
|
|
|
|
if err := row.Scan(&room.Id, &room.Name, &room.MaxOccupy); err != nil {
|
2024-12-10 19:28:29 +00:00
|
|
|
err = fmt.Errorf("error scanning row to get room: %w", err)
|
2022-09-21 20:24:08 +00:00
|
|
|
if !errors.Is(err, sql.ErrNoRows) {
|
|
|
|
log.Println(err.Error())
|
|
|
|
}
|
2022-08-29 20:58:19 +00:00
|
|
|
return models.Room{}, err
|
|
|
|
}
|
|
|
|
return room, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RoomRepo) GetAll() ([]models.Room, error) {
|
2022-09-26 12:25:51 +00:00
|
|
|
rows, err := r.db.Query("SELECT * FROM room ORDER BY name ASC")
|
2022-08-29 20:58:19 +00:00
|
|
|
if err != nil {
|
2024-12-10 19:28:29 +00:00
|
|
|
err = fmt.Errorf("error getting all rooms: %w", err)
|
2022-09-21 20:24:08 +00:00
|
|
|
log.Println(err.Error())
|
2022-08-29 20:58:19 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var rooms []models.Room
|
|
|
|
for rows.Next() {
|
|
|
|
var room models.Room
|
|
|
|
if err := rows.Scan(&room.Id, &room.Name, &room.MaxOccupy); err != nil {
|
2024-12-10 19:28:29 +00:00
|
|
|
err = fmt.Errorf("error scanning row to get room: %w", err)
|
2022-08-29 20:58:19 +00:00
|
|
|
return rooms, err
|
|
|
|
}
|
|
|
|
rooms = append(rooms, room)
|
|
|
|
}
|
|
|
|
return rooms, nil
|
|
|
|
}
|