41 lines
947 B
Go
41 lines
947 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 TeamHandler struct {
|
|
Service *service.TeamService
|
|
}
|
|
|
|
func (handler *TeamHandler) List(c *gin.Context) {
|
|
teams, err := handler.Service.List(c.Request.Context())
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
dtos := models.ConvertTeamsToDtos(teams)
|
|
c.JSON(http.StatusOK, dtos)
|
|
}
|
|
|
|
func (handler *TeamHandler) Get(c *gin.Context) {
|
|
id := c.Param("id")
|
|
team, err := handler.Service.Get(c.Request.Context(), id)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "team not found"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, team)
|
|
}
|
|
|
|
func NewTeamHandler(db *gorm.DB) *TeamHandler {
|
|
handler := &TeamHandler{Service: service.NewTeamService(repository.NewTeamRepository(db))}
|
|
return handler
|
|
}
|