From 019fb13d4ea1ce9f905edbacff39717892af01dc Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Mon, 26 Sep 2022 23:59:01 +0530 Subject: fixed all the delete routes, temporarily disabled adding new documents --- brand/brand_router.go | 47 +++++++++++++-------- client/client_router.go | 47 +++++++++++++-------- go.mod | 4 +- go.sum | 4 +- invoice/invoice_router.go | 105 ++++++++++++++++++++++++---------------------- item/item_router.go | 57 ++++++++++++++++++------- main.go | 17 ++++++++ 7 files changed, 177 insertions(+), 104 deletions(-) diff --git a/brand/brand_router.go b/brand/brand_router.go index c6aa661..264c693 100644 --- a/brand/brand_router.go +++ b/brand/brand_router.go @@ -1,7 +1,25 @@ +/* OpenBills-server - Server for libre billing software OpenBills-web + * Copyright (C) 2022 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 brand import ( "github.com/gin-gonic/gin" + "go.mongodb.org/mongo-driver/bson/primitive" "github.com/MikunoNaka/OpenBills-lib/brand" "log" "net/http" @@ -11,40 +29,35 @@ import ( func Routes(route *gin.Engine) { b := route.Group("/brand") { - b.GET("/", func(ctx *gin.Context) { + b.GET("/all", 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()) + return } ctx.JSON(http.StatusOK, brands) }) - b.POST("/", func(ctx *gin.Context) { - var x brand.Brand - ctx.Bind(&x) - err := x.Save() + b.DELETE("/:brandId", func(ctx *gin.Context) { + id := ctx.Param("brandId") + objectId, err := primitive.ObjectIDFromHex(id) 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()) + ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + log.Printf("ERROR: Failed to delete brand, Error parsing ID: %v\n", err.Error()) + return } - 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() + err = brand.DeleteBrand(objectId) 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.Printf("ERROR: Failed to delete brand %v: %v\n", objectId, err.Error()) + return } - log.Println("Delete brand: ", x.Name) + log.Printf("Deleted brand %v from database.\n", objectId ) ctx.JSON(http.StatusOK, nil) }) } diff --git a/client/client_router.go b/client/client_router.go index cc9e19d..3011a8b 100644 --- a/client/client_router.go +++ b/client/client_router.go @@ -1,3 +1,20 @@ +/* OpenBills-server - Server for libre billing software OpenBills-web + * Copyright (C) 2022 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 client import ( @@ -5,45 +22,41 @@ import ( "github.com/MikunoNaka/OpenBills-lib/client" "log" "net/http" + "go.mongodb.org/mongo-driver/bson/primitive" ) func Routes(route *gin.Engine) { c := route.Group("/client") { - c.GET("/", func(ctx *gin.Context) { + c.GET("/all", func(ctx *gin.Context) { // TODO: add functionality to filter results clients, err := client.GetClients(nil) if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) log.Printf("ERROR: Failed to read clients from DB: %v\n", err.Error()) + return } ctx.JSON(http.StatusOK, clients) }) - c.POST("/", func(ctx *gin.Context) { - var x client.Client - ctx.Bind(&x) - err := x.Save() + c.DELETE("/:clientId", func(ctx *gin.Context) { + id := ctx.Param("clientId") + objectId, err := primitive.ObjectIDFromHex(id) if err != nil { - ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - log.Printf("ERROR: Failed to add new client \"%s\": %v\n", x.Name, err.Error()) + ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + log.Printf("ERROR: Failed to delete client, Error parsing ID: %v\n", err.Error()) + return } - log.Println("Added new client to database: ", x.Name) - ctx.JSON(http.StatusOK, nil) - }) - - c.DELETE("/", func(ctx *gin.Context) { - var x client.Client - ctx.Bind(&x) - err := x.Delete() + err = client.DeleteClient(objectId) if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - log.Printf("ERROR: Failed to delete client \"%s\": %v\n", x.Name, err.Error()) + log.Printf("ERROR: Failed to delete client %v: %v\n", objectId, err.Error()) + return } - log.Println("Deleted client: ", x.Name) + log.Printf("Deleted client %v from database.\n", objectId ) ctx.JSON(http.StatusOK, nil) }) } diff --git a/go.mod b/go.mod index ab696a9..f40afc5 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,9 @@ module github.com/MikunoNaka/OpenBills-server go 1.19 require ( - github.com/MikunoNaka/OpenBills-lib v0.0.2 + github.com/MikunoNaka/OpenBills-lib v1.1.1 github.com/gin-gonic/gin v1.8.1 + go.mongodb.org/mongo-driver v1.10.2 ) require ( @@ -28,7 +29,6 @@ require ( github.com/xdg-go/scram v1.1.1 // indirect github.com/xdg-go/stringprep v1.0.3 // indirect github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect - go.mongodb.org/mongo-driver v1.10.2 // indirect golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect diff --git a/go.sum b/go.sum index 7338d42..d319660 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/MikunoNaka/OpenBills-lib v0.0.2 h1:65sqsQpxSd8IbaQpQmnKdKBjIY2CAzzYk6u5mi534X4= -github.com/MikunoNaka/OpenBills-lib v0.0.2/go.mod h1:uAM49uISC12jAgqstIgehBzSd9QBKUZDde6INRvLyGU= +github.com/MikunoNaka/OpenBills-lib v1.1.1 h1:mIpvg7S4qMsJFXZ2mMP9CfEoUs+kNByyQhGqI7IkLok= +github.com/MikunoNaka/OpenBills-lib v1.1.1/go.mod h1:uAM49uISC12jAgqstIgehBzSd9QBKUZDde6INRvLyGU= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= diff --git a/invoice/invoice_router.go b/invoice/invoice_router.go index 2752675..88c6308 100644 --- a/invoice/invoice_router.go +++ b/invoice/invoice_router.go @@ -1,3 +1,20 @@ +/* OpenBills-server - Server for libre billing software OpenBills-web + * Copyright (C) 2022 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 invoice import ( @@ -5,125 +22,111 @@ import ( "github.com/MikunoNaka/OpenBills-lib/invoice" "log" "net/http" + "go.mongodb.org/mongo-driver/bson/primitive" ) func Routes(route *gin.Engine) { i := route.Group("/invoice") { - i.GET("/", func(ctx *gin.Context) { + i.GET("/all", func(ctx *gin.Context) { // TODO: add functionality to filter results invoices, err := invoice.GetInvoices(nil) if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) log.Printf("ERROR: Failed to read invoices from DB: %v\n", err.Error()) + return } ctx.JSON(http.StatusOK, invoices) }) - i.POST("/", func(ctx *gin.Context) { - var x invoice.Invoice - ctx.Bind(&x) - err := x.Save() + i.DELETE("/:invoiceId", func(ctx *gin.Context) { + id := ctx.Param("invoiceId") + objectId, err := primitive.ObjectIDFromHex(id) if err != nil { - ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - log.Printf("ERROR: Failed to generate new invoice #%d: %v\n", x.InvoiceNumber, err.Error()) + ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + log.Printf("ERROR: Failed to delete invoice, Error parsing ID: %v\n", err.Error()) + return } - log.Printf("Generated new invoice #%d.\n", x.InvoiceNumber) - ctx.JSON(http.StatusOK, nil) - }) - - i.DELETE("/", func(ctx *gin.Context) { - var x invoice.Invoice - ctx.Bind(&x) - err := x.Delete() + err = invoice.DeleteInvoice(objectId) if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - log.Printf("ERROR: Failed to delete invoice #%d: %v\n", x.InvoiceNumber, err.Error()) + log.Printf("ERROR: Failed to delete invoice %v: %v\n", objectId, err.Error()) + return } - log.Printf("Deleted invoice invoice #%d.\n", x.InvoiceNumber) + log.Printf("Deleted invoice %v from database.\n", objectId ) ctx.JSON(http.StatusOK, nil) }) } transport := route.Group("/transport") { - transport.GET("/", func(ctx *gin.Context) { + transport.GET("/all", func(ctx *gin.Context) { // TODO: add functionality to filter results transports, err := invoice.GetTransports(nil) if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) log.Printf("ERROR: Failed to read transport vehicles from DB: %v\n", err.Error()) + return } ctx.JSON(http.StatusOK, transports) }) - transport.POST("/", func(ctx *gin.Context) { - var x invoice.Transport - ctx.Bind(&x) - err := x.Save() + transport.DELETE("/:transportId", func(ctx *gin.Context) { + id := ctx.Param("transportId") + objectId, err := primitive.ObjectIDFromHex(id) if err != nil { - ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - log.Printf("ERROR: Failed to add transport vehicle \"%s\": %v\n", x.VehicleNum, err.Error()) + ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + log.Printf("ERROR: Failed to delete transport vehicle, Error parsing ID: %v\n", err.Error()) + return } - log.Printf("Added new transport vehicle to database: \"%s\"\n", x.VehicleNum) - ctx.JSON(http.StatusOK, nil) - }) - - transport.DELETE("/", func(ctx *gin.Context) { - var x invoice.Transport - ctx.Bind(&x) - err := x.Delete() + err = invoice.DeleteTransport(objectId) if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - log.Printf("ERROR: Failed to delete transport vehicle \"%s\": %v\n", x.VehicleNum, err.Error()) + log.Printf("ERROR: Failed to delete transport vehicle %v: %v\n", objectId, err.Error()) + return } - log.Printf("Deleted transport vehicle: \"%s\"\n", x.VehicleNum) + log.Printf("Deleted transport vehicle %v from database.\n", objectId ) ctx.JSON(http.StatusOK, nil) }) } transporter := route.Group("/transporter") { - transporter.GET("/", func(ctx *gin.Context) { + transporter.GET("/all", func(ctx *gin.Context) { // TODO: add functionality to filter results transporters, err := invoice.GetTransporters(nil) if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) log.Printf("ERROR: Failed to read transporters from DB: %v\n", err.Error()) + return } ctx.JSON(http.StatusOK, transporters) }) - transporter.POST("/", func(ctx *gin.Context) { - var x invoice.Transporter - ctx.Bind(&x) - err := x.Save() + transporter.DELETE("/:transporterId", func(ctx *gin.Context) { + id := ctx.Param("transporterId") + objectId, err := primitive.ObjectIDFromHex(id) if err != nil { - ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - log.Printf("ERROR: Failed to add transporter \"%s\": %v\n", x.Name, err.Error()) + ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + log.Printf("ERROR: Failed to delete transporter, Error parsing ID: %v\n", err.Error()) + return } - log.Printf("Added new transporter to database: \"%s\"\n", x.Name) - ctx.JSON(http.StatusOK, nil) - }) - - transporter.DELETE("/", func(ctx *gin.Context) { - var x invoice.Transporter - ctx.Bind(&x) - err := x.Delete() + err = invoice.DeleteTransporter(objectId) if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - log.Printf("ERROR: Failed to delete transporter \"%s\": %v\n", x.Name, err.Error()) + log.Printf("ERROR: Failed to delete transporter %v: %v\n", objectId, err.Error()) + return } - log.Printf("Deleted transporter: \"%s\"\n", x.Name) + log.Printf("Deleted transporter %v from database.\n", objectId ) ctx.JSON(http.StatusOK, nil) }) } 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 + + * 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 ( "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) }) } diff --git a/main.go b/main.go index 35f1a81..d7a9cd3 100644 --- a/main.go +++ b/main.go @@ -1,3 +1,20 @@ +/* OpenBills-server - Server for libre billing software OpenBills-web + * Copyright (C) 2022 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 main import ( -- cgit v1.2.3