Login technically works
This commit is contained in:
+159
-29
@@ -1,50 +1,180 @@
|
||||
package db
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Entities
|
||||
|
||||
type Utente struct {
|
||||
type User struct {
|
||||
ID string
|
||||
Username string
|
||||
Permissions []string
|
||||
}
|
||||
|
||||
type Stanza struct {
|
||||
ID string
|
||||
Posti []string
|
||||
type Room struct {
|
||||
ID string
|
||||
SeatIDs []string
|
||||
|
||||
// gridRows, gridCols int
|
||||
}
|
||||
|
||||
// Actions
|
||||
type Seat struct {
|
||||
ID string
|
||||
|
||||
type AzioneBase struct {
|
||||
Tipo string
|
||||
}
|
||||
// OccupiedBy is an empty list or a singleton of a userID
|
||||
OccupiedBy []string
|
||||
|
||||
type AzioneStanza struct {
|
||||
AzioneBase
|
||||
UserID string
|
||||
StanzaID string
|
||||
}
|
||||
|
||||
type AzioneOccupaPosto struct {
|
||||
AzioneStanza
|
||||
PostoID string
|
||||
}
|
||||
|
||||
type AzioneLiberaPosto struct {
|
||||
AzioneStanza
|
||||
PostoID string
|
||||
// x, y, w, h int
|
||||
}
|
||||
|
||||
// Database Interfaces
|
||||
|
||||
type Store interface {
|
||||
// Entities
|
||||
GetUser(userID string) (*User, error)
|
||||
GetRoom(roomID string) (*Room, error)
|
||||
GetSeat(seatID string) (*Seat, error)
|
||||
|
||||
GetUtente(userID string) *Utente
|
||||
GetStanza(stanzaID string) *Stanza
|
||||
GetOccupiedSeats(roomID string) ([]string, error)
|
||||
GetFreeSeats(roomID string) ([]string, error)
|
||||
GetUserSeat(userID string) ([]string, error)
|
||||
|
||||
// Available Seats
|
||||
|
||||
GetPostiOccupati(stanzaID string) []string
|
||||
GetPostiLiberi(stanzaID string) []string
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user