lv8girl!
This commit is contained in:
37
internal/repositories/like.go
Normal file
37
internal/repositories/like.go
Normal file
@@ -0,0 +1,37 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user