Add existing codebase
This commit is contained in:
commit
e12550a643
25 changed files with 1409 additions and 0 deletions
27
pkg/brainbuffer/appointment/entity.go
Normal file
27
pkg/brainbuffer/appointment/entity.go
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
package appointment
|
||||
|
||||
import (
|
||||
"brainbuffer/pkg/brainbuffer/scheduling"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Status int
|
||||
|
||||
const (
|
||||
Overdue Status = iota
|
||||
Upcoming
|
||||
Missed
|
||||
Completed
|
||||
)
|
||||
|
||||
type Appointment struct {
|
||||
ID int64
|
||||
TaskID int64
|
||||
Status Status
|
||||
SchedulingPattern scheduling.Pattern
|
||||
Time time.Time
|
||||
DurationOffset time.Duration
|
||||
CreationTime time.Time
|
||||
}
|
||||
|
||||
type Appointments []*Appointment
|
||||
83
pkg/brainbuffer/appointment/in_memo_repo.go
Normal file
83
pkg/brainbuffer/appointment/in_memo_repo.go
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
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
|
||||
}
|
||||
19
pkg/brainbuffer/appointment/repository.go
Normal file
19
pkg/brainbuffer/appointment/repository.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package appointment
|
||||
|
||||
import (
|
||||
"brainbuffer/pkg/brainbuffer/repository"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
Save(appointment *Appointment) error
|
||||
FindByID(id int64) (*Appointment, error)
|
||||
GetAllByTask(taskId int64, page repository.Page) (Appointments, error)
|
||||
GetAllByTasks(taskIds []int64, page repository.Page) (Appointments, error)
|
||||
GetAllByTasksBefore(taskIds []int64, beforeTime time.Time, page repository.Page) (Appointments, error)
|
||||
DeleteAllByTask(taskId int64) error
|
||||
}
|
||||
|
||||
func NewRepository() Repository {
|
||||
return &inMemoryRepository{}
|
||||
}
|
||||
108
pkg/brainbuffer/appointment/service.go
Normal file
108
pkg/brainbuffer/appointment/service.go
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
package appointment
|
||||
|
||||
import (
|
||||
apperrors "brainbuffer/pkg/brainbuffer/errors"
|
||||
"brainbuffer/pkg/brainbuffer/repository"
|
||||
"time"
|
||||
)
|
||||
|
||||
var taskNotFoundError = apperrors.NotFoundError{Message: "Task with given ID was not found."}
|
||||
|
||||
type Service interface {
|
||||
Create(appointment *Appointment) error
|
||||
CreateNextAppointment(appointment *Appointment) error
|
||||
GetByID(id int64) (*Appointment, error)
|
||||
GetAllByTask(taskId int64, page repository.Page) (Appointments, error)
|
||||
GetAllByTasks(tasksIds []int64, page repository.Page) (Appointments, error)
|
||||
GetAllByTasksBefore(tasksIds []int64, beforeTime time.Time, page repository.Page) (Appointments, error)
|
||||
DeleteAllByTask(taskId int64) error
|
||||
}
|
||||
|
||||
type defaultService struct {
|
||||
repository Repository
|
||||
}
|
||||
|
||||
func NewService() Service {
|
||||
return &defaultService{repository: NewRepository()}
|
||||
}
|
||||
|
||||
func (service *defaultService) Create(appointment *Appointment) error {
|
||||
appointment.CreationTime = time.Now()
|
||||
|
||||
if err := service.repository.Save(appointment); err != nil {
|
||||
return apperrors.UnknownError{Err: err}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (service *defaultService) CreateNextAppointment(appointment *Appointment) error {
|
||||
nextTime, err := appointment.SchedulingPattern.NextTime(appointment.Time)
|
||||
if err != nil {
|
||||
return apperrors.UnknownError{Err: err}
|
||||
}
|
||||
|
||||
nextAppointment := Appointment{
|
||||
ID: 0,
|
||||
TaskID: appointment.TaskID,
|
||||
Status: Upcoming,
|
||||
SchedulingPattern: appointment.SchedulingPattern,
|
||||
Time: *nextTime,
|
||||
DurationOffset: 0,
|
||||
CreationTime: time.Now(),
|
||||
}
|
||||
|
||||
if err := service.Create(&nextAppointment); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (service *defaultService) GetByID(id int64) (*Appointment, error) {
|
||||
a, err := service.repository.FindByID(id)
|
||||
if err != nil {
|
||||
return nil, apperrors.UnknownError{Err: err}
|
||||
}
|
||||
|
||||
if a == nil {
|
||||
return nil, taskNotFoundError
|
||||
}
|
||||
|
||||
return a, nil
|
||||
}
|
||||
|
||||
func (service *defaultService) GetAllByTask(taskId int64, page repository.Page) (Appointments, error) {
|
||||
appointments, err := service.repository.GetAllByTask(taskId, page)
|
||||
if err != nil {
|
||||
return nil, apperrors.UnknownError{Err: err}
|
||||
}
|
||||
|
||||
return appointments, nil
|
||||
}
|
||||
|
||||
func (service *defaultService) GetAllByTasks(tasksIds []int64, page repository.Page) (Appointments, error) {
|
||||
appointments, err := service.repository.GetAllByTasks(tasksIds, page)
|
||||
if err != nil {
|
||||
return nil, apperrors.UnknownError{Err: err}
|
||||
}
|
||||
|
||||
return appointments, nil
|
||||
}
|
||||
|
||||
func (service *defaultService) GetAllByTasksBefore(tasksIds []int64, beforeTime time.Time, page repository.Page) (Appointments, error) {
|
||||
appointments, err := service.repository.GetAllByTasksBefore(tasksIds, beforeTime, page)
|
||||
if err != nil {
|
||||
return nil, apperrors.UnknownError{Err: err}
|
||||
}
|
||||
|
||||
return appointments, nil
|
||||
}
|
||||
|
||||
func (service *defaultService) DeleteAllByTask(taskId int64) error {
|
||||
if err := service.repository.DeleteAllByTask(taskId); err != nil {
|
||||
return apperrors.UnknownError{Err: err}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue