PropertyTypes integriert

This commit is contained in:
2026-08-04 22:51:10 +02:00
parent 8697336ba3
commit 19704fa89e
11 changed files with 647 additions and 3 deletions
@@ -0,0 +1,30 @@
import ky from 'ky'
import type { PropertyType } from '../../data/propertyType'
export const getPropertyTypes = async (): Promise<PropertyType[]> => {
return ky.get(`http://localhost:8088/propertytypes`).json<PropertyType[]>()
}
export const getPropertyTypeById = async (id: number): Promise<PropertyType> => {
return ky.get(`http://localhost:8088/propertytype/${id}`).json<PropertyType>()
}
export const createPropertyType = async (propertyType: PropertyType): Promise<PropertyType> => {
return ky.post(`http://localhost:8088/propertytype`, { json: propertyType }).json<PropertyType>()
}
export const updatePropertyType = async (propertyType: PropertyType): Promise<PropertyType> => {
return ky.put(`http://localhost:8088/propertytype`, { json: propertyType }).json<PropertyType>()
}
export const deletePropertyType = async (id: number): Promise<void> => {
return ky.delete(`http://localhost:8088/propertytype/${id}`).json<void>()
}
export const getPropertyTypesByPropertyGroupId = async (propertyGroupId: number): Promise<PropertyType[]> => {
return ky.get(`http://localhost:8088/propertytypes/${propertyGroupId}`).json<PropertyType[]>()
}
export const getAllPropertyTypes = async (): Promise<PropertyType[]> => {
return ky.get(`http://localhost:8088/propertytypes`).json<PropertyType[]>()
}
@@ -0,0 +1,108 @@
import * as React from 'react';
import type { propertyGroup } from '../data/propertyGroup';
import type { PropertyType } from '../data/propertyType';
import Paper from '@mui/material/Paper';
import DeleteIcon from '@mui/icons-material/Delete';
import AddIcon from '@mui/icons-material/Add';
import Tooltip from '@mui/material/Tooltip';
import FileDownloadIcon from '@mui/icons-material/FileDownload';
import { Typography } from '@mui/material';
import { DataGrid, Toolbar, ToolbarButton} from '@mui/x-data-grid';
import { ExportCsv } from '@mui/x-data-grid';
import { getPropertyGroupById } from '../api/language/propertyGroup.api';
import { updatePropertyType } from '../api/language/propertyTypes.api';
import { getPropertyTypesByPropertyGroupId } from '../api/language/propertyTypes.api';
const PropertyTypesTableView = ({ selectedRow }: { selectedRow: number }) => {
const [propertyTypes, setPropertyTypes] = React.useState<PropertyType[]>([]);
const [propertyGroup, setPropertyGroup] = React.useState<propertyGroup | null>(null);
React.useEffect(() => {
getPropertyTypesByPropertyGroupId(selectedRow).then(setPropertyTypes);
}, [selectedRow]);
React.useEffect(() => {
getPropertyGroupById(selectedRow).then(setPropertyGroup);
}, [selectedRow]);
function CustomToolbar() {
return (
<Toolbar>
<Typography variant="body1" component="div" sx={{ flexGrow: 1 }}>
Property types for group {propertyGroup?.label}.
</Typography>
<Tooltip title="Download as CSV">
<ExportCsv render={<ToolbarButton />}>
<FileDownloadIcon fontSize="small" />
</ExportCsv>
</Tooltip>
<ToolbarButton onClick={() => console.log('Delete Row clicked')}>
<DeleteIcon fontSize="small" sx={{ ml: 'auto' }} />
</ToolbarButton>
<ToolbarButton onClick={() => console.log('Add Row clicked')}>
<AddIcon fontSize="small" sx={{ ml: 'auto' }} />
</ToolbarButton>
</Toolbar>
)
}
return (
<div>
<Paper sx={{ height: 600, width: '50vw' }}>
<DataGrid
sx={{ border: 1, borderRadius: 2,
boxShadow: 2,
borderColor: 'primary.main',
'& .MuiDataGrid-cell:hover': { color: 'primary.main' },
'& .even-row': { backgroundColor: 'rgba(0, 0, 1, 0.02)' },
'& .odd-row': { backgroundColor: 'rgba(0, 0, 20, 0.04)' },
}}
columnHeaderHeight={36}
rowHeight={25}
rowSpacingType="margin"
initialState={{
sorting: {
sortModel: [{ field: 'id', sort: 'asc' }],
},
}}
rows={propertyTypes}
columns={[
{ field: 'id', headerName: 'ID', width: 100 ,align: 'right', headerAlign: 'right', editable: false},
{ field: 'label', headerName: 'Label', width: 200 , editable: true},
{ field: 'valueTypeId', headerName: 'Value Type Id', width: 100 , editable: true},
{ field: 'propertyGroupId', headerName: 'Property Group Id', width: 100 , editable: true},
{ field: 'valueComplexityId', headerName: 'Value Complexity Id', width: 100 , editable: true},
{ field: 'value', headerName: 'Value', width: 200 , editable: true},
]}
slots={{
toolbar: CustomToolbar,
}}
processRowUpdate={(updatedRow, originalRow) => {
updatePropertyType({
id: updatedRow.id,
valueTypeId: updatedRow.valueTypeId,
propertyGroupId: updatedRow.propertyGroupId,
valueComplexityId: updatedRow.valueComplexityId,
value: updatedRow.value
}
)
return updatedRow;
}}
getRowClassName={(params) =>
params.indexRelativeToCurrentPage % 2 === 0 ? 'even-row' : 'odd-row'
}
showToolbar
/>
</Paper>
</div>
);
};
export default PropertyTypesTableView;
@@ -0,0 +1,9 @@
export type PropertyType = {
id: number;
label: string;
valueTypeId: number;
propertyGroupId: number;
valueComplexityId: number;
value: string;
};
@@ -1,6 +1,6 @@
import React, { useEffect, useState } from 'react';
import PropertyTypesTableView from '../components/PropertyTypesTableView';
import PropertyGroupsTableView from '../components/PropertyGroupsTableView';
import TranslationsTableView from '../components/TranslationsTableView';
import Box from '@mui/material/Box';
import Typography from '@mui/material/Typography';
import type { propertyGroup } from '../data/propertyGroup';
@@ -28,7 +28,7 @@ const PropertyPage: React.FC = () => {
}}
>
<PropertyGroupsTableView propertyGroups={propertyGroups} setSelectedRow={setSelectedRow} />
<TranslationsTableView selectedRow={selectedRow} />
<PropertyTypesTableView selectedRow={selectedRow} />
</Box>
</Box>
);
@@ -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
}