bbs-backend/logic/domainservice/post_domain_service.go
2024-11-19 21:08:28 +08:00

48 lines
895 B
Go

package domainservice
import (
"bbs-backend/common/errcode"
"bbs-backend/dal/dao"
"bbs-backend/dal/model"
"errors"
"gorm.io/gorm"
)
func CreatePostDomainService(post *model.Post) error {
// 验证帖子内容
if post.Title == "" {
return errcode.ErrInvalidTitle
}
if post.Content == "" {
return errcode.ErrInvalidContent
}
err := dao.CreatePost(post)
if err != nil {
return errcode.ErrInternalServerError
}
return nil
}
func GetPostDomainService(postID uint) (*model.Post, error) {
post, err := dao.GetPostByID(postID)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, errcode.ErrPostNotFound
}
return nil, errcode.ErrInternalServerError
}
return post, nil
}
func GetPostsDomainService() ([]*model.Post, error) {
posts, err := dao.GetAllPosts()
if err != nil {
return nil, errcode.ErrInternalServerError
}
return posts, nil
}