Added some documentation
This commit is contained in:
+46
-1
@@ -1,3 +1,6 @@
|
||||
// This package contains the database interface and an in memory implementation.
|
||||
//
|
||||
// For the in-memory implementation see the "NewInMemoryStore()" function.
|
||||
package db
|
||||
|
||||
import (
|
||||
@@ -5,16 +8,29 @@ import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// ErrAlreadyExists is the error thrown from Store functions when an entity already exists.
|
||||
var ErrAlreadyExists = errors.New(`object already exists in database`)
|
||||
|
||||
// ErrDoesntExist is the error thrown from Store functions when an entity doesn't exist.
|
||||
var ErrDoesntExist = errors.New(`object doesn't exist in database`)
|
||||
|
||||
// Entities
|
||||
|
||||
// type FrontendUser struct {
|
||||
// Anonymous bool `json:"anonymous,omitempty"`
|
||||
// ID string `json:"id,omitempty"`
|
||||
// Permissions []string `json:"permissions,omitempty"`
|
||||
// }
|
||||
|
||||
// User represents a user in the database.
|
||||
type User struct {
|
||||
ID string `json:"id"`
|
||||
Permissions []string `json:"permissions"`
|
||||
|
||||
// passwordSaltHash string
|
||||
}
|
||||
|
||||
// HasPermission checks if the user has a specific permission.
|
||||
func (user User) HasPermission(neededPerm string) bool {
|
||||
for _, perm := range user.Permissions {
|
||||
if perm == neededPerm {
|
||||
@@ -25,6 +41,7 @@ func (user User) HasPermission(neededPerm string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// Room represents a room in the database, a room has an id, a name and a collection of seatIDs of this room.
|
||||
type Room struct {
|
||||
ID string `json:"id"`
|
||||
SeatIDs []string `json:"seatIds"`
|
||||
@@ -32,6 +49,7 @@ type Room struct {
|
||||
// gridRows, gridCols int
|
||||
}
|
||||
|
||||
// Seat represents a seat in the database, it belongs to a single room and can be free or occupied by some user (referenced by userID).
|
||||
type Seat struct {
|
||||
ID string `json:"id"`
|
||||
RoomID string `json:"roomId"`
|
||||
@@ -42,8 +60,26 @@ type Seat struct {
|
||||
// x, y, w, h int
|
||||
}
|
||||
|
||||
// Database Interfaces
|
||||
// type FrontendSeat struct {
|
||||
// ID string `json:"id"`
|
||||
// RoomID string `json:"roomId"`
|
||||
|
||||
// // OccupiedBy is an empty list or a singleton of a userID
|
||||
// OccupiedBy *FrontendUser `json:"occupiedBy"`
|
||||
|
||||
// DiagramRect struct {
|
||||
// X int `json:"x"`
|
||||
// Y int `json:"y"`
|
||||
// Width int `json:"width"`
|
||||
// Height int `json:"height"`
|
||||
// } `json:"diagramRect"`
|
||||
// }
|
||||
|
||||
//
|
||||
// Database Interfaces
|
||||
//
|
||||
|
||||
// Store is the main interface for interacting with database implementations, for now the only implementation is "memDB".
|
||||
type Store interface {
|
||||
CreateUser(user *User) error
|
||||
CreateRoom(room *Room) error
|
||||
@@ -70,12 +106,17 @@ type Store interface {
|
||||
|
||||
// TODO: Create an SQLite implementation
|
||||
|
||||
// memDB is the first Store implementation used for testing.
|
||||
type memDB struct {
|
||||
// FIXME: Giusto per la cronaca fare le modifiche in questo modo alle mappe non è per niente thread safe, servirebbe come minimo usare un mutex per quando si scrive su una di queste variabili
|
||||
// mutex *sync.Mutex
|
||||
|
||||
users map[string]*User
|
||||
rooms map[string]*Room
|
||||
seats map[string]*Seat
|
||||
}
|
||||
|
||||
// NewInMemoryStore creates an instance of memDB hidden behind the Store interface.
|
||||
func NewInMemoryStore() Store {
|
||||
db := &memDB{
|
||||
make(map[string]*User),
|
||||
@@ -115,6 +156,10 @@ func NewInMemoryStore() Store {
|
||||
return db
|
||||
}
|
||||
|
||||
//
|
||||
// memDB Implementation
|
||||
//
|
||||
|
||||
func (db *memDB) CreateUser(user *User) error {
|
||||
if _, present := db.users[user.ID]; present {
|
||||
return ErrAlreadyExists
|
||||
|
||||
Reference in New Issue
Block a user