'How to Go/Gorm a megamenu?
I'm writing an e-commerce app in Go, and I've been stuck on the megamenu for a while now, trying to implement it with PostgreSQL tables. The end result, JSON response, would look something like this:
[
{
"ID": 5000,
"Name": "Department 1",
"Slug": "",
"CategoryGroups": [
{
"ID": 1000,
"Name": "Category Group 1",
"Slug": "",
"Categories": [
{
"ID": 1,
"Name": "Category A",
"Slug": "category-a"
},
{
"ID": 2,
"Name": "Category B",
"Slug": "category-b"
},
},
{
"ID": 1001,
"Name": "Category Group 2",
"Slug": "category-group-2",
},
},
{
"ID": 5001,
"Name": "Department 2",
"Slug": "department-2",
}
]
I tried the following(SQLFiddle):
Tables:
type Course struct {
gorm.Model
CourseName string `json:"course_name"`
CourseSlug string `json:"course_slug"`
}
type Subject struct {
gorm.Model
SubjectName string `json:"subject_name"`
SubjectSlug string `json:"subject_slug"`
CourseID int `json:"course_id"`
}
type Chapter struct {
gorm.Model
ChapterName string `json:"chapter_name"`
ChapterSlug string `json:"chaper_slug"`
SubjectID int `json:"subject_id"`
}
type Subchapter struct {
gorm.Model
SubchapterName string `json:"subchapter_name"`
SubchapterSlug string `json:"subchapter_slug"`
ChapterID int `json:"chapter_id"`
}
type CourseSubjChapterSub struct {
CourseName string `json:"course_name"`
CourseSlug string `json:"course_slug"`
SubjectName string `json:"subject_name"`
SubjectSlug string `json:"subject_slug"`
ChapterName string `json:"chapter_name"`
ChapterSlug string `json:"chaper_slug"`
SubchapterName string `json:"subchapter_name"`
SubchapterSlug string `json:"subchapter_slug"`
}
Query:
var res []models.CourseSubjChapterSub
db.DBClient.Model(&models.Course{}).
Select("courses.course_name, courses.course_slug, subjects.subject_name, subjects.subject_slug, chapters.chapter_name, chapters.chapter_slug, subchapters.subchapter_name, subchapters.subchapter_slug").
Joins("LEFT JOIN subjects ON courses.id = subjects.course_id").
Joins("LEFT JOIN chapters ON subjects.id = chapters.subject_id").
Joins("LEFT JOIN subchapters ON chapters.id = subchapters.chapter_id").
Scan(&res)
log.Println(res)
Result(log):
[
{Web Designing web-design HTML html HTML Form html-form }
{Web Designing web-design HTML html HTML Image html-image }
{Web Designing web-design HTML html HTML Link html-link HTML Image Link html-image-link}
{Web Designing web-design HTML html HTML Link html-link HTML Text Link html-text-link}
{Web Designing web-design HTML html HTML List html-list HTML Ordered List html-ordered-list}
{Web Designing web-design HTML html HTML List html-list HTML Unordered List html-unordered-list}
{Web Designing web-design HTML html HTML Text html-text HTML Heading html-heading}
{Web Designing web-design HTML html HTML Text html-text HTML Paragraph html-paragraph}
{Web Designing web-design CSS css CSS Border css-border CSS Border Color css-border-color}
{Web Designing web-design CSS css CSS Border css-border CSS Border Style css-border-style}
{Web Designing web-design CSS css CSS Border css-border CSS Border Width css-border-width}
{Web Designing web-design CSS css CSS Position css-position CSS Absolute Position css-absolute-position}
{Web Designing web-design CSS css CSS Selector css-selector CSS Element Selector css-element-selector}
{Web Designing web-design JavaScript javascript }
{Web Development web-development PHP php PHP Arrays php-arrays PHP Associative Array php-associative-array}
{Web Development web-development PHP php PHP Arrays php-arrays PHP Index Array php-index-array}
{Web Development web-development PHP php PHP Conditions php-conditions PHP If Condition php-if-condition}
{Web Development web-development PHP php PHP Conditions php-conditions PHP Switch Condition php-switch-condition}
{Web Development web-development Python python }
{Web Development web-development .NET dotnet }
{Programming programming Java java Java Methods java-methods Java Method Overloading java-method-overloading}
{Programming programming Java java Java Methods java-methods Java Method Parameter java-method-parameter} {Programming programming C++ cpp }
]
I also tried using one table for categories and manually assigning department and category-group, and then doing some logic on the client-side via JavaScript to create the megamenu.. that also works, but not quite what I'm looking for...
Any help would be much appreciated, Cheers
Solution 1:[1]
The answer is simple, I just missed it in the Gorm docs.
- Modify structs like so:
type Course struct {
...
Subjects []Subject
}
- Update
res
variable for multiple Courses and query
var res []models.Course
db.Preload("Subjects.Chapters.Subchapters").
Preload(clause.Associations).
Find(&res)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Chandan |