41 lines
977 B
Go
41 lines
977 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 VendorHandler struct {
|
|
Service *service.VendorService
|
|
}
|
|
|
|
func (handler *VendorHandler) List(c *gin.Context) {
|
|
vendors, err := handler.Service.List(c.Request.Context())
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
dtos := models.ConvertVendorsToDtos(vendors)
|
|
c.JSON(http.StatusOK, dtos)
|
|
}
|
|
|
|
func (handler *VendorHandler) Get(c *gin.Context) {
|
|
id := c.Param("id")
|
|
vendor, err := handler.Service.Get(c.Request.Context(), id)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "vendor not found"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, vendor)
|
|
}
|
|
|
|
func NewVendorHandler(db *gorm.DB) *VendorHandler {
|
|
handler := &VendorHandler{Service: service.NewVendorService(repository.NewVendorRepository(db))}
|
|
return handler
|
|
}
|