🎨 update project structure

This commit is contained in:
landaiqing
2024-09-29 15:46:35 +08:00
parent 2769467ce2
commit 87f1ff6961
85 changed files with 693 additions and 431 deletions

View File

@@ -1,3 +0,0 @@
package comment_likes_service
type CommentLikes struct{}

View File

@@ -1 +0,0 @@
package comment_likes_service

View File

@@ -0,0 +1,10 @@
package service
import "schisandra-cloud-album/model"
type CommentReplyService interface {
CreateCommentReplyService(comment *model.ScaCommentReply) error
UpdateCommentReplyCountService(replyId int64) error
UpdateCommentLikesCountService(commentId int64, topicId string) error
DecrementCommentLikesCountService(commentId int64, topicId string) error
}

View File

@@ -1,3 +0,0 @@
package comment_reply_service
type CommentReplyService struct{}

View File

@@ -1,90 +0,0 @@
package comment_reply_service
import (
"fmt"
"gorm.io/gorm"
"schisandra-cloud-album/global"
"schisandra-cloud-album/model"
)
// CreateCommentReply 创建评论
func (CommentReplyService) CreateCommentReply(comment *model.ScaCommentReply) error {
if err := global.DB.Create(&comment).Error; err != nil {
return err
}
return nil
}
// GetCommentListOrderByCreatedTimeDesc 通过topic_id获取评论列表
func (CommentReplyService) 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 (CommentReplyService) 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 (CommentReplyService) 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 (CommentReplyService) 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 (CommentReplyService) 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
}

View File

@@ -0,0 +1,32 @@
package impl
import (
"schisandra-cloud-album/dao/impl"
"schisandra-cloud-album/model"
)
var commentReplyDao = impl.CommentReplyDaoImpl{}
type CommentReplyServiceImpl struct{}
// CreateCommentReplyService 创建评论回复
func (CommentReplyServiceImpl) CreateCommentReplyService(comment *model.ScaCommentReply) error {
return commentReplyDao.CreateCommentReply(comment)
}
// UpdateCommentReplyCountService 更新评论回复数
func (CommentReplyServiceImpl) UpdateCommentReplyCountService(replyId int64) error {
return commentReplyDao.UpdateCommentReplyCount(replyId)
}
// UpdateCommentLikesCountService 更新评论点赞数
func (CommentReplyServiceImpl) UpdateCommentLikesCountService(commentId int64, topicId string) error {
return commentReplyDao.UpdateCommentLikesCount(commentId, topicId)
}
// DecrementCommentLikesCountService 减少评论点赞数
func (CommentReplyServiceImpl) DecrementCommentLikesCountService(commentId int64, topicId string) error {
return commentReplyDao.DecrementCommentLikesCount(commentId, topicId)
}

View File

@@ -0,0 +1,15 @@
package impl
import (
"schisandra-cloud-album/dao/impl"
"schisandra-cloud-album/model"
)
var permissionDao = impl.PermissionDaoImpl{}
type PermissionServiceImpl struct{}
// CreatePermissionsService 创建权限
func (PermissionServiceImpl) CreatePermissionsService(permissions []model.ScaAuthPermission) error {
return permissionDao.CreatePermissions(permissions)
}

View File

@@ -0,0 +1,16 @@
package impl
import (
"schisandra-cloud-album/dao/impl"
"schisandra-cloud-album/model"
)
var roleDao = impl.RoleDaoImpl{}
type RoleServiceImpl struct{}
// AddRoleService 添加角色
func (RoleServiceImpl) AddRoleService(role model.ScaAuthRole) error {
return roleDao.AddRole(role)
}

View File

@@ -0,0 +1,25 @@
package impl
import (
"schisandra-cloud-album/dao/impl"
"schisandra-cloud-album/model"
)
var userDeviceDao = impl.UserDeviceImpl{}
type UserDeviceServiceImpl struct{}
// GetUserDeviceByUIDIPAgentService 获取用户设备信息 根据用户ID、IP、User-Agent
func (UserDeviceServiceImpl) GetUserDeviceByUIDIPAgentService(userId, ip, userAgent string) (*model.ScaAuthUserDevice, error) {
return userDeviceDao.GetUserDeviceByUIDIPAgent(userId, ip, userAgent)
}
// AddUserDeviceService 新增用户设备信息
func (UserDeviceServiceImpl) AddUserDeviceService(userDevice *model.ScaAuthUserDevice) error {
return userDeviceDao.AddUserDevice(userDevice)
}
// UpdateUserDeviceService 更新用户设备信息
func (UserDeviceServiceImpl) UpdateUserDeviceService(userId int64, userDevice *model.ScaAuthUserDevice) error {
return userDeviceDao.UpdateUserDevice(userId, userDevice)
}

View File

