init
This commit is contained in:
commit
d69342b2e9
160 changed files with 28681 additions and 0 deletions
70
internal/tenant/comments.go
Normal file
70
internal/tenant/comments.go
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
package tenant
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
func (q *Queries) ListComments(ctx context.Context, postSlug string) ([]Comment, error) {
|
||||
rows, err := q.db.QueryContext(ctx, `SELECT id, user_id, post_slug, content, content_html, parent_id, created_at, updated_at
|
||||
FROM comments WHERE post_slug = ? ORDER BY created_at ASC`, postSlug)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var comments []Comment
|
||||
for rows.Next() {
|
||||
c, err := scanComment(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
comments = append(comments, c)
|
||||
}
|
||||
return comments, rows.Err()
|
||||
}
|
||||
|
||||
func (q *Queries) GetComment(ctx context.Context, id int64) (*Comment, error) {
|
||||
row := q.db.QueryRowContext(ctx, `SELECT id, user_id, post_slug, content, content_html, parent_id, created_at, updated_at
|
||||
FROM comments WHERE id = ?`, id)
|
||||
|
||||
c, err := scanComment(row)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
func (q *Queries) CreateComment(ctx context.Context, c *Comment) error {
|
||||
result, err := q.db.ExecContext(ctx, `INSERT INTO comments (user_id, post_slug, content, content_html, parent_id)
|
||||
VALUES (?, ?, ?, ?, ?)`,
|
||||
c.UserID, c.PostSlug, c.Content, nullStr(c.ContentHTML), c.ParentID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.ID, _ = result.LastInsertId()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (q *Queries) DeleteComment(ctx context.Context, id int64) error {
|
||||
_, err := q.db.ExecContext(ctx, `DELETE FROM comments WHERE id = ?`, id)
|
||||
return err
|
||||
}
|
||||
|
||||
func scanComment(s scanner) (Comment, error) {
|
||||
var c Comment
|
||||
var contentHTML, createdAt, updatedAt sql.NullString
|
||||
|
||||
err := s.Scan(&c.ID, &c.UserID, &c.PostSlug, &c.Content, &contentHTML, &c.ParentID, &createdAt, &updatedAt)
|
||||
if err != nil {
|
||||
return c, err
|
||||
}
|
||||
|
||||
c.ContentHTML = contentHTML.String
|
||||
c.CreatedAt = parseTime(createdAt.String)
|
||||
c.UpdatedAt = parseTime(updatedAt.String)
|
||||
return c, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue