feat: start develop a comment logic

This commit is contained in:
d3m0k1d
2026-02-15 19:06:10 +03:00
parent 2c3e6578e9
commit 0eca2b1e68
7 changed files with 132 additions and 8 deletions

View File

@@ -70,9 +70,10 @@ func (p *postRepository) GetByID(ctx context.Context, id int) (storage.PostReq,
func (p *postRepository) Create(ctx context.Context, post storage.Post) error {
query, err := p.db.Exec(
"INSERT INTO posts(title, content) VALUES(?, ?)",
"INSERT INTO posts(title, content) VALUES(?, ?, ?)",
post.Title,
post.Content,
post.Tags,
)
if err != nil {
return err
@@ -94,7 +95,10 @@ func (p *postRepository) Update(ctx context.Context, id int, post storage.Post)
updates = append(updates, "title = ?")
args = append(args, post.Title)
}
if post.Tags != "" {
updates = append(updates, "tags = ?")
args = append(args, post.Tags)
}
if post.Content != "" {
updates = append(updates, "content = ?")
args = append(args, post.Content)
@@ -145,7 +149,7 @@ func (p *postRepository) IsExist(ctx context.Context, id int) bool {
func (p *postRepository) GetAllAdmin(ctx context.Context) ([]storage.PostReq, error) {
result := []storage.PostReq{}
rows, err := p.db.Query("SELECT id, title, content FROM posts")
rows, err := p.db.Query("SELECT id, title, content, tags, CREATED_AT FROM posts")
if err != nil {
p.logger.Error(err.Error())
return nil, err
@@ -154,14 +158,18 @@ func (p *postRepository) GetAllAdmin(ctx context.Context) ([]storage.PostReq, er
var title string
var content string
var id int
err := rows.Scan(&id, &title, &content)
var tags string
var createdAt string
err := rows.Scan(&id, &title, &content, &tags, &createdAt)
if err != nil {
p.logger.Error("error scan: " + err.Error())
}
result = append(result, storage.PostReq{
ID: id,
Title: title,
Content: content,
ID: id,
Title: title,
Content: content,
Tags: tags,
CreatedAt: createdAt,
})
}
return result, nil