83 lines
1.8 KiB
Go
83 lines
1.8 KiB
Go
package appointment
|
|
|
|
import (
|
|
"brainbuffer/pkg/brainbuffer/repository"
|
|
"time"
|
|
)
|
|
|
|
type inMemoryRepository struct {
|
|
db map[int64]*Appointment
|
|
idCounter int64
|
|
}
|
|
|
|
func (repo *inMemoryRepository) Save(appointment *Appointment) error {
|
|
repo.idCounter++
|
|
appointment.ID = repo.idCounter
|
|
repo.db[repo.idCounter] = appointment
|
|
|
|
return nil
|
|
}
|
|
|
|
func (repo *inMemoryRepository) FindByID(id int64) (*Appointment, error) {
|
|
if v, ok := repo.db[id]; ok {
|
|
return v, nil
|
|
} else {
|
|
return nil, nil
|
|
}
|
|
}
|
|
|
|
func (repo *inMemoryRepository) GetAllByTask(taskId int64, page repository.Page) (Appointments, error) {
|
|
taskAppointments := make(Appointments, 0)
|
|
|
|
for _, val := range repo.db {
|
|
if val.TaskID == taskId {
|
|
taskAppointments = append(taskAppointments, val)
|
|
}
|
|
}
|
|
|
|
return taskAppointments, nil
|
|
}
|
|
|
|
func (repo *inMemoryRepository) GetAllByTasks(taskIds []int64, page repository.Page) (Appointments, error) {
|
|
taskAppointments := make(Appointments, 0)
|
|
|
|
for _, val := range repo.db {
|
|
if _, found := findInIntSlice(val.TaskID, taskIds); found {
|
|
taskAppointments = append(taskAppointments, val)
|
|
}
|
|
}
|
|
|
|
return taskAppointments, nil
|
|
}
|
|
|
|
func (repo *inMemoryRepository) GetAllByTasksBefore(taskIds []int64, beforeTime time.Time, page repository.Page) (Appointments, error) {
|
|
taskAppointments := make(Appointments, 0)
|
|
|
|
for _, val := range repo.db {
|
|
if _, found := findInIntSlice(val.TaskID, taskIds); found && val.Time.Before(beforeTime) {
|
|
taskAppointments = append(taskAppointments, val)
|
|
}
|
|
}
|
|
|
|
return taskAppointments, nil
|
|
}
|
|
|
|
func (repo *inMemoryRepository) DeleteAllByTask(taskId int64) error {
|
|
for id, v := range repo.db {
|
|
if v.TaskID == taskId {
|
|
delete(repo.db, id)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func findInIntSlice(val int64, slice []int64) (int, bool) {
|
|
for i, v := range slice {
|
|
if v == val {
|
|
return i, true
|
|
}
|
|
}
|
|
|
|
return -1, false
|
|
}
|