Upstream sync
This commit is contained in:
@@ -33,11 +33,11 @@ func Load() error {
|
||||
Environment: getEnv("NOVATECH_ADMIN_SERVICE_ENVIRONMENT", "DEV"),
|
||||
},
|
||||
Database: Database{
|
||||
UserName: getEnv("NOVATECH_DATABASE_USERNAME", "root"),
|
||||
Password: getEnv("NOVATECH_DATABASE_PASSWORD", "root"),
|
||||
DatabaseName: getEnv("NOVATECH_DATABASE_NAME", "postgres"),
|
||||
UserName: getEnv("NOVATECH_DATABASE_USERNAME", "username"),
|
||||
Password: getEnv("NOVATECH_DATABASE_PASSWORD", "password"),
|
||||
DatabaseName: getEnv("NOVATECH_DATABASE_NAME", "dbname"),
|
||||
HostName: getEnv("NOVATECH_DATABASE_ADDRESS", "localhost"),
|
||||
Port: getEnv("NOVATECH_DATABASE_PORT", " "),
|
||||
Port: getEnv("NOVATECH_DATABASE_PORT", "5432"),
|
||||
},
|
||||
Blockchain: Blockchain{
|
||||
NetworkEndpoint: getEnv("NOVATECH_BLOCKCHAIN_NETWORK_ENDPOINT", ""),
|
||||
|
||||
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)
|
||||
|
||||
})
|
||||
}
|
||||
18
models/buyer.go
Normal file
18
models/buyer.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package models
|
||||
|
||||
import "github.com/jinzhu/gorm"
|
||||
|
||||
type Buyer struct {
|
||||
gorm.Model
|
||||
Name string
|
||||
}
|
||||
|
||||
func (Buyer) Update() (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
func (Buyer) Create() (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
func (Buyer) Delete() (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
31
models/json_type.go
Normal file
31
models/json_type.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type JSON json.RawMessage
|
||||
|
||||
// Scan scan value into Jsonb, implements sql.Scanner interface
|
||||
func (j *JSON) Scan(value interface{}) error {
|
||||
bytes, ok := value.([]byte)
|
||||
if !ok {
|
||||
return errors.New(fmt.Sprint("Failed to unmarshal JSONB value:", value))
|
||||
}
|
||||
|
||||
result := json.RawMessage{}
|
||||
err := json.Unmarshal(bytes, &result)
|
||||
*j = JSON(result)
|
||||
return err
|
||||
}
|
||||
|
||||
// Value return json value, implement driver.Valuer interface
|
||||
func (j JSON) Value() (driver.Value, error) {
|
||||
if len(j) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
return json.RawMessage(j).MarshalJSON()
|
||||
}
|
||||
20
models/product_template.go
Normal file
20
models/product_template.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package models
|
||||
|
||||
import "github.com/jinzhu/gorm"
|
||||
|
||||
type ProductTemplate struct {
|
||||
gorm.Model
|
||||
Name string
|
||||
ProductTemplateConfig map[string]interface{} `json:",omitempty" sql:"-"`
|
||||
Config JSON `json:"-" gorm:"embedded"`
|
||||
}
|
||||
|
||||
func (ProductTemplate) Update() (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
func (ProductTemplate) Create() (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
func (ProductTemplate) Delete() (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
18
models/text_template.go
Normal file
18
models/text_template.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package models
|
||||
|
||||
import "github.com/jinzhu/gorm"
|
||||
|
||||
type TextTemplate struct {
|
||||
gorm.Model
|
||||
Value string
|
||||
}
|
||||
|
||||
func (TextTemplate) Update() (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
func (TextTemplate) Create() (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
func (TextTemplate) Delete() (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
@@ -11,7 +11,12 @@ func RegisterPublicRoutes(r *gin.Engine) {
|
||||
r.GET("/dashboard/map/contract/devices", controllers.GetDevicesByContract)
|
||||
r.GET("/dashboard/map/contracts", controllers.GetLatestContracts)
|
||||
r.GET("/dashboard/map/device_data", controllers.GetDeviceData)
|
||||
|
||||
r.POST("/device_data/save", controllers.SaveDeviceInfo)
|
||||
|
||||
r.POST("/device_data/save", controllers.SaveDeviceInfo)
|
||||
r.GET("/buyers/", controllers.ListBuyers)
|
||||
r.GET("/product_templates/", controllers.ListProductTemplates)
|
||||
r.GET("/text_templates/", controllers.ListTextTemplates)
|
||||
r.POST("/text_templates/save", controllers.CreateTextTemplate)
|
||||
r.GET("/product_templates/:template_id", controllers.GetProductTemplate)
|
||||
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ func Init() error {
|
||||
return err
|
||||
}
|
||||
//TODO AUTOMIGRATE models once we have them
|
||||
db.AutoMigrate(&models.User{}, &models.Company{}, &models.Device{}, &models.DeviceInfo{}, &models.Contract{}, &models.ContractInfo{})
|
||||
db.AutoMigrate(&models.User{}, &models.Company{}, &models.Device{}, &models.DeviceInfo{}, &models.Contract{}, &models.ContractInfo{}, &models.Buyer{}, &models.ProductTemplate{}, &models.TextTemplate{})
|
||||
|
||||
return nil
|
||||
|
||||
|
||||
Reference in New Issue
Block a user