PropertyTypes integriert
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"scpq-editor-server/models"
|
||||
"scpq-editor-server/services"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type PropertytypesController struct {
|
||||
PropertytypesService *services.PropertytypesService
|
||||
}
|
||||
|
||||
func NewPropertytypesController(PropertytypesService *services.PropertytypesService) *PropertytypesController {
|
||||
return &PropertytypesController{
|
||||
PropertytypesService: PropertytypesService,
|
||||
}
|
||||
}
|
||||
|
||||
func (lc PropertytypesController) CreatePropertytype(ctx *gin.Context) {
|
||||
body, err := io.ReadAll(ctx.Request.Body)
|
||||
if err != nil {
|
||||
log.Println("Error while reading create Translation request body", err)
|
||||
ctx.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
var propertyType models.PropertyType
|
||||
err = json.Unmarshal(body, &propertyType)
|
||||
if err != nil {
|
||||
log.Println("Error while unmarshaling create PropertyType request body", err)
|
||||
ctx.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
response, responseErr := lc.PropertytypesService.CreatePropertyType(&propertyType)
|
||||
if responseErr != nil {
|
||||
ctx.AbortWithStatusJSON(responseErr.Status, responseErr)
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
func (lc PropertytypesController) UpdatePropertytype(ctx *gin.Context) {
|
||||
body, err := io.ReadAll(ctx.Request.Body)
|
||||
if err != nil {
|
||||
log.Println("Error while reading update PropertyType request body", err)
|
||||
ctx.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
var propertyType models.PropertyType
|
||||
err = json.Unmarshal(body, &propertyType)
|
||||
if err != nil {
|
||||
log.Println("Error while unmarshaling update PropertyType request body", err)
|
||||
ctx.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
responseErr := lc.PropertytypesService.UpdatePropertyType(&propertyType)
|
||||
if responseErr != nil {
|
||||
ctx.AbortWithStatusJSON(responseErr.Status, responseErr)
|
||||
return
|
||||
}
|
||||
ctx.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (lc PropertytypesController) DeletePropertytype(ctx *gin.Context) {
|
||||
propertyTypeId := ctx.Param("id")
|
||||
lId, err := strconv.Atoi(propertyTypeId)
|
||||
if err != nil {
|
||||
log.Println("Error propertyTypeId not int", err)
|
||||
ctx.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
responseErr := lc.PropertytypesService.DeletePropertyType(lId)
|
||||
if responseErr != nil {
|
||||
ctx.JSON(responseErr.Status, responseErr)
|
||||
return
|
||||
}
|
||||
ctx.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (lc PropertytypesController) GetPropertytype(ctx *gin.Context) {
|
||||
propertyTypeId := ctx.Param("id")
|
||||
lId, err := strconv.Atoi(propertyTypeId)
|
||||
if err != nil {
|
||||
log.Println("Error propertyTypeId not int", err)
|
||||
ctx.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
response, responseErr := lc.PropertytypesService.GetPropertyType(lId)
|
||||
if responseErr != nil {
|
||||
ctx.JSON(responseErr.Status, responseErr)
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
func (lc PropertytypesController) GetAllPropertytypes(ctx *gin.Context) {
|
||||
response, responseErr := lc.PropertytypesService.GetAllPropertyTypes()
|
||||
if responseErr != nil {
|
||||
ctx.JSON(responseErr.Status, responseErr)
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
func (lc PropertytypesController) GetAllPropertytypesByPropertyGroupId(ctx *gin.Context) {
|
||||
propertyGroupId := ctx.Param("propertyGroupId")
|
||||
lId, err := strconv.Atoi(propertyGroupId)
|
||||
if err != nil {
|
||||
log.Println("Error propertyGroupId not int", err)
|
||||
ctx.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
response, responseErr := lc.PropertytypesService.GetAllPropertyTypesByPropertyGroupId(lId)
|
||||
if responseErr != nil {
|
||||
ctx.JSON(responseErr.Status, responseErr)
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, response)
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package models
|
||||
|
||||
type PropertyType struct {
|
||||
ID int `json:"id"`
|
||||
Label string `json:"label"`
|
||||
ValueTypeID int `json:"valueTypeId"`
|
||||
PropertyGroupID int `json:"propertyGroupId"`
|
||||
ValueComplexityId string `json:"valueComplexityId"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
@@ -0,0 +1,292 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"net/http"
|
||||
"scpq-editor-server/models"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type PropertyTypesRepository struct {
|
||||
dbHandler *sql.DB
|
||||
}
|
||||
|
||||
func NewPropertyTypesRepository(dbHandler *sql.DB) *PropertyTypesRepository {
|
||||
return &PropertyTypesRepository{
|
||||
dbHandler: dbHandler,
|
||||
}
|
||||
}
|
||||
|
||||
func (rr PropertyTypesRepository) CreatePropertyType(PropertyType *models.PropertyType) (*models.PropertyType, *models.ResponseError) {
|
||||
query := `INSERT INTO property_types(label, value_type_id, property_group_id, value_complexity_id, value) VALUES($1,$2,$3,$4,$5) RETURNING id`
|
||||
rows, err := rr.dbHandler.Query(query, PropertyType.Label, PropertyType.ValueTypeID, PropertyType.PropertyGroupID, PropertyType.ValueComplexityId, PropertyType.Value)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
var idstr string
|
||||
for rows.Next() {
|
||||
err := rows.Scan(&idstr)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
}
|
||||
if rows.Err() != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
PropertyTypeId, err := strconv.Atoi(idstr)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: "PropertyType id not int",
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
|
||||
return &models.PropertyType{
|
||||
ID: PropertyTypeId,
|
||||
Label: PropertyType.Label,
|
||||
ValueTypeID: PropertyType.ValueTypeID,
|
||||
PropertyGroupID: PropertyType.PropertyGroupID,
|
||||
ValueComplexityId: PropertyType.ValueComplexityId,
|
||||
Value: PropertyType.Value,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (rr PropertyTypesRepository) UpdatePropertyType(PropertyType *models.PropertyType) *models.ResponseError {
|
||||
query := `UPDATE property_types SET value_type_id=$1, property_group_id=$2, value_complexity_id=$3, value=$4 WHERE id=$5`
|
||||
res, err := rr.dbHandler.Exec(query, PropertyType.ValueTypeID, PropertyType.PropertyGroupID, PropertyType.ValueComplexityId, PropertyType.Value, PropertyType.ID)
|
||||
if err != nil {
|
||||
return &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
|
||||
rowsAffeced, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
if rowsAffeced == 0 {
|
||||
return &models.ResponseError{
|
||||
Message: "No PropertyType found with the given id",
|
||||
Status: http.StatusNotFound,
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (rr PropertyTypesRepository) DeletePropertyType(PropertyTypeId int) *models.ResponseError {
|
||||
query := `DELETE FROM property_types WHERE id=$1`
|
||||
res, err := rr.dbHandler.Exec(query, PropertyTypeId)
|
||||
if err != nil {
|
||||
return &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
|
||||
rowsAffeced, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
if rowsAffeced == 0 {
|
||||
return &models.ResponseError{
|
||||
Message: "No PropertyType found with the given id",
|
||||
Status: http.StatusNotFound,
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rr PropertyTypesRepository) GetPropertyType(PropertyTypeId int) (*models.PropertyType, *models.ResponseError) {
|
||||
query := `SELECT id, label, value_type_id, property_group_id, value_complexity_id, value FROM property_types WHERE id=$1`
|
||||
rows, err := rr.dbHandler.Query(query, PropertyTypeId)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
var idstr, label, value_type_id_str, property_group_id_str, value_complexity_id, value string
|
||||
for rows.Next() {
|
||||
err := rows.Scan(&idstr, &label, &value_type_id_str, &property_group_id_str, &value_complexity_id, &value)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
}
|
||||
if rows.Err() != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
PropertyTypeId, err = strconv.Atoi(idstr)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: "PropertyType id not int",
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
PropertyGroupID, err := strconv.Atoi(property_group_id_str)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: "PropertyGroup id not int",
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
ValueTypeID, err := strconv.Atoi(value_type_id_str)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: "ValueType id not int",
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
|
||||
return &models.PropertyType{
|
||||
ID: PropertyTypeId,
|
||||
Label: label,
|
||||
ValueTypeID: ValueTypeID,
|
||||
ValueComplexityId: value_complexity_id,
|
||||
Value: value,
|
||||
PropertyGroupID: PropertyGroupID,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (rr PropertyTypesRepository) GetAllPropertyTypes() ([]*models.PropertyType, *models.ResponseError) {
|
||||
query := "SELECT id, label, value_type_id, property_group_id, value_complexity_id, value FROM property_types"
|
||||
rows, err := rr.dbHandler.Query(query)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
PropertyTypes := make([]*models.PropertyType, 0)
|
||||
var idstr, label, value_type_id_str, property_group_id_str, value_complexity_id, value string
|
||||
for rows.Next() {
|
||||
err := rows.Scan(&idstr, &label, &value_type_id_str, &property_group_id_str, &value_complexity_id, &value)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
PropertyTypeId, err := strconv.Atoi(idstr)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: "PropertyType id not int",
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
PropertyGroupID, err := strconv.Atoi(property_group_id_str)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: "PropertyGroup id not int",
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
ValueTypeID, err := strconv.Atoi(value_type_id_str)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: "ValueType id not int",
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
|
||||
PropertyType := &models.PropertyType{
|
||||
ID: PropertyTypeId,
|
||||
Label: label,
|
||||
ValueTypeID: ValueTypeID,
|
||||
ValueComplexityId: value_complexity_id,
|
||||
Value: value,
|
||||
PropertyGroupID: PropertyGroupID,
|
||||
}
|
||||
PropertyTypes = append(PropertyTypes, PropertyType)
|
||||
|
||||
}
|
||||
if rows.Err() != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
return PropertyTypes, nil
|
||||
}
|
||||
|
||||
func (rr PropertyTypesRepository) GetAllPropertyTypesByPropertyGroupId(propertyGroupId int) ([]*models.PropertyType, *models.ResponseError) {
|
||||
query := "SELECT id, label, value_type_id, property_group_id, value_complexity_id, value FROM property_types WHERE property_group_id=$1"
|
||||
rows, err := rr.dbHandler.Query(query, propertyGroupId)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
PropertyTypes := make([]*models.PropertyType, 0)
|
||||
var idstr, label, value_type_id_str, property_group_id_str, value_complexity_id, value string
|
||||
var ValueTypeID int
|
||||
for rows.Next() {
|
||||
err := rows.Scan(&idstr, &label, &value_type_id_str, &property_group_id_str, &value_complexity_id, &value)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
PropertyTypeId, err := strconv.Atoi(idstr)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: "PropertyType id not int",
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
PropertyGroupID, err := strconv.Atoi(property_group_id_str)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: "PropertyGroup id not int",
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
PropertyType := &models.PropertyType{
|
||||
ID: PropertyTypeId,
|
||||
Label: label,
|
||||
ValueTypeID: ValueTypeID,
|
||||
ValueComplexityId: value_complexity_id,
|
||||
Value: value,
|
||||
PropertyGroupID: PropertyGroupID,
|
||||
}
|
||||
PropertyTypes = append(PropertyTypes, PropertyType)
|
||||
|
||||
}
|
||||
if rows.Err() != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
return PropertyTypes, nil
|
||||
}
|
||||
@@ -30,10 +30,12 @@ func InitHttpServer(config *viper.Viper, dbHandler *sql.DB) HttpServer {
|
||||
valuetypesRepository := repositories.NewValuetypesRepository(dbHandler)
|
||||
valuecomplexitiesRepository := repositories.NewValuecomplexitiesRepository(dbHandler)
|
||||
translationsRepository := repositories.NewTranslationsRepository(dbHandler)
|
||||
propertytypesRepository := repositories.NewPropertyTypesRepository(dbHandler)
|
||||
|
||||
// service initialisation:
|
||||
languagesService := services.NewLanguagesService(languagesRepository)
|
||||
propertygroupsService := services.NewPropertygroupsService(propertygroupsRepository)
|
||||
propertytypesService := services.NewPropertytypesService(propertytypesRepository)
|
||||
valuetypesService := services.NewValuetypesService(valuetypesRepository)
|
||||
valuecomplexitiesService := services.NewValuecomplexitiesService(valuecomplexitiesRepository)
|
||||
translationsService := services.NewTranslationsService(translationsRepository)
|
||||
@@ -41,6 +43,7 @@ func InitHttpServer(config *viper.Viper, dbHandler *sql.DB) HttpServer {
|
||||
// controller initialisation:
|
||||
languageController := controller.NewLanguagesController(languagesService)
|
||||
propertygroupsController := controller.NewPropertygroupsController(propertygroupsService)
|
||||
propertytypesController := controller.NewPropertytypesController(propertytypesService)
|
||||
valuetypesController := controller.NewValuetypesController(valuetypesService)
|
||||
valuecomplexitiesController := controller.NewValuecomplexitiesController(valuecomplexitiesService)
|
||||
translationsController := controller.NewTranslationsController(translationsService)
|
||||
@@ -72,6 +75,14 @@ func InitHttpServer(config *viper.Viper, dbHandler *sql.DB) HttpServer {
|
||||
router.GET("/translations", translationsController.GetAllTranslations)
|
||||
router.GET("/translations/:languageId", translationsController.GetAllTranslationsByLanguageId)
|
||||
|
||||
// Property-Type centric routing
|
||||
router.POST("/propertytype", propertytypesController.CreatePropertytype)
|
||||
router.PUT("/propertytype", propertytypesController.UpdatePropertytype)
|
||||
router.DELETE("/propertytype/:id", propertytypesController.DeletePropertytype)
|
||||
router.GET("/propertytype/:id", propertytypesController.GetPropertytype)
|
||||
router.GET("/propertytypes", propertytypesController.GetAllPropertytypes)
|
||||
router.GET("/propertytypes/:propertyGroupId", propertytypesController.GetAllPropertytypesByPropertyGroupId)
|
||||
|
||||
// valuetype centric routing
|
||||
router.GET("/valuetype/:id", valuetypesController.GetValuetype)
|
||||
router.GET("/valuetypes", valuetypesController.GetAllValuetypes)
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"scpq-editor-server/models"
|
||||
"scpq-editor-server/repositories"
|
||||
)
|
||||
|
||||
type PropertytypesService struct {
|
||||
propertyTypesRepository *repositories.PropertyTypesRepository
|
||||
}
|
||||
|
||||
func NewPropertytypesService(propertyTypesRepository *repositories.PropertyTypesRepository) *PropertytypesService {
|
||||
return &PropertytypesService{
|
||||
propertyTypesRepository: propertyTypesRepository,
|
||||
}
|
||||
}
|
||||
|
||||
func (ls PropertytypesService) CreatePropertyType(propertyType *models.PropertyType) (*models.PropertyType, *models.ResponseError) {
|
||||
responseErr := validatePropertyType(propertyType)
|
||||
if responseErr != nil {
|
||||
return nil, responseErr
|
||||
}
|
||||
return ls.propertyTypesRepository.CreatePropertyType(propertyType)
|
||||
}
|
||||
|
||||
func (ls PropertytypesService) UpdatePropertyType(propertyType *models.PropertyType) *models.ResponseError {
|
||||
responseErr := validatePropertyType(propertyType)
|
||||
if responseErr != nil {
|
||||
return responseErr
|
||||
}
|
||||
return ls.propertyTypesRepository.UpdatePropertyType(propertyType)
|
||||
}
|
||||
|
||||
func (ls PropertytypesService) DeletePropertyType(propertyTypeId int) *models.ResponseError {
|
||||
return ls.propertyTypesRepository.DeletePropertyType(propertyTypeId)
|
||||
}
|
||||
|
||||
func (ls PropertytypesService) GetPropertyType(propertyTypeId int) (*models.PropertyType, *models.ResponseError) {
|
||||
return ls.propertyTypesRepository.GetPropertyType(propertyTypeId)
|
||||
}
|
||||
|
||||
func (ls PropertytypesService) GetAllPropertyTypes() ([]*models.PropertyType, *models.ResponseError) {
|
||||
return ls.propertyTypesRepository.GetAllPropertyTypes()
|
||||
}
|
||||
|
||||
func (ls PropertytypesService) GetAllPropertyTypesByPropertyGroupId(propertyGroupId int) ([]*models.PropertyType, *models.ResponseError) {
|
||||
return ls.propertyTypesRepository.GetAllPropertyTypesByPropertyGroupId(propertyGroupId)
|
||||
}
|
||||
|
||||
func validatePropertyType(propertyType *models.PropertyType) *models.ResponseError {
|
||||
if propertyType.Value == "" {
|
||||
return &models.ResponseError{
|
||||
Message: "Invalid PropertyType value",
|
||||
Status: http.StatusBadRequest,
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user