59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
// raum
|
|
package repositories
|
|
|
|
import (
|
|
"database/sql"
|
|
"officeHours/models"
|
|
)
|
|
|
|
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 {
|
|
return models.Room{}, err
|
|
}
|
|
return room, nil
|
|
}
|
|
|
|
func (r *RoomRepo) GetAll() ([]models.Room, error) {
|
|
rows, err := r.db.Query("SELECT * FROM room")
|
|
if err != nil {
|
|
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 {
|
|
return rooms, err
|
|
}
|
|
rooms = append(rooms, room)
|
|
}
|
|
return rooms, nil
|
|
}
|
|
func (r *RoomRepo) Save(room models.Room) error {
|
|
return nil
|
|
}
|
|
func (r *RoomRepo) Add(room models.Room) error {
|
|
return nil
|
|
}
|