Upstream sync
This commit is contained in:
@@ -14,6 +14,7 @@ func GetInvoices(c *gin.Context) {
|
||||
offsetStr := c.DefaultQuery("offset", "0")
|
||||
buyerName := c.Query("buyer_name")
|
||||
sortBy := c.Query("sort_by")
|
||||
iDsStr := c.QueryArray("ids[]")
|
||||
|
||||
limit, err := strconv.Atoi(limitStr)
|
||||
if err != nil {
|
||||
@@ -27,7 +28,18 @@ func GetInvoices(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
invoices, total, err := invoice.GetInvoices(buyerName, sortBy, limit, offset, 0)
|
||||
// Convert ids to []int64
|
||||
var invoiceIDs []int64
|
||||
for _, idStr := range iDsStr {
|
||||
id, err := strconv.ParseInt(idStr, 10, 64)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid id value"})
|
||||
return
|
||||
}
|
||||
invoiceIDs = append(invoiceIDs, id)
|
||||
}
|
||||
|
||||
invoices, total, err := invoice.GetInvoices(buyerName, sortBy, limit, offset, invoiceIDs)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
@@ -40,32 +52,30 @@ func GetInvoices(c *gin.Context) {
|
||||
}
|
||||
|
||||
func GetInvoiceByID(c *gin.Context) {
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.Atoi(idStr)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid ID"})
|
||||
return
|
||||
}
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.Atoi(idStr)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid ID"})
|
||||
return
|
||||
}
|
||||
|
||||
invoices, _, err := invoice.GetInvoices("", "", 1, 0, uint(id))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
invoices, _, err := invoice.GetInvoices("", "", 1, 0, []int64{int64(id)})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
invoiceResponses := models.ConvertInvoiceToResponse(invoices)
|
||||
|
||||
if len(invoices) > 0 {
|
||||
c.JSON(http.StatusOK, gin.H{"data": invoiceResponses[0]})
|
||||
} else {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Invoice not found"})
|
||||
}
|
||||
if len(invoices) > 0 {
|
||||
c.JSON(http.StatusOK, gin.H{"data": invoiceResponses[0]})
|
||||
} else {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Invoice not found"})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
func convertToResponseModel(invoices []models.Invoice) []models.ListInvoiceResponse {
|
||||
var listInvoiceResponses []models.ListInvoiceResponse
|
||||
listInvoiceResponses := []models.ListInvoiceResponse{}
|
||||
|
||||
// Get all statuses
|
||||
statuses := models.GetInvoiceStatuses()
|
||||
@@ -82,16 +92,14 @@ func convertToResponseModel(invoices []models.Invoice) []models.ListInvoiceRespo
|
||||
}
|
||||
|
||||
listInvoiceResponse := models.ListInvoiceResponse{
|
||||
Status: models.KeyValue{Key: status.Key, Value: status.Value},
|
||||
Buyer: models.CompanyShortResponse{ID: int(invoice.BuyerID), Name: invoice.BuyerName},
|
||||
ContractID: int(invoice.ContractID),
|
||||
DateCreated: invoice.InvoiceDate,
|
||||
DueDate: invoice.InvoiceDueDate,
|
||||
Amount: strconv.FormatInt(invoice.PriceCents, 10),
|
||||
Status: models.KeyValue{Key: status.Key, Value: status.Value},
|
||||
Buyer: models.CompanyShortResponse{ID: int(invoice.BuyerID), Name: invoice.BuyerName},
|
||||
ContractID: int(invoice.ContractID),
|
||||
DateCreated: invoice.InvoiceDate,
|
||||
DueDate: invoice.InvoiceDueDate,
|
||||
Amount: strconv.FormatInt(invoice.PriceCents, 10),
|
||||
}
|
||||
listInvoiceResponses = append(listInvoiceResponses, listInvoiceResponse)
|
||||
}
|
||||
return listInvoiceResponses
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user