/
subcategory.routes.go
119 lines (97 loc) · 3.52 KB
/
subcategory.routes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package routes
import (
"log"
"net/http"
"github.com/AlexVonEinzbern/go-rest-api/controllers"
"github.com/AlexVonEinzbern/go-rest-api/models"
"github.com/fatih/structs"
"github.com/gin-gonic/gin"
)
// CreateSubcategory godoc
// @Summary Create Subcategory
// @Description Create a Subcategory
// @Accept json
// @Produce json
// @Tags Subcategories
// @Param subcategory body schemas.Subcategory true "Create a subcategory"
// @Success 200 {object} schemas.SubcategoryResponse
// @Failure 404 {object} models.APIError "Can not find objects"
// @Router /subcategories [post]
func CreateSubcategory(c *gin.Context) {
var createsubcategory models.SubcategoryCreate
err := c.BindJSON(&createsubcategory)
if err != nil {
log.Fatal(err)
}
log.Println("Starting with Subcategory creation: ", createsubcategory)
c.IndentedJSON(http.StatusOK, controllers.CreateSubcategory(createsubcategory))
}
// SearchAllSubcategories godoc
// @Summary Search all Subcategories
// @Description Search all subcatagories in the DataBase
// @Accept json
// @Produce json
// @Tags Subcategories
// @Success 200 {object} []schemas.SubcategoryResponse
// @Failure 404 {object} models.APIError "Can not find objects"
// @Router /subcategories [get]
func SearchSubcategories(c *gin.Context) {
c.IndentedJSON(http.StatusOK, controllers.SearchSubcategories())
}
// SearchActiveSubcategory godoc
// @Summary Search all active Subcategories
// @Description Search all active categories in the DataBase
// @Accept json
// @Produce json
// @Tags Subcategories
// @Success 200 {object} []schemas.SubcategoryResponse
// @Failure 404 {object} models.APIError "Can not find objects"
// @Router /subcategories/active [get]
func SearchActiveSubcategory(c *gin.Context) {
c.IndentedJSON(http.StatusOK, controllers.SearchSubcategoriesActive())
}
// UpdateSubcategory godoc
// @Summary Update Subcategory by id
// @Description Update a subcategory by id
// @Accept json
// @Produce json
// @Tags Subcategories
// @Param id path string true "Subcategory ID" example(2ld1f12f-2227-8s08-18cc-222fdb9634xx)
// @Param Subcategory body schemas.Subcategory true "Update Subcategory"
// @Success 204 {object} schemas.SubcategoryResponse
// @Failure 404 {object} models.APIError "Can not find objects"
// @Router /subcategories/{id} [patch]
func UpdateSubcategory(c *gin.Context) {
var updatesubcategory models.SubcategoryBase
id := c.Param("id")
if err_bind := c.BindJSON(&updatesubcategory); err_bind != nil {
c.IndentedJSON(http.StatusBadRequest, models.APIError{ErrorCode: 401, ErrorMessage: err_bind.Error()})
return
}
log.Println("Data to be update: ", updatesubcategory)
subcategory, err := controllers.UpdateSubcategory(id, structs.Map(updatesubcategory))
if err != nil {
c.IndentedJSON(http.StatusNotFound, models.APIError{ErrorCode: 404, ErrorMessage: "Not found"})
return
}
c.IndentedJSON(http.StatusOK, subcategory)
}
// DeleteSubcategory godoc
// @Summary Delete a Subcategory by id
// @Description Delete a subcategory by id
// @Accept json
// @Produce json
// @Tags Subcategories
// @Param id path string true "Subcategory ID" example(2ld1f12f-2227-8s08-18cc-222fdb9634xx)
// @Success 204
// @Failure 404 {object} models.APIError "Can not find objects"
// @Router /subcategories/{id} [delete]
func DeleteSubcategoty(c *gin.Context) {
id := c.Param("id")
subcategory, err := controllers.DeleteSubcategory(id)
if err != nil {
c.IndentedJSON(http.StatusOK, models.APIError{ErrorCode: 404, ErrorMessage: "Not found"})
return
}
c.IndentedJSON(http.StatusNoContent, subcategory)
}