I need some theoretical/practical help in code organization.
I have such table in PostgreSQL
database. The table shows the relationship between organizations.
| 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 |
In my Go application I create struct
for this table then make SQL query.
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)
}
I need to make such response. What would you advise to reorganize in my current code?
[
{
"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 for example I add new record called Telsa
. As you can see it's parent is Nest
object.
| 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 |
Result of your code:
[
{
"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
}
]
}
]
If I understand properly what you want to do, here you have an unoptimized example, it's done with sqlite but it will work the same with postgres :
package main
import (
"database/sql"
"encoding/json"
"fmt"
"log"
"sort"
_ "github.com/mattn/go-sqlite3"
)
type Organization struct {
ID int `json:"organization_id"`
Name string `json:"organization_name"`
Rang int `json:"organization_rang"`
Children []*Organization `json:"children"`
}
func main() {
db, err := sql.Open("sqlite3", "./database")
if err != nil {
log.Fatal(err)
}
defer db.Close()
rows, err := db.Query("select ORGANIZATION_ID,ORGANIZATION_NAME,ORGANIZATION_RANG,PARENT_ORGANIZATION_ID from ORG")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
orgs := map[int]*Organization{}
for rows.Next() {
organization := &Organization{}
var parentID sql.NullInt64
if err = rows.Scan(&organization.ID, &organization.Name, &organization.Rang, &parentID); err != nil {
log.Fatal(err)
}
if parentID.Valid {
if parentOrg, ok := orgs[int(parentID.Int64)]; ok {
parentOrg.Children = append(parentOrg.Children, organization)
} else {
orgs[int(parentID.Int64)] = &Organization{ID: int(parentID.Int64)}
orgs[int(parentID.Int64)].Children = append(orgs[int(parentID.Int64)].Children, organization)
}
}
if _, ok := orgs[organization.ID]; ok {
orgs[organization.ID].Name = organization.Name
orgs[organization.ID].Rang = organization.Rang
continue
}
orgs[organization.ID] = organization
}
IDs := []int{}
for k := range orgs {
IDs = append(IDs, k)
}
sort.Ints(IDs)
organizations := []Organization{}
for _, ID := range IDs {
if len(orgs[ID].Children) > 0 && orgs[ID].Rang == 1 {
organizations = append(organizations, *orgs[ID])
}
}
content, err := json.MarshalIndent(organizations, "", " ")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(content))
}
I get this :
[
{
"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": 5,
"organization_name": "ATAP",
"organization_rang": 2,
"children": null
}
]
}
]