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.
65 lines
1.2 KiB
Go
65 lines
1.2 KiB
Go
2 years ago
|
package database
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"phc/website/model"
|
||
|
"phc/website/sl"
|
||
|
)
|
||
|
|
||
|
type Database interface {
|
||
|
sl.Service
|
||
|
|
||
|
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) Initialize(l *sl.ServiceLocator) error { return nil }
|
||
|
|
||
|
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)
|
||
|
}
|