Upstream sync
This commit is contained in:
60
controllers/buyers_controller.go
Normal file
60
controllers/buyers_controller.go
Normal file
@@ -0,0 +1,60 @@
|
||||
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, "buyers": buyers})
|
||||
}
|
||||
26
controllers/buyers_controller_test.go
Normal file
26
controllers/buyers_controller_test.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package controllers_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gitlab.com/pactual1/backend/shared"
|
||||
)
|
||||
|
||||
func TestListBuyers(t *testing.T) {
|
||||
ts := runTestServer()
|
||||
defer ts.Close()
|
||||
defer shared.CloseDb()
|
||||
|
||||
t.Run("it should return 200", func(t *testing.T) {
|
||||
resp, err := http.Get(fmt.Sprintf("%s/buyers?limit=20", ts.URL))
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
|
||||
assert.Equal(t, 200, resp.StatusCode)
|
||||
})
|
||||
}
|
||||
@@ -23,98 +23,96 @@ func SaveDeviceInfo(c *gin.Context) {
|
||||
err := json.Unmarshal(rawData, &deviceInfo)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid JSON payload"})
|
||||
log.Printf("Invalid json pyload : %v",err)
|
||||
log.Printf("Invalid json pyload : %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
deviceInfo.RawJSON = string(rawData)
|
||||
deviceInfo.X = deviceInfo.AccInfo.X
|
||||
deviceInfo.Y = deviceInfo.AccInfo.Y
|
||||
deviceInfo.Z = deviceInfo.AccInfo.Z
|
||||
|
||||
// Attempt to find the device by IMEI; if not found, create a new device
|
||||
var device models.Device
|
||||
if err := shared.GetDb().Unscoped().Where("device_id = ?", deviceInfo.ExternalDeviceID).First(&device).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
// Create new device
|
||||
newDevice := models.Device{
|
||||
IMEI: deviceInfo.IMEI,
|
||||
IMSI: deviceInfo.IMSI,
|
||||
DeviceID: deviceInfo.ExternalDeviceID,
|
||||
DeviceConfiguration: string(rawData),
|
||||
}
|
||||
if err := shared.GetDb().Create(&newDevice).Error; err != nil {
|
||||
log.Printf("CREATE -Device DB Error: %v",err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not create new device"})
|
||||
return
|
||||
}
|
||||
deviceInfo.DeviceID = newDevice.ID
|
||||
} else {
|
||||
log.Printf("CREATE -Device DB Error: %v",err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
// Attempt to find the device by IMEI; if not found, create a new device
|
||||
var device models.Device
|
||||
if err := shared.GetDb().Unscoped().Where("device_id = ?", deviceInfo.ExternalDeviceID).First(&device).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
// Create new device
|
||||
newDevice := models.Device{
|
||||
IMEI: deviceInfo.IMEI,
|
||||
IMSI: deviceInfo.IMSI,
|
||||
DeviceID: deviceInfo.ExternalDeviceID,
|
||||
DeviceConfiguration: string(rawData),
|
||||
}
|
||||
if err := shared.GetDb().Create(&newDevice).Error; err != nil {
|
||||
log.Printf("CREATE -Device DB Error: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not create new device"})
|
||||
return
|
||||
}
|
||||
deviceInfo.DeviceID = newDevice.ID
|
||||
} else {
|
||||
log.Printf("Current device deleted at: %v",device.DeletedAt)
|
||||
|
||||
if device.DeletedAt != nil {
|
||||
// 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)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not revive deleted device"})
|
||||
return
|
||||
} else {
|
||||
log.Printf("Device undeleted successfuly : %v",device.DeletedAt)
|
||||
}
|
||||
}
|
||||
deviceInfo.DeviceID = device.ID
|
||||
log.Printf("CREATE -Device DB Error: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
return
|
||||
}
|
||||
} else {
|
||||
log.Printf("Current device deleted at: %v", device.DeletedAt)
|
||||
|
||||
if device.DeletedAt != nil {
|
||||
// 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)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not revive deleted device"})
|
||||
return
|
||||
} else {
|
||||
log.Printf("Device undeleted successfuly : %v", device.DeletedAt)
|
||||
}
|
||||
}
|
||||
deviceInfo.DeviceID = device.ID
|
||||
}
|
||||
|
||||
// Save deviceInfo to your database
|
||||
if err := shared.GetDb().Create(&deviceInfo).Error; err != nil {
|
||||
log.Printf("SaveDeviceInfo CREATE -DeviceInfo DB Error: %v",err)
|
||||
log.Printf("SaveDeviceInfo CREATE -DeviceInfo DB Error: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not save device info"})
|
||||
return
|
||||
}
|
||||
log.Printf("Successfully received and saved device info: %v",deviceInfo)
|
||||
log.Printf("Successfully received and saved device info: %v", deviceInfo)
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Successfully received and saved device info", "data": deviceInfo})
|
||||
}
|
||||
|
||||
func GetDeviceData(c *gin.Context) {
|
||||
// Get the device ID and contract ID from query parameters
|
||||
deviceIDStr := c.DefaultQuery("device_id", "")
|
||||
contractIDStr := c.DefaultQuery("contract_id", "")
|
||||
// Get the device ID and contract ID from query parameters
|
||||
deviceIDStr := c.DefaultQuery("device_id", "")
|
||||
contractIDStr := c.DefaultQuery("contract_id", "")
|
||||
|
||||
if deviceIDStr == "" {
|
||||
log.Printf("GetDeviceData Error: Device ID is required")
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Device ID is required"})
|
||||
return
|
||||
}
|
||||
if deviceIDStr == "" {
|
||||
log.Printf("GetDeviceData Error: Device ID is required")
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Device ID is required"})
|
||||
return
|
||||
}
|
||||
|
||||
deviceID, err := strconv.ParseUint(deviceIDStr, 10, 32)
|
||||
if err != nil {
|
||||
log.Printf("GetDeviceData Error: Invalid Device ID: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid Device ID"})
|
||||
return
|
||||
}
|
||||
deviceID, err := strconv.ParseUint(deviceIDStr, 10, 32)
|
||||
if err != nil {
|
||||
log.Printf("GetDeviceData Error: Invalid Device ID: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid Device ID"})
|
||||
return
|
||||
}
|
||||
|
||||
contractID, err := strconv.ParseUint(contractIDStr, 10, 32)
|
||||
if err != nil {
|
||||
log.Printf("GetDeviceData Error: Invalid Contract ID: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid Contract ID"})
|
||||
return
|
||||
}
|
||||
contractID, err := strconv.ParseUint(contractIDStr, 10, 32)
|
||||
if err != nil {
|
||||
log.Printf("GetDeviceData Error: Invalid Contract ID: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid Contract ID"})
|
||||
return
|
||||
}
|
||||
|
||||
contract , st, err := contract.GetContractByID(contractID)
|
||||
contract, st, err := contract.GetContractByID(contractID)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(st, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
c.JSON(st, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
deviceConnectedToContract := false
|
||||
deviceConnectedToContract := false
|
||||
for _, contractDeviceID := range contract.DeviceIDs {
|
||||
if deviceID == uint64(contractDeviceID) {
|
||||
deviceConnectedToContract = true
|
||||
@@ -122,24 +120,22 @@ func GetDeviceData(c *gin.Context) {
|
||||
}
|
||||
|
||||
if !deviceConnectedToContract {
|
||||
log.Printf("Device %v is not connected to contract %v", deviceID, contractID )
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Device is not present int his contract"})
|
||||
return
|
||||
}
|
||||
log.Printf("Device %v is not connected to contract %v", deviceID, contractID)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Device is not present int his contract"})
|
||||
return
|
||||
}
|
||||
|
||||
featureCollection, st, err := device.GetDeviceInfoForContract(deviceID, contract)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(st, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(st, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// Respond with the GeoJSON feature collection
|
||||
c.JSON(http.StatusOK, featureCollection)
|
||||
// Respond with the GeoJSON feature collection
|
||||
c.JSON(http.StatusOK, featureCollection)
|
||||
}
|
||||
|
||||
|
||||
|
||||
func GetDevicesByContract(c *gin.Context) {
|
||||
// Get the contract ID from query parameter
|
||||
contractIDStr := c.DefaultQuery("contract_id", "")
|
||||
@@ -167,5 +163,3 @@ func GetDevicesByContract(c *gin.Context) {
|
||||
// Respond with the devices
|
||||
c.JSON(http.StatusOK, devices)
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package controllers
|
||||
package controllers_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@@ -13,6 +13,8 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"gitlab.com/pactual1/backend/config"
|
||||
"gitlab.com/pactual1/backend/controllers"
|
||||
"gitlab.com/pactual1/backend/routes"
|
||||
"gitlab.com/pactual1/backend/shared"
|
||||
)
|
||||
|
||||
@@ -29,7 +31,8 @@ func runTestServer() *httptest.Server {
|
||||
|
||||
shared.Init()
|
||||
r := gin.Default()
|
||||
r.POST("/device_info", SaveDeviceInfo)
|
||||
r.POST("/device_info", controllers.SaveDeviceInfo)
|
||||
routes.RegisterPublicRoutes(r)
|
||||
return httptest.NewServer(r)
|
||||
}
|
||||
|
||||
|
||||
96
controllers/product_templates_controller.go
Normal file
96
controllers/product_templates_controller.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"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 ListProductTemplates(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("ListProductTemplates 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("ListProductTemplates Error: Invalid offset value: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid offset value"})
|
||||
return
|
||||
}
|
||||
|
||||
// Create a slice to hold the product templates
|
||||
var productTemplates []models.ProductTemplate
|
||||
|
||||
// Count the total number of product templates
|
||||
var total int64
|
||||
if err := shared.GetDb().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 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"})
|
||||
} else {
|
||||
log.Printf("ListProductTemplates Error: Database error: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Respond with the product templates and the total count
|
||||
c.JSON(http.StatusOK, gin.H{"total": total, "product_templates": productTemplates})
|
||||
}
|
||||
|
||||
func GetProductTemplate(c *gin.Context) {
|
||||
// Get the product template ID from query parameters
|
||||
productTemplateIDStr := c.Param("template_id")
|
||||
|
||||
if productTemplateIDStr == "" {
|
||||
log.Printf("GetProductTemplate Error: Product Template ID is required")
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Product Template ID is required"})
|
||||
return
|
||||
}
|
||||
|
||||
productTemplateID, err := strconv.ParseUint(productTemplateIDStr, 10, 32)
|
||||
if err != nil {
|
||||
log.Printf("GetProductTemplate Error: Invalid Product Template ID: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid Product Template ID"})
|
||||
return
|
||||
}
|
||||
|
||||
// Fetch the product template
|
||||
var productTemplate models.ProductTemplate
|
||||
if err := shared.GetDb().Where("id = ?", productTemplateID).First(&productTemplate).Error; err != nil {
|
||||
log.Printf("GetProductTemplate Error: Could not fetch product template: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not fetch product template"})
|
||||
return
|
||||
}
|
||||
|
||||
err = json.Unmarshal(productTemplate.Config, &productTemplate.ProductTemplateConfig)
|
||||
if err != nil {
|
||||
log.Printf("GetProductTemplate Error: Could not fetch product template config: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not fetch product template config"})
|
||||
return
|
||||
}
|
||||
// Respond with the product template
|
||||
c.JSON(http.StatusOK, productTemplate)
|
||||
}
|
||||
26
controllers/product_templates_controller_test.go
Normal file
26
controllers/product_templates_controller_test.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package controllers_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gitlab.com/pactual1/backend/shared"
|
||||
)
|
||||
|
||||
func TestListProductTemplates(t *testing.T) {
|
||||
ts := runTestServer()
|
||||
defer ts.Close()
|
||||
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))
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
|
||||
assert.Equal(t, 200, resp.StatusCode)
|
||||
})
|
||||
}
|
||||
85
controllers/text_templates_controller.go
Normal file
85
controllers/text_templates_controller.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"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 ListTextTemplates(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("ListTextTemplates 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("ListTextTemplates Error: Invalid offset value: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid offset value"})
|
||||
return
|
||||
}
|
||||
|
||||
// Create a slice to hold the text templates
|
||||
var 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 {
|
||||
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 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"})
|
||||
} else {
|
||||
log.Printf("ListTextTemplates Error: Database error: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Respond with the text templates and the total count
|
||||
c.JSON(http.StatusOK, gin.H{"total": total, "text_templates": textTemplates})
|
||||
}
|
||||
|
||||
func CreateTextTemplate(c *gin.Context) {
|
||||
var textTemplate models.TextTemplate
|
||||
rawData, _ := c.GetRawData()
|
||||
|
||||
// Unmarshal to the important info structure
|
||||
err := json.Unmarshal(rawData, &textTemplate)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid JSON payload"})
|
||||
log.Printf("Invalid json pyload : %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Save text template to your database
|
||||
if err := shared.GetDb().Create(&textTemplate).Error; err != nil {
|
||||
log.Printf("CreateTextTemplate DB Error: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not create text template"})
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("Successfully received and saved text template: %v", textTemplate)
|
||||
c.JSON(http.StatusOK, gin.H{"data": textTemplate})
|
||||
}
|
||||
|
||||
50
controllers/text_templates_controller_test.go
Normal file
50
controllers/text_templates_controller_test.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package controllers_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gitlab.com/pactual1/backend/shared"
|
||||
)
|
||||
|
||||
func TestListTextTemplates(t *testing.T) {
|
||||
ts := runTestServer()
|
||||
defer ts.Close()
|
||||
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))
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
|
||||
assert.Equal(t, 200, resp.StatusCode)
|
||||
})
|
||||
}
|
||||
|
||||
func CreateTextTemplate(t *testing.T) {
|
||||
ts := runTestServer()
|
||||
defer ts.Close()
|
||||
defer shared.CloseDb()
|
||||
|
||||
t.Run("it should return 200", func(t *testing.T) {
|
||||
// Add a new text template
|
||||
data := map[string]interface{}{
|
||||
"Value": "text",
|
||||
}
|
||||
|
||||
payload, _ := json.Marshal(data)
|
||||
resp, err := http.Post(fmt.Sprintf("%s/text_templates/save", ts.URL), "application/json", bytes.NewBuffer(payload))
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
|
||||
assert.Equal(t, 200, resp.StatusCode)
|
||||
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user