mirror of
https://github.com/snowykami/neo-blog.git
synced 2025-09-26 02:56:22 +00:00
All checks were successful
Push to Helm Chart Repository / build (push) Successful in 9s
fix: Update Gravatar URL size and improve avatar rendering logic style: Adjust footer margin for better layout consistency refactor: Remove old navbar component and integrate new layout structure feat: Enhance user profile page with user header component chore: Remove unused user profile component fix: Update posts per page configuration for better pagination feat: Extend device context to support system theme mode refactor: Remove unused device hook fix: Improve storage state hook for better error handling i18n: Add new translations for blog home page feat: Implement pagination component for better navigation feat: Create theme toggle component for improved user experience feat: Introduce responsive navbar or side layout with theme toggle feat: Develop custom select component for better UI consistency feat: Create user header component to display user information chore: Add query key constants for better code maintainability
155 lines
4.2 KiB
Go
155 lines
4.2 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
|
|
"github.com/snowykami/neo-blog/internal/ctxutils"
|
|
"github.com/snowykami/neo-blog/internal/dto"
|
|
"github.com/snowykami/neo-blog/internal/model"
|
|
"github.com/snowykami/neo-blog/internal/repo"
|
|
"github.com/snowykami/neo-blog/pkg/errs"
|
|
)
|
|
|
|
type PostService struct{}
|
|
|
|
func NewPostService() *PostService {
|
|
return &PostService{}
|
|
}
|
|
|
|
func (p *PostService) CreatePost(ctx context.Context, req *dto.CreateOrUpdatePostReq) (uint, error) {
|
|
currentUser, ok := ctxutils.GetCurrentUser(ctx)
|
|
if !ok {
|
|
return 0, errs.ErrUnauthorized
|
|
}
|
|
post := &model.Post{
|
|
Title: req.Title,
|
|
Content: req.Content,
|
|
UserID: currentUser.ID,
|
|
Labels: func() []model.Label {
|
|
labelModels := make([]model.Label, 0)
|
|
for _, labelID := range req.Labels {
|
|
labelModel, err := repo.Label.GetLabelByID(strconv.Itoa(int(labelID)))
|
|
if err == nil {
|
|
labelModels = append(labelModels, *labelModel)
|
|
}
|
|
}
|
|
return labelModels
|
|
}(),
|
|
IsPrivate: req.IsPrivate,
|
|
}
|
|
if err := repo.Post.CreatePost(post); err != nil {
|
|
return 0, err
|
|
}
|
|
return post.ID, nil
|
|
}
|
|
|
|
func (p *PostService) DeletePost(ctx context.Context, id string) error {
|
|
currentUser, ok := ctxutils.GetCurrentUser(ctx)
|
|
if !ok {
|
|
return errs.ErrUnauthorized
|
|
}
|
|
if id == "" {
|
|
return errs.ErrBadRequest
|
|
}
|
|
post, err := repo.Post.GetPostByID(id)
|
|
if err != nil {
|
|
return errs.New(errs.ErrNotFound.Code, "post not found", err)
|
|
}
|
|
if post.UserID != currentUser.ID {
|
|
return errs.ErrForbidden
|
|
}
|
|
if err := repo.Post.DeletePost(id); err != nil {
|
|
return errs.ErrInternalServer
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (p *PostService) GetPost(ctx context.Context, id string) (*dto.PostDto, error) {
|
|
if id == "" {
|
|
return nil, errs.ErrBadRequest
|
|
}
|
|
post, err := repo.Post.GetPostByID(id)
|
|
if err != nil {
|
|
return nil, errs.New(errs.ErrNotFound.Code, "post not found", err)
|
|
}
|
|
currentUser, ok := ctxutils.GetCurrentUser(ctx)
|
|
if post.IsPrivate && (!ok || post.UserID != currentUser.ID) {
|
|
return nil, errs.ErrForbidden
|
|
}
|
|
return post.ToDto(), nil
|
|
}
|
|
|
|
func (p *PostService) UpdatePost(ctx context.Context, id string, req *dto.CreateOrUpdatePostReq) (uint, error) {
|
|
currentUser, ok := ctxutils.GetCurrentUser(ctx)
|
|
if !ok {
|
|
return 0, errs.ErrUnauthorized
|
|
}
|
|
if id == "" {
|
|
return 0, errs.ErrBadRequest
|
|
}
|
|
post, err := repo.Post.GetPostByID(id)
|
|
if err != nil {
|
|
return 0, errs.New(errs.ErrNotFound.Code, "post not found", err)
|
|
}
|
|
if post.UserID != currentUser.ID {
|
|
return 0, errs.ErrForbidden
|
|
}
|
|
post.Title = req.Title
|
|
post.Content = req.Content
|
|
post.IsPrivate = req.IsPrivate
|
|
post.Labels = func() []model.Label {
|
|
labelModels := make([]model.Label, len(req.Labels))
|
|
for _, labelID := range req.Labels {
|
|
labelModel, err := repo.Label.GetLabelByID(strconv.Itoa(int(labelID)))
|
|
if err == nil {
|
|
labelModels = append(labelModels, *labelModel)
|
|
}
|
|
}
|
|
return labelModels
|
|
}()
|
|
if err := repo.Post.UpdatePost(post); err != nil {
|
|
return 0, errs.ErrInternalServer
|
|
}
|
|
return post.ID, nil
|
|
}
|
|
|
|
func (p *PostService) ListPosts(ctx context.Context, req *dto.ListPostReq) ([]*dto.PostDto, int64, error) {
|
|
postDtos := make([]*dto.PostDto, 0)
|
|
currentUserID, _ := ctxutils.GetCurrentUserID(ctx)
|
|
posts, total, err := repo.Post.ListPosts(currentUserID, req.Keywords, req.Labels, req.LabelRule, req.Page, req.Size, req.OrderBy, req.Desc)
|
|
if err != nil {
|
|
return nil, total, errs.New(errs.ErrInternalServer.Code, "failed to list posts", err)
|
|
}
|
|
for _, post := range posts {
|
|
postDtos = append(postDtos, post.ToDtoWithShortContent(100))
|
|
}
|
|
return postDtos, total, nil
|
|
}
|
|
|
|
func (p *PostService) ToggleLikePost(ctx context.Context, id string) (bool, error) {
|
|
currentUser, ok := ctxutils.GetCurrentUser(ctx)
|
|
if !ok {
|
|
return false, errs.ErrUnauthorized
|
|
}
|
|
if id == "" {
|
|
return false, errs.ErrBadRequest
|
|
}
|
|
post, err := repo.Post.GetPostByID(id)
|
|
if err != nil {
|
|
return false, errs.New(errs.ErrNotFound.Code, "post not found", err)
|
|
}
|
|
if post.UserID == currentUser.ID {
|
|
return false, errs.ErrForbidden
|
|
}
|
|
idInt, err := strconv.ParseUint(id, 10, 64)
|
|
if err != nil {
|
|
return false, errs.New(errs.ErrBadRequest.Code, "invalid post ID", err)
|
|
}
|
|
liked, err := repo.Post.ToggleLikePost(uint(idInt), currentUser.ID)
|
|
if err != nil {
|
|
return false, errs.ErrInternalServer
|
|
}
|
|
return liked, nil
|
|
}
|