|
| 1 | +package comments |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + |
| 9 | + "github.com/uptrace/bun" |
| 10 | +) |
| 11 | + |
| 12 | +// Repository handles data access operations for comments. |
| 13 | +type Repository struct { |
| 14 | + db *bun.DB |
| 15 | +} |
| 16 | + |
| 17 | +// NewRepository creates a new Repository instance with the given database connection. |
| 18 | +func NewRepository(db *bun.DB) *Repository { |
| 19 | + return &Repository{db: db} |
| 20 | +} |
| 21 | + |
| 22 | +// Create inserts a new comment. |
| 23 | +func (r *Repository) Create(ctx context.Context, input CommentInput) (*Comment, error) { |
| 24 | + model := newCommentModel(input) |
| 25 | + |
| 26 | + if _, err := r.db.NewInsert().Model(model).Returning("*").Exec(ctx); err != nil { |
| 27 | + return nil, fmt.Errorf("failed to insert comment: %w", err) |
| 28 | + } |
| 29 | + |
| 30 | + return model.toDomain(), nil |
| 31 | +} |
| 32 | + |
| 33 | +// GetByID retrieves a comment by its ID. |
| 34 | +func (r *Repository) GetByID(ctx context.Context, id int64) (*Comment, error) { |
| 35 | + var model commentModel |
| 36 | + if err := r.db.NewSelect().Model(&model).Where("id = ?", id).Scan(ctx); err != nil { |
| 37 | + if errors.Is(err, sql.ErrNoRows) { |
| 38 | + return nil, ErrNotFound |
| 39 | + } |
| 40 | + return nil, fmt.Errorf("failed to get comment by ID: %w", err) |
| 41 | + } |
| 42 | + return model.toDomain(), nil |
| 43 | +} |
| 44 | + |
| 45 | +// List retrieves all comments for a specific task. |
| 46 | +func (r *Repository) List(ctx context.Context, taskID int64) ([]Comment, error) { |
| 47 | + models := make([]commentModel, 0) |
| 48 | + |
| 49 | + query := r.db.NewSelect().Model(&models).Where("task_id = ?", taskID) |
| 50 | + |
| 51 | + if err := query.OrderExpr("created_at ASC").Scan(ctx); err != nil { |
| 52 | + return nil, fmt.Errorf("failed to list comments by task: %w", err) |
| 53 | + } |
| 54 | + |
| 55 | + comments := make([]Comment, 0, len(models)) |
| 56 | + for _, model := range models { |
| 57 | + comments = append(comments, *model.toDomain()) |
| 58 | + } |
| 59 | + |
| 60 | + return comments, nil |
| 61 | +} |
| 62 | + |
| 63 | +// Update modifies an existing comment with the provided content. |
| 64 | +func (r *Repository) Update(ctx context.Context, id int64, content string) error { |
| 65 | + if _, err := r.db.NewUpdate(). |
| 66 | + Model((*commentModel)(nil)). |
| 67 | + Set("content = ?", content). |
| 68 | + Where("id = ?", id). |
| 69 | + Exec(ctx); err != nil { |
| 70 | + return fmt.Errorf("failed to update comment: %w", err) |
| 71 | + } |
| 72 | + |
| 73 | + return nil |
| 74 | +} |
| 75 | + |
| 76 | +// Delete soft-deletes a comment by setting the deleted_at timestamp. |
| 77 | +func (r *Repository) Delete(ctx context.Context, id int64) error { |
| 78 | + _, err := r.db.NewDelete(). |
| 79 | + Model((*commentModel)(nil)). |
| 80 | + Where("id = ?", id). |
| 81 | + Exec(ctx) |
| 82 | + |
| 83 | + if err != nil { |
| 84 | + return fmt.Errorf("failed to delete comment: %w", err) |
| 85 | + } |
| 86 | + |
| 87 | + return nil |
| 88 | +} |
0 commit comments