summaryrefslogtreecommitdiff
path: root/invoice/router.go
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@vidhukant.xyz>2022-11-11 22:57:19 +0530
committerVidhu Kant Sharma <vidhukant@vidhukant.xyz>2022-11-11 22:57:19 +0530
commit10eb82c7c00a0a1a75528644fbfbe108a769207f (patch)
treeac5d413830d8e1f15985450230fe20490966fcb6 /invoice/router.go
parent2f4a92b0f1d02096427a2d1c97746bb52cdcc38a (diff)
serving created invoices as both HTML and JSON
Diffstat (limited to 'invoice/router.go')
-rw-r--r--invoice/router.go88
1 files changed, 78 insertions, 10 deletions
diff --git a/invoice/router.go b/invoice/router.go
index 4e33e18..ba6665f 100644
--- a/invoice/router.go
+++ b/invoice/router.go
@@ -20,10 +20,12 @@ package invoice
import (
"github.com/gin-gonic/gin"
"log"
+ "errors"
"net/http"
"strconv"
"go.mongodb.org/mongo-driver/bson/primitive"
- "go.mongodb.org/mongo-driver/bson"
+ "go.mongodb.org/mongo-driver/mongo"
+ //"go.mongodb.org/mongo-driver/bson"
)
func Routes(route *gin.Engine) {
@@ -41,29 +43,95 @@ func Routes(route *gin.Engine) {
ctx.JSON(http.StatusOK, invoices)
})
- // preview invoice
- i.GET("/preview/:invoiceNumber", func(ctx *gin.Context) {
+ // TODO: /preview routes should send error codes as HTML
+ // send invoice as HTML, filtering by InvoiceNumber
+ i.GET("/preview/by-num/:invoiceNumber", func(ctx *gin.Context) {
num := ctx.Param("invoiceNumber")
numInt, _ := strconv.Atoi(num)
- invoice, err := getInvoices(bson.M{"InvoiceNumber": numInt})
+ invoice, err := getInvoiceByNumber(numInt)
if err != nil {
- ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
- log.Printf("ERROR: Failed to read invoice %v from DB: %v\n", numInt, err.Error())
+ if errors.Is(err, mongo.ErrNoDocuments) {
+ ctx.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
+ } else {
+ ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
+ }
+ log.Printf("ERROR: Failed to read invoice #%d from DB: %v\n", numInt, err.Error())
return
}
- if len(invoice) == 0 {
- ctx.JSON(http.StatusNotFound, gin.H{"error": "no invoice with this invoice number"})
- log.Printf("WARN: No invoice with number %v found", numInt)
+ ctx.HTML(http.StatusOK, "invoice.html", gin.H{
+ "Invoice": invoice,
+ })
+ })
+
+ // send invoice as HTML, filtering by ID
+ i.GET("/preview/by-id/:invoiceId", func(ctx *gin.Context) {
+ id, err := primitive.ObjectIDFromHex(ctx.Param("invoiceId"))
+ if err != nil {
+ ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
+ log.Printf("ERROR: Failed to get invoice with ID, Error parsing ID: %v\n", err.Error())
+ return
+ }
+
+ invoice, err := getInvoiceById(id)
+ if err != nil {
+ if errors.Is(err, mongo.ErrNoDocuments) {
+ ctx.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
+ } else {
+ ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
+ }
+ log.Printf("ERROR: Failed to read invoice %v from DB: %v\n", id, err.Error())
return
}
ctx.HTML(http.StatusOK, "invoice.html", gin.H{
- "Invoice": invoice[0],
+ "Invoice": invoice,
})
})
+ // send invoice as JSON, filtering by InvoiceNumber
+ i.GET("/by-num/:invoiceNumber", func(ctx *gin.Context) {
+ num := ctx.Param("invoiceNumber")
+ numInt, _ := strconv.Atoi(num)
+
+ invoice, err := getInvoiceByNumber(numInt)
+ if err != nil {
+ if errors.Is(err, mongo.ErrNoDocuments) {
+ ctx.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
+ } else {
+ ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
+ }
+ log.Printf("ERROR: Failed to read invoice #%d from DB: %v\n", numInt, err.Error())
+ return
+ }
+
+ ctx.JSON(http.StatusOK, invoice)
+ })
+
+ // send invoice as JSON, filtering by ID
+ i.GET("/by-id/:invoiceId", func(ctx *gin.Context) {
+ id, err := primitive.ObjectIDFromHex(ctx.Param("invoiceId"))
+ if err != nil {
+ ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
+ log.Printf("ERROR: Failed to get invoice with ID, Error parsing ID: %v\n", err.Error())
+ return
+ }
+
+ invoice, err := getInvoiceById(id)
+ if err != nil {
+ if errors.Is(err, mongo.ErrNoDocuments) {
+ ctx.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
+ } else {
+ ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
+ }
+ log.Printf("ERROR: Failed to read invoice %v from DB: %v\n", id, err.Error())
+ return
+ }
+
+ ctx.JSON(http.StatusOK, invoice)
+ })
+
i.POST("/new", func(ctx *gin.Context) {
var i Invoice
ctx.BindJSON(&i)