@@ -0,0 +1,54 @@
package impl
import (
"schisandra-cloud-album/dao/impl"
"schisandra-cloud-album/model"
)
var userDao = impl.UserDaoImpl{}
type UserServiceImpl struct{}
// GetUserListService 返回用户列表
func (UserServiceImpl) GetUserListService() []*model.ScaAuthUser {
return userDao.GetUserList()
}
// QueryUserByUsernameService 根据用户名查询用户
func (UserServiceImpl) QueryUserByUsernameService(username string) model.ScaAuthUser {
return userDao.QueryUserByUsername(username)
}
// QueryUserByUuidService 根据uid查询用户
func (UserServiceImpl) QueryUserByUuidService(uid *string) model.ScaAuthUser {
user, err := userDao.QueryUserByUuid(uid)
if err != nil {
return model.ScaAuthUser{}
}
return user
}
// DeleteUserService 根据uid删除用户
func (UserServiceImpl) DeleteUserService(uid string) error {
return userDao.DeleteUser(uid)
}
// QueryUserByPhoneService 根据手机号查询用户
func (UserServiceImpl) QueryUserByPhoneService(phone string) model.ScaAuthUser {
return userDao.QueryUserByPhone(phone)
}
// QueryUserByEmailService 根据邮箱查询用户
func (UserServiceImpl) QueryUserByEmailService(email string) model.ScaAuthUser {
return userDao.QueryUserByEmail(email)
}
// AddUserService 新增用户
func (UserServiceImpl) AddUserService(user model.ScaAuthUser) (*model.ScaAuthUser, error) {
return userDao.AddUser(user)
}
// UpdateUserService 更新用户信息
func (UserServiceImpl) UpdateUserService(phone, encrypt string) error {
return userDao.UpdateUser(phone, encrypt)
}

View File

@@ -0,0 +1,20 @@
package impl
import (
"schisandra-cloud-album/dao/impl"
"schisandra-cloud-album/model"
)
var userSocialDao = impl.UserSocialImpl{}
type UserSocialServiceImpl struct{}
// QueryUserSocialByOpenIDService 查询用户第三方账号信息
func (UserSocialServiceImpl) QueryUserSocialByOpenIDService(openID string, source string) (model.ScaAuthUserSocial, error) {
return userSocialDao.QueryUserSocialByOpenID(openID, source)
}
// AddUserSocialService 新增用户第三方账号信息
func (UserSocialServiceImpl) AddUserSocialService(userSocial model.ScaAuthUserSocial) error {
return userSocialDao.AddUserSocial(userSocial)
}

View File

@@ -0,0 +1,8 @@
package service
import "schisandra-cloud-album/model"
type PermissionService interface {
// CreatePermissionsService 创建权限
CreatePermissionsService(permissions []model.ScaAuthPermission) error
}

View File

@@ -1,3 +0,0 @@
package permission_service
type PermissionService struct{}

View File

@@ -1,23 +0,0 @@
package permission_service
import (
"schisandra-cloud-album/global"
"schisandra-cloud-album/model"
)
// GetPermissionsByIds 通过权限ID列表获取权限列表
func (PermissionService) 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 (PermissionService) CreatePermissions(permissions []model.ScaAuthPermission) error {
if err := global.DB.Model(&model.ScaAuthPermission{}).CreateInBatches(&permissions, len(permissions)).Error; err != nil {
return err
}
return nil
}

8
service/role_service.go Normal file
View File

@@ -0,0 +1,8 @@
package service
import "schisandra-cloud-album/model"
type RoleService interface {
// AddRoleService 添加角色
AddRoleService(role model.ScaAuthRole) error
}

View File

@@ -1,3 +0,0 @@
package role_service
type RoleService struct{}

View File

@@ -1,23 +0,0 @@
package role_service
import (
"schisandra-cloud-album/global"
"schisandra-cloud-album/model"
)
// GetRoleListByIds : 通过Id列表获取角色信息列表
func (RoleService) 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 (RoleService) AddRole(role model.ScaAuthRole) error {
if err := global.DB.Create(&role).Error; err != nil {
return err
}
return nil
}

View File

@@ -1,23 +0,0 @@
package service
import (
"schisandra-cloud-album/service/comment_reply_service"
"schisandra-cloud-album/service/permission_service"
"schisandra-cloud-album/service/role_service"
"schisandra-cloud-album/service/user_device_service"
"schisandra-cloud-album/service/user_service"
"schisandra-cloud-album/service/user_social_service"
)
// Services 统一导出的service
type Services struct {
UserService user_service.UserService
RoleService role_service.RoleService
PermissionService permission_service.PermissionService
UserSocialService user_social_service.UserSocialService
UserDeviceService user_device_service.UserDeviceService
CommentReplyService comment_reply_service.CommentReplyService
}
// Service new函数实例化实例化完成后会返回结构体地指针类型
var Service = new(Services)

