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
|
||||
}
|
||||
22
pkg/brainbuffer/config.go
Normal file
22
pkg/brainbuffer/config.go
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
package brainbuffer
|
||||
|
||||
import (
|
||||
"github.com/knadh/koanf"
|
||||
"github.com/knadh/koanf/providers/env"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func NewConfig() *koanf.Koanf {
|
||||
config := koanf.New(".")
|
||||
|
||||
loadValues(config)
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
func loadValues(c *koanf.Koanf) {
|
||||
envProvider := env.Provider("", ".", func(s string) string {
|
||||
return strings.Replace(strings.ToLower(s), "_", ".", -1)
|
||||
})
|
||||
_ = c.Load(envProvider, nil)
|
||||
}
|
||||
47
pkg/brainbuffer/database/database.go
Normal file
47
pkg/brainbuffer/database/database.go
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/jackc/pgx/v4/pgxpool"
|
||||
"github.com/knadh/koanf"
|
||||
)
|
||||
|
||||
func Pool(config *koanf.Koanf) *pgxpool.Pool {
|
||||
connStr, err := getConnectionString(config)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
pool, err := pgxpool.Connect(context.Background(), connStr)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return pool
|
||||
}
|
||||
|
||||
func getConnectionString(config *koanf.Koanf) (string, error) {
|
||||
username := config.String("db.username")
|
||||
if username == "" {
|
||||
return "", errors.New("database username is missing")
|
||||
}
|
||||
|
||||
password := config.String("db.password")
|
||||
if password == "" {
|
||||
return "", errors.New("database password is missing")
|
||||
}
|
||||
|
||||
name := config.String("db.name")
|
||||
if name == "" {
|
||||
return "", errors.New("database name is missing")
|
||||
}
|
||||
|
||||
host := config.String("db.host")
|
||||
if name == "" {
|
||||
return "", errors.New("database host is missing")
|
||||
}
|
||||
|
||||
return fmt.Sprintf("postgresql://%s:%s@%s/%s", username, password, host, name), nil
|
||||
}
|
||||
27
pkg/brainbuffer/database/migrate.go
Normal file
27
pkg/brainbuffer/database/migrate.go
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/jackc/pgx/v4/pgxpool"
|
||||
"github.com/jackc/tern/migrate"
|
||||
)
|
||||
|
||||
func Migrate(pool *pgxpool.Pool) {
|
||||
conn, err := pool.Acquire(context.Background())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
migrator, err := migrate.NewMigrator(context.Background(), conn.Conn(), "schema_version")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if err = migrator.LoadMigrations("./migrations"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if err = migrator.Migrate(context.Background()); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
29
pkg/brainbuffer/errors/errors.go
Normal file
29
pkg/brainbuffer/errors/errors.go
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
package errors
|
||||
|
||||
type UnknownError struct {
|
||||
Err error
|
||||
}
|
||||
|
||||
func (err UnknownError) Error() string {
|
||||
return err.Err.Error()
|
||||
}
|
||||
|
||||
func (err UnknownError) Unwrap() error {
|
||||
return err.Err
|
||||
}
|
||||
|
||||
type NotFoundError struct {
|
||||
Message string
|
||||
}
|
||||
|
||||
func (err NotFoundError) Error() string {
|
||||
return err.Message
|
||||
}
|
||||
|
||||
type AlreadyExistsError struct {
|
||||
Message string
|
||||
}
|
||||
|
||||
func (err AlreadyExistsError) Error() string {
|
||||
return err.Message
|
||||
}
|
||||
17
pkg/brainbuffer/repository/page.go
Normal file
17
pkg/brainbuffer/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/scheduling/pattern.go
Normal file
112
pkg/brainbuffer/scheduling/pattern.go
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
package scheduling
|
||||
|
||||
import (
|
||||
apperrors "brainbuffer/pkg/brainbuffer/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/scheduling/pattern_test.go
Normal file
62
pkg/brainbuffer/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)
|
||||
}
|
||||
34
pkg/brainbuffer/server/middleware.go
Normal file
34
pkg/brainbuffer/server/middleware.go
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/rs/zerolog/log"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Logger() echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) (err error) {
|
||||
req := c.Request()
|
||||
res := c.Response()
|
||||
start := time.Now()
|
||||
if err = next(c); err != nil {
|
||||
c.Error(err)
|
||||
}
|
||||
stop := time.Now()
|
||||
|
||||
log.Info().
|
||||
Str("remote_ip", c.RealIP()).
|
||||
Str("host", req.Host).
|
||||
Str("uri", req.RequestURI).
|
||||
Str("method", req.Method).
|
||||
Str("path", req.URL.Path).
|
||||
Str("user_agent", req.UserAgent()).
|
||||
Int("status", res.Status).
|
||||
Str("latency", stop.Sub(start).String()).
|
||||
Send()
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
60
pkg/brainbuffer/server/server.go
Normal file
60
pkg/brainbuffer/server/server.go
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
apperrors "brainbuffer/pkg/brainbuffer/errors"
|
||||
"brainbuffer/pkg/brainbuffer/task"
|
||||
"github.com/jackc/pgx/v4/pgxpool"
|
||||
"github.com/knadh/koanf"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
"github.com/rs/zerolog/log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func New(conf *koanf.Koanf, pool *pgxpool.Pool) *echo.Echo {
|
||||
server := echo.New()
|
||||
|
||||
server.HTTPErrorHandler = errorHandler(server)
|
||||
|
||||
registerHandlers(server, pool)
|
||||
registerMiddleware(server, conf)
|
||||
|
||||
return server
|
||||
}
|
||||
|
||||
func registerHandlers(server *echo.Echo, pool *pgxpool.Pool) {
|
||||
task.AddHandlers(server)
|
||||
}
|
||||
|
||||
func registerMiddleware(server *echo.Echo, conf *koanf.Koanf) {
|
||||
server.Use(middleware.CORS())
|
||||
|
||||
server.Use(Logger())
|
||||
|
||||
server.Use(middleware.JWTWithConfig(middleware.JWTConfig{
|
||||
Skipper: func(ctx echo.Context) bool {
|
||||
return false
|
||||
},
|
||||
SigningMethod: middleware.AlgorithmHS256,
|
||||
SigningKey: conf.Bytes("jwt.key"),
|
||||
}))
|
||||
}
|
||||
|
||||
func errorHandler(s *echo.Echo) echo.HTTPErrorHandler {
|
||||
return func(err error, c echo.Context) {
|
||||
mappedErr := err
|
||||
|
||||
switch v := err.(type) {
|
||||
case apperrors.NotFoundError:
|
||||
mappedErr = echo.NewHTTPError(http.StatusNotFound, v.Error())
|
||||
case apperrors.UnknownError:
|
||||
log.Err(v.Err).Send()
|
||||
mappedErr = echo.NewHTTPError(http.StatusInternalServerError)
|
||||
case apperrors.AlreadyExistsError:
|
||||
// TODO: Change status code to hide user identity.
|
||||
mappedErr = echo.NewHTTPError(http.StatusBadRequest, v.Error())
|
||||
}
|
||||
|
||||
s.DefaultHTTPErrorHandler(mappedErr, c)
|
||||
}
|
||||
}
|
||||
12
pkg/brainbuffer/summary/entity.go
Normal file
12
pkg/brainbuffer/summary/entity.go
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
package summary
|
||||
|
||||
import (
|
||||
"brainbuffer/pkg/brainbuffer/appointment"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Summary struct {
|
||||
StartTime time.Time
|
||||
EndTime time.Time
|
||||
Entries map[time.Time]appointment.Appointments
|
||||
}
|
||||
59
pkg/brainbuffer/task/dto.go
Normal file
59
pkg/brainbuffer/task/dto.go
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
package task
|
||||
|
||||
import (
|
||||
"brainbuffer/pkg/brainbuffer/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/task/entity.go
Normal file
18
pkg/brainbuffer/task/entity.go
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
package task
|
||||
|
||||
import (
|
||||
"brainbuffer/pkg/brainbuffer/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/task/handlers.go
Normal file
34
pkg/brainbuffer/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)
|
||||
})
|
||||
}
|
||||
42
pkg/brainbuffer/task/in_memo_repo.go
Normal file
42
pkg/brainbuffer/task/in_memo_repo.go
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
package task
|
||||
|
||||
import "brainbuffer/pkg/brainbuffer/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/task/repository.go
Normal file
16
pkg/brainbuffer/task/repository.go
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
package task
|
||||
|
||||
import (
|
||||
"brainbuffer/pkg/brainbuffer/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/task/service.go
Normal file
98
pkg/brainbuffer/task/service.go
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
package task
|
||||
|
||||
import (
|
||||
"brainbuffer/pkg/brainbuffer/appointment"
|
||||
apperrors "brainbuffer/pkg/brainbuffer/errors"
|
||||
"brainbuffer/pkg/brainbuffer/repository"
|
||||
"brainbuffer/pkg/brainbuffer/scheduling"
|
||||
"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,
|
||||
Status: appointment.Upcoming,
|
||||
SchedulingPattern: pattern,
|
||||
Time: *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