v0.0.15 Добавлен запрос getUserPosts на получение постов по ID пользователя
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
madipo2611 2025-08-08 00:28:45 +03:00
parent 9f6c39e5ac
commit ad9781a34b
4 changed files with 165 additions and 2 deletions

View File

@ -133,6 +133,7 @@ type ComplexityRoot struct {
Comments func(childComplexity int, postID int) int Comments func(childComplexity int, postID int) int
GetChatHistory func(childComplexity int, userID int) int GetChatHistory func(childComplexity int, userID int) int
GetUserChats func(childComplexity int) int GetUserChats func(childComplexity int) int
GetUserPosts func(childComplexity int, userID int) int
Me func(childComplexity int) int Me func(childComplexity int) int
MySessions func(childComplexity int) int MySessions func(childComplexity int) int
Post func(childComplexity int, id int) int Post func(childComplexity int, id int) int
@ -224,6 +225,7 @@ type QueryResolver interface {
Me(ctx context.Context) (*domain.User, error) Me(ctx context.Context) (*domain.User, error)
Post(ctx context.Context, id int) (*domain.Post, error) Post(ctx context.Context, id int) (*domain.Post, error)
Posts(ctx context.Context) ([]*domain.Post, error) Posts(ctx context.Context) ([]*domain.Post, error)
GetUserPosts(ctx context.Context, userID int) ([]*domain.Post, error)
User(ctx context.Context, id int) (*domain.User, error) User(ctx context.Context, id int) (*domain.User, error)
Users(ctx context.Context) ([]*domain.User, error) Users(ctx context.Context) ([]*domain.User, error)
GetChatHistory(ctx context.Context, userID int) ([]*domain.Message, error) GetChatHistory(ctx context.Context, userID int) ([]*domain.Message, error)
@ -730,6 +732,18 @@ func (e *executableSchema) Complexity(ctx context.Context, typeName, field strin
return e.complexity.Query.GetUserChats(childComplexity), true return e.complexity.Query.GetUserChats(childComplexity), true
case "Query.getUserPosts":
if e.complexity.Query.GetUserPosts == nil {
break
}
args, err := ec.field_Query_getUserPosts_args(ctx, rawArgs)
if err != nil {
return 0, false
}
return e.complexity.Query.GetUserPosts(childComplexity, args["userId"].(int)), true
case "Query.me": case "Query.me":
if e.complexity.Query.Me == nil { if e.complexity.Query.Me == nil {
break break
@ -1304,6 +1318,17 @@ func (ec *executionContext) field_Query_getChatHistory_args(ctx context.Context,
return args, nil return args, nil
} }
func (ec *executionContext) field_Query_getUserPosts_args(ctx context.Context, rawArgs map[string]any) (map[string]any, error) {
var err error
args := map[string]any{}
arg0, err := processArgField(ctx, rawArgs, "userId", ec.unmarshalNInt2int)
if err != nil {
return nil, err
}
args["userId"] = arg0
return args, nil
}
func (ec *executionContext) field_Query_post_args(ctx context.Context, rawArgs map[string]any) (map[string]any, error) { func (ec *executionContext) field_Query_post_args(ctx context.Context, rawArgs map[string]any) (map[string]any, error) {
var err error var err error
args := map[string]any{} args := map[string]any{}
@ -4282,6 +4307,83 @@ func (ec *executionContext) fieldContext_Query_posts(_ context.Context, field gr
return fc, nil return fc, nil
} }
func (ec *executionContext) _Query_getUserPosts(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) {
fc, err := ec.fieldContext_Query_getUserPosts(ctx, field)
if err != nil {
return graphql.Null
}
ctx = graphql.WithFieldContext(ctx, fc)
defer func() {
if r := recover(); r != nil {
ec.Error(ctx, ec.Recover(ctx, r))
ret = graphql.Null
}
}()
resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (any, error) {
ctx = rctx // use context from middleware stack in children
return ec.resolvers.Query().GetUserPosts(rctx, fc.Args["userId"].(int))
})
if err != nil {
ec.Error(ctx, err)
return graphql.Null
}
if resTmp == nil {
if !graphql.HasFieldError(ctx, fc) {
ec.Errorf(ctx, "must not be null")
}
return graphql.Null
}
res := resTmp.([]*domain.Post)
fc.Result = res
return ec.marshalNPost2ᚕᚖtailly_back_v2ᚋinternalᚋdomainᚐPostᚄ(ctx, field.Selections, res)
}
func (ec *executionContext) fieldContext_Query_getUserPosts(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) {
fc = &graphql.FieldContext{
Object: "Query",
Field: field,
IsMethod: true,
IsResolver: true,
Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) {
switch field.Name {
case "id":
return ec.fieldContext_Post_id(ctx, field)
case "title":
return ec.fieldContext_Post_title(ctx, field)
case "content":
return ec.fieldContext_Post_content(ctx, field)
case "author":
return ec.fieldContext_Post_author(ctx, field)
case "commentsCount":
return ec.fieldContext_Post_commentsCount(ctx, field)
case "likes":
return ec.fieldContext_Post_likes(ctx, field)
case "likesCount":
return ec.fieldContext_Post_likesCount(ctx, field)
case "isLiked":
return ec.fieldContext_Post_isLiked(ctx, field)
case "createdAt":
return ec.fieldContext_Post_createdAt(ctx, field)
case "updatedAt":
return ec.fieldContext_Post_updatedAt(ctx, field)
}
return nil, fmt.Errorf("no field named %q was found under type Post", field.Name)
},
}
defer func() {
if r := recover(); r != nil {
err = ec.Recover(ctx, r)
ec.Error(ctx, err)
}
}()
ctx = graphql.WithFieldContext(ctx, fc)
if fc.Args, err = ec.field_Query_getUserPosts_args(ctx, field.ArgumentMap(ec.Variables)); err != nil {
ec.Error(ctx, err)
return fc, err
}
return fc, nil
}
func (ec *executionContext) _Query_user(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { func (ec *executionContext) _Query_user(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) {
fc, err := ec.fieldContext_Query_user(ctx, field) fc, err := ec.fieldContext_Query_user(ctx, field)
if err != nil { if err != nil {
@ -8830,6 +8932,28 @@ func (ec *executionContext) _Query(ctx context.Context, sel ast.SelectionSet) gr
func(ctx context.Context) graphql.Marshaler { return innerFunc(ctx, out) }) func(ctx context.Context) graphql.Marshaler { return innerFunc(ctx, out) })
} }
out.Concurrently(i, func(ctx context.Context) graphql.Marshaler { return rrm(innerCtx) })
case "getUserPosts":
field := field
innerFunc := func(ctx context.Context, fs *graphql.FieldSet) (res graphql.Marshaler) {
defer func() {
if r := recover(); r != nil {
ec.Error(ctx, ec.Recover(ctx, r))
}
}()
res = ec._Query_getUserPosts(ctx, field)
if res == graphql.Null {
atomic.AddUint32(&fs.Invalids, 1)
}
return res
}
rrm := func(ctx context.Context) graphql.Marshaler {
return ec.OperationContext.RootResolverMiddleware(ctx,
func(ctx context.Context) graphql.Marshaler { return innerFunc(ctx, out) })
}
out.Concurrently(i, func(ctx context.Context) graphql.Marshaler { return rrm(innerCtx) }) out.Concurrently(i, func(ctx context.Context) graphql.Marshaler { return rrm(innerCtx) })
case "user": case "user":
field := field field := field

View File

@ -159,3 +159,12 @@ func (r *mutationResolver) DeletePost(ctx context.Context, id int) (bool, error)
} }
return true, nil return true, nil
} }
// GetUserPosts is the resolver for the getUserPosts field.
func (r *queryResolver) GetUserPosts(ctx context.Context, userID int) ([]*domain.Post, error) {
posts, err := r.Services.Post.GetByAuthorID(ctx, userID)
if err != nil {
return nil, fmt.Errorf("failed to get posts: %w", err)
}
return posts, nil
}

