summaryrefslogtreecommitdiff
path: root/item
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@vidhukant.xyz>2022-09-26 23:59:01 +0530
committerVidhu Kant Sharma <vidhukant@vidhukant.xyz>2022-09-26 23:59:01 +0530
commit019fb13d4ea1ce9f905edbacff39717892af01dc (patch)
treea532f1187fc5de7e1c74fef695fcd0bbd36c7fef /item
parent3637ed7f0a50e36f3acfbacc0f81bb7141e7a2d4 (diff)
fixed all the delete routes, temporarily disabled adding new documents
Diffstat (limited to 'item')
-rw-r--r--item/item_router.go57
1 files changed, 42 insertions, 15 deletions
diff --git a/item/item_router.go b/item/item_router.go
index f57dd2e..cc58793 100644
--- a/item/item_router.go
+++ b/item/item_router.go
@@ -1,50 +1,77 @@
+/* OpenBills-server - Server for libre billing software OpenBills-web
+ * Copyright (C) 2022 Vidhu Kant Sharma <vidhukant@vidhukant.xyz>
+
+ * 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 (
"github.com/gin-gonic/gin"
"github.com/MikunoNaka/OpenBills-lib/item"
+ "go.mongodb.org/mongo-driver/bson/primitive"
"log"
"net/http"
)
-
func Routes(route *gin.Engine) {
i := route.Group("/item")
{
- i.GET("/", func(ctx *gin.Context) {
- // TODO: add functionality to filter results
+ // TODO: add functionality to filter results
+ // /all returns all the saved items
+ i.GET("/all", func(ctx *gin.Context) {
items, err := item.GetItems(nil)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
log.Printf("ERROR: Failed to read items from DB: %v\n", err.Error())
+ return
}
ctx.JSON(http.StatusOK, items)
})
- i.POST("/", func(ctx *gin.Context) {
- var x item.Item
- ctx.Bind(&x)
- err := x.Save()
+ i.POST("/new", func(ctx *gin.Context) {
+ var i item.Item
+ ctx.BindJSON(&i)
+ _, err := item.SaveItem(i)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
- log.Printf("ERROR: Failed to add new item \"%s\": %v\n", x.Name, err.Error())
+ log.Printf("ERROR: Failed to add new item %v to DB: %v\n", i, err.Error())
+ return
}
- log.Println("Added new item to database: ", x.Name)
+ log.Printf("Successfully saved new item to DB: %v", i)
ctx.JSON(http.StatusOK, nil)
})
- i.DELETE("/", func(ctx *gin.Context) {
- var x item.Item
- ctx.Bind(&x)
- err := x.Delete()
+ i.DELETE("/:itemId", func(ctx *gin.Context) {
+ id := ctx.Param("itemId")
+ objectId, err := primitive.ObjectIDFromHex(id)
+ if err != nil {
+ ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
+ log.Printf("ERROR: Failed to delete item, Error parsing ID: %v\n", err.Error())
+ return
+ }
+
+ err = item.DeleteItem(objectId)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
- log.Printf("ERROR: Failed to delete item \"%s\": %v\n", x.Name, err.Error())
+ log.Printf("ERROR: Failed to delete item %v: %v\n", objectId, err.Error())
+ return
}
- log.Println("Deleted item: ", x.Name)
+ log.Printf("Deleted item %v from database.\n", objectId )
ctx.JSON(http.StatusOK, nil)
})
}