forked from ebhomengo/niki
65 lines
1.2 KiB
Go
65 lines
1.2 KiB
Go
package database
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.gocasts.ir/ebhomengo/niki/staffapp/service"
|
|
)
|
|
|
|
type DB struct {
|
|
staffs []service.Staff
|
|
nextID int
|
|
}
|
|
|
|
func New() service.StaffRepository {
|
|
return &DB{
|
|
staffs: []service.Staff{},
|
|
nextID: 1,
|
|
}
|
|
}
|
|
|
|
func (d *DB) Create(staff service.Staff) (service.Staff, error) {
|
|
staff.ID = d.nextID
|
|
d.staffs = append(d.staffs, staff)
|
|
d.nextID++
|
|
return staff, nil
|
|
}
|
|
|
|
func (d *DB) Get(id int) (*service.Staff, error) {
|
|
for _, v := range d.staffs {
|
|
if id == v.ID {
|
|
return &v, nil
|
|
}
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("The user not found with id = %d", id)
|
|
}
|
|
|
|
func (d *DB) List() ([]service.Staff, error) {
|
|
return d.staffs, nil
|
|
}
|
|
func (d *DB) Remove(id int) error {
|
|
for _, v := range d.staffs {
|
|
if id == v.ID {
|
|
d.staffs = append(d.staffs[:id], d.staffs[id+1:]...)
|
|
return nil
|
|
}
|
|
|
|
}
|
|
return fmt.Errorf("User with ID: %d notfound", id)
|
|
}
|
|
func (d *DB) Update(id int, name, lastName, phoneNumber string) (*service.Staff, error) {
|
|
for _, v := range d.staffs {
|
|
if id == v.ID {
|
|
v.Name = name
|
|
v.LastName = lastName
|
|
v.PhoneNumber = phoneNumber
|
|
|
|
return &v, nil
|
|
}
|
|
|
|
}
|
|
return nil, fmt.Errorf("User with this Id(%d)couldn't found for updating", id)
|
|
}
|