Raggiunto primo stato presentabile

This commit is contained in:
2022-03-05 04:45:12 +01:00
parent b0732c9c4f
commit bbd6b9eb7e
19 changed files with 1326 additions and 468 deletions
+108 -40
View File
@@ -1,27 +1,33 @@
package db
import "fmt"
import (
"errors"
"fmt"
)
var ErrAlreadyExists = errors.New(`object already exists in database`)
var ErrDoesntExist = errors.New(`object doesn't exist in database`)
// Entities
type User struct {
ID string
Username string
Permissions []string
ID string `json:"id"`
Permissions []string `json:"permissions"`
}
type Room struct {
ID string
SeatIDs []string
ID string `json:"id"`
SeatIDs []string `json:"seatIds"`
// gridRows, gridCols int
}
type Seat struct {
ID string
ID string `json:"id"`
RoomID string `json:"roomId"`
// OccupiedBy is an empty list or a singleton of a userID
OccupiedBy []string
OccupiedBy []string `json:"occupiedBy"`
// x, y, w, h int
}
@@ -29,16 +35,27 @@ type Seat struct {
// Database Interfaces
type Store interface {
CreateUser(user *User) error
CreateRoom(room *Room) error
CreateSeat(seat *Seat) error
DeleteUser(user *User) error
DeleteRoom(room *Room) error
DeleteSeat(seat *Seat) error
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)
GetRooms() ([]string, error)
GetRoomOccupiedSeats(roomID string) ([]string, error)
GetRoomFreeSeats(roomID string) ([]string, error)
GetUserSeat(userID string) ([]string, error)
OccupySeat(userID, roomID, seatID string) error
FreeSeat(userID, roomID, seatID string) error
OccupySeat(seatID, userID string) error
FreeSeat(seatID string) error
}
// TODO: Create an SQLite implementation
@@ -57,47 +74,88 @@ func NewInMemoryStore() Store {
}
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",
},
ID: "aula-stud",
SeatIDs: []string{},
}
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{},
for i := 1; i <= 11; i++ {
seatID := fmt.Sprintf(`aula-stud/posto-%d`, i)
db.rooms["aula-stud"].SeatIDs = append(db.rooms["aula-stud"].SeatIDs, seatID)
db.seats[seatID] = &Seat{
ID: seatID,
RoomID: "aula-stud",
OccupiedBy: []string{},
}
}
db.seats["aula-stud/posto-7"].OccupiedBy = []string{"aziis98"}
db.seats["aula-stud/posto-8"].OccupiedBy = []string{"jack"}
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) CreateUser(user *User) error {
if _, present := db.users[user.ID]; present {
return ErrAlreadyExists
}
db.users[user.ID] = user
return nil
}
func (db *memDB) CreateRoom(room *Room) error {
if _, present := db.rooms[room.ID]; present {
return ErrAlreadyExists
}
db.rooms[room.ID] = room
return nil
}
func (db *memDB) CreateSeat(seat *Seat) error {
if _, present := db.seats[seat.ID]; present {
return ErrAlreadyExists
}
db.seats[seat.ID] = seat
return nil
}
func (db *memDB) DeleteUser(user *User) error {
if _, present := db.users[user.ID]; !present {
return ErrDoesntExist
}
delete(db.users, user.ID)
return nil
}
func (db *memDB) DeleteRoom(room *Room) error {
if _, present := db.rooms[room.ID]; !present {
return ErrDoesntExist
}
delete(db.rooms, room.ID)
return nil
}
func (db *memDB) DeleteSeat(seat *Seat) error {
if _, present := db.seats[seat.ID]; !present {
return ErrDoesntExist
}
delete(db.seats, seat.ID)
return nil
}
func (db *memDB) GetUser(userID string) (*User, error) {
user, present := db.users[userID]
if !present {
@@ -125,7 +183,17 @@ func (db *memDB) GetSeat(seatID string) (*Seat, error) {
return seat, nil
}
func (db *memDB) GetOccupiedSeats(roomID string) ([]string, error) {
func (db *memDB) GetRooms() ([]string, error) {
roomIDs := []string{}
for roomID := range db.rooms {
roomIDs = append(roomIDs, roomID)
}
return roomIDs, nil
}
func (db *memDB) GetRoomOccupiedSeats(roomID string) ([]string, error) {
room, err := db.GetRoom(roomID)
if err != nil {
return nil, err
@@ -142,7 +210,7 @@ func (db *memDB) GetOccupiedSeats(roomID string) ([]string, error) {
return seats, nil
}
func (db *memDB) GetFreeSeats(roomID string) ([]string, error) {
func (db *memDB) GetRoomFreeSeats(roomID string) ([]string, error) {
room, err := db.GetRoom(roomID)
if err != nil {
return nil, err
@@ -169,12 +237,12 @@ func (db *memDB) GetUserSeat(userID string) ([]string, error) {
return []string{}, nil
}
func (db *memDB) OccupySeat(userID string, roomID string, seatID string) error {
func (db *memDB) OccupySeat(seatID, userID string) error {
db.seats[seatID].OccupiedBy = []string{userID}
return nil
}
func (db *memDB) FreeSeat(userID string, roomID string, seatID string) error {
func (db *memDB) FreeSeat(seatID string) error {
db.seats[seatID].OccupiedBy = []string{}
return nil
}