Мне нужна теоретическая / практическая помощь в организации кода.
У меня есть такая таблица в базе данных PostgreSQL
. В таблице приведены отношения между организациями.
| ORGANIZATION_ID | ORGANIZATION_NAME | PARENT_ORGANIZATION_ID | ORGANIZATION_RANG | TREE_ORGANIZATION_ID | TREE_ORGANIZATION_ NAME |
|-----------------|-------------------|------------------------|-------------------|----------------------|-------------------------|
| 1 | Google | | 1 | \1 | \Google |
| 2 | Nest | 1 | 2 | \1\2 | \Google\Nest |
| 3 | Verily | 1 | 2 | \1\3 | \Google\Verily |
| 4 | Calico | | 1 | \4 | \Calico |
| 5 | ATAP | 4 | 2 | \4\5 | \Calico\ATAP |
В моем приложении Go я создаю struct
для этой таблицы, а затем выполняю SQL-запрос.
type Organization struct {
ID int `json:"organization_id"`
Name string `json:"organization_name"`
Rang int `json:"organization_rang"`
Children []Organization `json:"children"`
}
var GetOrganizations = func(responseWriter http.ResponseWriter, request *http.Request) {
rows,err := db.Query("select * from ORG")
if err != nil {
fmt.Println(err)
return
}
defer rows.Close()
var organizations []Organization
for rows.Next() {
var organization Organization
err = rows.Scan(&organization.ID, &organization.Name, &organization.Rang)
if err != nil {
fmt.Println(err)
return
}
organizations = append(organizations, organization)
}
utils.Response(responseWriter, http.StatusOK, organizations)
}
Мне нужно сделать такой ответ. Что бы вы посоветовали реорганизовать в моем текущем коде?
[
{
"organization_id": 1,
"organization_name": "Google",
"organization_rang": 1,
"children": [
{
"organization_id": 2,
"organization_name": "Nest",
"organization_rang": 2,
"children": null
},
{
"organization_id": 3,
"organization_name": "Verily",
"organization_rang": 2,
"children": null
}
]
},
{
"organization_id": 4,
"organization_name": "Calico",
"organization_rang": 1,
"children": [
{
"organization_id": 2,
"organization_name": "Nest",
"organization_rang": 2,
"children": null
}
]
}
]
EDIT
@ antham например я добавляю новую запись с именем Telsa
. Как вы можете видеть, его родитель - Nest
объект.
| ORGANIZATION_ID | ORGANIZATION_NAME | PARENT_ORGANIZATION_ID | ORGANIZATION_RANG | TREE_ORGANIZATION_ID | TREE_ORGANIZATION_ NAME |
|-----------------|-------------------|------------------------|-------------------|----------------------|-------------------------|
| 1 | Google | | 1 | \1 | \Google |
| 2 | Nest | 1 | 2 | \1\2 | \Google\Nest |
| 3 | Verily | 1 | 2 | \1\3 | \Google\Verily |
| 4 | Calico | | 1 | \4 | \Calico |
| 5 | ATAP | 4 | 2 | \4\5 | \Calico\ATAP |
| 6 | Tesla | 2 | 3 | \1\2\6 | \Google\Nest\Tesla |
Результат вашего кода:
[
{
"organization_id": 1,
"organization_name": "Google",
"organization_rang": 1,
"children": [
{
"organization_id": 3,
"organization_name": "Verily",
"organization_rang": 2,
"children": null
},
{
"organization_id": 2,
"organization_name": "Nest",
"organization_rang": 2,
"children": [
{
"organization_id": 6,
"organization_name": "Tesla",
"organization_rang": 3,
"children": null
}
]
}
]
},
{
"organization_id": 2,
"organization_name": "Nest",
"organization_rang": 2,
"children": [
{
"organization_id": 6,
"organization_name": "Tesla",
"organization_rang": 3,
"children": null
}
]
},
{
"organization_id": 4,
"organization_name": "Calico",
"organization_rang": 1,
"children": [
{
"organization_id": 5,
"organization_name": "ATAP",
"organization_rang": 2,
"children": null
}
]
}
]