View File

@@ -0,0 +1,9 @@
package service
import "schisandra-cloud-album/model"
type UserDeviceService interface {
GetUserDeviceByUIDIPAgentService(userId, ip, userAgent string) (*model.ScaAuthUserDevice, error)
AddUserDeviceService(userDevice *model.ScaAuthUserDevice) error
UpdateUserDeviceService(userId int64, userDevice *model.ScaAuthUserDevice) error
}

View File

@@ -1,3 +0,0 @@
package user_device_service
type UserDeviceService struct{}

View File

@@ -1,45 +0,0 @@
package user_device_service
import (
"schisandra-cloud-album/global"
"schisandra-cloud-album/model"
)
// AddUserDevice 新增用户设备信息
func (UserDeviceService) AddUserDevice(userDevice *model.ScaAuthUserDevice) error {
if err := global.DB.Create(&userDevice).Error; err != nil {
return err
}
return nil
}
// GetUserDeviceByUIDIPAgent 根据uid / IP / agent 查询用户设备信息
func (UserDeviceService) 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 (UserDeviceService) 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
}

22
service/user_service.go Normal file
View File

@@ -0,0 +1,22 @@
package service
import "schisandra-cloud-album/model"
type UserService interface {
// GetUserListService 返回用户列表
GetUserListService() []*model.ScaAuthUser
// QueryUserByUsernameService 根据用户名查询用户
QueryUserByUsernameService(username string) model.ScaAuthUser
// QueryUserByUuidService 根据用户ID查询用户
QueryUserByUuidService(uid *string) model.ScaAuthUser
// DeleteUserService 根据用户ID删除用户
DeleteUserService(uid string) error
// QueryUserByPhoneService 根据手机号查询用户
QueryUserByPhoneService(phone string) model.ScaAuthUser
// QueryUserByEmailService 根据邮箱查询用户
QueryUserByEmailService(email string) model.ScaAuthUser
// AddUserService 新增用户
AddUserService(user model.ScaAuthUser) (*model.ScaAuthUser, error)
// UpdateUserService 更新用户信息
UpdateUserService(phone, encrypt string) error
}

View File

@@ -1,3 +0,0 @@
package user_service
type UserService struct{}

View File

@@ -1,80 +0,0 @@
package user_service
import (
"schisandra-cloud-album/common/enum"
"schisandra-cloud-album/global"
"schisandra-cloud-album/model"
)
// GetUserList 获取所有用户列表
func (UserService) GetUserList() []*model.ScaAuthUser {
data := make([]*model.ScaAuthUser, 0)
global.DB.Where("deleted = 0 ").Find(&data)
return data
}
// QueryUserByUsername 根据用户名查询用户
func (UserService) 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 (UserService) 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 (UserService) 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 (UserService) 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 (UserService) 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 (UserService) DeleteUser(uuid string) error {
authUser := model.ScaAuthUser{}
return global.DB.Model(&authUser).Where("uid = ?", uuid).Updates(&model.ScaAuthUser{Deleted: &enum.DELETED}).Error
}
// QueryUserByPhone 根据手机号查询用户
func (UserService) QueryUserByPhone(phone string) model.ScaAuthUser {
authUser := model.ScaAuthUser{}
global.DB.Where("phone = ? and deleted = 0", phone).First(&authUser)
return authUser
}
// QueryUserByEmail 根据邮箱查询用户
func (UserService) QueryUserByEmail(email string) model.ScaAuthUser {
authUser := model.ScaAuthUser{}
global.DB.Where("email = ? and deleted = 0", email).First(&authUser)
return authUser
}

View File

@@ -0,0 +1,8 @@
package service
import "schisandra-cloud-album/model"
type UserSocialService interface {
QueryUserSocialByOpenIDService(openID string, source string) (model.ScaAuthUserSocial, error)
AddUserSocialService(userSocial model.ScaAuthUserSocial) error
}

View File

@@ -1,3 +0,0 @@
package user_social_service
type UserSocialService struct{}

View File

@@ -1,44 +0,0 @@
package user_social_service
import (
"errors"
"gorm.io/gorm"
"schisandra-cloud-album/global"
"schisandra-cloud-album/model"
)
// AddUserSocial 添加社会化登录用户信息
func (UserSocialService) AddUserSocial(user model.ScaAuthUserSocial) error {
result := global.DB.Create(&user)
if result.Error != nil {
return result.Error
}
return nil
}
// QueryUserSocialByOpenID 根据openID和source查询用户信息
func (UserSocialService) 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
}
// QueryUserSocialByUUID 根据uuid和source查询用户信息
func (UserSocialService) QueryUserSocialByUUID(openID string, source string) (model.ScaAuthUserSocial, error) {
var user model.ScaAuthUserSocial
result := global.DB.Where("uuid = ? 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
}