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.
64 lines
1.2 KiB
Go
64 lines
1.2 KiB
Go
package database
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.phc.dm.unipi.it/phc/website/model"
|
|
"git.phc.dm.unipi.it/phc/website/sl"
|
|
)
|
|
|
|
var Slot = sl.NewSlot[Database]()
|
|
|
|
type Database interface {
|
|
CreateUser(user model.User) error
|
|
ReadUser(id string) (model.User, error)
|
|
ReadUsers() ([]model.User, error)
|
|
UpdateUser(id string, user model.User) error
|
|
DeleteUser(id string) error
|
|
}
|
|
|
|
type Memory struct {
|
|
Users []model.User
|
|
}
|
|
|
|
func (m *Memory) CreateUser(user model.User) error {
|
|
m.Users = append(m.Users, user)
|
|
return nil
|
|
}
|
|
|
|
func (m *Memory) ReadUser(id string) (model.User, error) {
|
|
for _, u := range m.Users {
|
|
if u.Id == id {
|
|
return u, nil
|
|
}
|
|
}
|
|
|
|
return model.User{}, fmt.Errorf(`no user with id "%s"`, id)
|
|
}
|
|
|
|
func (m *Memory) ReadUsers() ([]model.User, error) {
|
|
return m.Users, nil
|
|
}
|
|
|
|
func (m *Memory) UpdateUser(id string, user model.User) error {
|
|
for i, u := range m.Users {
|
|
if u.Id == id {
|
|
m.Users[i] = user
|
|
return nil
|
|
}
|
|
}
|
|
|
|
return fmt.Errorf(`no user with id "%s"`, id)
|
|
}
|
|
|
|
func (m *Memory) DeleteUser(id string) error {
|
|
for i, u := range m.Users {
|
|
if u.Id == id {
|
|
m.Users = append(m.Users[:i], m.Users[i+1:]...)
|
|
return nil
|
|
}
|
|
}
|
|
|
|
return fmt.Errorf(`no user with id "%s"`, id)
|
|
}
|