/
mpstrkv
/
SpbTechRun
Обзор
Документация
Войти
/
mpstrkv
/
SpbTechRun
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
backend/internal/handler/category.go
45 строк
1 KB
mihailpestrikov
Initial commit: SpbTechRun recommendation system
05 дек 2025, 16:11
05 дек 2025, 16:11
8ba7137
Код
Авторство
О чём код?
package handler import ( "net/http" "strconv" "github.com/gin-gonic/gin" "github.com/mpstrkv/spbtechrun/internal/dto" "github.com/mpstrkv/spbtechrun/internal/repository" ) type CategoryHandler struct { repo *repository.CategoryRepository } func NewCategoryHandler(repo *repository.CategoryRepository) *CategoryHandler { return &CategoryHandler{repo: repo} } func (h *CategoryHandler) GetCategories(c *gin.Context) { categories, err := h.repo.GetAll(c.Request.Context()) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, dto.CategoriesToResponse(categories)) } func (h *CategoryHandler) GetCategory(c *gin.Context) { id, err := strconv.Atoi(c.Param("id")) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "invalid category id"}) return } category, err := h.repo.GetByID(c.Request.Context(), id) if err != nil { c.JSON(http.StatusNotFound, gin.H{"error": "category not found"}) return } c.JSON(http.StatusOK, dto.CategoryToResponse(category)) }