Dump changes

This commit is contained in:
Andrey Chervyakov 2022-06-03 23:44:08 +06:00
parent e12550a643
commit aac2ea1b74
Signed by: cognio
GPG key ID: DAA316147EB0D58D
25 changed files with 129 additions and 76 deletions

View file

@ -1,83 +0,0 @@
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
}