diff options
Diffstat (limited to 'item')
-rw-r--r-- | item/item_router.go | 57 |
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) }) } |