Dump changes
This commit is contained in:
parent
e12550a643
commit
aac2ea1b74
25 changed files with 129 additions and 76 deletions
42
pkg/brainbuffer/domain/appointment/entity.go
Normal file
42
pkg/brainbuffer/domain/appointment/entity.go
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
package appointment
|
||||
|
||||
import (
|
||||
"brainbuffer/pkg/brainbuffer/domain/scheduling"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CompletionStatus int
|
||||
|
||||
const (
|
||||
None CompletionStatus = iota
|
||||
Missed
|
||||
Completed
|
||||
Skipped
|
||||
)
|
||||
|
||||
type TimeStatus int
|
||||
|
||||
const (
|
||||
Overdue TimeStatus = iota
|
||||
Upcoming
|
||||
)
|
||||
|
||||
type Appointment struct {
|
||||
ID int64
|
||||
TaskID int64
|
||||
CompletionStatus CompletionStatus
|
||||
SchedulingPattern scheduling.Pattern
|
||||
PlannedTime time.Time
|
||||
DurationOffset time.Duration
|
||||
CreationTime time.Time
|
||||
}
|
||||
|
||||
func (a *Appointment) TimeStatus() TimeStatus {
|
||||
if a.PlannedTime.Before(time.Now()) {
|
||||
return Overdue
|
||||
} else {
|
||||
return Upcoming
|
||||
}
|
||||
}
|
||||
|
||||
type Appointments []*Appointment
|
||||
83
pkg/brainbuffer/domain/appointment/in_memo_repo.go
Normal file
83
pkg/brainbuffer/domain/appointment/in_memo_repo.go
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
package appointment
|
||||
|
||||
import (
|
||||
"brainbuffer/pkg/brainbuffer/domain/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.PlannedTime.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/domain/appointment/repository.go
Normal file
19
pkg/brainbuffer/domain/appointment/repository.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package appointment
|
||||
|
||||
import (
|
||||
"brainbuffer/pkg/brainbuffer/domain/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/domain/appointment/service.go
Normal file
108
pkg/brainbuffer/domain/appointment/service.go
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
package appointment
|
||||
|
||||
import (
|
||||
"brainbuffer/pkg/brainbuffer/domain/repository"
|
||||
apperrors "brainbuffer/pkg/brainbuffer/infrastructure/errors"
|
||||
"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.PlannedTime)
|
||||
if err != nil {
|
||||
return apperrors.UnknownError{Err: err}
|
||||
}
|
||||
|
||||
nextAppointment := Appointment{
|
||||
ID: 0,
|
||||
TaskID: appointment.TaskID,
|
||||
CompletionStatus: None,
|
||||
SchedulingPattern: appointment.SchedulingPattern,
|
||||
PlannedTime: *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
|
||||
}
|
||||
17
pkg/brainbuffer/domain/repository/page.go
Normal file
17
pkg/brainbuffer/domain/repository/page.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package repository
|
||||
|
||||
type Page struct {
|
||||
size int
|
||||
offset int
|
||||
}
|
||||
|
||||
func DefaultPage() Page {
|
||||
return Page{
|
||||
size: 20,
|
||||
offset: 0,
|
||||
}
|
||||
}
|
||||
|
||||
func EmptyPage() Page {
|
||||
return Page{}
|
||||
}
|
||||
112
pkg/brainbuffer/domain/scheduling/pattern.go
Normal file
112
pkg/brainbuffer/domain/scheduling/pattern.go
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
package scheduling
|
||||
|
||||
import (
|
||||
apperrors "brainbuffer/pkg/brainbuffer/infrastructure/errors"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/robfig/cron/v3"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Pattern string
|
||||
|
||||
func (pattern Pattern) NextTime(fromTime time.Time) (*time.Time, error) {
|
||||
schedule, err := pattern.Parse()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
nextTime := schedule.Next(fromTime)
|
||||
|
||||
return &nextTime, nil
|
||||
}
|
||||
|
||||
func (pattern Pattern) NextTimesUntil(fromTime time.Time, untilTime time.Time) ([]time.Time, error) {
|
||||
schedule, err := pattern.Parse()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
nextTimes := make([]time.Time, 0, 0)
|
||||
|
||||
for nextTime := schedule.Next(fromTime); nextTime.Before(untilTime); nextTime = schedule.Next(nextTime) {
|
||||
nextTimes = append(nextTimes, nextTime)
|
||||
}
|
||||
|
||||
return nextTimes, nil
|
||||
}
|
||||
|
||||
func (pattern Pattern) NextNTimes(fromTime time.Time, n int) ([]time.Time, error) {
|
||||
schedule, err := pattern.Parse()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
nextTimes := make([]time.Time, n)
|
||||
|
||||
for nextTime, i := schedule.Next(fromTime), 0; i < n; i++ {
|
||||
nextTimes = append(nextTimes, nextTime)
|
||||
nextTime = schedule.Next(nextTime)
|
||||
}
|
||||
|
||||
return nextTimes, nil
|
||||
}
|
||||
|
||||
func (pattern Pattern) MatchesTime(t time.Time, precision time.Duration) (*time.Time, error) {
|
||||
if precision > 24*time.Hour {
|
||||
return nil, errors.New("unsupported precision value")
|
||||
}
|
||||
|
||||
matchingTime, err := pattern.NextTime(time.Now())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
truncMatchingTime := matchingTime.Truncate(precision)
|
||||
truncT := t.Truncate(precision)
|
||||
|
||||
for !truncMatchingTime.Equal(truncT) && truncMatchingTime.Before(truncT) {
|
||||
matchingTime, _ = pattern.NextTime(*matchingTime)
|
||||
truncMatchingTime = matchingTime.Truncate(precision)
|
||||
}
|
||||
|
||||
if truncMatchingTime.Equal(truncT) {
|
||||
return matchingTime, nil
|
||||
} else {
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (pattern Pattern) Parse() (cron.Schedule, error) {
|
||||
schedule, err := cron.ParseStandard(string(pattern))
|
||||
if err != nil {
|
||||
return nil, apperrors.UnknownError{Err: err}
|
||||
}
|
||||
|
||||
return schedule, nil
|
||||
}
|
||||
|
||||
type Patterns []Pattern
|
||||
|
||||
func (patterns Patterns) NextTime(fromTime time.Time) (*time.Time, Pattern, error) {
|
||||
nextTimes := make(map[Pattern]time.Time)
|
||||
|
||||
for i, val := range patterns {
|
||||
nextTime, err := patterns[i].NextTime(fromTime)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
nextTimes[val] = *nextTime
|
||||
}
|
||||
|
||||
earliestNextTime := nextTimes[patterns[0]]
|
||||
earliestNextTimePattern := patterns[0]
|
||||
for k, v := range nextTimes {
|
||||
if v.Before(earliestNextTime) {
|
||||
earliestNextTime = v
|
||||
earliestNextTimePattern = k
|
||||
}
|
||||
}
|
||||
|
||||
return &earliestNextTime, earliestNextTimePattern, nil
|
||||
}
|
||||
62
pkg/brainbuffer/domain/scheduling/pattern_test.go
Normal file
62
pkg/brainbuffer/domain/scheduling/pattern_test.go
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
package scheduling
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestNextTime(t *testing.T) {
|
||||
pattern := Pattern("0 * * * *")
|
||||
|
||||
now := time.Now()
|
||||
|
||||
nextTime, _ := pattern.NextTime(now)
|
||||
|
||||
assert.True(t, now.Add(time.Hour).Truncate(time.Hour).Equal(*nextTime))
|
||||
}
|
||||
|
||||
func TestNextTimeUntil(t *testing.T) {
|
||||
pattern := Pattern("0 0 * * *")
|
||||
|
||||
now := time.Now()
|
||||
|
||||
nextTimes, _ := pattern.NextTimesUntil(now, now.Add(5*24*time.Hour))
|
||||
|
||||
assert.True(t, len(nextTimes) == 5)
|
||||
}
|
||||
|
||||
func TestMatchesTime(t *testing.T) {
|
||||
testCases := []struct {
|
||||
pattern Pattern
|
||||
expected time.Time
|
||||
precision time.Duration
|
||||
}{
|
||||
{
|
||||
pattern: Pattern("0 * * * *"),
|
||||
expected: time.Now().Add(time.Hour).Truncate(time.Hour),
|
||||
precision: time.Hour,
|
||||
},
|
||||
{
|
||||
pattern: Pattern("5 4 * * *"),
|
||||
expected: time.Now().Add(24 * time.Hour),
|
||||
precision: 24 * time.Hour,
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
matchingTime, _ := testCase.pattern.MatchesTime(testCase.expected, testCase.precision)
|
||||
|
||||
assert.NotNil(t, matchingTime)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotMatchesTime(t *testing.T) {
|
||||
pattern := Pattern("0 * * * *")
|
||||
|
||||
nextHour := time.Now().Add(time.Hour + 30*time.Minute)
|
||||
|
||||
matchingTime, _ := pattern.MatchesTime(nextHour, time.Minute)
|
||||
|
||||
assert.Nil(t, matchingTime)
|
||||
}
|
||||
12
pkg/brainbuffer/domain/summary/entity.go
Normal file
12
pkg/brainbuffer/domain/summary/entity.go
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
package summary
|
||||
|
||||
import (
|
||||
"brainbuffer/pkg/brainbuffer/domain/appointment"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Summary struct {
|
||||
StartTime time.Time
|
||||
EndTime time.Time
|
||||
Entries map[time.Time]appointment.Appointments
|
||||
}
|
||||
59
pkg/brainbuffer/domain/task/dto.go
Normal file
59
pkg/brainbuffer/domain/task/dto.go
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
package task
|
||||
|
||||
import (
|
||||
"brainbuffer/pkg/brainbuffer/domain/scheduling"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CreationModel struct {
|
||||
Patterns []string `json:"patterns"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Duration int64 `json:"duration"`
|
||||
}
|
||||
|
||||
type ResourceModel struct {
|
||||
ID int64 `json:"id"`
|
||||
UserID string `json:"userId"`
|
||||
Patterns []string `json:"patterns"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Duration int64 `json:"duration"`
|
||||
CreationTime int64 `json:"creationTime"`
|
||||
}
|
||||
|
||||
func (model *CreationModel) MapModelToEntity(userId string) Task {
|
||||
patterns := make([]scheduling.Pattern, len(model.Patterns))
|
||||
|
||||
for i, v := range model.Patterns {
|
||||
patterns[i] = scheduling.Pattern(v)
|
||||
}
|
||||
|
||||
return Task{
|
||||
ID: 0,
|
||||
UserID: userId,
|
||||
SchedulingPatterns: patterns,
|
||||
Name: model.Name,
|
||||
Description: model.Description,
|
||||
Duration: time.Duration(model.Duration),
|
||||
CreationTime: time.Time{},
|
||||
}
|
||||
}
|
||||
|
||||
func MapEntityToModel(task *Task) ResourceModel {
|
||||
patterns := make([]string, len(task.SchedulingPatterns))
|
||||
|
||||
for i, v := range task.SchedulingPatterns {
|
||||
patterns[i] = string(v)
|
||||
}
|
||||
|
||||
return ResourceModel{
|
||||
ID: task.ID,
|
||||
UserID: task.UserID,
|
||||
Patterns: patterns,
|
||||
Name: task.Name,
|
||||
Description: task.Description,
|
||||
Duration: int64(task.Duration),
|
||||
CreationTime: task.CreationTime.Unix(),
|
||||
}
|
||||
}
|
||||
18
pkg/brainbuffer/domain/task/entity.go
Normal file
18
pkg/brainbuffer/domain/task/entity.go
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
package task
|
||||
|
||||
import (
|
||||
"brainbuffer/pkg/brainbuffer/domain/scheduling"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Task struct {
|
||||
ID int64
|
||||
UserID string
|
||||
SchedulingPatterns []scheduling.Pattern
|
||||
Name string
|
||||
Description string
|
||||
Duration time.Duration
|
||||
CreationTime time.Time
|
||||
}
|
||||
|
||||
type Tasks []*Task
|
||||
34
pkg/brainbuffer/domain/task/handlers.go
Normal file
34
pkg/brainbuffer/domain/task/handlers.go
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
package task
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/labstack/echo/v4"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func creationHandler(ctx echo.Context, service Service) error {
|
||||
var model CreationModel
|
||||
if err := json.NewDecoder(ctx.Request().Body).Decode(&model); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Invalid data format.")
|
||||
}
|
||||
|
||||
userId := ctx.Param("userId")
|
||||
|
||||
entity := model.MapModelToEntity(userId)
|
||||
|
||||
if err := service.Create(&entity); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return ctx.NoContent(http.StatusCreated)
|
||||
}
|
||||
|
||||
func AddHandlers(e *echo.Echo) {
|
||||
taskService := NewService()
|
||||
|
||||
userTasksGroup := e.Group("/users/:userId/tasks")
|
||||
|
||||
userTasksGroup.POST("", func(ctx echo.Context) error {
|
||||
return creationHandler(ctx, taskService)
|
||||
})
|
||||
}
|
||||
44
pkg/brainbuffer/domain/task/in_memo_repo.go
Normal file
44
pkg/brainbuffer/domain/task/in_memo_repo.go
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
package task
|
||||
|
||||
import (
|
||||
"brainbuffer/pkg/brainbuffer/domain/repository"
|
||||
)
|
||||
|
||||
type inMemoryRepository struct {
|
||||
db map[int64]*Task
|
||||
idCounter int64
|
||||
}
|
||||
|
||||
func (repo *inMemoryRepository) Save(task *Task) (int64, error) {
|
||||
repo.idCounter++
|
||||
task.ID = repo.idCounter
|
||||
repo.db[repo.idCounter] = task
|
||||
|
||||
return task.ID, nil
|
||||
}
|
||||
|
||||
func (repo *inMemoryRepository) FindByID(id int64) (*Task, error) {
|
||||
if v, ok := repo.db[id]; ok {
|
||||
return v, nil
|
||||
} else {
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (repo *inMemoryRepository) FindAllByUser(userId string, page repository.Page) (Tasks, error) {
|
||||
userTasks := make(Tasks, 0)
|
||||
|
||||
for _, val := range repo.db {
|
||||
if val.UserID == userId {
|
||||
userTasks = append(userTasks, val)
|
||||
}
|
||||
}
|
||||
|
||||
return userTasks, nil
|
||||
}
|
||||
|
||||
func (repo *inMemoryRepository) DeleteByID(id int64) error {
|
||||
delete(repo.db, id)
|
||||
|
||||
return nil
|
||||
}
|
||||
16
pkg/brainbuffer/domain/task/repository.go
Normal file
16
pkg/brainbuffer/domain/task/repository.go
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
package task
|
||||
|
||||
import (
|
||||
"brainbuffer/pkg/brainbuffer/domain/repository"
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
Save(task *Task) (int64, error)
|
||||
FindByID(id int64) (*Task, error)
|
||||
FindAllByUser(userId string, page repository.Page) (Tasks, error)
|
||||
DeleteByID(id int64) error
|
||||
}
|
||||
|
||||
func NewRepository() Repository {
|
||||
return &inMemoryRepository{}
|
||||
}
|
||||
98
pkg/brainbuffer/domain/task/service.go
Normal file
98
pkg/brainbuffer/domain/task/service.go
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
package task
|
||||
|
||||
import (
|
||||
"brainbuffer/pkg/brainbuffer/domain/appointment"
|
||||
"brainbuffer/pkg/brainbuffer/domain/repository"
|
||||
"brainbuffer/pkg/brainbuffer/domain/scheduling"
|
||||
apperrors "brainbuffer/pkg/brainbuffer/infrastructure/errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Service interface {
|
||||
Create(task *Task) error
|
||||
GetAllByUser(userId string, page repository.Page) (Tasks, error)
|
||||
GetByID(id int64) (*Task, error)
|
||||
DeleteByID(id int64) error
|
||||
}
|
||||
|
||||
type defaultService struct {
|
||||
repository Repository
|
||||
appointmentService appointment.Service
|
||||
}
|
||||
|
||||
func NewService() Service {
|
||||
return &defaultService{
|
||||
repository: NewRepository(),
|
||||
appointmentService: appointment.NewService(),
|
||||
}
|
||||
}
|
||||
|
||||
func (serv *defaultService) GetByID(id int64) (*Task, error) {
|
||||
task, err := serv.repository.FindByID(id)
|
||||
if err != nil {
|
||||
return nil, apperrors.UnknownError{Err: err}
|
||||
}
|
||||
|
||||
if task == nil {
|
||||
return nil, apperrors.NotFoundError{Message: "Task with given ID was not found."}
|
||||
}
|
||||
|
||||
return task, err
|
||||
}
|
||||
|
||||
func (serv *defaultService) Create(task *Task) error {
|
||||
if len(task.SchedulingPatterns) != 0 {
|
||||
nextTime, pattern, err := scheduling.Patterns(task.SchedulingPatterns).NextTime(time.Now())
|
||||
if err != nil {
|
||||
return apperrors.UnknownError{Err: err}
|
||||
}
|
||||
|
||||
firstAppointment := appointment.Appointment{
|
||||
ID: 0,
|
||||
TaskID: 0,
|
||||
CompletionStatus: appointment.None,
|
||||
SchedulingPattern: pattern,
|
||||
PlannedTime: *nextTime,
|
||||
DurationOffset: 0,
|
||||
CreationTime: time.Now(),
|
||||
}
|
||||
|
||||
taskId, err := serv.repository.Save(task)
|
||||
if err != nil {
|
||||
return apperrors.UnknownError{Err: err}
|
||||
}
|
||||
|
||||
firstAppointment.TaskID = taskId
|
||||
|
||||
if err := serv.appointmentService.Create(&firstAppointment); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if _, err := serv.repository.Save(task); err != nil {
|
||||
return apperrors.UnknownError{Err: err}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (serv *defaultService) GetAllByUser(userId string, page repository.Page) (Tasks, error) {
|
||||
tasks, err := serv.repository.FindAllByUser(userId, page)
|
||||
if err != nil {
|
||||
return nil, apperrors.UnknownError{Err: err}
|
||||
}
|
||||
|
||||
return tasks, nil
|
||||
}
|
||||
|
||||
func (serv *defaultService) DeleteByID(id int64) error {
|
||||
if err := serv.appointmentService.DeleteAllByTask(id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := serv.repository.DeleteByID(id); err != nil {
|
||||
return apperrors.UnknownError{Err: err}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue