如何创建/设计一个超级菜单?

huangapple go评论95阅读模式
英文:

How to Go/Gorm a megamenu?

问题

我正在使用Go编写一个电子商务应用程序,并且在使用PostgreSQL表实现megamenu时遇到了困难。最终的JSON响应应该类似于这样:

[
{ 
    "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",
}
]

我尝试了以下方法(SQLFiddle):

表:

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:"chapter_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:"chapter_slug"`
	SubchapterName string `json:"subchapter_name"`
	SubchapterSlug string `json:"subchapter_slug"`
}

查询:

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)

结果(日志):

[
{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    }
]

我还尝试过使用一个表格来存储类别,并手动分配部门和类别组,然后在客户端使用JavaScript进行一些逻辑操作来创建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

答案1

得分: 0

答案很简单,我只是在 Gorm 文档中错过了它。

  1. 修改结构体如下:
type Course struct {
  ...
  Subjects []Subject
}
  1. 更新 res 变量以支持多个课程和查询:
var res []models.Course
db.Preload("Subjects.Chapters.Subchapters").
  Preload(clause.Associations).
  Find(&res)
英文:

The answer is simple, I just missed it in the Gorm docs.

  1. Modify structs like so:
type Course struct {
  ...
  Subjects []Subject
}
  1. Update res variable for multiple Courses and query
var res []models.Course
db.Preload("Subjects.Chapters.Subchapters").
  Preload(clause.Associations).
  Find(&res)

huangapple
  • 本文由 发表于 2022年5月4日 02:41:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/72104034.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定