diff --git a/docker-compose.yml b/docker-compose.yml index d82e137..98ea8a4 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,6 +6,8 @@ services: dockerfile: ./docker/golang/Dockerfile target: dev container_name: devlocator + depends_on: + - db volumes: - .:/go/src/app ports: diff --git a/go.work.sum b/go.work.sum index 14b9304..b00a56e 100644 --- a/go.work.sum +++ b/go.work.sum @@ -4,7 +4,7 @@ github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= diff --git a/handlers/handlers.go b/handlers/handlers.go index 3511285..9f9452b 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -4,13 +4,17 @@ import ( "devlocator/database" "devlocator/models" "devlocator/openapi" + "devlocator/repositories" "net/http" - "strings" "github.com/labstack/echo/v4" + "gorm.io/gorm" ) -type Server struct{} +type Server struct { + DB *gorm.DB +} + type TestResponse struct { Message string } @@ -33,50 +37,11 @@ func (s Server) GetApiEvent(ctx echo.Context, params openapi.GetApiEventParams) return echo.NewHTTPError(http.StatusBadRequest, err.Error()) } - db, err := database.DBConnectGorm() + events, count, err := repositories.NewEventRepository(s.DB).GetEvents(params) if err != nil { return echo.NewHTTPError(http.StatusInternalServerError, err.Error()) } - var events = []models.Event{} - var count int64 - eventResponseFields := []string{"event_id", "title", "event_url", "started_at", "ended_at", "limit", "accepted", "waiting", "updated_at", "place", "address", "lat", "lon"} - - query := db.Model(&events) - - searchMethod := "and" - if params.SearchMethod != nil && *params.SearchMethod == "or" { - searchMethod = "or" - } - - if params.Keyword != nil { - keywords := strings.Split(*params.Keyword, ",") - if searchMethod == "and" { - for _, keyword := range keywords { - query = query.Where("title LIKE ? OR description LIKE ?", "%"+keyword+"%", "%"+keyword+"%") - } - } - } - - if params.Date != nil { - dates := strings.Split(*params.Date, ",") - query = query.Where("DATE(started_at) IN ?", dates) - } - - if params.Prefecture != nil { - query = query.Where("address LIKE ?", "%"+*params.Prefecture+"%") - } - - if params.EventId != nil { - query = query.Where("event_id = ?", *params.EventId) - } - - query = query. - Select(eventResponseFields). - Order("started_at ASC"). - Find(&events). - Count(&count) - return ctx.JSON(http.StatusOK, models.EventsResponse{ ResultsReturned: count, Events: events, diff --git a/interfaces/event.go b/interfaces/event.go new file mode 100644 index 0000000..f11aa7b --- /dev/null +++ b/interfaces/event.go @@ -0,0 +1,10 @@ +package interfaces + +import ( + "devlocator/models" + "devlocator/openapi" +) + +type EventRepository interface { + GetEvents(params openapi.GetApiEventParams) ([]models.Event, int64, error) +} diff --git a/main.go b/main.go index e8ece01..9147143 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "devlocator/database" "devlocator/handlers" "devlocator/openapi" "fmt" @@ -49,7 +50,13 @@ func main() { if err := e.Validator.(*CustomValidator).validator.RegisterValidation("datetime", validateDateTime); err != nil { e.Logger.Fatal(err) } - s := handlers.Server{} + + db, err := database.DBConnectGorm() + if err != nil { + e.Logger.Fatal(err) + } + + s := handlers.Server{DB: db} openapi.RegisterHandlers(e, s) e.Logger.Fatal(e.Start(":" + port)) } diff --git a/repositories/event.go b/repositories/event.go new file mode 100644 index 0000000..5719922 --- /dev/null +++ b/repositories/event.go @@ -0,0 +1,64 @@ +package repositories + +import ( + "devlocator/models" + "devlocator/openapi" + "strings" + + "gorm.io/gorm" +) + +type EventRepository struct { + db *gorm.DB +} + +type EventRepositoryInterface interface { + GetEvents(openapi.GetApiEventParams) ([]models.Event, int64, error) +} + +func NewEventRepository(db *gorm.DB) *EventRepository { + return &EventRepository{db: db} +} + +func (repo *EventRepository) GetEvents(params openapi.GetApiEventParams) ([]models.Event, int64, error) { + var events []models.Event + var count int64 + eventResponseFields := []string{"event_id", "title", "event_url", "started_at", "ended_at", "limit", "accepted", "waiting", "updated_at", "place", "address", "lat", "lon"} + + query := repo.db.Model(&events) + + searchMethod := "and" + if params.SearchMethod != nil && *params.SearchMethod == "or" { + searchMethod = "or" + } + + if params.Keyword != nil { + keywords := strings.Split(*params.Keyword, ",") + if searchMethod == "and" { + for _, keyword := range keywords { + query = query.Where("title LIKE ? OR description LIKE ?", "%"+keyword+"%", "%"+keyword+"%") + } + } + } + + if params.Date != nil { + dates := strings.Split(*params.Date, ",") + query = query.Where("DATE(started_at) IN ?", dates) + } + + if params.Prefecture != nil { + query = query.Where("address LIKE ?", "%"+*params.Prefecture+"%") + } + + if params.EventId != nil { + query = query.Where("event_id = ?", *params.EventId) + } + + query = query. + Select(eventResponseFields). + Order("started_at ASC"). + Find(&events). + Count(&count) + + return events, count, nil +}