203 lines
5.4 KiB
Go
203 lines
5.4 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"simplycpq/models"
|
|
"simplycpq/services"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type BOMController struct {
|
|
bomService *services.BomService
|
|
}
|
|
|
|
func NewBOMController(b *services.BomService) *BOMController {
|
|
return &BOMController{
|
|
bomService: b,
|
|
}
|
|
}
|
|
|
|
func (bc *BOMController) CreateBillOfMaterial(c *gin.Context) {
|
|
var bom models.BillOfMaterial
|
|
err := c.ShouldBindJSON(&bom)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
createdBOM, err := bc.bomService.CreateBillOfMaterial(&bom)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusCreated, createdBOM)
|
|
// Removed redundant error check and status setting
|
|
}
|
|
|
|
func (bc *BOMController) GetBOMByProductID(c *gin.Context) {
|
|
productIdStr := c.Param("productId")
|
|
productId, err := strconv.Atoi(productIdStr)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
contents, err := bc.bomService.GetBOMByProductID(productId)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, contents)
|
|
}
|
|
|
|
func (bc *BOMController) GetBOMVariantNodesByBOMId(c *gin.Context) {
|
|
bomIdStr := c.Param("bomId")
|
|
bomId, err := strconv.Atoi(bomIdStr)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
variantNodes, err := bc.bomService.GetBOMVariantNodesByBOMId(bomId)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, variantNodes)
|
|
}
|
|
|
|
func (bc *BOMController) CreateVariantNode(c *gin.Context) {
|
|
var variantNode models.VariantNode
|
|
err := c.ShouldBindJSON(&variantNode)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
_, err = bc.bomService.CreateVariantNode(&variantNode)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.Status(http.StatusCreated)
|
|
}
|
|
|
|
func (bc *BOMController) UpdateVariantNode(c *gin.Context) {
|
|
var variantNode models.VariantNode
|
|
err := c.ShouldBindJSON(&variantNode)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
updatedVariantNode, err := bc.bomService.UpdateVariantNode(&variantNode)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, updatedVariantNode)
|
|
}
|
|
|
|
func (bc *BOMController) DeleteVariantNode(c *gin.Context) {
|
|
variantNodeIdStr := c.Query("variantNodeId")
|
|
variantNodeId, err := strconv.Atoi(variantNodeIdStr)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
err = bc.bomService.DeleteVariantNode(variantNodeId)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.Status(http.StatusOK)
|
|
}
|
|
|
|
func (bc *BOMController) GetVariantNode(c *gin.Context) {
|
|
variantNodeIdStr := c.Query("variantNodeId")
|
|
variantNodeId, err := strconv.Atoi(variantNodeIdStr)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
variantNode, err := bc.bomService.GetVariantNode(variantNodeId)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, variantNode)
|
|
}
|
|
|
|
func (bc *BOMController) GetVariantNodeProductsByVariantNodeId(c *gin.Context) {
|
|
variantNodeIdStr := c.Query("variantNodeId")
|
|
variantNodeId, err := strconv.Atoi(variantNodeIdStr)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
products, err := bc.bomService.GetVariantNodeProductsByVariantNodeId(variantNodeId)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, products)
|
|
}
|
|
|
|
func (bc *BOMController) UpdateBillOfMaterial(c *gin.Context) {
|
|
var bom models.BillOfMaterial
|
|
err := c.ShouldBindJSON(&bom)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
updatedBOM, err := bc.bomService.UpdateBillOfMaterial(&bom)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, updatedBOM)
|
|
}
|
|
|
|
func (bc *BOMController) DeleteBillOfMaterial(c *gin.Context) {
|
|
bomIdStr := c.Query("id")
|
|
bomId, err := strconv.Atoi(bomIdStr)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
err = bc.bomService.DeleteBillOfMaterial(bomId)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.Status(http.StatusOK)
|
|
}
|
|
|
|
func (bc *BOMController) GetBillOfMaterial(c *gin.Context) {
|
|
bomIdStr := c.Query("id")
|
|
bomId, err := strconv.Atoi(bomIdStr)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
bom, err := bc.bomService.GetBillOfMaterial(bomId)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, bom)
|
|
}
|
|
|
|
func (bc *BOMController) GetBOMContentByBOMId(c *gin.Context) {
|
|
bomIdStr := c.Param("bomId")
|
|
bomId, err := strconv.Atoi(bomIdStr)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
contents, err := bc.bomService.GetBOMContentByBOMId(bomId)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, contents)
|
|
}
|