Skip to content

Commit

Permalink
Add serialization step on GetTasks
Browse files Browse the repository at this point in the history
  • Loading branch information
pacholoamit committed Jul 8, 2022
1 parent cedef9b commit e730181
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 42 deletions.
35 changes: 35 additions & 0 deletions api/internal/dto/dto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package dto

import (
"time"
)

type Project struct {
ID uint
CreatedAt time.Time
UpdatedAt time.Time
Name string `json:"name" form:"name" validate:"required"`
Description string `json:"description" form:"description"`
}

//TODO: Remove this later

type Task struct {
ID uint
Title string `json:"title" form:"title" validate:"required"`
Description string `json:"description" form:"description"`
Status string `json:"status" form:"status"`
Label string `json:"label" form:"label"`
ProjectID uint `json:"projectId" form:"projectId"`
CreatedAt time.Time
UpdatedAt time.Time
}

type ProjectWithTasks struct {
ID uint
CreatedAt time.Time
UpdatedAt time.Time
Name string `json:"name" form:"name" validate:"required"`
Description string `json:"description" form:"description"`
Tasks []Task `json:"tasks"`
}
16 changes: 0 additions & 16 deletions api/internal/dto/project.dto.go

This file was deleted.

16 changes: 0 additions & 16 deletions api/internal/dto/task.dto.go

This file was deleted.

22 changes: 15 additions & 7 deletions api/internal/project/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Repository interface {
Create(t *dto.Project) (*dto.Project, error)
Update(id int, t *dto.Project) (*dto.Project, error)
Delete(id int) (dto.Project, error)
GetTasks(id int) (models.Project, error)
GetTasks(id int) (dto.ProjectWithTasks, error)
}

type repository struct {
Expand Down Expand Up @@ -64,17 +64,25 @@ func (r repository) Delete(id int) (dto.Project, error) {
return project, nil
}

func (r repository) GetTasks(id int) (models.Project, error) {
func (r repository) GetTasks(id int) (dto.ProjectWithTasks, error) {
project := models.Project{}
tasks := []models.Task{}
tasks := []dto.Task{}
if err := r.db.Find(&project, id).Error; err != nil {
return project, err
return dto.ProjectWithTasks{}, err
}

if err := r.db.Model(&project).Association("Tasks").Find(&tasks); err != nil {
return project, err
return dto.ProjectWithTasks{}, err
}
project.Tasks = tasks

return project, nil
projectWithTasks := dto.ProjectWithTasks{
ID: project.ID,
Name: project.Name,
Description: project.Description,
CreatedAt: project.CreatedAt,
UpdatedAt: project.UpdatedAt,
Tasks: tasks,
}

return projectWithTasks, nil
}
5 changes: 2 additions & 3 deletions api/internal/project/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/pacholoamit/GO-TASK-MGR/common/log"

"github.com/pacholoamit/GO-TASK-MGR/internal/dto"
"github.com/pacholoamit/GO-TASK-MGR/internal/models"
)

type Service interface {
Expand All @@ -15,7 +14,7 @@ type Service interface {
Create(t *dto.Project) (*dto.Project, error)
Update(id int, t *dto.Project) (*dto.Project, error)
Delete(id int) (dto.Project, error)
GetTasks(id int) (models.Project, error)
GetTasks(id int) (dto.ProjectWithTasks, error)
}

type service struct {
Expand Down Expand Up @@ -93,7 +92,7 @@ func (s *service) Delete(id int) (dto.Project, error) {
return project, nil
}

func (s *service) GetTasks(id int) (models.Project, error) {
func (s *service) GetTasks(id int) (dto.ProjectWithTasks, error) {
data, err := s.repo.GetTasks(id)
if err != nil {
s.logger.Error("Error when Getting tasks:", err)
Expand Down
22 changes: 22 additions & 0 deletions api/pkg/dbcontext.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package dbcontext

import "gorm.io/gorm"

type DBContext interface {
ValidateIfExists(args ...interface{}) error
}

type dbcontext struct {
db *gorm.DB
}

func NewDbContext(db *gorm.DB) *dbcontext {
return &dbcontext{db}
}

func (d *dbcontext) ValidateIfExists(args ...interface{}) error {
if err := d.db.First(args).Error; err != nil {
return err
}
return nil
}

0 comments on commit e730181

Please sign in to comment.