[feature/backend] add categories param in posts

This commit is contained in:
CDN 2025-02-22 02:42:55 +08:00
parent 958e3c2886
commit be8bf22017
Signed by: CDN
GPG key ID: 0C656827F9F80080
21 changed files with 448 additions and 281 deletions

View file

@ -27,8 +27,8 @@ const (
EdgeContents = "contents"
// EdgeContributors holds the string denoting the contributors edge name in mutations.
EdgeContributors = "contributors"
// EdgeCategory holds the string denoting the category edge name in mutations.
EdgeCategory = "category"
// EdgeCategories holds the string denoting the categories edge name in mutations.
EdgeCategories = "categories"
// Table holds the table name of the post in the database.
Table = "posts"
// ContentsTable is the table that holds the contents relation/edge.
@ -45,13 +45,11 @@ const (
ContributorsInverseTable = "post_contributors"
// ContributorsColumn is the table column denoting the contributors relation/edge.
ContributorsColumn = "post_contributors"
// CategoryTable is the table that holds the category relation/edge.
CategoryTable = "posts"
// CategoryInverseTable is the table name for the Category entity.
// CategoriesTable is the table that holds the categories relation/edge. The primary key declared below.
CategoriesTable = "category_posts"
// CategoriesInverseTable is the table name for the Category entity.
// It exists in this package in order to avoid circular dependency with the "category" package.
CategoryInverseTable = "categories"
// CategoryColumn is the table column denoting the category relation/edge.
CategoryColumn = "category_posts"
CategoriesInverseTable = "categories"
)
// Columns holds all SQL columns for post fields.
@ -63,11 +61,11 @@ var Columns = []string{
FieldUpdatedAt,
}
// ForeignKeys holds the SQL foreign-keys that are owned by the "posts"
// table and are not defined as standalone fields in the schema.
var ForeignKeys = []string{
"category_posts",
}
var (
// CategoriesPrimaryKey and CategoriesColumn2 are the table columns denoting the
// primary key for the categories relation (M2M).
CategoriesPrimaryKey = []string{"category_id", "post_id"}
)
// ValidColumn reports if the column name is valid (part of the table columns).
func ValidColumn(column string) bool {
@ -76,11 +74,6 @@ func ValidColumn(column string) bool {
return true
}
}
for i := range ForeignKeys {
if column == ForeignKeys[i] {
return true
}
}
return false
}
@ -178,10 +171,17 @@ func ByContributors(term sql.OrderTerm, terms ...sql.OrderTerm) OrderOption {
}
}
// ByCategoryField orders the results by category field.
func ByCategoryField(field string, opts ...sql.OrderTermOption) OrderOption {
// ByCategoriesCount orders the results by categories count.
func ByCategoriesCount(opts ...sql.OrderTermOption) OrderOption {
return func(s *sql.Selector) {
sqlgraph.OrderByNeighborTerms(s, newCategoryStep(), sql.OrderByField(field, opts...))
sqlgraph.OrderByNeighborsCount(s, newCategoriesStep(), opts...)
}
}
// ByCategories orders the results by categories terms.
func ByCategories(term sql.OrderTerm, terms ...sql.OrderTerm) OrderOption {
return func(s *sql.Selector) {
sqlgraph.OrderByNeighborTerms(s, newCategoriesStep(), append([]sql.OrderTerm{term}, terms...)...)
}
}
func newContentsStep() *sqlgraph.Step {
@ -198,10 +198,10 @@ func newContributorsStep() *sqlgraph.Step {
sqlgraph.Edge(sqlgraph.O2M, false, ContributorsTable, ContributorsColumn),
)
}
func newCategoryStep() *sqlgraph.Step {
func newCategoriesStep() *sqlgraph.Step {
return sqlgraph.NewStep(
sqlgraph.From(Table, FieldID),
sqlgraph.To(CategoryInverseTable, FieldID),
sqlgraph.Edge(sqlgraph.M2O, true, CategoryTable, CategoryColumn),
sqlgraph.To(CategoriesInverseTable, FieldID),
sqlgraph.Edge(sqlgraph.M2M, true, CategoriesTable, CategoriesPrimaryKey...),
)
}

View file

@ -281,21 +281,21 @@ func HasContributorsWith(preds ...predicate.PostContributor) predicate.Post {
})
}
// HasCategory applies the HasEdge predicate on the "category" edge.
func HasCategory() predicate.Post {
// HasCategories applies the HasEdge predicate on the "categories" edge.
func HasCategories() predicate.Post {
return predicate.Post(func(s *sql.Selector) {
step := sqlgraph.NewStep(
sqlgraph.From(Table, FieldID),
sqlgraph.Edge(sqlgraph.M2O, true, CategoryTable, CategoryColumn),
sqlgraph.Edge(sqlgraph.M2M, true, CategoriesTable, CategoriesPrimaryKey...),
)
sqlgraph.HasNeighbors(s, step)
})
}
// HasCategoryWith applies the HasEdge predicate on the "category" edge with a given conditions (other predicates).
func HasCategoryWith(preds ...predicate.Category) predicate.Post {
// HasCategoriesWith applies the HasEdge predicate on the "categories" edge with a given conditions (other predicates).
func HasCategoriesWith(preds ...predicate.Category) predicate.Post {
return predicate.Post(func(s *sql.Selector) {
step := newCategoryStep()
step := newCategoriesStep()
sqlgraph.HasNeighborsWith(s, step, func(s *sql.Selector) {
for _, p := range preds {
p(s)