You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
181 lines
3.5 KiB
Go
181 lines
3.5 KiB
Go
package db
|
|
|
|
import "fmt"
|
|
|
|
// Entities
|
|
|
|
type User struct {
|
|
ID string
|
|
Username string
|
|
Permissions []string
|
|
}
|
|
|
|
type Room struct {
|
|
ID string
|
|
SeatIDs []string
|
|
|
|
// gridRows, gridCols int
|
|
}
|
|
|
|
type Seat struct {
|
|
ID string
|
|
|
|
// OccupiedBy is an empty list or a singleton of a userID
|
|
OccupiedBy []string
|
|
|
|
// x, y, w, h int
|
|
}
|
|
|
|
// Database Interfaces
|
|
|
|
type Store interface {
|
|
GetUser(userID string) (*User, error)
|
|
GetRoom(roomID string) (*Room, error)
|
|
GetSeat(seatID string) (*Seat, error)
|
|
|
|
GetOccupiedSeats(roomID string) ([]string, error)
|
|
GetFreeSeats(roomID string) ([]string, error)
|
|
GetUserSeat(userID string) ([]string, error)
|
|
|
|
OccupySeat(userID, roomID, seatID string) error
|
|
FreeSeat(userID, roomID, seatID string) error
|
|
}
|
|
|
|
// TODO: Create an SQLite implementation
|
|
|
|
type memDB struct {
|
|
users map[string]*User
|
|
rooms map[string]*Room
|
|
seats map[string]*Seat
|
|
}
|
|
|
|
func NewInMemoryStore() Store {
|
|
db := &memDB{
|
|
make(map[string]*User),
|
|
make(map[string]*Room),
|
|
make(map[string]*Seat),
|
|
}
|
|
|
|
db.rooms["aula-stud"] = &Room{
|
|
ID: "aula-stud",
|
|
SeatIDs: []string{
|
|
"aula-stud/posto-1",
|
|
"aula-stud/posto-2",
|
|
"aula-stud/posto-3",
|
|
"aula-stud/posto-4",
|
|
},
|
|
}
|
|
|
|
db.seats["aula-stud/posto-1"] = &Seat{
|
|
ID: "aula-stud/posto-1",
|
|
OccupiedBy: []string{},
|
|
}
|
|
db.seats["aula-stud/posto-2"] = &Seat{
|
|
ID: "aula-stud/posto-2",
|
|
OccupiedBy: []string{},
|
|
}
|
|
db.seats["aula-stud/posto-3"] = &Seat{
|
|
ID: "aula-stud/posto-3",
|
|
OccupiedBy: []string{},
|
|
}
|
|
db.seats["aula-stud/posto-4"] = &Seat{
|
|
ID: "aula-stud/posto-4",
|
|
OccupiedBy: []string{},
|
|
}
|
|
|
|
db.users["aziis98"] = &User{
|
|
ID: "aziis98",
|
|
Username: "aziis98",
|
|
Permissions: []string{"admin"},
|
|
}
|
|
|
|
db.users["bachoseven"] = &User{
|
|
ID: "bachoseven",
|
|
Username: "bachoseven",
|
|
Permissions: []string{"admin"},
|
|
}
|
|
|
|
return db
|
|
}
|
|
|
|
func (db *memDB) GetUser(userID string) (*User, error) {
|
|
user, present := db.users[userID]
|
|
if !present {
|
|
return nil, fmt.Errorf(`no such user "%s"`, userID)
|
|
}
|
|
|
|
return user, nil
|
|
}
|
|
|
|
func (db *memDB) GetRoom(roomID string) (*Room, error) {
|
|
room, present := db.rooms[roomID]
|
|
if !present {
|
|
return nil, fmt.Errorf(`no such room "%s"`, roomID)
|
|
}
|
|
|
|
return room, nil
|
|
}
|
|
|
|
func (db *memDB) GetSeat(seatID string) (*Seat, error) {
|
|
seat, present := db.seats[seatID]
|
|
if !present {
|
|
return nil, fmt.Errorf(`no such seat "%s"`, seatID)
|
|
}
|
|
|
|
return seat, nil
|
|
}
|
|
|
|
func (db *memDB) GetOccupiedSeats(roomID string) ([]string, error) {
|
|
room, err := db.GetRoom(roomID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
seats := []string{}
|
|
for _, seatID := range room.SeatIDs {
|
|
seat := db.seats[seatID]
|
|
if len(seat.OccupiedBy) == 1 {
|
|
seats = append(seats, seatID)
|
|
}
|
|
}
|
|
|
|
return seats, nil
|
|
}
|
|
|
|
func (db *memDB) GetFreeSeats(roomID string) ([]string, error) {
|
|
room, err := db.GetRoom(roomID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
seats := []string{}
|
|
for _, seatID := range room.SeatIDs {
|
|
seat := db.seats[seatID]
|
|
if len(seat.OccupiedBy) == 0 {
|
|
seats = append(seats, seatID)
|
|
}
|
|
}
|
|
|
|
return seats, nil
|
|
}
|
|
|
|
func (db *memDB) GetUserSeat(userID string) ([]string, error) {
|
|
for _, seat := range db.seats {
|
|
if len(seat.OccupiedBy) > 0 && seat.OccupiedBy[0] == userID {
|
|
return []string{userID}, nil
|
|
}
|
|
}
|
|
|
|
return []string{}, nil
|
|
}
|
|
|
|
func (db *memDB) OccupySeat(userID string, roomID string, seatID string) error {
|
|
db.seats[seatID].OccupiedBy = []string{userID}
|
|
return nil
|
|
}
|
|
|
|
func (db *memDB) FreeSeat(userID string, roomID string, seatID string) error {
|
|
db.seats[seatID].OccupiedBy = []string{}
|
|
return nil
|
|
}
|