48 lines
1.3 KiB
Go
48 lines
1.3 KiB
Go
package repositories
|
|
|
|
import "lv8girl/internal/models"
|
|
|
|
type CommentRepository struct{}
|
|
|
|
func NewCommentRepository() *CommentRepository {
|
|
return &CommentRepository{}
|
|
}
|
|
|
|
func (r *CommentRepository) FindByPostID(postID uint) ([]models.Comment, error) {
|
|
var comments []models.Comment
|
|
err := DB.Preload("User").Where("post_id = ?", postID).Order("created_at DESC").Find(&comments).Error
|
|
return comments, err
|
|
}
|
|
|
|
func (r *CommentRepository) FindAll() ([]models.Comment, error) {
|
|
var comments []models.Comment
|
|
err := DB.Preload("User").Order("created_at DESC").Find(&comments).Error
|
|
return comments, err
|
|
}
|
|
|
|
func (r *CommentRepository) Create(comment *models.Comment) error {
|
|
return DB.Create(comment).Error
|
|
}
|
|
|
|
func (r *CommentRepository) Delete(id uint) error {
|
|
return DB.Delete(&models.Comment{}, id).Error
|
|
}
|
|
|
|
func (r *CommentRepository) Count() (int64, error) {
|
|
var count int64
|
|
err := DB.Model(&models.Comment{}).Count(&count).Error
|
|
return count, err
|
|
}
|
|
|
|
func (r *CommentRepository) CountByPostID(postID uint) (int64, error) {
|
|
var count int64
|
|
err := DB.Model(&models.Comment{}).Where("post_id = ?", postID).Count(&count).Error
|
|
return count, err
|
|
}
|
|
|
|
func (r *CommentRepository) CountByUserID(userID uint) (int64, error) {
|
|
var count int64
|
|
err := DB.Model(&models.Comment{}).Where("user_id = ?", userID).Count(&count).Error
|
|
return count, err
|
|
}
|