diff --git a/internal/http/graph/generated.go b/internal/http/graph/generated.go index 420cbd0..45c79f3 100644 --- a/internal/http/graph/generated.go +++ b/internal/http/graph/generated.go @@ -133,6 +133,7 @@ type ComplexityRoot struct { Comments func(childComplexity int, postID int) int GetChatHistory func(childComplexity int, userID int) int GetUserChats func(childComplexity int) int + GetUserPosts func(childComplexity int, userID int) int Me func(childComplexity int) int MySessions func(childComplexity int) int Post func(childComplexity int, id int) int @@ -224,6 +225,7 @@ type QueryResolver interface { Me(ctx context.Context) (*domain.User, error) Post(ctx context.Context, id int) (*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) Users(ctx context.Context) ([]*domain.User, 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 + 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": if e.complexity.Query.Me == nil { break @@ -1304,6 +1318,17 @@ func (ec *executionContext) field_Query_getChatHistory_args(ctx context.Context, 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) { var err error args := map[string]any{} @@ -4282,6 +4307,83 @@ func (ec *executionContext) fieldContext_Query_posts(_ context.Context, field gr 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) { fc, err := ec.fieldContext_Query_user(ctx, field) 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) }) } + 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) }) case "user": field := field diff --git a/internal/http/graph/post_resolvers.go b/internal/http/graph/post_resolvers.go index b6d2167..fddb8b3 100644 --- a/internal/http/graph/post_resolvers.go +++ b/internal/http/graph/post_resolvers.go @@ -159,3 +159,12 @@ func (r *mutationResolver) DeletePost(ctx context.Context, id int) (bool, error) } 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 +} diff --git a/internal/http/graph/schema.graphql b/internal/http/graph/schema.graphql index 24cc874..d4abe51 100644 --- a/internal/http/graph/schema.graphql +++ b/internal/http/graph/schema.graphql @@ -98,6 +98,7 @@ type Query { me: User! # Получить текущего пользователя post(id: Int!): Post! # Получить пост по ID posts: [Post!]! # Получить все посты + getUserPosts(userId: Int!): [Post!]! user(id: Int!): User! # Получить пользователя по ID users: [User!]! getChatHistory(userId: Int!): [Message!]! diff --git a/internal/repository/post_repository.go b/internal/repository/post_repository.go index 715cea7..57d3637 100644 --- a/internal/repository/post_repository.go +++ b/internal/repository/post_repository.go @@ -26,8 +26,37 @@ type postRepository struct { } func (r *postRepository) GetByAuthorID(ctx context.Context, authorID int) ([]*domain.Post, error) { - //TODO implement me - panic("implement me") + query := ` + 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 {