Skip to content

Commit

Permalink
Merge pull request #26 from takumi-pro/feature/event-detail-api
Browse files Browse the repository at this point in the history
イベント詳細情報取得API実装
  • Loading branch information
takumi-pro authored Jan 6, 2024
2 parents fd81486 + e16ae34 commit ed334aa
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 15 deletions.
10 changes: 8 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ services:
target: dev
container_name: devlocator
depends_on:
- db
db:
condition: service_healthy
volumes:
- .:/go/src/app
ports:
Expand All @@ -24,4 +25,9 @@ services:
MYSQL_USER: ${DB_USER}
MYSQL_PASSWORD: ${DB_PASSWORD}
ports:
- 3307:3306
- 3307:3306
healthcheck: # healthcheckの設定
test: mysqladmin ping -h 127.0.0.1 -u ${MYSQL_USER} -p ${MYSQL_PASSWORD} # MySQLサーバーへのping
interval: 10s # 10秒ごとにhealthcheckを実行
timeout: 5s # healthcheckのタイムアウト時間
retries: 3 # 失敗時のリトライ回数
20 changes: 13 additions & 7 deletions handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ package handlers

import (
"devlocator/database"
"devlocator/interfaces"
"devlocator/models"
"devlocator/openapi"
"devlocator/repositories"
"net/http"

"github.com/labstack/echo/v4"
"gorm.io/gorm"
)

type Server struct {
DB *gorm.DB
EventRepository interfaces.EventRepositoryInterface
}

type TestResponse struct {
Expand All @@ -37,7 +36,7 @@ func (s Server) GetApiEvent(ctx echo.Context, params openapi.GetApiEventParams)
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}

events, count, err := repositories.NewEventRepository(s.DB).GetEvents(params)
events, count, err := s.EventRepository.GetEvents(params)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
Expand All @@ -55,9 +54,16 @@ func (s Server) PutApiEventBookmark(ctx echo.Context) error {
}

func (s Server) GetApiEventsEventId(ctx echo.Context, eventId string) error {
return ctx.JSON(http.StatusOK, TestResponse{
Message: "detail event",
})
event, err := s.EventRepository.GetDetailEvent(eventId)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}

type EventDetailResponse struct {
Event models.Event `json:"event"`
}
var responseEvent = EventDetailResponse{Event: event}
return ctx.JSON(http.StatusOK, responseEvent)
}

func (s Server) GetApiUsers(ctx echo.Context) error {
Expand Down
3 changes: 2 additions & 1 deletion interfaces/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"devlocator/openapi"
)

type EventRepository interface {
type EventRepositoryInterface interface {
GetEvents(params openapi.GetApiEventParams) ([]models.Event, int64, error)
GetDetailEvent(eventId string) (models.Event, error)
}
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"devlocator/database"
"devlocator/handlers"
"devlocator/openapi"
"devlocator/repositories"
"fmt"
"os"
"regexp"
Expand Down Expand Up @@ -56,7 +57,8 @@ func main() {
e.Logger.Fatal(err)
}

s := handlers.Server{DB: db}
eventRepo := repositories.NewEventRepository(db)
s := handlers.Server{EventRepository: eventRepo}
openapi.RegisterHandlers(e, s)
e.Logger.Fatal(e.Start(":" + port))
}
10 changes: 6 additions & 4 deletions repositories/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ 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) GetDetailEvent(eventId string) (models.Event, error) {
var event models.Event
result := repo.db.Model(&models.Event{}).Where("event_id = ?", eventId).First(&event)
return event, result.Error
}

func (repo *EventRepository) GetEvents(params openapi.GetApiEventParams) ([]models.Event, int64, error) {
var events []models.Event
var count int64
Expand Down

0 comments on commit ed334aa

Please sign in to comment.