From 127d4fb8ee7f8b141f9fb2f1a8fda45d350f34fc Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sat, 2 Sep 2023 20:12:53 +0530 Subject: first commit --- item/controller.go | 156 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 item/controller.go (limited to 'item/controller.go') diff --git a/item/controller.go b/item/controller.go new file mode 100644 index 0000000..b4e27c1 --- /dev/null +++ b/item/controller.go @@ -0,0 +1,156 @@ +/* openbills - Server for web based Libre Billing Software + * Copyright (C) 2023 Vidhu Kant Sharma + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package item + +import ( + e "vidhukant.com/openbills/errors" + "github.com/gin-gonic/gin" + "net/http" + "strconv" +) + +func handleGetBrandItems (ctx *gin.Context) { + id, err := strconv.ParseUint(ctx.Param("id"), 10, 64) + if err != nil { + ctx.Error(e.ErrInvalidID) + return + } + + var items []SavedItem + err = getBrandItems(&items, uint(id)) + if err != nil { + ctx.Error(err) + ctx.Abort() + return + } + + ctx.JSON(http.StatusOK, gin.H{ + "message": "success", + "data": items, + }) +} + +func handleGetBrands (ctx *gin.Context) { + var brands []Brand + + err := getBrands(&brands) + if err != nil { + ctx.Error(err) + ctx.Abort() + return + } + + ctx.JSON(http.StatusOK, gin.H{ + "message": "success", + "data": brands, + }) +} + +func handleSaveBrand (ctx *gin.Context) { + var brand Brand + ctx.Bind(&brand) + + err := brand.upsert() + if err != nil { + ctx.Error(err) + ctx.Abort() + return + } + + ctx.JSON(http.StatusOK, gin.H{ + "message": "success", + "data": brand, + }) +} + +func handleDelBrand (ctx *gin.Context) { + id, err := strconv.ParseUint(ctx.Param("id"), 10, 64) + if err != nil { + ctx.Error(e.ErrInvalidID) + return + } + + var brand Brand + brand.ID = uint(id) + + err = brand.del() + if err != nil { + ctx.Error(err) + ctx.Abort() + return + } + + ctx.JSON(http.StatusOK, gin.H{ + "message": "success", + }) +} + +func handleGetItems (ctx *gin.Context) { + var items []SavedItem + + err := getItems(&items) + if err != nil { + ctx.Error(err) + ctx.Abort() + return + } + + ctx.JSON(http.StatusOK, gin.H{ + "message": "success", + "data": items, + }) +} + +func handleSaveItem (ctx *gin.Context) { + var item SavedItem + ctx.Bind(&item) + + err := item.upsert() + if err != nil { + ctx.Error(err) + ctx.Abort() + return + } + + ctx.JSON(http.StatusOK, gin.H{ + "message": "success", + "data": item, + }) +} + +func handleDelItem (ctx *gin.Context) { + id, err := strconv.ParseUint(ctx.Param("id"), 10, 64) + if err != nil { + ctx.Error(e.ErrInvalidID) + return + } + + var item SavedItem + item.ID = uint(id) + + err = item.del() + if err != nil { + ctx.Error(err) + ctx.Abort() + return + } + + ctx.JSON(http.StatusOK, gin.H{ + "message": "success", + }) +} -- cgit v1.2.3