N
N
nurzhannogerbek2019-03-04 15:49:09
PostgreSQL
nurzhannogerbek, 2019-03-04 15:49:09

How to create a tree structure in Golang?

Hello comrades! Help please solve the problem.
There are 3 tables in a PostgreSQL database : SURVEY , QUESTION , and OPTION . SURVEY
table :

| survey_id (uuid4)                    | survey_name (varchar) |
|--------------------------------------|-----------------------|
| 0cf1cf18-d5fd-474e-a8be-754fbdc89720 | April                 |
| b9fg55d9-n5fy-s7fe-s5bh-856fbdc89720 | May                   |

QUESTION table :
| question_id (int) | question_text (text)         |
|-------------------|------------------------------|
| 1                 | What is your favorite color? |

OPTION table :
| option_id (int)   | option_text (text) |
|-------------------|--------------------|
| 1                 | red                |
| 2                 | blue               |
| 3                 | grey               |
| 4                 | green              |
| 5                 | brown              |

In a Golang application, I use the GORM package . For each of the above tables created structures. Created another table in the database SURVEYS_QUESTIONS_OPTIONSthat joins the previous three tables. In fact, this table has a many-to-many relationship.
| survey_id                            | question_id | option_id |
|--------------------------------------|-------------|-----------|
| 0cf1cf18-d5fd-474e-a8be-754fbdc89720 | 1           | 1         |
| 0cf1cf18-d5fd-474e-a8be-754fbdc89720 | 1           | 2         |
| 0cf1cf18-d5fd-474e-a8be-754fbdc89720 | 1           | 3         |
| b9fg55d9-n5fy-s7fe-s5bh-856fbdc89720 | 1           | 3         |
| b9fg55d9-n5fy-s7fe-s5bh-856fbdc89720 | 1           | 4         |
| b9fg55d9-n5fy-s7fe-s5bh-856fbdc89720 | 1           | 5         |

I have a question. How to create a tree-like JSON from the latest table? For example, with a GET request, return a list of all surveys. Each survey contains a list of questions. Each question has a list of possible answers. In a word, something similar to this structure must be returned:
[
    {
        "survey_id": "0cf1cf18-d5fd-474e-a8be-754fbdc89720",
        "survey_name": "April",
        "questions": [
            {
                "question_id": 1,
                "question_text": "What is your favorite color?",
                "options": [
                    {
                        "option_id": 1,
                        "option_text": "red"
                    },
                    {
                        "option_id": 2,
                        "option_text": "blue"
                    },
                    {
                        "option_id": 3,
                        "option_text": "grey"
                    },
                ]
            }
        ]
    },
    {
        "survey_id": "b9fg55d9-n5fy-s7fe-s5bh-856fbdc89720",
        "survey_name": "May",
        "questions": [
            {
                "question_id": 1,
                "question_text": "What is your favorite color?",
                "options": [
                    {
                        "option_id": 3,
                        "option_text": "grey"
                    },
                    {
                        "option_id": 4,
                        "option_text": "green"
                    },
                    {
                        "option_id": 5,
                        "option_text": "brown"
                    },
                ]
            }
        ]
    }
]

Is it possible to do this with GORM?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Shvedov, 2019-03-04
@mmmaaak

here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question