feat: update handlers in posts
All checks were successful
Backend ci / build (push) Successful in 3m29s
All checks were successful
Backend ci / build (push) Successful in 3m29s
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user