34 lines
754 B
Go
34 lines
754 B
Go
package handlers
|
|
|
|
import (
|
|
"kontor-api-gin/internal/models"
|
|
"kontor-api-gin/internal/service"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type PlayerHandler struct {
|
|
Service *service.PlayerService
|
|
}
|
|
|
|
func (handler *PlayerHandler) List(c *gin.Context) {
|
|
players, err := handler.Service.List(c.Request.Context())
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
dtos := models.ConvertPlayersToDtos(players)
|
|
c.JSON(http.StatusOK, dtos)
|
|
}
|
|
|
|
func (handler *PlayerHandler) Get(c *gin.Context) {
|
|
id := c.Param("id")
|
|
player, err := handler.Service.Get(c.Request.Context(), id)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "player not found"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, player)
|
|
}
|