Dump changes
This commit is contained in:
parent
e12550a643
commit
aac2ea1b74
25 changed files with 129 additions and 76 deletions
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