View File

@ -98,6 +98,7 @@ type Query {
me: User! # Получить текущего пользователя me: User! # Получить текущего пользователя
post(id: Int!): Post! # Получить пост по ID post(id: Int!): Post! # Получить пост по ID
posts: [Post!]! # Получить все посты posts: [Post!]! # Получить все посты
getUserPosts(userId: Int!): [Post!]!
user(id: Int!): User! # Получить пользователя по ID user(id: Int!): User! # Получить пользователя по ID
users: [User!]! users: [User!]!
getChatHistory(userId: Int!): [Message!]! getChatHistory(userId: Int!): [Message!]!

View File

@ -26,8 +26,37 @@ type postRepository struct {
} }
func (r *postRepository) GetByAuthorID(ctx context.Context, authorID int) ([]*domain.Post, error) { func (r *postRepository) GetByAuthorID(ctx context.Context, authorID int) ([]*domain.Post, error) {
//TODO implement me query := `
panic("implement me") SELECT id, title, content, author_id, created_at, updated_at
FROM posts
WHERE author_id = $1
ORDER BY created_at DESC
`
rows, err := r.db.QueryContext(ctx, query, authorID)
if err != nil {
return nil, err
}
defer rows.Close()
var posts []*domain.Post
for rows.Next() {
post := &domain.Post{}
err := rows.Scan(
&post.ID,
&post.Title,
&post.Content,
&post.AuthorID,
&post.CreatedAt,
&post.UpdatedAt,
)
if err != nil {
return nil, err
}
posts = append(posts, post)
}
return posts, nil
} }
func (r *postRepository) Update(ctx context.Context, post *domain.Post) error { func (r *postRepository) Update(ctx context.Context, post *domain.Post) error {