44 lines
658 B
Go
44 lines
658 B
Go
package things
|
|
|
|
import "slices"
|
|
|
|
type Thing struct {
|
|
ID int
|
|
Name string
|
|
}
|
|
|
|
type Service struct {
|
|
things map[int]Thing
|
|
maxID int
|
|
}
|
|
|
|
func NewThingsService() *Service {
|
|
return &Service{
|
|
things: make(map[int]Thing),
|
|
}
|
|
}
|
|
|
|
func (s *Service) NewThing(name string) {
|
|
s.maxID++
|
|
s.things[s.maxID] = Thing{
|
|
Name: name,
|
|
ID: s.maxID,
|
|
}
|
|
|
|
print(name)
|
|
}
|
|
|
|
func (s *Service) GetThings() []Thing {
|
|
things := make([]Thing, 0, len(s.things))
|
|
for _, t := range s.things {
|
|
things = append(things, t)
|
|
}
|
|
slices.SortFunc(things, func(a, b Thing) int { return a.ID - b.ID })
|
|
return things
|
|
}
|
|
|
|
func (s *Service) DeleteThing(id int) {
|
|
delete(s.things, id)
|
|
|
|
}
|