aboutsummaryrefslogtreecommitdiff
path: root/item/controller.go
diff options
context:
space:
mode:
Diffstat (limited to 'item/controller.go')
-rw-r--r--item/controller.go156
1 files changed, 156 insertions, 0 deletions
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 <vidhukant@vidhukant.com>
+ *
+ * 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 <https://www.gnu.org/licenses/>.
+ */
+
+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",
+ })
+}