Skip to content

Commit

Permalink
Merge pull request #25 from takumi-pro/feature/refactor-eventapi
Browse files Browse the repository at this point in the history
feature: リポジトリ層の実装
  • Loading branch information
takumi-pro authored Jan 6, 2024
2 parents 7c736cc + 479b9ae commit fd81486
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 44 deletions.
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ services:
dockerfile: ./docker/golang/Dockerfile
target: dev
container_name: devlocator
depends_on:
- db
volumes:
- .:/go/src/app
ports:
Expand Down
2 changes: 1 addition & 1 deletion go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
49 changes: 7 additions & 42 deletions handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions interfaces/event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package interfaces

import (
"devlocator/models"
"devlocator/openapi"
)

type EventRepository interface {
GetEvents(params openapi.GetApiEventParams) ([]models.Event, int64, error)
}
9 changes: 8 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"devlocator/database"
"devlocator/handlers"
"devlocator/openapi"
"fmt"
Expand Down Expand Up @@ -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))
}
64 changes: 64 additions & 0 deletions repositories/event.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit fd81486

Please sign in to comment.