🎨 update project structure
This commit is contained in:
92
dao/impl/comment_reply_dao_impl.go
Normal file
92
dao/impl/comment_reply_dao_impl.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package impl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gorm.io/gorm"
|
||||
"schisandra-cloud-album/global"
|
||||
"schisandra-cloud-album/model"
|
||||
)
|
||||
|
||||
type CommentReplyDaoImpl struct{}
|
||||
|
||||
// CreateCommentReply 创建评论
|
||||
func (CommentReplyDaoImpl) CreateCommentReply(comment *model.ScaCommentReply) error {
|
||||
if err := global.DB.Create(&comment).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetCommentListOrderByCreatedTimeDesc 通过topic_id获取评论列表
|
||||
func (CommentReplyDaoImpl) GetCommentListOrderByCreatedTimeDesc(topicID uint, page, pageSize int) ([]model.ScaCommentReply, error) {
|
||||
var comments []model.ScaCommentReply
|
||||
// 计算偏移量
|
||||
offset := (page - 1) * pageSize
|
||||
|
||||
if err := global.DB.Where("topic_id =? and deleted = 0", topicID).Order("created_time desc").
|
||||
Offset(offset).Limit(pageSize).Find(&comments).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return comments, nil
|
||||
}
|
||||
|
||||
// GetCommentListOrderByLikesDesc 通过topic_id获取评论列表
|
||||
func (CommentReplyDaoImpl) GetCommentListOrderByLikesDesc(topicID uint, page, pageSize int) ([]model.ScaCommentReply, error) {
|
||||
var comments []model.ScaCommentReply
|
||||
// 计算偏移量
|
||||
offset := (page - 1) * pageSize
|
||||
|
||||
if err := global.DB.Where("topic_id =? and deleted = 0", topicID).Order("likes desc").
|
||||
Offset(offset).Limit(pageSize).Find(&comments).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return comments, nil
|
||||
}
|
||||
|
||||
// UpdateCommentReplyCount 更新评论
|
||||
func (CommentReplyDaoImpl) UpdateCommentReplyCount(commentID int64) error {
|
||||
// 使用事务处理错误
|
||||
err := global.DB.Transaction(func(tx *gorm.DB) error {
|
||||
result := tx.Model(&model.ScaCommentReply{}).Where("id = ? and deleted = 0", commentID).Update("reply_count", gorm.Expr("reply_count + ?", 1))
|
||||
if result.Error != nil {
|
||||
return result.Error // 返回更新错误
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
return fmt.Errorf("comment not found") // 处理评论不存在的情况
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// UpdateCommentLikesCount 更新评论 likes 数量
|
||||
func (CommentReplyDaoImpl) UpdateCommentLikesCount(commentID int64, topicID string) error {
|
||||
// 使用事务处理错误
|
||||
err := global.DB.Transaction(func(tx *gorm.DB) error {
|
||||
result := tx.Model(&model.ScaCommentReply{}).Where("id = ? and topic_id = ? and deleted = 0", commentID, topicID).Update("likes", gorm.Expr("likes + ?", 1))
|
||||
if result.Error != nil {
|
||||
return result.Error // 返回更新错误
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
return fmt.Errorf("comment not found") // 处理评论不存在的情况
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// DecrementCommentLikesCount 减少评论 likes 数量
|
||||
func (CommentReplyDaoImpl) DecrementCommentLikesCount(commentID int64, topicID string) error {
|
||||
// 使用事务处理错误
|
||||
err := global.DB.Transaction(func(tx *gorm.DB) error {
|
||||
result := tx.Model(&model.ScaCommentReply{}).Where("id = ? and topic_id = ? and deleted = 0", commentID, topicID).Update("likes", gorm.Expr("likes - ?", 1))
|
||||
if result.Error != nil {
|
||||
return result.Error // 返回更新错误
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
return fmt.Errorf("comment not found") // 处理评论不存在的情况
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return err
|
||||
}
|
25
dao/impl/permission_dao_impl.go
Normal file
25
dao/impl/permission_dao_impl.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package impl
|
||||
|
||||
import (
|
||||
"schisandra-cloud-album/global"
|
||||
"schisandra-cloud-album/model"
|
||||
)
|
||||
|
||||
type PermissionDaoImpl struct{}
|
||||
|
||||
// GetPermissionsByIds 通过权限ID列表获取权限列表
|
||||
func (PermissionDaoImpl) GetPermissionsByIds(ids []int64) ([]model.ScaAuthPermission, error) {
|
||||
var permissions []model.ScaAuthPermission
|
||||
if err := global.DB.Where("id IN ? and deleted = 0", ids).Find(&permissions).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return permissions, nil
|
||||
}
|
||||
|
||||
// CreatePermissions 批量创建权限
|
||||
func (PermissionDaoImpl) CreatePermissions(permissions []model.ScaAuthPermission) error {
|
||||
if err := global.DB.Model(&model.ScaAuthPermission{}).CreateInBatches(&permissions, len(permissions)).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
25
dao/impl/role_dao_impl.go
Normal file
25
dao/impl/role_dao_impl.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package impl
|
||||
|
||||
import (
|
||||
"schisandra-cloud-album/global"
|
||||
"schisandra-cloud-album/model"
|
||||
)
|
||||
|
||||
type RoleDaoImpl struct{}
|
||||
|
||||
// GetRoleListByIds : 通过Id列表获取角色信息列表
|
||||
func (RoleDaoImpl) GetRoleListByIds(id []*int64) ([]model.ScaAuthRole, error) {
|
||||
var roles []model.ScaAuthRole
|
||||
if err := global.DB.Where("id IN ?", id).Find(&roles).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return roles, nil
|
||||
}
|
||||
|
||||
// AddRole 新增角色
|
||||
func (RoleDaoImpl) AddRole(role model.ScaAuthRole) error {
|
||||
if err := global.DB.Create(&role).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
83
dao/impl/user_dao_impl.go
Normal file
83
dao/impl/user_dao_impl.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package impl
|
||||
|
||||
import (
|
||||
"schisandra-cloud-album/common/enum"
|
||||
"schisandra-cloud-album/global"
|
||||
"schisandra-cloud-album/model"
|
||||
)
|
||||
|
||||
type UserDaoImpl struct {
|
||||
}
|
||||
|
||||
// GetUserList 获取所有用户列表
|
||||
func (UserDaoImpl) GetUserList() []*model.ScaAuthUser {
|
||||
data := make([]*model.ScaAuthUser, 0)
|
||||
global.DB.Where("deleted = 0 ").Find(&data)
|
||||
return data
|
||||
}
|
||||
|
||||
// QueryUserByUsername 根据用户名查询用户
|
||||
func (UserDaoImpl) QueryUserByUsername(username string) model.ScaAuthUser {
|
||||
authUser := model.ScaAuthUser{}
|
||||
err := global.DB.Where("username = ? and deleted = 0", username).First(&authUser).Error
|
||||
if err != nil {
|
||||
return model.ScaAuthUser{}
|
||||
}
|
||||
return authUser
|
||||
}
|
||||
|
||||
// QueryUserByUuid 根据用户uuid查询用户
|
||||
func (UserDaoImpl) QueryUserByUuid(uuid *string) (model.ScaAuthUser, error) {
|
||||
authUser := model.ScaAuthUser{}
|
||||
if err := global.DB.Where("uid = ? and deleted = 0", uuid).First(&authUser).Error; err != nil {
|
||||
return model.ScaAuthUser{}, err
|
||||
}
|
||||
return authUser, nil
|
||||
}
|
||||
|
||||
// QueryUserById 根据用户id查询用户
|
||||
func (UserDaoImpl) QueryUserById(id *int64) (model.ScaAuthUser, error) {
|
||||
authUser := model.ScaAuthUser{}
|
||||
if err := global.DB.Where("id = ? and deleted = 0", id).First(&authUser).Error; err != nil {
|
||||
return model.ScaAuthUser{}, err
|
||||
}
|
||||
return authUser, nil
|
||||
}
|
||||
|
||||
// AddUser 添加用户
|
||||
func (UserDaoImpl) AddUser(user model.ScaAuthUser) (*model.ScaAuthUser, error) {
|
||||
if err := global.DB.Create(&user).Error; err != nil {
|
||||
return &model.ScaAuthUser{}, err
|
||||
}
|
||||
// 查询创建后的用户信息
|
||||
var createdUser model.ScaAuthUser
|
||||
if err := global.DB.First(&createdUser, user.ID).Error; err != nil {
|
||||
return &model.ScaAuthUser{}, err
|
||||
}
|
||||
return &createdUser, nil
|
||||
}
|
||||
|
||||
// UpdateUser 更新用户
|
||||
func (UserDaoImpl) UpdateUser(phone string, password string) error {
|
||||
return global.DB.Model(&model.ScaAuthUser{}).Where("phone = ? and deleted = 0", phone).Updates(&model.ScaAuthUser{Password: &password}).Error
|
||||
}
|
||||
|
||||
// DeleteUser 删除用户
|
||||
func (UserDaoImpl) DeleteUser(uuid string) error {
|
||||
authUser := model.ScaAuthUser{}
|
||||
return global.DB.Model(&authUser).Where("uid = ?", uuid).Updates(&model.ScaAuthUser{Deleted: &enum.DELETED}).Error
|
||||
}
|
||||
|
||||
// QueryUserByPhone 根据手机号查询用户
|
||||
func (UserDaoImpl) QueryUserByPhone(phone string) model.ScaAuthUser {
|
||||
authUser := model.ScaAuthUser{}
|
||||
global.DB.Where("phone = ? and deleted = 0", phone).First(&authUser)
|
||||
return authUser
|
||||
}
|
||||
|
||||
// QueryUserByEmail 根据邮箱查询用户
|
||||
func (UserDaoImpl) QueryUserByEmail(email string) model.ScaAuthUser {
|
||||
authUser := model.ScaAuthUser{}
|
||||
global.DB.Where("email = ? and deleted = 0", email).First(&authUser)
|
||||
return authUser
|
||||
}
|
47
dao/impl/user_device_dao_impl.go
Normal file
47
dao/impl/user_device_dao_impl.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package impl
|
||||
|
||||
import (
|
||||
"schisandra-cloud-album/global"
|
||||
"schisandra-cloud-album/model"
|
||||
)
|
||||
|
||||
type UserDeviceImpl struct{}
|
||||
|
||||
// AddUserDevice 新增用户设备信息
|
||||
func (UserDeviceImpl) AddUserDevice(userDevice *model.ScaAuthUserDevice) error {
|
||||
if err := global.DB.Create(&userDevice).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetUserDeviceByUIDIPAgent 根据uid / IP / agent 查询用户设备信息
|
||||
func (UserDeviceImpl) GetUserDeviceByUIDIPAgent(uid, ip, agent string) (*model.ScaAuthUserDevice, error) {
|
||||
var userDevice model.ScaAuthUserDevice
|
||||
if err := global.DB.Where("user_id =? AND ip =? AND agent =? AND deleted = 0 ", uid, ip, agent).First(&userDevice).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &userDevice, nil
|
||||
}
|
||||
|
||||
// UpdateUserDevice 更新用户设备信息
|
||||
func (UserDeviceImpl) UpdateUserDevice(id int64, userDevice *model.ScaAuthUserDevice) error {
|
||||
result := global.DB.Model(&userDevice).Where("id =? AND deleted = 0 ", id).Updates(model.ScaAuthUserDevice{
|
||||
IP: userDevice.IP,
|
||||
Location: userDevice.Location,
|
||||
Agent: userDevice.Agent,
|
||||
Browser: userDevice.Browser,
|
||||
BrowserVersion: userDevice.BrowserVersion,
|
||||
OperatingSystem: userDevice.OperatingSystem,
|
||||
Mobile: userDevice.Mobile,
|
||||
Bot: userDevice.Bot,
|
||||
Mozilla: userDevice.Mozilla,
|
||||
Platform: userDevice.Platform,
|
||||
EngineName: userDevice.EngineName,
|
||||
EngineVersion: userDevice.EngineVersion,
|
||||
})
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
}
|
32
dao/impl/user_social_dao_impl.go
Normal file
32
dao/impl/user_social_dao_impl.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package impl
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"gorm.io/gorm"
|
||||
"schisandra-cloud-album/global"
|
||||
"schisandra-cloud-album/model"
|
||||
)
|
||||
|
||||
type UserSocialImpl struct{}
|
||||
|
||||
// AddUserSocial 添加社会化登录用户信息
|
||||
func (UserSocialImpl) AddUserSocial(user model.ScaAuthUserSocial) error {
|
||||
result := global.DB.Create(&user)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// QueryUserSocialByOpenID 根据openID和source查询用户信息
|
||||
func (UserSocialImpl) QueryUserSocialByOpenID(openID string, source string) (model.ScaAuthUserSocial, error) {
|
||||
var user model.ScaAuthUserSocial
|
||||
result := global.DB.Where("open_id = ? and source = ? and deleted = 0", openID, source).First(&user)
|
||||
if result.Error != nil {
|
||||
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||
return model.ScaAuthUserSocial{}, result.Error
|
||||
}
|
||||
return model.ScaAuthUserSocial{}, result.Error
|
||||
}
|
||||
return user, nil
|
||||
}
|
Reference in New Issue
Block a user