summaryrefslogtreecommitdiff
path: root/brand/brand_router.go
blob: c6aa6618b7ac0e2a8ebd1a92dc6850d37302751b (plain)
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
package brand

import (
	"github.com/gin-gonic/gin"
	"github.com/MikunoNaka/OpenBills-lib/brand"
	"log"
	"net/http"
)


func Routes(route *gin.Engine) {
	b := route.Group("/brand")
	{
		b.GET("/", func(ctx *gin.Context) {
			// TODO: add functionality to filter results
			brands, err := brand.GetBrands(nil)
			if err != nil {
				ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
				log.Printf("ERROR: Failed to read brands from DB: %v\n", err.Error())
			}

			ctx.JSON(http.StatusOK, brands)
		})

		b.POST("/", func(ctx *gin.Context) {
			var x brand.Brand
			ctx.Bind(&x)
			err := x.Save()
			if err != nil {
				ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
				log.Printf("ERROR: Failed to add new brand \"%s\": %v\n", x.Name, err.Error())
			}

			log.Println("Added new brand to database: ", x.Name)
			ctx.JSON(http.StatusOK, nil)
		})

		b.DELETE("/", func(ctx *gin.Context) {
			var x brand.Brand
			ctx.Bind(&x)
			err := x.Delete()
			if err != nil {
				ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
				log.Printf("ERROR: Failed to delete brand \"%s\": %v\n", x.Name, err.Error())
			}

			log.Println("Delete brand: ", x.Name)
			ctx.JSON(http.StatusOK, nil)
		})
	}
}