[feature/backend] add categories param in posts
This commit is contained in:
parent
958e3c2886
commit
be8bf22017
21 changed files with 448 additions and 281 deletions
|
@ -28,8 +28,7 @@ type PostQuery struct {
|
|||
predicates []predicate.Post
|
||||
withContents *PostContentQuery
|
||||
withContributors *PostContributorQuery
|
||||
withCategory *CategoryQuery
|
||||
withFKs bool
|
||||
withCategories *CategoryQuery
|
||||
// intermediate query (i.e. traversal path).
|
||||
sql *sql.Selector
|
||||
path func(context.Context) (*sql.Selector, error)
|
||||
|
@ -110,8 +109,8 @@ func (pq *PostQuery) QueryContributors() *PostContributorQuery {
|
|||
return query
|
||||
}
|
||||
|
||||
// QueryCategory chains the current query on the "category" edge.
|
||||
func (pq *PostQuery) QueryCategory() *CategoryQuery {
|
||||
// QueryCategories chains the current query on the "categories" edge.
|
||||
func (pq *PostQuery) QueryCategories() *CategoryQuery {
|
||||
query := (&CategoryClient{config: pq.config}).Query()
|
||||
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
|
||||
if err := pq.prepareQuery(ctx); err != nil {
|
||||
|
@ -124,7 +123,7 @@ func (pq *PostQuery) QueryCategory() *CategoryQuery {
|
|||
step := sqlgraph.NewStep(
|
||||
sqlgraph.From(post.Table, post.FieldID, selector),
|
||||
sqlgraph.To(category.Table, category.FieldID),
|
||||
sqlgraph.Edge(sqlgraph.M2O, true, post.CategoryTable, post.CategoryColumn),
|
||||
sqlgraph.Edge(sqlgraph.M2M, true, post.CategoriesTable, post.CategoriesPrimaryKey...),
|
||||
)
|
||||
fromU = sqlgraph.SetNeighbors(pq.driver.Dialect(), step)
|
||||
return fromU, nil
|
||||
|
@ -326,7 +325,7 @@ func (pq *PostQuery) Clone() *PostQuery {
|
|||
predicates: append([]predicate.Post{}, pq.predicates...),
|
||||
withContents: pq.withContents.Clone(),
|
||||
withContributors: pq.withContributors.Clone(),
|
||||
withCategory: pq.withCategory.Clone(),
|
||||
withCategories: pq.withCategories.Clone(),
|
||||
// clone intermediate query.
|
||||
sql: pq.sql.Clone(),
|
||||
path: pq.path,
|
||||
|
@ -355,14 +354,14 @@ func (pq *PostQuery) WithContributors(opts ...func(*PostContributorQuery)) *Post
|
|||
return pq
|
||||
}
|
||||
|
||||
// WithCategory tells the query-builder to eager-load the nodes that are connected to
|
||||
// the "category" edge. The optional arguments are used to configure the query builder of the edge.
|
||||
func (pq *PostQuery) WithCategory(opts ...func(*CategoryQuery)) *PostQuery {
|
||||
// WithCategories tells the query-builder to eager-load the nodes that are connected to
|
||||
// the "categories" edge. The optional arguments are used to configure the query builder of the edge.
|
||||
func (pq *PostQuery) WithCategories(opts ...func(*CategoryQuery)) *PostQuery {
|
||||
query := (&CategoryClient{config: pq.config}).Query()
|
||||
for _, opt := range opts {
|
||||
opt(query)
|
||||
}
|
||||
pq.withCategory = query
|
||||
pq.withCategories = query
|
||||
return pq
|
||||
}
|
||||
|
||||
|
@ -443,20 +442,13 @@ func (pq *PostQuery) prepareQuery(ctx context.Context) error {
|
|||
func (pq *PostQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Post, error) {
|
||||
var (
|
||||
nodes = []*Post{}
|
||||
withFKs = pq.withFKs
|
||||
_spec = pq.querySpec()
|
||||
loadedTypes = [3]bool{
|
||||
pq.withContents != nil,
|
||||
pq.withContributors != nil,
|
||||
pq.withCategory != nil,
|
||||
pq.withCategories != nil,
|
||||
}
|
||||
)
|
||||
if pq.withCategory != nil {
|
||||
withFKs = true
|
||||
}
|
||||
if withFKs {
|
||||
_spec.Node.Columns = append(_spec.Node.Columns, post.ForeignKeys...)
|
||||
}
|
||||
_spec.ScanValues = func(columns []string) ([]any, error) {
|
||||
return (*Post).scanValues(nil, columns)
|
||||
}
|
||||
|
@ -489,9 +481,10 @@ func (pq *PostQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Post, e
|
|||
return nil, err
|
||||
}
|
||||
}
|
||||
if query := pq.withCategory; query != nil {
|
||||
if err := pq.loadCategory(ctx, query, nodes, nil,
|
||||
func(n *Post, e *Category) { n.Edges.Category = e }); err != nil {
|
||||
if query := pq.withCategories; query != nil {
|
||||
if err := pq.loadCategories(ctx, query, nodes,
|
||||
func(n *Post) { n.Edges.Categories = []*Category{} },
|
||||
func(n *Post, e *Category) { n.Edges.Categories = append(n.Edges.Categories, e) }); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
@ -560,34 +553,63 @@ func (pq *PostQuery) loadContributors(ctx context.Context, query *PostContributo
|
|||
}
|
||||
return nil
|
||||
}
|
||||
func (pq *PostQuery) loadCategory(ctx context.Context, query *CategoryQuery, nodes []*Post, init func(*Post), assign func(*Post, *Category)) error {
|
||||
ids := make([]int, 0, len(nodes))
|
||||
nodeids := make(map[int][]*Post)
|
||||
for i := range nodes {
|
||||
if nodes[i].category_posts == nil {
|
||||
continue
|
||||
func (pq *PostQuery) loadCategories(ctx context.Context, query *CategoryQuery, nodes []*Post, init func(*Post), assign func(*Post, *Category)) error {
|
||||
edgeIDs := make([]driver.Value, len(nodes))
|
||||
byID := make(map[int]*Post)
|
||||
nids := make(map[int]map[*Post]struct{})
|
||||
for i, node := range nodes {
|
||||
edgeIDs[i] = node.ID
|
||||
byID[node.ID] = node
|
||||
if init != nil {
|
||||
init(node)
|
||||
}
|
||||
fk := *nodes[i].category_posts
|
||||
if _, ok := nodeids[fk]; !ok {
|
||||
ids = append(ids, fk)
|
||||
}
|
||||
nodeids[fk] = append(nodeids[fk], nodes[i])
|
||||
}
|
||||
if len(ids) == 0 {
|
||||
return nil
|
||||
query.Where(func(s *sql.Selector) {
|
||||
joinT := sql.Table(post.CategoriesTable)
|
||||
s.Join(joinT).On(s.C(category.FieldID), joinT.C(post.CategoriesPrimaryKey[0]))
|
||||
s.Where(sql.InValues(joinT.C(post.CategoriesPrimaryKey[1]), edgeIDs...))
|
||||
columns := s.SelectedColumns()
|
||||
s.Select(joinT.C(post.CategoriesPrimaryKey[1]))
|
||||
s.AppendSelect(columns...)
|
||||
s.SetDistinct(false)
|
||||
})
|
||||
if err := query.prepareQuery(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
query.Where(category.IDIn(ids...))
|
||||
neighbors, err := query.All(ctx)
|
||||
qr := QuerierFunc(func(ctx context.Context, q Query) (Value, error) {
|
||||
return query.sqlAll(ctx, func(_ context.Context, spec *sqlgraph.QuerySpec) {
|
||||
assign := spec.Assign
|
||||
values := spec.ScanValues
|
||||
spec.ScanValues = func(columns []string) ([]any, error) {
|
||||
values, err := values(columns[1:])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return append([]any{new(sql.NullInt64)}, values...), nil
|
||||
}
|
||||
spec.Assign = func(columns []string, values []any) error {
|
||||
outValue := int(values[0].(*sql.NullInt64).Int64)
|
||||
inValue := int(values[1].(*sql.NullInt64).Int64)
|
||||
if nids[inValue] == nil {
|
||||
nids[inValue] = map[*Post]struct{}{byID[outValue]: {}}
|
||||
return assign(columns[1:], values[1:])
|
||||
}
|
||||
nids[inValue][byID[outValue]] = struct{}{}
|
||||
return nil
|
||||
}
|
||||
})
|
||||
})
|
||||
neighbors, err := withInterceptors[[]*Category](ctx, query, qr, query.inters)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, n := range neighbors {
|
||||
nodes, ok := nodeids[n.ID]
|
||||
nodes, ok := nids[n.ID]
|
||||
if !ok {
|
||||
return fmt.Errorf(`unexpected foreign-key "category_posts" returned %v`, n.ID)
|
||||
return fmt.Errorf(`unexpected "categories" node returned %v`, n.ID)
|
||||
}
|
||||
for i := range nodes {
|
||||
assign(nodes[i], n)
|
||||
for kn := range nodes {
|
||||
assign(kn, n)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue