package repositories import ( "clapclap/internal/models" "log" "clapclap/internal/uuid" "gorm.io/gorm/clause" ) func (r *Repo) CreateComment(comment *models.Comment) error { err := r.db.Create(comment).Error if err != nil { return err } log.Printf("new comment for track %s was created", comment.TrackID) return nil } func (r *Repo) GetCommentsByTrack(trackID string, preloads []string) ([]models.Comment, error) { var comments []models.Comment query := r.db for _, p := range preloads { query = query.Preload(p) } // Only fetch top-level comments; replies are preloaded err := query.Where("track_id = ? AND reply_to_id IS NULL", trackID). Find(&comments).Error return comments, err } func (r *Repo) UpdateComment(comment *models.Comment) error { if err := r.db.Model(comment).Updates(comment).Error; err != nil { return err } log.Printf("comment is successfully updated for track %s", comment.TrackID) return nil } func (r *Repo) DeleteComment(id string) error { uid, err := uuid.Parse(id) if err != nil { return err } var comment models.Comment err = r.db.Clauses(clause.Returning{}). Where("id = ?", uid). Delete(&comment).Error if err != nil { return err } log.Printf("Comment for track %s was deleted", comment.TrackID) return nil } func (r *Repo) GetCommentByID(id string, preloads []string) (*models.Comment, error) { var comment models.Comment uid, err := uuid.Parse(id) if err != nil { return nil, err } query := r.db for _, p := range preloads { query = query.Preload(p) } err = query. First(&comment, "id = ?", uid).Error return &comment, err }