Upstream sync
This commit is contained in:
@@ -1,60 +0,0 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jinzhu/gorm"
|
||||
"gitlab.com/pactual1/backend/models"
|
||||
"gitlab.com/pactual1/backend/shared"
|
||||
)
|
||||
|
||||
func ListBuyers(c *gin.Context) {
|
||||
// Get limit and offset from query parameter with defaults
|
||||
limitStr := c.DefaultQuery("limit", "99999999999999999999999999999999")
|
||||
offsetStr := c.DefaultQuery("offset", "0")
|
||||
|
||||
// Convert limit and offset to int
|
||||
limit, err := strconv.Atoi(limitStr)
|
||||
if err != nil {
|
||||
log.Printf("ListBuyers Error: Invalid limit value: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid limit value"})
|
||||
return
|
||||
}
|
||||
|
||||
offset, err := strconv.Atoi(offsetStr)
|
||||
if err != nil {
|
||||
log.Printf("ListBuyers Error: Invalid offset value: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid offset value"})
|
||||
return
|
||||
}
|
||||
|
||||
// Create a slice to hold the buyers
|
||||
var buyers []models.Buyer
|
||||
|
||||
// Count the total number of buyers
|
||||
var total int64
|
||||
if err := shared.GetDb().Model(&models.Buyer{}).Count(&total).Error; err != nil {
|
||||
log.Printf("ListBuyers Error: Database error: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
return
|
||||
}
|
||||
|
||||
// Fetch the buyers from the database with LIMIT and OFFSET
|
||||
if err := shared.GetDb().Order("created_at desc").Limit(limit).Offset(offset).Find(&buyers).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
log.Printf("ListBuyers Error: No buyers found: %v", err)
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "No buyers found"})
|
||||
} else {
|
||||
log.Printf("ListBuyers Error: Database error: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Respond with the buyers and the total count
|
||||
c.JSON(http.StatusOK, gin.H{"total": total, "data": buyers})
|
||||
}
|
||||
81
controllers/companies_controller.go
Normal file
81
controllers/companies_controller.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gitlab.com/pactual1/backend/database/company"
|
||||
)
|
||||
|
||||
func ListCompanies(c *gin.Context) {
|
||||
// Get limit and offset from query parameter with defaults
|
||||
limitStr := c.DefaultQuery("limit", "50")
|
||||
offsetStr := c.DefaultQuery("offset", "0")
|
||||
iDsStr := c.QueryArray("ids[]")
|
||||
|
||||
// Convert limit and offset to int
|
||||
limit, err := strconv.Atoi(limitStr)
|
||||
if err != nil {
|
||||
log.Printf("ListCompanies Error: Invalid limit value: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid limit value"})
|
||||
return
|
||||
}
|
||||
|
||||
offset, err := strconv.Atoi(offsetStr)
|
||||
if err != nil {
|
||||
log.Printf("ListCompanies Error: Invalid offset value: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid offset value"})
|
||||
return
|
||||
}
|
||||
|
||||
// Convert ids to []int64
|
||||
var companyIDs []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
|
||||
}
|
||||
companyIDs = append(companyIDs, id)
|
||||
}
|
||||
|
||||
// Fetch companies
|
||||
companies, total, st, err := company.GetCompanies(companyIDs, limit, offset)
|
||||
if err != nil {
|
||||
c.JSON(st, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// Respond with the companies and the total count
|
||||
c.JSON(http.StatusOK, gin.H{"total": total, "data": companies})
|
||||
}
|
||||
|
||||
func GetCompanyByID(c *gin.Context) {
|
||||
// Get the company ID from url parameters
|
||||
companyIDStr := c.Param("company_id")
|
||||
|
||||
if companyIDStr == "" {
|
||||
log.Printf("GetCompanyByID Error: ID is required")
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "ID is required"})
|
||||
return
|
||||
}
|
||||
|
||||
companyID, err := strconv.ParseUint(companyIDStr, 10, 32)
|
||||
if err != nil {
|
||||
log.Printf("GetCompanyByID Error: Invalid ID: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid ID"})
|
||||
return
|
||||
}
|
||||
|
||||
// Fetch company
|
||||
companies, _, st, err := company.GetCompanies([]int64{int64(companyID)}, 1, 0)
|
||||
if err != nil {
|
||||
c.JSON(st, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// Respond with the companies and the total count
|
||||
c.JSON(http.StatusOK, gin.H{"data": companies[0]})
|
||||
}
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"gitlab.com/pactual1/backend/shared"
|
||||
)
|
||||
|
||||
func TestListBuyers(t *testing.T) {
|
||||
func TestListCompanies(t *testing.T) {
|
||||
ts := runTestServer()
|
||||
defer ts.Close()
|
||||
defer shared.CloseDb()
|
||||
@@ -15,10 +15,10 @@ import (
|
||||
)
|
||||
|
||||
func GetLatestContracts(c *gin.Context) {
|
||||
// Existing parameters
|
||||
limitStr := c.DefaultQuery("limit", "50")
|
||||
offsetStr := c.DefaultQuery("offset", "0")
|
||||
status := strings.Split(c.DefaultQuery("status", models.ContractStatusActive), ",")
|
||||
// Existing parameters
|
||||
limitStr := c.DefaultQuery("limit", "50")
|
||||
offsetStr := c.DefaultQuery("offset", "0")
|
||||
status := strings.Split(c.DefaultQuery("status", models.ContractStatusActive), ",")
|
||||
|
||||
// New/Updated optional parameters
|
||||
companyName := c.Query("company_name")
|
||||
@@ -29,6 +29,7 @@ func GetLatestContracts(c *gin.Context) {
|
||||
startTimeStr := c.Query("start_time")
|
||||
endTimeStr := c.Query("end_time")
|
||||
deviceIDsStr := c.QueryArray("deviceIDs[]")
|
||||
iDsStr := c.QueryArray("ids[]")
|
||||
|
||||
// Convert limit and offset to int
|
||||
limit, err := strconv.Atoi(limitStr)
|
||||
@@ -76,84 +77,119 @@ func GetLatestContracts(c *gin.Context) {
|
||||
deviceIDs = append(deviceIDs, id)
|
||||
}
|
||||
|
||||
// Fetch contracts
|
||||
contracts, total, st, err := contract.GetContracts(status, companyName, companyAddress, companyEmail, companyPhone, &startTime, &endTime, contractName, deviceIDs, 0, nil, limit, offset)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(st, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
// Convert ids to []int64
|
||||
var contractIDs []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
|
||||
}
|
||||
contractIDs = append(contractIDs, id)
|
||||
}
|
||||
|
||||
// Respond with the contracts and the total count
|
||||
c.JSON(http.StatusOK, gin.H{"total": total, "data": models.ConvertContractToResponse(contracts)})
|
||||
}
|
||||
// Fetch contracts
|
||||
contracts, total, st, err := contract.GetContracts(status, companyName, companyAddress, companyEmail, companyPhone, &startTime, &endTime, contractName, deviceIDs, contractIDs, nil, limit, offset)
|
||||
|
||||
|
||||
func GetBuyerContracts(c *gin.Context) {
|
||||
// Existing parameters
|
||||
limitStr := c.DefaultQuery("limit", "50")
|
||||
offsetStr := c.DefaultQuery("offset", "0")
|
||||
status := strings.Split(c.DefaultQuery("status", models.ContractStatusActive), ",")
|
||||
|
||||
contractIDStr := c.DefaultQuery("contract_id","0")
|
||||
dateCreatedStr := c.Query("date_created")
|
||||
|
||||
|
||||
// Convert limit and offset to int
|
||||
limit, err := strconv.Atoi(limitStr)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid limit value"})
|
||||
return
|
||||
}
|
||||
|
||||
offset, err := strconv.Atoi(offsetStr)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid offset value"})
|
||||
return
|
||||
}
|
||||
|
||||
// Convert limit and offset to int
|
||||
contractID, err := strconv.Atoi(contractIDStr)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid contractID value"})
|
||||
return
|
||||
}
|
||||
|
||||
// Convert startTime to time.Time
|
||||
var dateCreated time.Time
|
||||
if dateCreatedStr != "" {
|
||||
startTimeUnix, err := strconv.ParseInt(dateCreatedStr, 10, 64)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid start_time value"})
|
||||
return
|
||||
}
|
||||
dateCreated = time.Unix(startTimeUnix, 0)
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Fetch contracts
|
||||
contracts, total, st, err := contract.GetContracts(status, "", "", "", "", nil, nil, "", nil, contractID, &dateCreated, limit, offset)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(st, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
c.JSON(st, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// Respond with the contracts and the total count
|
||||
c.JSON(http.StatusOK, gin.H{"total": total, "data": models.ConvertContractToResponseModel(contracts)})
|
||||
c.JSON(http.StatusOK, gin.H{"total": total, "data": models.ConvertContractToResponse(contracts)})
|
||||
}
|
||||
|
||||
func GetBuyerContracts(c *gin.Context) {
|
||||
// Existing parameters
|
||||
limitStr := c.DefaultQuery("limit", "50")
|
||||
offsetStr := c.DefaultQuery("offset", "0")
|
||||
status := c.QueryArray("status")
|
||||
iDsStr := c.QueryArray("ids[]")
|
||||
dateCreatedStr := c.Query("date_created")
|
||||
startTimeStr := c.Query("start_time")
|
||||
endTimeStr := c.Query("end_time")
|
||||
|
||||
// Convert limit and offset to int
|
||||
limit, err := strconv.Atoi(limitStr)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid limit value"})
|
||||
return
|
||||
}
|
||||
|
||||
offset, err := strconv.Atoi(offsetStr)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid offset value"})
|
||||
return
|
||||
}
|
||||
|
||||
// Convert startTime to time.Time
|
||||
var startTime *time.Time
|
||||
if startTimeStr != "" {
|
||||
startTimeUnix, err := strconv.ParseInt(startTimeStr, 10, 64)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid start_time value"})
|
||||
return
|
||||
}
|
||||
startTimeValue := time.Unix(startTimeUnix, 0)
|
||||
startTime = &startTimeValue
|
||||
}
|
||||
|
||||
// Convert endTime to time.Time
|
||||
var endTime *time.Time
|
||||
if endTimeStr != "" {
|
||||
endTimeUnix, err := strconv.ParseInt(endTimeStr, 10, 64)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid end_time value"})
|
||||
return
|
||||
}
|
||||
endTimeValue := time.Unix(endTimeUnix, 0)
|
||||
endTime = &endTimeValue
|
||||
}
|
||||
|
||||
// Convert startTime to time.Time
|
||||
var dateCreated time.Time
|
||||
if dateCreatedStr != "" {
|
||||
startTimeUnix, err := strconv.ParseInt(dateCreatedStr, 10, 64)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid start_time value"})
|
||||
return
|
||||
}
|
||||
dateCreated = time.Unix(startTimeUnix, 0)
|
||||
}
|
||||
|
||||
// Convert ids to []int64
|
||||
var contractIDs []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
|
||||
}
|
||||
contractIDs = append(contractIDs, id)
|
||||
}
|
||||
|
||||
// Fetch contracts
|
||||
contracts, total, st, err := contract.GetContracts(status, "", "", "", "", startTime, endTime, "", nil, contractIDs, &dateCreated, limit, offset)
|
||||
if err != nil {
|
||||
c.JSON(st, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// Respond with the contracts and the total count
|
||||
c.JSON(http.StatusOK, gin.H{"total": total, "data": models.ConvertContractToResponseModel(contracts)})
|
||||
}
|
||||
|
||||
func GetContractStatuses(c *gin.Context) {
|
||||
|
||||
// Respond with the contract statuses
|
||||
c.JSON(http.StatusOK, gin.H{"data": models.GetContractStatuses()})
|
||||
// Respond with the contract statuses
|
||||
c.JSON(http.StatusOK, gin.H{"data": models.GetContractStatuses()})
|
||||
}
|
||||
|
||||
func CreateContract(c *gin.Context) {
|
||||
|
||||
var payload models.CreateContractRequestPayload
|
||||
|
||||
var payload models.CreateContractRequestPayload
|
||||
|
||||
if err := c.ShouldBindJSON(&payload); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid JSON payload"})
|
||||
log.Printf("Invalid JSON payload: %v", err)
|
||||
@@ -163,16 +199,19 @@ func CreateContract(c *gin.Context) {
|
||||
db := shared.GetDb()
|
||||
|
||||
newContract := models.Contract{
|
||||
BuyerID: payload.BuyerID,
|
||||
SellerID: payload.SellerID,
|
||||
Description: payload.Description,
|
||||
ProductID: payload.ProductID,
|
||||
MinTemp: payload.MinTemp,
|
||||
MaxTemp: payload.MaxTemp,
|
||||
ArrivalDate: time.Unix(payload.ArrivalDate, 0),
|
||||
PenaltyType: payload.PenaltyRec,
|
||||
PenaltyValue : payload.PenaltyValue,
|
||||
PenaltyRec : payload.PenaltyRec,
|
||||
BuyerID: payload.BuyerID,
|
||||
SellerID: payload.SellerID,
|
||||
Description: payload.Description,
|
||||
ProductID: payload.ProductID,
|
||||
MinTemp: payload.MinTemp,
|
||||
MaxTemp: payload.MaxTemp,
|
||||
ArrivalDate: time.Unix(payload.ArrivalDate, 0),
|
||||
PenaltyType: payload.PenaltyRec,
|
||||
PenaltyValue: payload.PenaltyValue,
|
||||
PenaltyRec: payload.PenaltyRec,
|
||||
StartLat: payload.StartLat,
|
||||
StartLon: payload.StartLon,
|
||||
Status: models.ContractStatusDraft,
|
||||
}
|
||||
|
||||
if err := db.Create(&newContract).Error; err != nil {
|
||||
@@ -228,7 +267,6 @@ func UpdateContract(c *gin.Context) {
|
||||
|
||||
// Get the contract ID from url parameters
|
||||
contractIDStr := c.Param("contract_id")
|
||||
|
||||
if contractIDStr == "" {
|
||||
log.Printf("UpdateContract Error: ID is required")
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "ID is required"})
|
||||
@@ -254,4 +292,3 @@ func UpdateContract(c *gin.Context) {
|
||||
// Respond with the contracts and the total count
|
||||
c.JSON(http.StatusOK, gin.H{"data": contractModel})
|
||||
}
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ func SaveDeviceInfo(c *gin.Context) {
|
||||
} else {
|
||||
log.Printf("Current device deleted at: %v", device.DeletedAt)
|
||||
|
||||
if device.DeletedAt != nil {
|
||||
if device.DeletedAt.Valid {
|
||||
// Use raw SQL to update the record
|
||||
if err := shared.GetDb().Exec("UPDATE devices SET deleted_at = NULL, company_id = NULL WHERE id = ?", device.ID).Error; err != nil {
|
||||
log.Printf("UNDELETE -Device DB Error: %v", err)
|
||||
@@ -161,5 +161,5 @@ func GetDevicesByContract(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
// Respond with the devices
|
||||
c.JSON(http.StatusOK,gin.H{"data" : models.ConvertDeviceToResponse(devices)})
|
||||
c.JSON(http.StatusOK, gin.H{"data": models.ConvertDeviceToResponse(devices)})
|
||||
}
|
||||
|
||||
11
controllers/health_controller.go
Normal file
11
controllers/health_controller.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func HealthCheck(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"health": "ready"})
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -15,8 +15,9 @@ import (
|
||||
|
||||
func ListProductTemplates(c *gin.Context) {
|
||||
// Get limit and offset from query parameter with defaults
|
||||
limitStr := c.DefaultQuery("limit", "99999999999999999999999999999999")
|
||||
limitStr := c.DefaultQuery("limit", "50")
|
||||
offsetStr := c.DefaultQuery("offset", "0")
|
||||
iDsStr := c.QueryArray("ids[]")
|
||||
|
||||
// Convert limit and offset to int
|
||||
limit, err := strconv.Atoi(limitStr)
|
||||
@@ -34,18 +35,36 @@ func ListProductTemplates(c *gin.Context) {
|
||||
}
|
||||
|
||||
// Create a slice to hold the product templates
|
||||
var productTemplates []models.ProductTemplate
|
||||
productTemplates := []models.ProductTemplate{}
|
||||
|
||||
// Convert ids to []int64
|
||||
var productIDs []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
|
||||
}
|
||||
productIDs = append(productIDs, id)
|
||||
}
|
||||
|
||||
countDb := shared.GetDb()
|
||||
db := shared.GetDb()
|
||||
if len(productIDs) > 0 {
|
||||
countDb = countDb.Where("id IN (?)", productIDs)
|
||||
db = db.Where("id IN (?)", productIDs)
|
||||
}
|
||||
|
||||
// Count the total number of product templates
|
||||
var total int64
|
||||
if err := shared.GetDb().Model(&models.ProductTemplate{}).Count(&total).Error; err != nil {
|
||||
if err := countDb.Model(&models.ProductTemplate{}).Count(&total).Error; err != nil {
|
||||
log.Printf("ListProductTemplates Error: Database error: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
return
|
||||
}
|
||||
|
||||
// Fetch the product templates from the database with LIMIT and OFFSET
|
||||
if err := shared.GetDb().Order("created_at desc").Limit(limit).Offset(offset).Find(&productTemplates).Error; err != nil {
|
||||
if err := db.Order("created_at desc").Limit(limit).Offset(offset).Find(&productTemplates).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
log.Printf("ListProductTemplates Error: No product templates found: %v", err)
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "No product templates found"})
|
||||
|
||||
@@ -15,7 +15,7 @@ func TestListProductTemplates(t *testing.T) {
|
||||
defer shared.CloseDb()
|
||||
|
||||
t.Run("it should return 200", func(t *testing.T) {
|
||||
resp, err := http.Get(fmt.Sprintf("%s/product_templates?limit=20", ts.URL))
|
||||
resp, err := http.Get(fmt.Sprintf("%s/products?limit=20", ts.URL))
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
|
||||
@@ -15,8 +15,9 @@ import (
|
||||
|
||||
func ListTextTemplates(c *gin.Context) {
|
||||
// Get limit and offset from query parameter with defaults
|
||||
limitStr := c.DefaultQuery("limit", "99999999999999999999999999999999")
|
||||
limitStr := c.DefaultQuery("limit", "50")
|
||||
offsetStr := c.DefaultQuery("offset", "0")
|
||||
iDsStr := c.QueryArray("ids[]")
|
||||
|
||||
// Convert limit and offset to int
|
||||
limit, err := strconv.Atoi(limitStr)
|
||||
@@ -33,19 +34,36 @@ func ListTextTemplates(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Convert ids to []int64
|
||||
var templateIDs []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
|
||||
}
|
||||
templateIDs = append(templateIDs, id)
|
||||
}
|
||||
|
||||
// Create a slice to hold the text templates
|
||||
var textTemplates []models.TextTemplate
|
||||
textTemplates := []models.TextTemplate{}
|
||||
|
||||
// Count the total number of text templates
|
||||
var total int64
|
||||
if err := shared.GetDb().Model(&models.TextTemplate{}).Count(&total).Error; err != nil {
|
||||
countDb := shared.GetDb()
|
||||
db := shared.GetDb()
|
||||
if len(templateIDs) > 0 {
|
||||
countDb = countDb.Where("id IN (?)", templateIDs)
|
||||
db = db.Where("id IN (?)", templateIDs)
|
||||
}
|
||||
if err := countDb.Model(&models.TextTemplate{}).Count(&total).Error; err != nil {
|
||||
log.Printf("ListTextTemplates Error: Database error: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
return
|
||||
}
|
||||
|
||||
// Fetch the text templates from the database with LIMIT and OFFSET
|
||||
if err := shared.GetDb().Order("created_at desc").Limit(limit).Offset(offset).Find(&textTemplates).Error; err != nil {
|
||||
if err := db.Order("created_at desc").Limit(limit).Offset(offset).Find(&textTemplates).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
log.Printf("ListTextTemplates Error: No text templates found: %v", err)
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "No text templates found"})
|
||||
@@ -82,4 +100,3 @@ func CreateTextTemplate(c *gin.Context) {
|
||||
log.Printf("Successfully received and saved text template: %v", textTemplate)
|
||||
c.JSON(http.StatusOK, gin.H{"data": textTemplate})
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ func TestListTextTemplates(t *testing.T) {
|
||||
defer shared.CloseDb()
|
||||
|
||||
t.Run("it should return 200", func(t *testing.T) {
|
||||
resp, err := http.Get(fmt.Sprintf("%s/text_templates?limit=20", ts.URL))
|
||||
resp, err := http.Get(fmt.Sprintf("%s/templates?limit=20", ts.URL))
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
@@ -39,7 +39,7 @@ func CreateTextTemplate(t *testing.T) {
|
||||
}
|
||||
|
||||
payload, _ := json.Marshal(data)
|
||||
resp, err := http.Post(fmt.Sprintf("%s/text_templates/save", ts.URL), "application/json", bytes.NewBuffer(payload))
|
||||
resp, err := http.Post(fmt.Sprintf("%s/templates/save", ts.URL), "application/json", bytes.NewBuffer(payload))
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user