Skip to content

Commit

Permalink
refactor: change the dependencies to interface instead of implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
peterxcli committed Jan 19, 2024
1 parent 7c1b755 commit 6b5a6ec
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
6 changes: 2 additions & 4 deletions pkg/controller/event_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ package controller

import (
"bikefest/pkg/model"
"bikefest/pkg/service"

"github.com/gin-gonic/gin"
)

type EventController struct {
eventService model.EventService
asynqService service.AsynqServiceImpl
asynqService model.AsynqNotificationService
}

func NewEventController(eventService model.EventService, asynqService service.AsynqServiceImpl) *EventController {
func NewEventController(eventService model.EventService, asynqService model.AsynqNotificationService) *EventController {
return &EventController{
eventService: eventService,
asynqService: asynqService,
Expand Down
4 changes: 4 additions & 0 deletions pkg/model/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@ type EventService interface {
Delete(ctx context.Context, event *Event) (rowAffected int64, err error)
DeleteByUser(ctx context.Context, userID string, eventID string) (rowAffected int64, err error)
}

type AsynqNotificationService interface {
EnqueueEvent(user_id, event_id, event_start_time string)
}
3 changes: 1 addition & 2 deletions pkg/router/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import (
"bikefest/pkg/controller"
"bikefest/pkg/middleware"
"bikefest/pkg/model"
"bikefest/pkg/service"
)

type Services struct {
UserService model.UserService
EventService model.EventService
AsynqService service.AsynqServiceImpl
AsynqService model.AsynqNotificationService
}

func RegisterRoutes(app *bootstrap.Application, services *Services) {
Expand Down
6 changes: 4 additions & 2 deletions pkg/service/asynq_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"bikefest/pkg/bootstrap"
"bikefest/pkg/model"
"encoding/json"
"log"
"time"
Expand Down Expand Up @@ -41,6 +42,7 @@ func (as *AsynqServiceImpl) EnqueueEvent(user_id, event_id, event_start_time str

location, _ := time.LoadLocation(as.env.Server.TimeZone)
timeForm := "2006/01/02 15:04:05"
//TODO: currently we only set the process time 30 minutes before the event start time
process_time, _ := time.ParseInLocation(timeForm, event_start_time, location)
process_time = process_time.Add(-time.Minute * 30)

Expand All @@ -51,8 +53,8 @@ func (as *AsynqServiceImpl) EnqueueEvent(user_id, event_id, event_start_time str
log.Printf(" [*] Successfully enqueued task: %+v\nThe task should be executed at %s", info, process_time.String())
}

func NewAsynqService(client *asynq.Client, env *bootstrap.Env) AsynqServiceImpl {
return AsynqServiceImpl{
func NewAsynqService(client *asynq.Client, env *bootstrap.Env) model.AsynqNotificationService {
return &AsynqServiceImpl{
client: client,
env: env,
}
Expand Down

0 comments on commit 6b5a6ec

Please sign in to comment.