Property Group Label Translation und erste TreeView fertig

This commit is contained in:
2026-07-30 23:18:02 +02:00
parent c50780b6cf
commit 0e72fb23b7
8 changed files with 71 additions and 29 deletions
+3 -2
View File
@@ -48,12 +48,11 @@
.sidebar { .sidebar {
display: flex; display: flex;
flex-flow: column; flex-flow: column;
height: 70vh;
padding: 16px; padding: 16px;
color: gray; color: gray;
background: var(--code-bg); background: var(--code-bg);
box-shadow: 0px 4px 4px rgba(0, 0, 0, 0.25); box-shadow: 0px 4px 4px rgba(0, 0, 0, 0.25);
width: 200px; width: 300px;
} }
.sidebar h1 { .sidebar h1 {
@@ -94,6 +93,8 @@
.side-links { .side-links {
font-size: 12px; font-size: 12px;
background: whitesmoke; background: whitesmoke;
overflow: auto;
height: 70vh;
} }
footer { footer {
@@ -6,10 +6,9 @@ import { DocumentIcon, FolderIcon } from '@heroicons/react/16/solid';
const PropertyTreeView = ({ node, key }) => { const PropertyTreeView = ({ node, key }) => {
const [expanded, setExpanded] = useState(true); const [expanded, setExpanded] = useState(true);
console.log('node:', key, node.id, node.label, node.subPropertygroups);
return ( return (
<li key={node.id}> <li key={node.id}>
<span className="flex items-center gap-1.5 py-1"> <span className="flex items-center gap-1 py-1">
{node.subPropertygroups && node.subPropertygroups.length > 0 && ( {node.subPropertygroups && node.subPropertygroups.length > 0 && (
<button onClick={() => setExpanded(!expanded)} className="p-1 -m-1"> <button onClick={() => setExpanded(!expanded)} className="p-1 -m-1">
<ChevronRightIcon <ChevronRightIcon
@@ -17,11 +16,11 @@ const PropertyTreeView = ({ node, key }) => {
/> />
</button> </button>
)} )}
<FolderIcon className={`size-3 text-gray-500`} />{node.label} <FolderIcon className={`size-3 text-gray-500`} />{node.labelTranslation}
</span> </span>
{expanded && ( {expanded && (
<ul> <ul className="ml-1 border-l border-gray-300 pl-8">
{node.subPropertygroups?.map((node) => ( {node.subPropertygroups?.map((node) => (
<PropertyTreeView node={node} key={node.id} /> <PropertyTreeView node={node} key={node.id} />
))} ))}
@@ -4,7 +4,7 @@ import PropertySideBar from '../components/PropertySideBar';
import UseFetch from '../components/tools/UseFetch'; import UseFetch from '../components/tools/UseFetch';
const PropertyPage = () => { const PropertyPage = () => {
const { data: properties, isLoading, error } = UseFetch(`http://localhost:8088/propertygroup/1`) const { data: properties, isLoading, error } = UseFetch(`http://localhost:8088/propertygroup/1/lang/1`)
if (isLoading) return <p>Loading profile...</p> if (isLoading) return <p>Loading profile...</p>
if (error) return <p>Error: {error}</p> if (error) return <p>Error: {error}</p>
@@ -12,7 +12,7 @@ const PropertyPage = () => {
return ( return (
<div className="content"> <div className="content">
<div><PropertySideBar properties={[properties]} /></div> <div><PropertySideBar properties={[properties]} /></div>
<div className="main">{JSON.stringify(properties)}</div> <div className="main"></div>
</div> </div>
); );
}; };
@@ -31,7 +31,7 @@ func (lc PropertygroupsController) CreatePropertygroup(ctx *gin.Context) {
ctx.AbortWithError(http.StatusInternalServerError, err) ctx.AbortWithError(http.StatusInternalServerError, err)
return return
} }
parent, responseErr := lc.PropertygroupsService.GetPropertygroup(lId) parent, responseErr := lc.PropertygroupsService.GetPropertygroup(lId, 1)
if responseErr != nil { if responseErr != nil {
ctx.JSON(responseErr.Status, responseErr) ctx.JSON(responseErr.Status, responseErr)
return return
@@ -90,7 +90,7 @@ func (lc PropertygroupsController) ChangePropertygroupParent(ctx *gin.Context) {
ctx.AbortWithError(http.StatusInternalServerError, err) ctx.AbortWithError(http.StatusInternalServerError, err)
return return
} }
oldparent, responseErr := lc.PropertygroupsService.GetPropertygroup(lId) oldparent, responseErr := lc.PropertygroupsService.GetPropertygroup(lId, 1)
if responseErr != nil { if responseErr != nil {
ctx.JSON(responseErr.Status, responseErr) ctx.JSON(responseErr.Status, responseErr)
return return
@@ -102,7 +102,7 @@ func (lc PropertygroupsController) ChangePropertygroupParent(ctx *gin.Context) {
ctx.AbortWithError(http.StatusInternalServerError, err) ctx.AbortWithError(http.StatusInternalServerError, err)
return return
} }
newparent, responseErr := lc.PropertygroupsService.GetPropertygroup(lId) newparent, responseErr := lc.PropertygroupsService.GetPropertygroup(lId, 1)
if responseErr != nil { if responseErr != nil {
ctx.JSON(responseErr.Status, responseErr) ctx.JSON(responseErr.Status, responseErr)
return return
@@ -148,13 +148,21 @@ func (lc PropertygroupsController) DeletePropertygroup(ctx *gin.Context) {
func (lc PropertygroupsController) GetPropertygroup(ctx *gin.Context) { func (lc PropertygroupsController) GetPropertygroup(ctx *gin.Context) {
PropertygroupId := ctx.Param("id") PropertygroupId := ctx.Param("id")
LangId := ctx.Param("langid")
lId, err := strconv.Atoi(PropertygroupId) lId, err := strconv.Atoi(PropertygroupId)
if err != nil { if err != nil {
log.Println("Error PropertygroupId not int", err) log.Println("Error PropertygroupId not int", err)
ctx.AbortWithError(http.StatusInternalServerError, err) ctx.AbortWithError(http.StatusInternalServerError, err)
return return
} }
response, responseErr := lc.PropertygroupsService.GetPropertygroup(lId) 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)
if responseErr != nil { if responseErr != nil {
ctx.JSON(responseErr.Status, responseErr) ctx.JSON(responseErr.Status, responseErr)
return return
@@ -3,6 +3,8 @@ package models
type Propertygroup struct { type Propertygroup struct {
ID int `json:"id"` ID int `json:"id"`
Label string `json:"label"` Label string `json:"label"`
LabelTranslation string `json:"labelTranslation"`
Description string `json:"description"` Description string `json:"description"`
DescriptionTranslation string `json:"descriptionTranslationKey"`
SubPropertygroups []*Propertygroup `json:"subPropertygroups"` SubPropertygroups []*Propertygroup `json:"subPropertygroups"`
} }
@@ -169,7 +169,7 @@ func (rr PropertygroupsRepository) DeletePropertygroup(PropertygroupId int) *mod
return nil return nil
} }
func (rr PropertygroupsRepository) GetPropertygroup(PropertygroupId int) (*models.Propertygroup, *models.ResponseError) { func (rr PropertygroupsRepository) GetPropertygroup(PropertygroupId int, langId int) (*models.Propertygroup, *models.ResponseError) {
if pg, exists := rr.m[PropertygroupId]; exists { if pg, exists := rr.m[PropertygroupId]; exists {
return pg, nil return pg, nil
@@ -209,13 +209,45 @@ func (rr PropertygroupsRepository) GetPropertygroup(PropertygroupId int) (*model
} }
} }
// 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{ pg := &models.Propertygroup{
ID: id, ID: id,
Label: label, Label: label,
LabelTranslation: translation,
Description: description, Description: description,
} }
err2 := rr.GetSubPropertygroups(pg) err2 := rr.GetSubPropertygroups(pg, langId)
if err2 != nil { if err2 != nil {
return nil, &models.ResponseError{ return nil, &models.ResponseError{
Message: "Error reading sub propertygroups for propertygroup with id " + strconv.Itoa(pg.ID) + " : " + err2.Message, Message: "Error reading sub propertygroups for propertygroup with id " + strconv.Itoa(pg.ID) + " : " + err2.Message,
@@ -227,7 +259,7 @@ func (rr PropertygroupsRepository) GetPropertygroup(PropertygroupId int) (*model
return pg, nil return pg, nil
} }
func (rr PropertygroupsRepository) GetSubPropertygroups(Propertygroup *models.Propertygroup) *models.ResponseError { func (rr PropertygroupsRepository) GetSubPropertygroups(Propertygroup *models.Propertygroup, langId int) *models.ResponseError {
query := `SELECT id FROM property_groups WHERE parent_property_group_id=$1` query := `SELECT id FROM property_groups WHERE parent_property_group_id=$1`
rows, err := rr.dbHandler.Query(query, Propertygroup.ID) rows, err := rr.dbHandler.Query(query, Propertygroup.ID)
if err != nil { if err != nil {
@@ -255,7 +287,7 @@ func (rr PropertygroupsRepository) GetSubPropertygroups(Propertygroup *models.Pr
Status: http.StatusInternalServerError, Status: http.StatusInternalServerError,
} }
} }
pg, err2 := rr.GetPropertygroup(id) pg, err2 := rr.GetPropertygroup(id, langId)
if err2 != nil { if err2 != nil {
return &models.ResponseError{ return &models.ResponseError{
Message: "SubPropertygroup not found with the given id " + idstr, Message: "SubPropertygroup not found with the given id " + idstr,
@@ -270,7 +302,7 @@ func (rr PropertygroupsRepository) GetSubPropertygroups(Propertygroup *models.Pr
} }
if rows.Err() != nil { if rows.Err() != nil {
return &models.ResponseError{ return &models.ResponseError{
Message: err.Error(), Message: rows.Err().Error(),
Status: http.StatusInternalServerError, Status: http.StatusInternalServerError,
} }
} }
@@ -52,7 +52,7 @@ func InitHttpServer(config *viper.Viper, dbHandler *sql.DB) HttpServer {
router.PUT("/propertygroup", propertygroupsController.UpdatePropertygroup) router.PUT("/propertygroup", propertygroupsController.UpdatePropertygroup)
router.PUT("/propertygroup/:id/change-parent/:newparentid", propertygroupsController.ChangePropertygroupParent) router.PUT("/propertygroup/:id/change-parent/:newparentid", propertygroupsController.ChangePropertygroupParent)
router.DELETE("/propertygroup/:id", propertygroupsController.DeletePropertygroup) router.DELETE("/propertygroup/:id", propertygroupsController.DeletePropertygroup)
router.GET("/propertygroup/:id", propertygroupsController.GetPropertygroup) router.GET("/propertygroup/:id/lang/:langid", propertygroupsController.GetPropertygroup)
// server // server
return HttpServer{ return HttpServer{
@@ -44,8 +44,8 @@ func (ls PropertygroupsService) DeletePropertygroup(PropertygroupId int) *models
return ls.PropertygroupsRepository.DeletePropertygroup(PropertygroupId) return ls.PropertygroupsRepository.DeletePropertygroup(PropertygroupId)
} }
func (ls PropertygroupsService) GetPropertygroup(PropertygroupId int) (*models.Propertygroup, *models.ResponseError) { func (ls PropertygroupsService) GetPropertygroup(PropertygroupId int, langId int) (*models.Propertygroup, *models.ResponseError) {
return ls.PropertygroupsRepository.GetPropertygroup(PropertygroupId) return ls.PropertygroupsRepository.GetPropertygroup(PropertygroupId, langId)
} }
func validatePropertygroup(Propertygroup *models.Propertygroup) *models.ResponseError { func validatePropertygroup(Propertygroup *models.Propertygroup) *models.ResponseError {