Vereinfachung des Datenmodells und Anpassen der UI
This commit is contained in:
@@ -1,22 +1,18 @@
|
||||
import {useState, useEffect} from 'react';
|
||||
import Tooltip from '@mui/material/Tooltip';
|
||||
import { ExportCsv } from '@mui/x-data-grid';
|
||||
import type {Language} from '../data/language';
|
||||
|
||||
import { DataGrid, Toolbar, ToolbarButton, useGridApiRef } from '@mui/x-data-grid';
|
||||
import { DataGrid, Toolbar, ToolbarButton} from '@mui/x-data-grid';
|
||||
import Paper from '@mui/material/Paper';
|
||||
import DeleteIcon from '@mui/icons-material/Delete';
|
||||
import AddIcon from '@mui/icons-material/Add';
|
||||
import FileDownloadIcon from '@mui/icons-material/FileDownload';
|
||||
|
||||
import {getLanguages} from '../api/language/languages.api';
|
||||
import {updateLanguage} from '../api/language/languages.api';
|
||||
import type { GridSelectionModel } from '@mui/x-data-grid';
|
||||
import { Typography } from '@mui/material';
|
||||
|
||||
const LanguagesTableView = () => {
|
||||
const [languages, setLanguages] = useState<Language[]>([]);
|
||||
|
||||
const apiRef = useGridApiRef();
|
||||
const LanguagesTableView = ({ languages, setSelectedRow }: { languages: Language[]; setSelectedRow: (id: number) => void }) => {
|
||||
// const [languages, setLanguages] = useState<Language[]>([]);
|
||||
|
||||
function CustomToolbar() {
|
||||
return (
|
||||
@@ -38,34 +34,50 @@ const LanguagesTableView = () => {
|
||||
</Toolbar>
|
||||
)
|
||||
}
|
||||
useEffect(() => {
|
||||
getLanguages().then(setLanguages);
|
||||
}, []);
|
||||
|
||||
|
||||
return (
|
||||
<div>
|
||||
<Paper sx={{ height: 600, width: '50%' }}>
|
||||
<Paper sx={{ height: 600, width: '100%' }}>
|
||||
<DataGrid
|
||||
initialState={{
|
||||
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: 'language', sort: 'asc' }],
|
||||
},
|
||||
}}
|
||||
rows={languages}
|
||||
columns={[
|
||||
{ field: 'id', headerName: 'ID', width: 100 ,align: 'right'},
|
||||
{ field: 'id', headerName: 'ID', width: 100 ,align: 'right', headerAlign: 'right', editable: false},
|
||||
{ field: 'locale', headerName: 'Locale', width: 200, editable: true },
|
||||
{ field: 'language', headerName: 'Language', width: 200 , editable: true},
|
||||
]}
|
||||
slots={{
|
||||
toolbar: CustomToolbar,
|
||||
}}
|
||||
pageSize={5}
|
||||
rowsPerPageOptions={[5]}
|
||||
processRowUpdate={(updatedRow, originalRow) => {
|
||||
updateLanguage({id: updatedRow.id, locale: updatedRow.locale, language: updatedRow.language});
|
||||
return updatedRow;
|
||||
}}
|
||||
getRowClassName={(params) =>
|
||||
params.indexRelativeToCurrentPage % 2 === 0 ? 'even-row' : 'odd-row'
|
||||
}
|
||||
onCellClick={(params) => {
|
||||
const selectedRowId = params.id as number;
|
||||
const selectedRow = languages.find((row) => row.id === selectedRowId);
|
||||
setSelectedRow(selectedRow ? selectedRow.id : 0);
|
||||
}}
|
||||
|
||||
showToolbar
|
||||
/>
|
||||
</Paper>
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
import Tooltip from '@mui/material/Tooltip';
|
||||
import { ExportCsv } from '@mui/x-data-grid';
|
||||
import type {Language} from '../data/language';
|
||||
|
||||
import { DataGrid, Toolbar, ToolbarButton} from '@mui/x-data-grid';
|
||||
import Paper from '@mui/material/Paper';
|
||||
import DeleteIcon from '@mui/icons-material/Delete';
|
||||
import AddIcon from '@mui/icons-material/Add';
|
||||
import FileDownloadIcon from '@mui/icons-material/FileDownload';
|
||||
import {updateLanguage} from '../api/language/languages.api';
|
||||
import type { GridSelectionModel } from '@mui/x-data-grid';
|
||||
import { Typography } from '@mui/material';
|
||||
|
||||
const TranslationsTableView = ({ selectedRow }: { selectedRow: number }) => {
|
||||
// const [languages, setLanguages] = useState<Language[]>([]);
|
||||
|
||||
function CustomToolbar() {
|
||||
return (
|
||||
<Toolbar>
|
||||
<Typography variant="body1" component="div" sx={{ flexGrow: 1 }}>
|
||||
Here you see all your available languages for translations.
|
||||
</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: '100%' }}>{selectedRow}</Paper>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
/*{ <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: 'language', sort: 'asc' }],
|
||||
},
|
||||
}}
|
||||
rows={languages}
|
||||
columns={[
|
||||
{ field: 'id', headerName: 'ID', width: 100 ,align: 'right', headerAlign: 'right', editable: false},
|
||||
{ field: 'locale', headerName: 'Locale', width: 200, editable: true },
|
||||
{ field: 'language', headerName: 'Language', width: 200 , editable: true},
|
||||
]}
|
||||
slots={{
|
||||
toolbar: CustomToolbar,
|
||||
}}
|
||||
processRowUpdate={(updatedRow, originalRow) => {
|
||||
updateLanguage({id: updatedRow.id, locale: updatedRow.locale, language: updatedRow.language});
|
||||
return updatedRow;
|
||||
}}
|
||||
getRowClassName={(params) =>
|
||||
params.indexRelativeToCurrentPage % 2 === 0 ? 'even-row' : 'odd-row'
|
||||
}
|
||||
onSelectionModelChange={(newSelection) => {
|
||||
const selectedRowId = newSelection[0];
|
||||
const selectedRow = languages.find((row) => row.id === selectedRowId);
|
||||
setSelectedRow(selectedRow ? selectedRow.id : 0);
|
||||
}}
|
||||
|
||||
showToolbar
|
||||
/> }
|
||||
</Paper>
|
||||
</div>
|
||||
);
|
||||
};*/
|
||||
|
||||
export default TranslationsTableView;
|
||||
@@ -1,17 +1,37 @@
|
||||
import React from 'react';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import LanguagesTableView from '../components/LanguagesTableView';
|
||||
import TranslationsTableView from '../components/TranslationsTableView';
|
||||
import Box from '@mui/material/Box';
|
||||
import Typography from '@mui/material/Typography';
|
||||
import Grid from '@mui/material/Grid';
|
||||
import type { Language } from '../data/language';
|
||||
import { getLanguages } from '../api/language/languages.api';
|
||||
|
||||
const LanguagesPage: React.FC = () => {
|
||||
return (
|
||||
<Box component="section" sx={{ p: 2, border: '1px dashed grey' }}>
|
||||
<Typography variant="h3">
|
||||
Language Management
|
||||
</Typography>
|
||||
<LanguagesTableView />
|
||||
</Box>
|
||||
);
|
||||
};
|
||||
const [languages, setLanguages] = useState<Language[]>([]);
|
||||
const [selectedRow, setSelectedRow] = useState(0);
|
||||
useEffect(() => {
|
||||
getLanguages().then(setLanguages);
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<Box component="section" sx={{ p: 2, border: '1px dashed grey' }}>
|
||||
<Typography variant="h3">
|
||||
Language Management
|
||||
</Typography>
|
||||
<Box
|
||||
sx={{
|
||||
display: "flex", // Arrange children in a row
|
||||
flexDirection: "row",
|
||||
p: 2, // Padding around the flex container
|
||||
bgcolor: "#f5f5f5"
|
||||
}}
|
||||
>
|
||||
<LanguagesTableView languages={languages} setSelectedRow={setSelectedRow} />
|
||||
<TranslationsTableView selectedRow={selectedRow} />
|
||||
</Box>
|
||||
</Box>
|
||||
);
|
||||
};
|
||||
|
||||
export default LanguagesPage;
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
import React from 'react';
|
||||
import Box from '@mui/material/Box';
|
||||
import Typography from '@mui/material/Typography';
|
||||
|
||||
const PropertyPage: React.FC = () => {
|
||||
return (
|
||||
<div>
|
||||
<h1>Welcome to the Property Page</h1>
|
||||
<p>This is the main landing page for Property handling.</p>
|
||||
</div>
|
||||
<Box component="section" sx={{ p: 2, border: '1px dashed grey' }}>
|
||||
<Typography variant="h3">
|
||||
Property Management
|
||||
</Typography>
|
||||
</Box>
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
@@ -24,19 +24,6 @@ func NewPropertygroupsController(PropertygroupsService *services.PropertygroupsS
|
||||
|
||||
func (lc PropertygroupsController) CreatePropertygroup(ctx *gin.Context) {
|
||||
|
||||
PropertygroupParentId := ctx.Param("parentid")
|
||||
lId, err := strconv.Atoi(PropertygroupParentId)
|
||||
if err != nil {
|
||||
log.Println("Error PropertygroupId not int", err)
|
||||
ctx.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
parent, responseErr := lc.PropertygroupsService.GetPropertygroup(lId, 1)
|
||||
if responseErr != nil {
|
||||
ctx.JSON(responseErr.Status, responseErr)
|
||||
return
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(ctx.Request.Body)
|
||||
if err != nil {
|
||||
log.Println("Error while reading create Propertygroup request body", err)
|
||||
@@ -51,7 +38,7 @@ func (lc PropertygroupsController) CreatePropertygroup(ctx *gin.Context) {
|
||||
ctx.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
response, responseErr := lc.PropertygroupsService.CreatePropertygroup(&Propertygroup, parent)
|
||||
response, responseErr := lc.PropertygroupsService.CreatePropertygroup(&Propertygroup)
|
||||
if responseErr != nil {
|
||||
ctx.AbortWithStatusJSON(responseErr.Status, responseErr)
|
||||
return
|
||||
@@ -81,54 +68,6 @@ func (lc PropertygroupsController) UpdatePropertygroup(ctx *gin.Context) {
|
||||
ctx.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (lc PropertygroupsController) ChangePropertygroupParent(ctx *gin.Context) {
|
||||
params := ctx.Request.URL.Query()
|
||||
oldPropertygroupParentId := params.Get("oldparentid")
|
||||
lId, err := strconv.Atoi(oldPropertygroupParentId)
|
||||
if err != nil {
|
||||
log.Println("Error PropertygroupId not int", err)
|
||||
ctx.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
oldparent, responseErr := lc.PropertygroupsService.GetPropertygroup(lId, 1)
|
||||
if responseErr != nil {
|
||||
ctx.JSON(responseErr.Status, responseErr)
|
||||
return
|
||||
}
|
||||
newPropertygroupParentId := params.Get("newparentid")
|
||||
lId, err = strconv.Atoi(newPropertygroupParentId)
|
||||
if err != nil {
|
||||
log.Println("Error PropertygroupId not int", err)
|
||||
ctx.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
newparent, responseErr := lc.PropertygroupsService.GetPropertygroup(lId, 1)
|
||||
if responseErr != nil {
|
||||
ctx.JSON(responseErr.Status, responseErr)
|
||||
return
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(ctx.Request.Body)
|
||||
if err != nil {
|
||||
log.Println("Error while reading update Propertygroup request body", err)
|
||||
ctx.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
var Propertygroup models.Propertygroup
|
||||
err = json.Unmarshal(body, &Propertygroup)
|
||||
if err != nil {
|
||||
log.Println("Error while unmarshaling update Propertygroup request body", err)
|
||||
ctx.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
responseErr = lc.PropertygroupsService.ChangePropertygroupParent(&Propertygroup, oldparent, newparent)
|
||||
if responseErr != nil {
|
||||
ctx.AbortWithStatusJSON(responseErr.Status, responseErr)
|
||||
return
|
||||
}
|
||||
ctx.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (lc PropertygroupsController) DeletePropertygroup(ctx *gin.Context) {
|
||||
PropertygroupId := ctx.Param("id")
|
||||
lId, err := strconv.Atoi(PropertygroupId)
|
||||
@@ -148,21 +87,23 @@ func (lc PropertygroupsController) DeletePropertygroup(ctx *gin.Context) {
|
||||
|
||||
func (lc PropertygroupsController) GetPropertygroup(ctx *gin.Context) {
|
||||
PropertygroupId := ctx.Param("id")
|
||||
LangId := ctx.Param("langid")
|
||||
lId, err := strconv.Atoi(PropertygroupId)
|
||||
if err != nil {
|
||||
log.Println("Error PropertygroupId not int", err)
|
||||
ctx.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
languageId, err := strconv.Atoi(LangId)
|
||||
if err != nil {
|
||||
log.Println("Error LangId not int", err)
|
||||
ctx.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
response, responseErr := lc.PropertygroupsService.GetPropertygroup(lId, languageId)
|
||||
response, responseErr := lc.PropertygroupsService.GetPropertygroup(lId)
|
||||
if responseErr != nil {
|
||||
ctx.JSON(responseErr.Status, responseErr)
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
func (lc PropertygroupsController) GetAllPropertygroups(ctx *gin.Context) {
|
||||
response, responseErr := lc.PropertygroupsService.GetAllPropertygroups()
|
||||
if responseErr != nil {
|
||||
ctx.JSON(responseErr.Status, responseErr)
|
||||
return
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"scpq-editor-server/services"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type ValuecomplexitiesController struct {
|
||||
ValuecomplexitiesService *services.ValuecomplexitiesService
|
||||
}
|
||||
|
||||
func NewValuecomplexitiesController(ValuecomplexitiesService *services.ValuecomplexitiesService) *ValuecomplexitiesController {
|
||||
return &ValuecomplexitiesController{
|
||||
ValuecomplexitiesService: ValuecomplexitiesService,
|
||||
}
|
||||
}
|
||||
|
||||
func (vc ValuecomplexitiesController) GetAllValuecomplexities(ctx *gin.Context) {
|
||||
response, responseErr := vc.ValuecomplexitiesService.GetAllValuecomplexities()
|
||||
if responseErr != nil {
|
||||
ctx.JSON(responseErr.Status, responseErr)
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, response)
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"scpq-editor-server/services"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type ValuetypesController struct {
|
||||
ValuetypesService *services.ValuetypesService
|
||||
}
|
||||
|
||||
func NewValuetypesController(ValuetypesService *services.ValuetypesService) *ValuetypesController {
|
||||
return &ValuetypesController{
|
||||
ValuetypesService: ValuetypesService,
|
||||
}
|
||||
}
|
||||
|
||||
func (vc ValuetypesController) GetValuetype(ctx *gin.Context) {
|
||||
ValuetypeId := ctx.Param("id")
|
||||
lId, err := strconv.Atoi(ValuetypeId)
|
||||
if err != nil {
|
||||
log.Println("Error ValuetypeId not int", err)
|
||||
ctx.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
response, responseErr := vc.ValuetypesService.GetValuetype(lId)
|
||||
if responseErr != nil {
|
||||
ctx.JSON(responseErr.Status, responseErr)
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
func (vc ValuetypesController) GetAllValuetypes(ctx *gin.Context) {
|
||||
response, responseErr := vc.ValuetypesService.GetAllValuetypes()
|
||||
if responseErr != nil {
|
||||
ctx.JSON(responseErr.Status, responseErr)
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, response)
|
||||
return
|
||||
}
|
||||
@@ -1,10 +1,7 @@
|
||||
package models
|
||||
|
||||
type Propertygroup struct {
|
||||
ID int `json:"id"`
|
||||
Label string `json:"label"`
|
||||
LabelTranslation string `json:"labelTranslation"`
|
||||
Description string `json:"description"`
|
||||
DescriptionTranslation string `json:"descriptionTranslationKey"`
|
||||
SubPropertygroups []*Propertygroup `json:"subPropertygroups"`
|
||||
ID int `json:"id"`
|
||||
Label string `json:"label"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package models
|
||||
|
||||
type Valuecomplexity struct {
|
||||
ID int `json:"id"`
|
||||
Complexity string `json:"complexity"`
|
||||
Valuedefinition string `json:"valuedefinition"`
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package models
|
||||
|
||||
type Valuetype struct {
|
||||
ID int `json:"id"`
|
||||
Label string `json:"label"`
|
||||
}
|
||||
@@ -20,9 +20,9 @@ func NewPropertiesgroupsRepository(dbHandler *sql.DB) *PropertygroupsRepository
|
||||
}
|
||||
}
|
||||
|
||||
func (rr PropertygroupsRepository) CreatePropertygroup(Propertygroup *models.Propertygroup, parent *models.Propertygroup) (*models.Propertygroup, *models.ResponseError) {
|
||||
query := `INSERT INTO property_groups(label, description, parent_property_group_id) VALUES($1,$2,$2) RETURNING id`
|
||||
rows, err := rr.dbHandler.Query(query, Propertygroup.Label, Propertygroup.Description, parent.ID)
|
||||
func (rr PropertygroupsRepository) CreatePropertygroup(Propertygroup *models.Propertygroup) (*models.Propertygroup, *models.ResponseError) {
|
||||
query := `INSERT INTO property_groups(label, description) VALUES($1,$2) RETURNING id`
|
||||
rows, err := rr.dbHandler.Query(query, Propertygroup.Label, Propertygroup.Description)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
@@ -56,15 +56,11 @@ func (rr PropertygroupsRepository) CreatePropertygroup(Propertygroup *models.Pro
|
||||
}
|
||||
|
||||
pg := &models.Propertygroup{
|
||||
ID: PropertygroupId,
|
||||
Label: Propertygroup.Label,
|
||||
Description: Propertygroup.Description,
|
||||
SubPropertygroups: []*models.Propertygroup{},
|
||||
ID: PropertygroupId,
|
||||
Label: Propertygroup.Label,
|
||||
Description: Propertygroup.Description,
|
||||
}
|
||||
|
||||
if parent != nil {
|
||||
parent.SubPropertygroups = append(parent.SubPropertygroups, pg)
|
||||
}
|
||||
rr.m[pg.ID] = pg
|
||||
return pg, nil
|
||||
}
|
||||
@@ -96,39 +92,6 @@ func (rr PropertygroupsRepository) UpdatePropertygroup(Propertygroup *models.Pro
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rr PropertygroupsRepository) ChangePropertygroupParent(Propertygroup *models.Propertygroup, opPropertygroup *models.Propertygroup, npPropertygroup *models.Propertygroup) *models.ResponseError {
|
||||
query := `UPDATE property_groups SET parent=$1 WHERE id=$2`
|
||||
res, err := rr.dbHandler.Exec(query, npPropertygroup.ID, Propertygroup.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 Propertygroup found with the given id",
|
||||
Status: http.StatusNotFound,
|
||||
}
|
||||
}
|
||||
|
||||
index := findIndex(opPropertygroup.SubPropertygroups, *Propertygroup)
|
||||
if index != -1 {
|
||||
opPropertygroup.SubPropertygroups = RemoveAt(opPropertygroup.SubPropertygroups, index)
|
||||
}
|
||||
npPropertygroup.SubPropertygroups = append(npPropertygroup.SubPropertygroups, Propertygroup)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func findIndex(slice []*models.Propertygroup, target models.Propertygroup) int {
|
||||
for i, v := range slice {
|
||||
if v.ID == target.ID {
|
||||
@@ -169,7 +132,7 @@ func (rr PropertygroupsRepository) DeletePropertygroup(PropertygroupId int) *mod
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rr PropertygroupsRepository) GetPropertygroup(PropertygroupId int, langId int) (*models.Propertygroup, *models.ResponseError) {
|
||||
func (rr PropertygroupsRepository) GetPropertygroup(PropertygroupId int) (*models.Propertygroup, *models.ResponseError) {
|
||||
|
||||
if pg, exists := rr.m[PropertygroupId]; exists {
|
||||
return pg, nil
|
||||
@@ -197,8 +160,8 @@ func (rr PropertygroupsRepository) GetPropertygroup(PropertygroupId int, langId
|
||||
}
|
||||
if rows.Err() != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
Message: "property group id not found",
|
||||
Status: http.StatusNotFound,
|
||||
}
|
||||
}
|
||||
id, err := strconv.Atoi(idstr)
|
||||
@@ -209,61 +172,22 @@ func (rr PropertygroupsRepository) GetPropertygroup(PropertygroupId int, langId
|
||||
}
|
||||
}
|
||||
|
||||
// translate label and description based on langId
|
||||
translation_query := `SELECT translation FROM property_group_translations WHERE property_group_id=$1 AND language_id=$2`
|
||||
translation_rows, err := rr.dbHandler.Query(translation_query, id, langId)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
|
||||
defer translation_rows.Close()
|
||||
var translation string
|
||||
for translation_rows.Next() {
|
||||
err := translation_rows.Scan(&translation)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
}
|
||||
if translation_rows.Err() != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
|
||||
if translation == "" {
|
||||
translation = label
|
||||
}
|
||||
pg := &models.Propertygroup{
|
||||
ID: id,
|
||||
Label: label,
|
||||
LabelTranslation: translation,
|
||||
Description: description,
|
||||
}
|
||||
|
||||
err2 := rr.GetSubPropertygroups(pg, langId)
|
||||
if err2 != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: "Error reading sub propertygroups for propertygroup with id " + strconv.Itoa(pg.ID) + " : " + err2.Message,
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
ID: id,
|
||||
Label: label,
|
||||
Description: description,
|
||||
}
|
||||
|
||||
rr.m[pg.ID] = pg
|
||||
return pg, nil
|
||||
}
|
||||
|
||||
func (rr PropertygroupsRepository) GetSubPropertygroups(Propertygroup *models.Propertygroup, langId int) *models.ResponseError {
|
||||
query := `SELECT id FROM property_groups WHERE parent_property_group_id=$1`
|
||||
rows, err := rr.dbHandler.Query(query, Propertygroup.ID)
|
||||
func (rr PropertygroupsRepository) GetAllPropertygroups() ([]*models.Propertygroup, *models.ResponseError) {
|
||||
var Propertygroups []*models.Propertygroup
|
||||
query := `SELECT id FROM property_groups`
|
||||
rows, err := rr.dbHandler.Query(query)
|
||||
if err != nil {
|
||||
return &models.ResponseError{
|
||||
return nil, &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
@@ -273,38 +197,38 @@ func (rr PropertygroupsRepository) GetSubPropertygroups(Propertygroup *models.Pr
|
||||
var idstr string
|
||||
for rows.Next() {
|
||||
err := rows.Scan(&idstr)
|
||||
log.Println("Sub Propertygroup id", idstr)
|
||||
log.Println("Propertygroup id", idstr)
|
||||
if err != nil {
|
||||
return &models.ResponseError{
|
||||
return nil, &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
id, err := strconv.Atoi(idstr)
|
||||
if err != nil {
|
||||
return &models.ResponseError{
|
||||
Message: "Sub Propertygroup id not int " + idstr,
|
||||
return nil, &models.ResponseError{
|
||||
Message: "Propertygroup id not int " + idstr,
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
pg, err2 := rr.GetPropertygroup(id, langId)
|
||||
pg, err2 := rr.GetPropertygroup(id)
|
||||
if err2 != nil {
|
||||
return &models.ResponseError{
|
||||
Message: "SubPropertygroup not found with the given id " + idstr,
|
||||
return nil, &models.ResponseError{
|
||||
Message: "Propertygroup not found with the given id " + idstr,
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
if pg != nil {
|
||||
rr.m[pg.ID] = pg
|
||||
Propertygroup.SubPropertygroups = append(Propertygroup.SubPropertygroups, pg)
|
||||
Propertygroups = append(Propertygroups, pg)
|
||||
}
|
||||
|
||||
}
|
||||
if rows.Err() != nil {
|
||||
return &models.ResponseError{
|
||||
return nil, &models.ResponseError{
|
||||
Message: rows.Err().Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return Propertygroups, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"net/http"
|
||||
"scpq-editor-server/models"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type ValuecomplexitiesRepository struct {
|
||||
dbHandler *sql.DB
|
||||
}
|
||||
|
||||
func NewValuecomplexitiesRepository(dbHandler *sql.DB) *ValuecomplexitiesRepository {
|
||||
return &ValuecomplexitiesRepository{
|
||||
dbHandler: dbHandler,
|
||||
}
|
||||
}
|
||||
|
||||
func (rr ValuecomplexitiesRepository) GetAllValuecomplexities() ([]*models.Valuecomplexity, *models.ResponseError) {
|
||||
query := "SELECT id, complexity, value_definition FROM value_complexities"
|
||||
rows, err := rr.dbHandler.Query(query)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
valuecomplexities := make([]*models.Valuecomplexity, 0)
|
||||
var idstr, complexity, valuedefinition string
|
||||
for rows.Next() {
|
||||
err := rows.Scan(&idstr, &complexity, &valuedefinition)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
id, err := strconv.Atoi(idstr)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: "value complexity id not int",
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
valuecomplexity := &models.Valuecomplexity{
|
||||
ID: id,
|
||||
Complexity: complexity,
|
||||
Valuedefinition: valuedefinition,
|
||||
}
|
||||
valuecomplexities = append(valuecomplexities, valuecomplexity)
|
||||
}
|
||||
if rows.Err() != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: "error iterating over value complexities rows: " + rows.Err().Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
return valuecomplexities, nil
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"net/http"
|
||||
"scpq-editor-server/models"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type ValuetypesRepository struct {
|
||||
dbHandler *sql.DB
|
||||
}
|
||||
|
||||
func NewValuetypesRepository(dbHandler *sql.DB) *ValuetypesRepository {
|
||||
return &ValuetypesRepository{
|
||||
dbHandler: dbHandler,
|
||||
}
|
||||
}
|
||||
|
||||
func (rr ValuetypesRepository) GetValuetype(valuetypeId int) (*models.Valuetype, *models.ResponseError) {
|
||||
query := `SELECT id, name FROM value_types WHERE id=$1`
|
||||
rows, err := rr.dbHandler.Query(query, valuetypeId)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
var idstr, label string
|
||||
for rows.Next() {
|
||||
err := rows.Scan(&idstr, &label)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
}
|
||||
if rows.Err() != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: "value type id not found",
|
||||
Status: http.StatusNotFound,
|
||||
}
|
||||
}
|
||||
|
||||
return &models.Valuetype{
|
||||
ID: valuetypeId,
|
||||
Label: label,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (rr ValuetypesRepository) GetAllValuetypes() ([]*models.Valuetype, *models.ResponseError) {
|
||||
query := "SELECT id, label FROM value_types"
|
||||
rows, err := rr.dbHandler.Query(query)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
valuetypes := make([]*models.Valuetype, 0)
|
||||
var idstr, label string
|
||||
for rows.Next() {
|
||||
err := rows.Scan(&idstr, &label)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
id, err := strconv.Atoi(idstr)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: "value type id not int",
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
valuetype := &models.Valuetype{
|
||||
ID: id,
|
||||
Label: label,
|
||||
}
|
||||
valuetypes = append(valuetypes, valuetype)
|
||||
}
|
||||
if rows.Err() != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: "error iterating over value types",
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
return valuetypes, nil
|
||||
}
|
||||
@@ -19,6 +19,7 @@ type HttpServer struct {
|
||||
dbHandler *sql.DB
|
||||
languageController *controller.LanguagesController
|
||||
propertygroupsController *controller.PropertygroupsController
|
||||
valuetypesController *controller.ValuetypesController
|
||||
}
|
||||
|
||||
func InitHttpServer(config *viper.Viper, dbHandler *sql.DB) HttpServer {
|
||||
@@ -26,14 +27,20 @@ func InitHttpServer(config *viper.Viper, dbHandler *sql.DB) HttpServer {
|
||||
// repository initialisation:
|
||||
languagesRepository := repositories.NewLanguagesRepository(dbHandler)
|
||||
propertygroupsRepository := repositories.NewPropertiesgroupsRepository(dbHandler)
|
||||
valuetypesRepository := repositories.NewValuetypesRepository(dbHandler)
|
||||
valuecomplexitiesRepository := repositories.NewValuecomplexitiesRepository(dbHandler)
|
||||
|
||||
// service initialisation:
|
||||
languagesService := services.NewLanguagesService(languagesRepository)
|
||||
propertygroupsService := services.NewPropertygroupsService(propertygroupsRepository)
|
||||
valuetypesService := services.NewValuetypesService(valuetypesRepository)
|
||||
valuecomplexitiesService := services.NewValuecomplexitiesService(valuecomplexitiesRepository)
|
||||
|
||||
// controller initialisation:
|
||||
languageController := controller.NewLanguagesController(languagesService)
|
||||
propertygroupsController := controller.NewPropertygroupsController(propertygroupsService)
|
||||
valuetypesController := controller.NewValuetypesController(valuetypesService)
|
||||
valuecomplexitiesController := controller.NewValuecomplexitiesController(valuecomplexitiesService)
|
||||
|
||||
// Service API
|
||||
router := gin.Default()
|
||||
@@ -48,11 +55,18 @@ func InitHttpServer(config *viper.Viper, dbHandler *sql.DB) HttpServer {
|
||||
router.GET("/languages", languageController.GetAllLanguages)
|
||||
|
||||
// propertygroup centric routing
|
||||
router.POST("/propertygroup/:parentid", propertygroupsController.CreatePropertygroup)
|
||||
router.POST("/propertygroup", propertygroupsController.CreatePropertygroup)
|
||||
router.PUT("/propertygroup", propertygroupsController.UpdatePropertygroup)
|
||||
router.PUT("/propertygroup/:id/change-parent/:newparentid", propertygroupsController.ChangePropertygroupParent)
|
||||
router.DELETE("/propertygroup/:id", propertygroupsController.DeletePropertygroup)
|
||||
router.GET("/propertygroup/:id/lang/:langid", propertygroupsController.GetPropertygroup)
|
||||
router.GET("/propertygroup/:id", propertygroupsController.GetPropertygroup)
|
||||
router.GET("/propertygroups", propertygroupsController.GetAllPropertygroups)
|
||||
|
||||
// valuetype centric routing
|
||||
router.GET("/valuetype/:id", valuetypesController.GetValuetype)
|
||||
router.GET("/valuetypes", valuetypesController.GetAllValuetypes)
|
||||
|
||||
// valuecomplexities centric routing
|
||||
router.GET("/valuecomplexities", valuecomplexitiesController.GetAllValuecomplexities)
|
||||
|
||||
// server
|
||||
return HttpServer{
|
||||
|
||||
@@ -16,12 +16,12 @@ func NewPropertygroupsService(PropertygroupsRepository *repositories.Propertygro
|
||||
}
|
||||
}
|
||||
|
||||
func (ls PropertygroupsService) CreatePropertygroup(Propertygroup *models.Propertygroup, parent *models.Propertygroup) (*models.Propertygroup, *models.ResponseError) {
|
||||
func (ls PropertygroupsService) CreatePropertygroup(Propertygroup *models.Propertygroup) (*models.Propertygroup, *models.ResponseError) {
|
||||
responseErr := validatePropertygroup(Propertygroup)
|
||||
if responseErr != nil {
|
||||
return nil, responseErr
|
||||
}
|
||||
return ls.PropertygroupsRepository.CreatePropertygroup(Propertygroup, parent)
|
||||
return ls.PropertygroupsRepository.CreatePropertygroup(Propertygroup)
|
||||
}
|
||||
|
||||
func (ls PropertygroupsService) UpdatePropertygroup(Propertygroup *models.Propertygroup) *models.ResponseError {
|
||||
@@ -32,20 +32,16 @@ func (ls PropertygroupsService) UpdatePropertygroup(Propertygroup *models.Proper
|
||||
return ls.PropertygroupsRepository.UpdatePropertygroup(Propertygroup)
|
||||
}
|
||||
|
||||
func (ls PropertygroupsService) ChangePropertygroupParent(Propertygroup *models.Propertygroup, opPropertygroup *models.Propertygroup, npPropertygroup *models.Propertygroup) *models.ResponseError {
|
||||
responseErr := validatePropertygroup(Propertygroup)
|
||||
if responseErr != nil {
|
||||
return responseErr
|
||||
}
|
||||
return ls.PropertygroupsRepository.ChangePropertygroupParent(Propertygroup, opPropertygroup, npPropertygroup)
|
||||
}
|
||||
|
||||
func (ls PropertygroupsService) DeletePropertygroup(PropertygroupId int) *models.ResponseError {
|
||||
return ls.PropertygroupsRepository.DeletePropertygroup(PropertygroupId)
|
||||
}
|
||||
|
||||
func (ls PropertygroupsService) GetPropertygroup(PropertygroupId int, langId int) (*models.Propertygroup, *models.ResponseError) {
|
||||
return ls.PropertygroupsRepository.GetPropertygroup(PropertygroupId, langId)
|
||||
func (ls PropertygroupsService) GetPropertygroup(PropertygroupId int) (*models.Propertygroup, *models.ResponseError) {
|
||||
return ls.PropertygroupsRepository.GetPropertygroup(PropertygroupId)
|
||||
}
|
||||
|
||||
func (ls PropertygroupsService) GetAllPropertygroups() ([]*models.Propertygroup, *models.ResponseError) {
|
||||
return ls.PropertygroupsRepository.GetAllPropertygroups()
|
||||
}
|
||||
|
||||
func validatePropertygroup(Propertygroup *models.Propertygroup) *models.ResponseError {
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"scpq-editor-server/models"
|
||||
"scpq-editor-server/repositories"
|
||||
)
|
||||
|
||||
type ValuecomplexitiesService struct {
|
||||
valuecomplexitiesRepository *repositories.ValuecomplexitiesRepository
|
||||
}
|
||||
|
||||
func NewValuecomplexitiesService(valuecomplexitiesRepository *repositories.ValuecomplexitiesRepository) *ValuecomplexitiesService {
|
||||
return &ValuecomplexitiesService{
|
||||
valuecomplexitiesRepository: valuecomplexitiesRepository,
|
||||
}
|
||||
}
|
||||
|
||||
func (vs ValuecomplexitiesService) GetAllValuecomplexities() ([]*models.Valuecomplexity, *models.ResponseError) {
|
||||
return vs.valuecomplexitiesRepository.GetAllValuecomplexities()
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"scpq-editor-server/models"
|
||||
"scpq-editor-server/repositories"
|
||||
)
|
||||
|
||||
type ValuetypesService struct {
|
||||
valuetypesRepository *repositories.ValuetypesRepository
|
||||
}
|
||||
|
||||
func NewValuetypesService(valuetypesRepository *repositories.ValuetypesRepository) *ValuetypesService {
|
||||
return &ValuetypesService{
|
||||
valuetypesRepository: valuetypesRepository,
|
||||
}
|
||||
}
|
||||
|
||||
func (vs ValuetypesService) GetValuetype(valuetypeId int) (*models.Valuetype, *models.ResponseError) {
|
||||
return vs.valuetypesRepository.GetValuetype(valuetypeId)
|
||||
}
|
||||
|
||||
func (vs ValuetypesService) GetAllValuetypes() ([]*models.Valuetype, *models.ResponseError) {
|
||||
return vs.valuetypesRepository.GetAllValuetypes()
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user