Skip to content

Commit

Permalink
add handling for last_updated
Browse files Browse the repository at this point in the history
  • Loading branch information
furqanmk committed Mar 30, 2023
1 parent 503de9d commit 36ad364
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 49 deletions.
22 changes: 1 addition & 21 deletions batect.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,11 @@ include:
ref: v0.4.0
path: batect-grpc-bundle.yml

containers:
app:
image: golang:1.19
volumes:
- local: <{batect.project_directory}
container: /code
options: cached
- type: cache
name: go-cache
container: /go
- type: local
local: $HOME/.gitconfig
container: /home/container-user/.gitconfig
environment:
GOCACHE: /go/cache
working_directory: /code
run_as_current_user:
enabled: true
home_directory: /home/container-user

tasks:
run:
description: Run the application locally.
run:
container: app
container: golang-build-env
command: go run main.go
environment:
LOG_LEVEL: debug
Expand Down
5 changes: 5 additions & 0 deletions pkg/model/model.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package model

import (
"time"
)

type Task struct {
Id int64
Title string
Description string
Status string
Assignee *string
LastUpdated *time.Time `db:"last_updated"`
}
55 changes: 43 additions & 12 deletions pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"time"

"google.golang.org/protobuf/types/known/timestamppb"

api "github.com/Wattpad/TaskManager/api/task_manager"
"github.com/Wattpad/TaskManager/pkg/model"
"github.com/Wattpad/TaskManager/pkg/storage"
Expand All @@ -22,20 +24,17 @@ func NewService(logger log.Logger, db *storage.Storage) *Service {

func (s *Service) CreateTask(ctx context.Context, request *api.CreateTaskRequest) (*api.CreateTaskResponse, error) {
var lastUpdated time.Time
switch request.GetStatus() {
case api.TaskStatus_TASK_STATUS_NOT_STARTED:
lastUpdated = request.GetDetails().GetNotStartedDetails().GetCreatedDate().AsTime()
case api.TaskStatus_TASK_STATUS_IN_PROGRESS:
lastUpdated = request.GetDetails().GetInProgressDetails().GetStartedDate().AsTime()
// TODO add DONE handling
}

switch request.GetDetails().GetDetails() {
case api.TaskDetails_InProgressDetails:
switch details := request.GetDetails().GetDetails().(type) {
case *api.TaskDetails_NotStartedDetails:
lastUpdated = details.NotStartedDetails.GetCreatedDate().AsTime()
case *api.TaskDetails_InProgressDetails:
lastUpdated = details.InProgressDetails.GetStartedDate().AsTime()
case *api.TaskDetails_DoneDetails:
lastUpdated = details.DoneDetails.GetCompletedDate().AsTime()
}

// TODO include last updated in DB call
task, err := s.db.CreateTask(ctx, request.GetTitle(), request.GetDescription(), request.GetStatus().String())
task, err := s.db.CreateTask(ctx, request.GetTitle(), request.GetDescription(), request.GetStatus().String(), lastUpdated)
if err != nil {
return nil, err
}
Expand All @@ -51,8 +50,40 @@ func (s *Service) GetTask(ctx context.Context, request *api.GetTaskRequest) (*ap
return nil, err
}

status := api.TaskStatus(api.TaskStatus_value[task.Status])
var details api.TaskDetails

switch status {
case api.TaskStatus_TASK_STATUS_NOT_STARTED:
details = api.TaskDetails{
Details: &api.TaskDetails_NotStartedDetails{
NotStartedDetails: &api.NotStartedDetails{CreatedDate: timestamppb.New(*task.LastUpdated)},
},
}
case api.TaskStatus_TASK_STATUS_IN_PROGRESS:
details = api.TaskDetails{
Details: &api.TaskDetails_InProgressDetails{
InProgressDetails: &api.InProgressDetails{StartedDate: timestamppb.New(*task.LastUpdated)},
},
}
case api.TaskStatus_TASK_STATUS_DONE:
details = api.TaskDetails{
Details: &api.TaskDetails_DoneDetails{
DoneDetails: &api.DoneTaskDetails{CompletedDate: timestamppb.New(*task.LastUpdated)},
},
}
}

apiTask := api.Task{
Id: task.Id,
Title: task.Title,
Description: task.Description,
Status: status,
Details: &details,
}

return &api.GetTaskResponse{
Task: task,
Task: &apiTask,
}, nil
}

Expand Down
17 changes: 6 additions & 11 deletions pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package storage

import (
"context"
"time"

_ "github.com/go-sql-driver/mysql" // required for connecting to mysql
"github.com/jmoiron/sqlx"
Expand All @@ -26,9 +27,9 @@ func NewStorage(db *sql.DB, logger log.Logger, driverName sql.DriverName) (*Stor
}, nil
}

func (s *Storage) CreateTask(ctx context.Context, title, description, status string) (*api.Task, error) {
query := "INSERT INTO task(title, description, status) VALUES(?, ?, ?)"
result, err := s.DB.ExecContext(ctx, query, title, description, status)
func (s *Storage) CreateTask(ctx context.Context, title, description, status string, lastUpdated time.Time) (*api.Task, error) {
query := "INSERT INTO task(title, description, status, last_updated) VALUES(?, ?, ?, ?)"
result, err := s.DB.ExecContext(ctx, query, title, description, status, lastUpdated)
if err != nil {
return nil, err
}
Expand All @@ -48,7 +49,7 @@ func (s *Storage) CreateTask(ctx context.Context, title, description, status str
return &task, nil
}

func (s *Storage) GetTaskByID(ctx context.Context, id int64) (*api.Task, error) {
func (s *Storage) GetTaskByID(ctx context.Context, id int64) (*model.Task, error) {
var task model.Task
// functionally equivalent to task := api.Task{}

Expand All @@ -60,13 +61,7 @@ func (s *Storage) GetTaskByID(ctx context.Context, id int64) (*api.Task, error)
return nil, err
}

return &api.Task{
Id: task.Id,
Title: task.Title,
Description: task.Description,
Status: api.TaskStatus(api.TaskStatus_value[task.Status]),
Assignee: task.Assignee,
}, nil
return &task, nil
}

func (s *Storage) UpdateTask(ctx context.Context, id int64, title, description, status string) (*api.Task, error) {
Expand Down
11 changes: 6 additions & 5 deletions pkg/storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package storage_test
import (
"context"
"testing"
"time"

"github.com/DATA-DOG/go-sqlmock"
"github.com/jmoiron/sqlx"
Expand Down Expand Up @@ -35,20 +36,20 @@ func (suite *StorageTestSuite) SetupTest() {
}

func (suite *StorageTestSuite) TestCreateTask() {
// TODO Set expectations
// Set expectations
expectedId := int64(1000001)
title := "test_title"
description := "test_desc"
status := "test_status"

suite.mock.ExpectExec("INSERT INTO task(title, description, status) VALUES(?, ?, ?)").
suite.mock.ExpectExec("INSERT INTO task(title, description, status, last_updated) VALUES(?, ?, ?, ?)").
WithArgs(title, description, status).
WillReturnResult(sqlmock.NewResult(expectedId, 1))

// TODO Call the method under test
task, err := suite.repo.CreateTask(context.Background(), title, description, status)
// Call the method under test
task, err := suite.repo.CreateTask(context.Background(), title, description, status, time.Now())

// TODO Verify method acted as expected
// Verify method acted as expected
suite.NoError(err)
suite.NotNil(task)

Expand Down
11 changes: 11 additions & 0 deletions schema/liquibase/changelog/initial.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,14 @@ databaseChangeLog:
tableName: task
columnName: id
columnDataType: bigint

- changeSet:
id: add-last-updated-column
author: furqan
changes:
- addColumn:
tableName: task
columns:
- column:
name: last_updated
type: datetime

0 comments on commit 36ad364

Please sign in to comment.