38 lines
987 B
Go
38 lines
987 B
Go
package repositories
|
|
|
|
import "lv8girl/internal/models"
|
|
|
|
type LikeRepository struct{}
|
|
|
|
func NewLikeRepository() *LikeRepository {
|
|
return &LikeRepository{}
|
|
}
|
|
|
|
func (r *LikeRepository) FindByPostAndUser(postID, userID uint) (*models.Like, error) {
|
|
var like models.Like
|
|
err := DB.Where("post_id = ? AND user_id = ?", postID, userID).First(&like).Error
|
|
return &like, err
|
|
}
|
|
|
|
func (r *LikeRepository) Exists(postID, userID uint) bool {
|
|
var count int64
|
|
DB.Model(&models.Like{}).Where("post_id = ? AND user_id = ?", postID, userID).Count(&count)
|
|
return count > 0
|
|
}
|
|
|
|
func (r *LikeRepository) Create(like *models.Like) error {
|
|
return DB.Create(like).Error
|
|
}
|
|
|
|
func (r *LikeRepository) Count() (int64, error) {
|
|
var count int64
|
|
err := DB.Model(&models.Like{}).Count(&count).Error
|
|
return count, err
|
|
}
|
|
|
|
func (r *LikeRepository) CountByPostID(postID uint) (int64, error) {
|
|
var count int64
|
|
err := DB.Model(&models.Like{}).Where("post_id = ?", postID).Count(&count).Error
|
|
return count, err
|
|
}
|