diff options
-rw-r--r-- | invoice/controller.go | 40 | ||||
-rw-r--r-- | invoice/router.go | 1 | ||||
-rw-r--r-- | invoice/service.go | 15 | ||||
-rw-r--r-- | main.go | 2 |
4 files changed, 57 insertions, 1 deletions
diff --git a/invoice/controller.go b/invoice/controller.go index 6bd5ad5..d3dd51c 100644 --- a/invoice/controller.go +++ b/invoice/controller.go @@ -192,6 +192,46 @@ func handleDelInvoice (ctx *gin.Context) { }) } +// get items belonging to a certain invoice +func handleGetInvoiceItems (ctx *gin.Context) { + id, err := strconv.ParseUint(ctx.Param("id"), 10, 64) + if err != nil { + ctx.Error(e.ErrInvalidID) + ctx.Abort() + return + } + + uId, ok := ctx.Get("UserID") + if !ok { + ctx.Error(e.ErrUnauthorized) + ctx.Abort() + return + } + + userId := uId.(uint) + + + err = checkInvoiceOwnership(uint(id), userId) + if err != nil { + ctx.Error(err) + ctx.Abort() + return + } + + var items []InvoiceItem + err = getInvoiceItems(&items, uint(id)) + if err != nil { + ctx.Error(err) + ctx.Abort() + return + } + + ctx.JSON(http.StatusOK, gin.H{ + "message": "success", + "data": items, + }) +} + func addItem (ctx *gin.Context) { id, err := strconv.ParseUint(ctx.Param("id"), 10, 64) if err != nil { diff --git a/invoice/router.go b/invoice/router.go index 7631cb9..290eacb 100644 --- a/invoice/router.go +++ b/invoice/router.go @@ -29,6 +29,7 @@ func Routes(route *gin.RouterGroup) { g.GET("/:id", handleGetSingleInvoice) g.POST("/", handleSaveInvoice) g.DELETE("/:id", handleDelInvoice) + g.GET("/:id/item", handleGetInvoiceItems) g.POST("/:id/item", addItem) g.DELETE("/item/:id", removeItem) } diff --git a/invoice/service.go b/invoice/service.go index 91e2707..91579c6 100644 --- a/invoice/service.go +++ b/invoice/service.go @@ -81,6 +81,21 @@ func getInvoices(invoices *[]Invoice, userId uint, isDraft bool) error { return nil } +func getInvoiceItems(items *[]InvoiceItem, invoiceId uint) error { + res := db.Where("invoice_id = ?", invoiceId).Find(&items) + + // TODO: handle potential errors + if res.Error != nil { + return res.Error + } + + if res.RowsAffected == 0 { + return e.ErrEmptyResponse + } + + return nil +} + // TODO: route to only get the invouce's items func (i *Invoice) upsert() error { @@ -38,7 +38,7 @@ import ( "log" ) -const OPENBILLS_VERSION = "v0.3.0" +const OPENBILLS_VERSION = "v0.4.0" func init() { if !viper.GetBool("debug_mode") { |