category.go 671 B

1234567891011121314151617181920212223242526272829
  1. package controller
  2. import (
  3. "kng_feed_api/model"
  4. "net/http"
  5. "github.com/gin-gonic/gin"
  6. )
  7. type Categories struct {
  8. Categories []model.Category `binding:"dive" json:"categories"`
  9. }
  10. func CreateUpdateCategories(context *gin.Context) {
  11. var categories Categories
  12. err := context.ShouldBindJSON(&categories)
  13. if err != nil {
  14. context.JSON(http.StatusBadRequest, gin.H{"error": err.Error})
  15. }
  16. for _, cat := range categories.Categories {
  17. _, err := cat.Save()
  18. if err != nil {
  19. context.JSON(http.StatusBadRequest, gin.H{"error": err.Error, "desc": "Error whyle saving data"})
  20. return
  21. }
  22. }
  23. context.JSON(http.StatusCreated, gin.H{"ok": "Сохранено"})
  24. }