This commit is contained in:
d3m0k1d
2026-02-18 13:42:42 +03:00
parent 5a375d67c7
commit 3fcaefba1b
2 changed files with 32 additions and 3 deletions

View File

@@ -21,12 +21,41 @@ func NewCommentsRepository(db *sql.DB) CommentRepository {
} }
} }
func (c *commentsRepository) CreateComment(ctx context.Context, comment *storage.Comment) error { func (c *commentsRepository) CreateComment(ctx context.Context, comment *storage.CommentCreate) error {
_, err := c.db.Exec("INSERT INTO comments(content, post_id) VALUES(?, ?, ?)", comment.Content, comment.PostID, comment.UserID)
if err != nil {
c.logger.Error("error insert: " + err.Error())
return err
}
return nil return nil
} }
func (c *commentsRepository) GetAllComments(ctx context.Context) ([]storage.Comment, error) { func (c *commentsRepository) GetAllComments(ctx context.Context) ([]storage.Comment, error) {
return nil, nil var result []storage.Comment
rows, err := c.db.Query("SELECT id, content, post_id, user_id, created_at FROM comments")
if err != nil {
c.logger.Error("error scan " + err.Error())
return nil, err
}
for rows.Next() {
var id int
var content string
var postID int
var userID int
var createdAt string
err := rows.Scan(&id, &content, &postID, &userID, &createdAt)
if err != nil {
c.logger.Error("error scan: " + err.Error())
}
result = append(result, storage.Comment{
ID: id,
Content: content,
PostID: postID,
UserID: userID,
CreatedAt: createdAt,
})
}
return result, nil
} }
func (c *commentsRepository) GetCommentsOfPost(ctx context.Context, id int) ([]storage.Comment, error) { func (c *commentsRepository) GetCommentsOfPost(ctx context.Context, id int) ([]storage.Comment, error) {

View File

@@ -24,7 +24,7 @@ type AuthRepository interface {
} }
type CommentRepository interface { type CommentRepository interface {
CreateComment(ctx context.Context, comment *storage.Comment) error CreateComment(ctx context.Context, comment *storage.CommentCreate) error
GetAllComments(ctx context.Context) ([]storage.Comment, error) GetAllComments(ctx context.Context) ([]storage.Comment, error)
GetCommentsOfPost(ctx context.Context, id int) ([]storage.Comment, error) GetCommentsOfPost(ctx context.Context, id int) ([]storage.Comment, error)
DeleteComment(ctx context.Context, id int) error DeleteComment(ctx context.Context, id int) error