// UpdateComment - updates a comment in the database
func (d *Database) UpdateComment(ctx context.Context, id string, cmt comment.Comment) (comment.Comment, error) {
cmtRow := CommentRow{
ID: id,
Slug: sql.NullString{String: cmt.Slug, Valid: true},
Body: sql.NullString{String: cmt.Body, Valid: true},
Author: sql.NullString{String: cmt.Author, Valid: true},
}
rows, err := d.Client.NamedQueryContext(
ctx,
`UPDATE comments SET
slug = :slug,
author = :author,
body = :body
WHERE id = :id`,
cmtRow,
)
if err != nil {
return comment.Comment{}, fmt.Errorf("failed to insert comment: %w", err)
}
if err := rows.Close(); err != nil {
return comment.Comment{}, fmt.Errorf("failed to close rows: %w", err)
}
return convertCommentRowToComment(cmtRow), nil
}
// DeleteComment - deletes a comment from the database
func (d *Database) DeleteComment(ctx context.Context, id string) error {
_, err := d.Client.ExecContext(
ctx,
`DELETE FROM comments where id = $1`,
id,
)
if err != nil {
return fmt.Errorf("failed to delete comment from the database: %w", err)
}
return nil
}