Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/todanni/api/service/auth"
"github.com/todanni/api/service/dashboard"
"github.com/todanni/api/service/project"
"github.com/todanni/api/service/routine"
"github.com/todanni/api/service/task"
"github.com/todanni/api/token"
)
Expand All @@ -34,7 +35,13 @@ func main() {
}

// Perform migrations
err = db.AutoMigrate(&models.User{}, &models.Dashboard{}, &models.Project{}, &models.Task{})
err = db.AutoMigrate(
&models.User{},
&models.Dashboard{},
&models.Project{},
&models.Task{},
&models.Routine{},
&models.RoutineRecord{})
if err != nil {
log.Fatalf("couldn't auto migrate: %v", err)
}
Expand All @@ -50,12 +57,14 @@ func main() {
projectRepo := repository.NewProjectRepository(db)
taskRepo := repository.NewTaskRepository(db)
dashboardRepo := repository.NewDashboardRepository(db)
routineRepo := repository.NewRoutineRepository(db)

// Initialise services
project.NewProjectService(r, *authMiddleware, projectRepo)
task.NewTaskService(r, taskRepo, *authMiddleware)
dashboard.NewDashboardService(r, dashboardRepo)
auth.NewAuthService(r, cfg, userRepo, dashboardRepo, projectRepo, *authMiddleware)
routine.NewRoutineService(r, *authMiddleware, routineRepo)

// Start the servers and listen
log.Fatal(http.ListenAndServe(":8083", r))
Expand Down
19 changes: 19 additions & 0 deletions models/routine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package models

import (
"time"

"gorm.io/gorm"
)

type Routine struct {
gorm.Model
Name string `json:"name"`
Days string `json:"days"`
UserID string `json:"-"`
}

type RoutineRecord struct {
ID uint `json:"id"`
Timestamp time.Time `json:"timestamp"`
}
65 changes: 65 additions & 0 deletions repository/routine_repo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package repository

import (
"gorm.io/gorm"
"gorm.io/gorm/clause"

"github.com/todanni/api/models"
)

type RoutineRepository interface {
CreateRoutine(routine models.Routine) (models.Routine, error)
UpdateRoutine(routine models.Routine) (models.Routine, error)
ListRoutinesByUser(userID string) ([]models.Routine, error)
GetRoutineByID(routineID string) (models.Routine, error)
DeleteRoutine(routineID string) error
CreateRoutineRecord(record models.RoutineRecord) (models.RoutineRecord, error)
DeleteRoutineRecord(record models.RoutineRecord) error
}

type routineRepo struct {
db *gorm.DB
}

func (r *routineRepo) CreateRoutineRecord(record models.RoutineRecord) (models.RoutineRecord, error) {
result := r.db.Create(&record)
return record, result.Error
}

func (r *routineRepo) DeleteRoutineRecord(record models.RoutineRecord) error {
result := r.db.Delete(record)
return result.Error
}

func (r *routineRepo) GetRoutineByID(routineID string) (models.Routine, error) {
var routine models.Routine
result := r.db.First(&routine, routineID)
return routine, result.Error
}

func (r *routineRepo) DeleteRoutine(routineID string) error {
result := r.db.Delete(&models.Routine{}, routineID)
return result.Error
}

func (r *routineRepo) ListRoutinesByUser(userID string) ([]models.Routine, error) {
var routines []models.Routine
result := r.db.Where("user_id = ?", userID).Find(&routines)
return routines, result.Error
}

func (r *routineRepo) CreateRoutine(routine models.Routine) (models.Routine, error) {
result := r.db.Create(&routine)
return routine, result.Error
}

func (r *routineRepo) UpdateRoutine(routine models.Routine) (models.Routine, error) {
result := r.db.Model(&routine).Clauses(clause.Returning{}).Updates(routine)
return routine, result.Error
}

func NewRoutineRepository(db *gorm.DB) RoutineRepository {
return &routineRepo{
db: db,
}
}
31 changes: 31 additions & 0 deletions service/routine/request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package routine

import (
"time"
)

type CreateRoutineRequest struct {
Name string `json:"name"`
Days string `json:"days"`
}

type CreateRoutineResponse struct {
ID uint `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Name string `json:"name"`
Days string `json:"days"`
}

type ListRoutinesResponse struct {
ID uint `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Name string `json:"name"`
Days string `json:"days"`
}

type UpdateRoutineRequest struct {
Name string `json:"name"`
Days string `json:"days"`
}
18 changes: 18 additions & 0 deletions service/routine/routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package routine

import "net/http"

const (
APIPath = "/routines"
)

func (s *routineService) routes() {
r := s.router.PathPrefix(APIPath).Subrouter()
r.Use(s.middleware.JwtMiddleware)

r.HandleFunc("/", s.ListRoutinesHandler).Methods(http.MethodGet)
r.HandleFunc("/", s.CreateRoutineHandler).Methods(http.MethodPost)
r.HandleFunc("/{id}", s.GetRoutineHandler).Methods(http.MethodGet)
r.HandleFunc("/{id}", s.UpdateRoutineHandler).Methods(http.MethodPatch)
r.HandleFunc("/{id}", s.DeleteRoutineHandler).Methods(http.MethodDelete)
}
Loading