Files
simply_cpq/buildtime/server/server/httpServer.go
T

140 lines
9.9 KiB
Go

package server
import (
"database/sql"
"log"
"net/http"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"simplycpq/controller"
"simplycpq/repositories"
"simplycpq/services"
)
type HttpServer struct {
config *viper.Viper
router *gin.Engine
dbHandler *sql.DB
valueController *controller.ValueController
productGroupController *controller.ProductGroupController
productController *controller.ProductController
bomController *controller.BOMController
}
func InitHttpServer(config *viper.Viper, dbHandler *sql.DB) HttpServer {
// repository initialisation:
valueRepository := repositories.NewValueRepository(dbHandler)
productGroupRepository := repositories.NewProductGroupRepository(dbHandler, valueRepository)
productRepository := repositories.NewProductRepository(dbHandler, productGroupRepository, valueRepository)
importRepository := repositories.NewImportRepository(dbHandler, productGroupRepository, productRepository, valueRepository)
bomRepository := repositories.NewBOMRepository(dbHandler, productRepository)
// service initialisation:
valueService := services.NewValueService(valueRepository)
productGroupService := services.NewProductGroupService(productGroupRepository)
productService := services.NewProductService(productRepository)
importService := services.NewImportService(importRepository)
bomService := services.NewBOMService(bomRepository)
// controller initialisation:
valueController := controller.NewValueController(valueService)
productGroupController := controller.NewProductGroupController(productGroupService, valueService)
productController := controller.NewProductController(productService, productGroupService, valueService)
bomController := controller.NewBOMController(bomService)
importController := controller.NewImportController(importService)
// Service API
router := gin.Default()
router.Use(cors.Default())
router.GET("/test", Test)
// Value centric routing
router.POST("/createPropertyTypeGroup", valueController.CreatePropertyTypeGroup) // create PropertyTypeGroup
router.PUT("/updatePropertyTypeGroup", valueController.UpdatePropertyTypeGroup) // update PropertyTypeGroup
router.DELETE("/deletePropertyTypeGroup/:id", valueController.DeletePropertyTypeGroup) // delete PropertyTypeGroup
router.GET("/getPropertyTypeGroup/:id", valueController.GetPropertyTypeGroup) // get PropertyTypeGroup by id
router.GET("/getPropertyTypeGroupsByParentId/:parentId", valueController.GetPropertyTypeGroupsByParentId) // get all PropertyTypeGroups by parent ID
router.DELETE("/deletePropertyTypeFromPropertyTypeGroupId/:propertyTypeGroupId", valueController.DeletePropertyTypeFromPropertyTypeGroupId) // delete PropertyType from PropertyTypeGroup by ID
router.POST("/createPropertyType", valueController.CreatePropertyType) // create PropertyType
router.PUT("/updatePropertyType", valueController.UpdatePropertyType) // update PropertyType
router.DELETE("/deletePropertyType/:id", valueController.DeletePropertyType) // delete PropertyType
router.GET("/getPropertyType/:id", valueController.GetPropertyType) // get PropertyType by id
router.GET("/getPropertyTypesByGroupId/:groupId", valueController.GetPropertyTypesByGroupId) // get all PropertyTypes by group ID
router.POST("/createValueForPropertyTypeId/:propertyTypeId", valueController.CreateValueForPropertyTypeId) // create Value for PropertyType by ID
router.DELETE("/deleteValueFromPropertyTypeId/:propertyTypeId", valueController.DeleteValueFromPropertyTypeId) // delete Value from PropertyType by ID
router.GET("/getValuesFromPropertyTypeId/:propertyTypeId", valueController.GetValuesFromPropertyTypeId) // get all Values from PropertyType by ID
router.GET("/getValuesFromPropertyId/:propertyId", valueController.GetValuesFromPropertyId) // get all Values from Property by ID
router.DELETE("/deleteValueFromPropertyId/:valueId/:propertyId", valueController.DeleteValueFromPropertyId) // delete Value from Property by ID
router.GET("/getValue/:id", valueController.GetValue)
router.POST("/SetTerminalValueForProperty/:propertyId/:value", valueController.SetTerminalValueForProperty) // set Terminal Value for Property
// Product centric routing
// ProductGroup
router.POST("/createProductGroup", productGroupController.CreateProductGroup) // create ProductGroup
router.PUT("/updateProductGroup", productGroupController.UpdateProductGroup) // update ProductGroup
router.DELETE("/deleteProductGroup/:id", productGroupController.DeleteProductGroup) // delete ProductGroup
router.GET("/getProductGroup/:id", productGroupController.GetProductGroup) // get ProductGroup by id
router.GET("/getProductGroupsByParentId/:parentId", productGroupController.GetProductGroupsByParentId) // get all ProductGroups by parent ID
router.DELETE("/deleteProductFromProductGroupId/:productId", productGroupController.DeleteProductFromProductGroupId) // delete Product from ProductGroup by ID
router.POST("/getPropertiesFromProductGroupId/:productGroupId", productGroupController.GetPropertiesFromProductGroupId) // get Properties from ProductGroup by ID
router.DELETE("/deletePropertyFromProductGroupId/:propertyId/:productGroupId", productGroupController.DeletePropertyFromProductGroupId) // delete Property from ProductGroup by ID
router.GET("/createPropertyToProductGroupId/:propertyTypeId/:productGroupId", productGroupController.CreatePropertyToProductGroupId) // add Property to ProductGroup by ID
router.POST("/createTerminalPropertyForProductGroupId/:productGroupId/:propertyTypeId/:value", valueController.CreateTerminalPropertyForProductGroupId) // create Terminal Property for ProductGroup by ID
router.POST("/createTerminalPropertyForProductId/:productId/:propertyTypeId/:value", valueController.CreateTerminalPropertyForProductId) // create Terminal Property for Product by ID
// Product
router.POST("/createProduct", productController.CreateProduct) // create Product
router.PUT("/updateProduct", productController.UpdateProduct) // update Product
router.DELETE("/deleteProduct/:id", productController.DeleteProduct) // delete Product
router.GET("/getProduct/:id", productController.GetProduct) // get Product by id
router.GET("/getProductsByGroupId/:groupId", productController.GetProductsByGroupId) // get all Products by group ID
router.GET("/getPropertiesFromProductId/:productId", productController.GetPropertiesFromProductId) // get Properties from ProductGroup by ID
router.POST("/addPropertyToProductId/:productId", productController.AddPropertyToProductId) // add Property to Product by ID
router.DELETE("/deletePropertyFromProductId/:productId", productController.DeletePropertyFromProductId) // delete Property from Product by ID
// BOM centric routing
router.POST("/createBillOfMaterial", bomController.CreateBillOfMaterial)
router.PUT("/updateBillOfMaterial", bomController.UpdateBillOfMaterial)
router.DELETE("/deleteBillOfMaterial/:id", bomController.DeleteBillOfMaterial)
router.GET("/getBillOfMaterial/:id", bomController.GetBillOfMaterial)
router.GET("/getBOMProductsByBOMId/:bomId", bomController.GetBOMProductsByBOMId) // get all BOMContents by BOM ID
router.GET("/getBOMVariantNodesByBOMId/:bomId", bomController.GetBOMVariantNodesByBOMId) // get all VariantNodes by BOM ID
router.POST("/createVariantNode/:bomId", bomController.CreateVariantNode)
router.PUT("/updateVariantNode", bomController.UpdateVariantNode)
router.DELETE("/deleteVariantNode/:id", bomController.DeleteVariantNode)
router.GET("/getVariantNode/:id", bomController.GetVariantNode)
router.GET("/getVariantNodeProductsByVariantNodeId/:variantNodeId", bomController.GetVariantNodeProductsByVariantNodeId)
// Import functionality
router.POST("/importPropertyTypeGroups", importController.ImportPropertyTypeGroups) // import PropertyTypeGroups
router.POST("/importProductGroups", importController.ImportProductGroups) // import ProductGroups
// router.POST("/importProducts", importController.ImportProducts) // import Products
// server
return HttpServer{
config: config,
router: router,
dbHandler: dbHandler,
valueController: valueController,
productGroupController: productGroupController,
productController: productController,
}
}
func (hs HttpServer) Start() {
err := hs.router.Run(hs.config.GetString("http.server_address"))
if err != nil {
log.Fatalf("Error while starting HTTP server: %v", err)
}
}
func Test(ctx *gin.Context) {
ctx.JSON(http.StatusOK, gin.H{
"message": "Test",
})
}