41 lines
965 B
Go
41 lines
965 B
Go
package handlers
|
|
|
|
import (
|
|
"kontor-api-gin/internal/models"
|
|
"kontor-api-gin/internal/repository"
|
|
"kontor-api-gin/internal/service"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type SportHandler struct {
|
|
Service *service.SportService
|
|
}
|
|
|
|
func (handler *SportHandler) List(c *gin.Context) {
|
|
sports, _, err := handler.Service.List(c.Request.Context())
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
dtos := models.ConvertSportsToDtos(sports)
|
|
c.JSON(http.StatusOK, dtos)
|
|
}
|
|
|
|
func (handler *SportHandler) Get(c *gin.Context) {
|
|
id := c.Param("id")
|
|
sport, err := handler.Service.Get(c.Request.Context(), id)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "sport not found"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, sport)
|
|
}
|
|
|
|
func NewSportHandler(db *gorm.DB) *SportHandler {
|
|
handler := &SportHandler{Service: service.NewSportService(repository.NewSportRepository(db))}
|
|
return handler
|
|
}
|