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 CardHandler struct {
|
|
Service *service.CardService
|
|
}
|
|
|
|
func (handler *CardHandler) List(c *gin.Context) {
|
|
cards, err := handler.Service.List(c.Request.Context())
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
dtos := models.ConvertCardsToDtos(cards)
|
|
c.JSON(http.StatusOK, dtos)
|
|
}
|
|
|
|
func (handler *CardHandler) Get(c *gin.Context) {
|
|
id := c.Param("id")
|
|
card, err := handler.Service.Get(c.Request.Context(), id)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "card not found"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, card)
|
|
}
|
|
|
|
func NewCardHandler(db *gorm.DB) *CardHandler {
|
|
handler := &CardHandler{Service: service.NewCardService(repository.NewCardRepository(db))}
|
|
return handler
|
|
}
|