diff options
Diffstat (limited to 'invoice')
-rw-r--r-- | invoice/controller.go | 27 | ||||
-rw-r--r-- | invoice/router.go | 1 | ||||
-rw-r--r-- | invoice/service.go | 4 |
3 files changed, 29 insertions, 3 deletions
diff --git a/invoice/controller.go b/invoice/controller.go index 7260834..6bd5ad5 100644 --- a/invoice/controller.go +++ b/invoice/controller.go @@ -74,7 +74,32 @@ func handleGetInvoices (ctx *gin.Context) { userId := uId.(uint) - err := getInvoices(&invoices, userId) + err := getInvoices(&invoices, userId, false) + if err != nil { + ctx.Error(err) + ctx.Abort() + return + } + + ctx.JSON(http.StatusOK, gin.H{ + "message": "success", + "data": invoices, + }) +} + +func handleGetDrafts (ctx *gin.Context) { + var invoices []Invoice + + uId, ok := ctx.Get("UserID") + if !ok { + ctx.Error(e.ErrUnauthorized) + ctx.Abort() + return + } + + userId := uId.(uint) + + err := getInvoices(&invoices, userId, true) if err != nil { ctx.Error(err) ctx.Abort() diff --git a/invoice/router.go b/invoice/router.go index 8e0a0ca..7631cb9 100644 --- a/invoice/router.go +++ b/invoice/router.go @@ -25,6 +25,7 @@ func Routes(route *gin.RouterGroup) { g := route.Group("/invoice") { g.GET("/", handleGetInvoices) + g.GET("/draft", handleGetDrafts) g.GET("/:id", handleGetSingleInvoice) g.POST("/", handleSaveInvoice) g.DELETE("/:id", handleDelInvoice) diff --git a/invoice/service.go b/invoice/service.go index 9408a0d..91e2707 100644 --- a/invoice/service.go +++ b/invoice/service.go @@ -66,8 +66,8 @@ func getInvoice(invoice *Invoice, id uint) error { return nil } -func getInvoices(invoices *[]Invoice, userId uint) error { - res := db.Where("user_id = ?", userId).Find(&invoices) +func getInvoices(invoices *[]Invoice, userId uint, isDraft bool) error { + res := db.Where("user_id = ? and is_draft = ?", userId, isDraft).Find(&invoices) // TODO: handle potential errors if res.Error != nil { |