feat: update handlers in posts
All checks were successful
Backend ci / build (push) Successful in 3m29s

This commit is contained in:
d3m0k1d
2026-02-13 14:46:44 +03:00
parent 15cf6cca8a
commit 2c198e56eb
4 changed files with 69 additions and 5 deletions

View File

@@ -9,6 +9,8 @@ import (
type PostRepository interface {
GetAll(ctx context.Context) ([]storage.PostReq, error)
GetByID(ctx context.Context, id int) (storage.PostReq, error)
GetLastID(ctx context.Context) (int, error)
IsExist(ctx context.Context, id int) bool
Create(ctx context.Context, post storage.Post) error
Update(ctx context.Context, id int, post storage.Post) error
Delete(ctx context.Context, id int) error

View File

@@ -92,6 +92,33 @@ func (p *postRepository) Update(ctx context.Context, id int, post storage.Post)
}
func (p *postRepository) Delete(ctx context.Context, id int) error {
_, err := p.db.Exec("DELETE FROM posts WHERE id = ?", id)
if err != nil {
return err
}
p.logger.Info("Post deleted:", "id", id)
return nil
}
func (p *postRepository) GetLastID(ctx context.Context) (int, error) {
var id int
row := p.db.QueryRow("SELECT id FROM posts ORDER BY id DESC LIMIT 1")
err := row.Scan(&id)
if err != nil {
p.logger.Error("error scan: " + err.Error())
}
return id, nil
}
func (p *postRepository) IsExist(ctx context.Context, id int) bool {
var exists int
err := p.db.QueryRowContext(ctx, "SELECT 1 FROM posts WHERE id = ? LIMIT 1", id).Scan(&exists)
if err != nil {
if err == sql.ErrNoRows {
return false
}
p.logger.Error("error checking post existence: " + err.Error())
return false
}
return true
}