chore: Add logic for put and get by id handler
Some checks failed
Backend ci / build (push) Has been cancelled

This commit is contained in:
d3m0k1d
2026-02-04 12:14:35 +03:00
parent 83cf741f4e
commit 01b6ab746e
8 changed files with 195 additions and 16 deletions

View File

@@ -21,7 +21,7 @@ func NewPostRepository(db *sql.DB) PostRepository {
}
func (p *postRepository) GetAll(ctx context.Context) ([]storage.PostReq, error) {
var result []storage.PostReq
rows, err := p.db.Query("SELECT title content FROM posts")
rows, err := p.db.Query("SELECT id, title, content FROM posts")
if err != nil {
p.logger.Error(err.Error())
return nil, err
@@ -29,11 +29,13 @@ func (p *postRepository) GetAll(ctx context.Context) ([]storage.PostReq, error)
for rows.Next() {
var title string
var content string
err := rows.Scan(&title, &content)
var id int
err := rows.Scan(&id, &title, &content)
if err != nil {
p.logger.Error("error scan: " + err.Error())
}
result = append(result, storage.PostReq{
ID: id,
Title: title,
Content: content,
})
@@ -43,7 +45,7 @@ func (p *postRepository) GetAll(ctx context.Context) ([]storage.PostReq, error)
func (p *postRepository) GetByID(ctx context.Context, id int) (storage.PostReq, error) {
var result storage.PostReq
row := p.db.QueryRow("SELECT title content FROM posts WHERE id = ?", id)
row := p.db.QueryRow("SELECT title, content FROM posts WHERE id = ?", id)
var title string
var content string
err := row.Scan(&title, &content)
@@ -51,6 +53,7 @@ func (p *postRepository) GetByID(ctx context.Context, id int) (storage.PostReq,
p.logger.Error("error scan: " + err.Error())
}
result = storage.PostReq{
ID: id,
Title: title,
Content: content,
}
@@ -75,7 +78,16 @@ func (p *postRepository) Create(ctx context.Context, post storage.Post) error {
}
func (p *postRepository) Update(ctx context.Context, id int, post storage.Post) error {
_, err := p.db.Exec(
"UPDATE posts SET title = ?, content = ? WHERE id = ?",
post.Title,
post.Content,
id,
)
if err != nil {
return err
}
p.logger.Info("Post updated:", "id", id)
return nil
}