mirror of
https://github.com/snowykami/neo-blog.git
synced 2025-09-03 15:56:22 +00:00
⚡️ feat: update global styles and color variables for improved theming
refactor: change import paths for DeviceContext and GravatarAvatar components fix: adjust login form API call and update UI text for clarity feat: add post API for listing posts with pagination and filtering options feat: implement BlogCard component for displaying blog posts with enhanced UI feat: create Badge component for consistent styling of labels and indicators refactor: reintroduce DeviceContext with improved functionality for theme and language management feat: define Label and Post models for better type safety and structure
This commit is contained in:
@ -3,28 +3,105 @@ package v1
|
||||
import (
|
||||
"context"
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
"github.com/cloudwego/hertz/pkg/common/utils"
|
||||
"github.com/snowykami/neo-blog/internal/dto"
|
||||
"github.com/snowykami/neo-blog/internal/service"
|
||||
"github.com/snowykami/neo-blog/pkg/errs"
|
||||
"github.com/snowykami/neo-blog/pkg/resps"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type labelType struct{}
|
||||
|
||||
var Label = new(labelType)
|
||||
|
||||
func (l *labelType) Create(ctx context.Context, c *app.RequestContext) {
|
||||
// TODO: Impl
|
||||
type LabelController struct {
|
||||
service *service.LabelService
|
||||
}
|
||||
|
||||
func (l *labelType) Delete(ctx context.Context, c *app.RequestContext) {
|
||||
// TODO: Impl
|
||||
func NewLabelController() *LabelController {
|
||||
return &LabelController{
|
||||
service: service.NewLabelService(),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *labelType) Get(ctx context.Context, c *app.RequestContext) {
|
||||
// TODO: Impl
|
||||
func (l *LabelController) Create(ctx context.Context, c *app.RequestContext) {
|
||||
var req dto.LabelDto
|
||||
if err := c.BindAndValidate(&req); err != nil {
|
||||
resps.BadRequest(c, resps.ErrParamInvalid)
|
||||
return
|
||||
}
|
||||
labelID, err := l.service.CreateLabel(&req)
|
||||
if err != nil {
|
||||
serviceErr := errs.AsServiceError(err)
|
||||
resps.Custom(c, serviceErr.Code, serviceErr.Message, nil)
|
||||
return
|
||||
}
|
||||
resps.Ok(c, resps.Success, utils.H{"id": labelID})
|
||||
}
|
||||
|
||||
func (l *labelType) Update(ctx context.Context, c *app.RequestContext) {
|
||||
// TODO: Impl
|
||||
func (l *LabelController) Delete(ctx context.Context, c *app.RequestContext) {
|
||||
id := c.Param("id")
|
||||
if id == "" {
|
||||
resps.BadRequest(c, resps.ErrParamInvalid)
|
||||
return
|
||||
}
|
||||
err := l.service.DeleteLabel(id)
|
||||
if err != nil {
|
||||
serviceErr := errs.AsServiceError(err)
|
||||
resps.Custom(c, serviceErr.Code, serviceErr.Message, nil)
|
||||
return
|
||||
}
|
||||
resps.Ok(c, resps.Success, nil)
|
||||
}
|
||||
|
||||
func (l *labelType) List(ctx context.Context, c *app.RequestContext) {
|
||||
// TODO: Impl
|
||||
func (l *LabelController) Get(ctx context.Context, c *app.RequestContext) {
|
||||
id := c.Param("id")
|
||||
if id == "" {
|
||||
resps.BadRequest(c, resps.ErrParamInvalid)
|
||||
return
|
||||
}
|
||||
label, err := l.service.GetLabelByID(id)
|
||||
if err != nil {
|
||||
serviceErr := errs.AsServiceError(err)
|
||||
resps.Custom(c, serviceErr.Code, serviceErr.Message, nil)
|
||||
return
|
||||
}
|
||||
if label == nil {
|
||||
resps.NotFound(c, resps.ErrNotFound)
|
||||
return
|
||||
}
|
||||
resps.Ok(c, resps.Success, label)
|
||||
}
|
||||
|
||||
func (l *LabelController) Update(ctx context.Context, c *app.RequestContext) {
|
||||
var req dto.LabelDto
|
||||
if err := c.BindAndValidate(&req); err != nil {
|
||||
resps.BadRequest(c, resps.ErrParamInvalid)
|
||||
return
|
||||
}
|
||||
id := c.Param("id")
|
||||
if id == "" {
|
||||
resps.BadRequest(c, resps.ErrParamInvalid)
|
||||
return
|
||||
}
|
||||
idInt, err := strconv.Atoi(id)
|
||||
if err != nil {
|
||||
resps.BadRequest(c, "Invalid label ID")
|
||||
return
|
||||
}
|
||||
req.ID = uint(idInt)
|
||||
labelID, err := l.service.UpdateLabel(&req)
|
||||
if err != nil {
|
||||
serviceErr := errs.AsServiceError(err)
|
||||
resps.Custom(c, serviceErr.Code, serviceErr.Message, nil)
|
||||
return
|
||||
}
|
||||
resps.Ok(c, resps.Success, utils.H{"id": labelID})
|
||||
}
|
||||
|
||||
func (l *LabelController) List(ctx context.Context, c *app.RequestContext) {
|
||||
labels, err := l.service.ListLabels()
|
||||
if err != nil {
|
||||
serviceErr := errs.AsServiceError(err)
|
||||
resps.Custom(c, serviceErr.Code, serviceErr.Message, nil)
|
||||
return
|
||||
}
|
||||
resps.Ok(c, resps.Success, labels)
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package v1
|
||||
import (
|
||||
"context"
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
"github.com/cloudwego/hertz/pkg/common/utils"
|
||||
"github.com/snowykami/neo-blog/internal/ctxutils"
|
||||
"github.com/snowykami/neo-blog/internal/dto"
|
||||
"github.com/snowykami/neo-blog/internal/service"
|
||||
@ -28,12 +29,13 @@ func (p *PostController) Create(ctx context.Context, c *app.RequestContext) {
|
||||
if err := c.BindAndValidate(&req); err != nil {
|
||||
resps.BadRequest(c, resps.ErrParamInvalid)
|
||||
}
|
||||
if err := p.service.CreatePost(ctx, &req); err != nil {
|
||||
postID, err := p.service.CreatePost(ctx, &req)
|
||||
if err != nil {
|
||||
serviceErr := errs.AsServiceError(err)
|
||||
resps.Custom(c, serviceErr.Code, serviceErr.Message, nil)
|
||||
return
|
||||
}
|
||||
resps.Ok(c, resps.Success, nil)
|
||||
resps.Ok(c, resps.Success, utils.H{"id": postID})
|
||||
}
|
||||
|
||||
func (p *PostController) Delete(ctx context.Context, c *app.RequestContext) {
|
||||
@ -80,16 +82,20 @@ func (p *PostController) Update(ctx context.Context, c *app.RequestContext) {
|
||||
resps.BadRequest(c, resps.ErrParamInvalid)
|
||||
return
|
||||
}
|
||||
if err := p.service.UpdatePost(ctx, id, &req); err != nil {
|
||||
postID, err := p.service.UpdatePost(ctx, id, &req)
|
||||
if err != nil {
|
||||
serviceErr := errs.AsServiceError(err)
|
||||
resps.Custom(c, serviceErr.Code, serviceErr.Message, nil)
|
||||
return
|
||||
}
|
||||
resps.Ok(c, resps.Success, nil)
|
||||
resps.Ok(c, resps.Success, utils.H{"id": postID})
|
||||
}
|
||||
|
||||
func (p *PostController) List(ctx context.Context, c *app.RequestContext) {
|
||||
pagination := ctxutils.GetPaginationParams(c)
|
||||
if pagination.OrderedBy == "" {
|
||||
pagination.OrderedBy = constant.OrderedByUpdatedAt
|
||||
}
|
||||
if pagination.OrderedBy != "" && !slices.Contains(constant.OrderedByEnumPost, pagination.OrderedBy) {
|
||||
resps.BadRequest(c, "无效的排序字段")
|
||||
return
|
||||
@ -103,11 +109,11 @@ func (p *PostController) List(ctx context.Context, c *app.RequestContext) {
|
||||
OrderedBy: pagination.OrderedBy,
|
||||
Reverse: pagination.Reverse,
|
||||
}
|
||||
resp, err := p.service.ListPosts(ctx, req)
|
||||
posts, err := p.service.ListPosts(ctx, req)
|
||||
if err != nil {
|
||||
serviceErr := errs.AsServiceError(err)
|
||||
resps.Custom(c, serviceErr.Code, serviceErr.Message, nil)
|
||||
return
|
||||
}
|
||||
resps.Ok(c, resps.Success, resp)
|
||||
resps.Ok(c, resps.Success, posts)
|
||||
}
|
||||
|
@ -70,15 +70,13 @@ func (u *UserController) Logout(ctx context.Context, c *app.RequestContext) {
|
||||
}
|
||||
|
||||
func (u *UserController) OidcList(ctx context.Context, c *app.RequestContext) {
|
||||
resp, err := u.service.ListOidcConfigs()
|
||||
oidcConfigs, err := u.service.ListOidcConfigs()
|
||||
if err != nil {
|
||||
serviceErr := errs.AsServiceError(err)
|
||||
resps.Custom(c, serviceErr.Code, serviceErr.Message, nil)
|
||||
return
|
||||
}
|
||||
resps.Ok(c, resps.Success, map[string]any{
|
||||
"oidc_configs": resp.OidcConfigs,
|
||||
})
|
||||
resps.Ok(c, resps.Success, oidcConfigs)
|
||||
}
|
||||
|
||||
func (u *UserController) OidcLogin(ctx context.Context, c *app.RequestContext) {
|
||||
@ -109,7 +107,12 @@ func (u *UserController) GetUser(ctx context.Context, c *app.RequestContext) {
|
||||
userID := c.Param("id")
|
||||
userIDInt, err := strconv.Atoi(userID)
|
||||
if err != nil || userIDInt <= 0 {
|
||||
userIDInt = int(ctxutils.GetCurrentUserID(ctx))
|
||||
currentUserID, ok := ctxutils.GetCurrentUserID(ctx)
|
||||
if !ok {
|
||||
resps.Unauthorized(c, resps.ErrUnauthorized)
|
||||
return
|
||||
}
|
||||
userIDInt = int(currentUserID)
|
||||
}
|
||||
|
||||
resp, err := u.service.GetUser(&dto.GetUserReq{UserID: uint(userIDInt)})
|
||||
@ -138,8 +141,8 @@ func (u *UserController) UpdateUser(ctx context.Context, c *app.RequestContext)
|
||||
return
|
||||
}
|
||||
updateUserReq.ID = uint(userIDInt)
|
||||
currentUser := ctxutils.GetCurrentUser(ctx)
|
||||
if currentUser == nil {
|
||||
currentUser, ok := ctxutils.GetCurrentUser(ctx)
|
||||
if !ok {
|
||||
resps.Unauthorized(c, resps.ErrUnauthorized)
|
||||
return
|
||||
}
|
||||
|
@ -4,25 +4,29 @@ import (
|
||||
"context"
|
||||
"github.com/snowykami/neo-blog/internal/model"
|
||||
"github.com/snowykami/neo-blog/internal/repo"
|
||||
"github.com/snowykami/neo-blog/pkg/constant"
|
||||
)
|
||||
|
||||
func GetCurrentUser(ctx context.Context) *model.User {
|
||||
userIDValue := ctx.Value("user_id").(uint)
|
||||
if userIDValue <= 0 {
|
||||
return nil
|
||||
// GetCurrentUser 从上下文中获取当前用户
|
||||
func GetCurrentUser(ctx context.Context) (*model.User, bool) {
|
||||
val := ctx.Value(constant.ContextKeyUserID)
|
||||
if val == nil {
|
||||
return nil, false
|
||||
}
|
||||
user, err := repo.User.GetUserByID(userIDValue)
|
||||
if err != nil || user == nil || user.ID == 0 {
|
||||
return nil
|
||||
user, err := repo.User.GetUserByID(val.(uint))
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
return user
|
||||
|
||||
return user, true
|
||||
}
|
||||
|
||||
// GetCurrentUserID 获取当前用户ID,如果未认证则返回0
|
||||
func GetCurrentUserID(ctx context.Context) uint {
|
||||
user := GetCurrentUser(ctx)
|
||||
if user == nil {
|
||||
return 0
|
||||
// GetCurrentUserID 从上下文中获取当前用户ID
|
||||
func GetCurrentUserID(ctx context.Context) (uint, bool) {
|
||||
user, ok := GetCurrentUser(ctx)
|
||||
if !ok || user == nil {
|
||||
return 0, false
|
||||
}
|
||||
return user.ID
|
||||
|
||||
return user.ID, true
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package dto
|
||||
|
||||
type LabelDto struct {
|
||||
ID uint `json:"id"` // 标签ID
|
||||
Key string `json:"key"`
|
||||
Value string `json:"value"`
|
||||
Color string `json:"color"`
|
||||
|
@ -1,22 +1,30 @@
|
||||
package dto
|
||||
|
||||
import "time"
|
||||
|
||||
type PostDto struct {
|
||||
ID uint `json:"id"` // 帖子ID
|
||||
UserID uint `json:"user_id"` // 发布者的用户ID
|
||||
Title string `json:"title"` // 帖子标题
|
||||
Content string `json:"content"`
|
||||
Cover string `json:"cover"` // 帖子封面图
|
||||
Type string `json:"type"` // 帖子类型 markdown / html / text
|
||||
Labels []LabelDto `json:"labels"` // 关联的标签
|
||||
IsPrivate bool `json:"is_private"` // 是否为私密帖子
|
||||
LikeCount uint64 `json:"like_count"` // 点赞数
|
||||
CommentCount uint64 `json:"comment_count"` // 评论数
|
||||
ViewCount uint64 `json:"view_count"` // 浏览数
|
||||
Heat uint64 `json:"heat"` // 热度
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"` // 更新时间
|
||||
}
|
||||
|
||||
type CreateOrUpdatePostReq struct {
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
Cover string `json:"cover"`
|
||||
IsPrivate bool `json:"is_private"`
|
||||
Type string `json:"type"`
|
||||
Labels []uint `json:"labels"` // 标签ID列表
|
||||
}
|
||||
|
||||
|
@ -20,7 +20,7 @@ func UseAuth(block bool) app.HandlerFunc {
|
||||
// 尝试用普通 token 认证
|
||||
tokenClaims, err := utils.Jwt.ParseJsonWebTokenWithoutState(token)
|
||||
if err == nil && tokenClaims != nil {
|
||||
ctx = context.WithValue(ctx, "user_id", tokenClaims.UserID)
|
||||
ctx = context.WithValue(ctx, constant.ContextKeyUserID, tokenClaims.UserID)
|
||||
c.Next(ctx)
|
||||
return
|
||||
}
|
||||
@ -30,8 +30,7 @@ func UseAuth(block bool) app.HandlerFunc {
|
||||
if err == nil && refreshTokenClaims != nil {
|
||||
ok, err := isStatefulJwtValid(refreshTokenClaims)
|
||||
if err == nil && ok {
|
||||
ctx = context.WithValue(ctx, "user_id", refreshTokenClaims.UserID)
|
||||
|
||||
ctx = context.WithValue(ctx, constant.ContextKeyUserID, refreshTokenClaims.UserID)
|
||||
// 生成新 token
|
||||
newTokenClaims := utils.Jwt.NewClaims(
|
||||
refreshTokenClaims.UserID,
|
||||
|
@ -7,7 +7,7 @@ import (
|
||||
|
||||
type Label struct {
|
||||
gorm.Model
|
||||
Key string `gorm:"uniqueIndex"` // 标签键,唯一标识
|
||||
Key string `gorm:"index"` // 标签键,唯一标识
|
||||
Value string `gorm:"type:text"` // 标签值,描述标签的内容
|
||||
Color string `gorm:"type:text"` // 前端可用颜色代码
|
||||
TailwindClassName string `gorm:"type:text"` // Tailwind CSS 的类名,用于前端样式
|
||||
|
@ -11,7 +11,9 @@ type Post struct {
|
||||
UserID uint `gorm:"index"` // 发布者的用户ID
|
||||
User User `gorm:"foreignKey:UserID;references:ID"` // 关联的用户
|
||||
Title string `gorm:"type:text;not null"` // 帖子标题
|
||||
Cover string `gorm:"type:text"` // 帖子封面图
|
||||
Content string `gorm:"type:text;not null"` // 帖子内容
|
||||
Type string `gorm:"type:text;default:markdown"` // markdown类型,支持markdown或html
|
||||
CategoryID uint `gorm:"index"` // 帖子分类ID
|
||||
Category Category `gorm:"foreignKey:CategoryID;references:ID"` // 关联的分类
|
||||
Labels []Label `gorm:"many2many:post_labels;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"` // 关联的标签
|
||||
@ -48,10 +50,14 @@ func (p *Post) ToDto() dto.PostDto {
|
||||
UserID: p.UserID,
|
||||
Title: p.Title,
|
||||
Content: p.Content,
|
||||
Cover: p.Cover,
|
||||
Type: p.Type,
|
||||
IsPrivate: p.IsPrivate,
|
||||
LikeCount: p.LikeCount,
|
||||
CommentCount: p.CommentCount,
|
||||
ViewCount: p.ViewCount,
|
||||
Heat: p.Heat,
|
||||
CreatedAt: p.CreatedAt,
|
||||
UpdatedAt: p.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
@ -6,13 +6,7 @@ type labelRepo struct{}
|
||||
|
||||
var Label = &labelRepo{}
|
||||
|
||||
func (l *labelRepo) CreateLabel(key, value, color, tailwindClassName string) error {
|
||||
label := &model.Label{
|
||||
Key: key,
|
||||
Value: value,
|
||||
Color: color,
|
||||
TailwindClassName: tailwindClassName,
|
||||
}
|
||||
func (l *labelRepo) CreateLabel(label *model.Label) error {
|
||||
return GetDB().Create(label).Error
|
||||
}
|
||||
|
||||
@ -24,7 +18,7 @@ func (l *labelRepo) GetLabelByKey(key string) (*model.Label, error) {
|
||||
return &label, nil
|
||||
}
|
||||
|
||||
func (l *labelRepo) GetLabelByID(id uint) (*model.Label, error) {
|
||||
func (l *labelRepo) GetLabelByID(id string) (*model.Label, error) {
|
||||
var label model.Label
|
||||
if err := GetDB().Where("id = ?", id).First(&label).Error; err != nil {
|
||||
return nil, err
|
||||
@ -47,7 +41,7 @@ func (l *labelRepo) UpdateLabel(label *model.Label) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *labelRepo) DeleteLabel(id uint) error {
|
||||
func (l *labelRepo) DeleteLabel(id string) error {
|
||||
if err := GetDB().Where("id = ?", id).Delete(&model.Label{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -7,14 +7,15 @@ import (
|
||||
)
|
||||
|
||||
func registerLabelRoutes(group *route.RouterGroup) {
|
||||
labelController := v1.NewLabelController()
|
||||
labelGroup := group.Group("/label").Use(middleware.UseAuth(true))
|
||||
labelGroupWithoutAuth := group.Group("/label").Use(middleware.UseAuth(false))
|
||||
{
|
||||
labelGroupWithoutAuth.GET("/l/:id", v1.Label.Get)
|
||||
labelGroupWithoutAuth.GET("/list", v1.Label.List)
|
||||
labelGroupWithoutAuth.GET("/l/:id", labelController.Get)
|
||||
labelGroupWithoutAuth.GET("/list", labelController.List)
|
||||
|
||||
labelGroup.POST("/l", v1.Label.Create)
|
||||
labelGroup.DELETE("/l/:id", v1.Label.Delete)
|
||||
labelGroup.PUT("/l/:id", v1.Label.Update)
|
||||
labelGroup.POST("/l", labelController.Create)
|
||||
labelGroup.DELETE("/l/:id", labelController.Delete)
|
||||
labelGroup.PUT("/l/:id", labelController.Update)
|
||||
}
|
||||
}
|
||||
|
@ -1 +1,85 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"github.com/snowykami/neo-blog/internal/dto"
|
||||
"github.com/snowykami/neo-blog/internal/model"
|
||||
"github.com/snowykami/neo-blog/internal/repo"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type LabelService struct{}
|
||||
|
||||
func NewLabelService() *LabelService {
|
||||
return &LabelService{}
|
||||
}
|
||||
|
||||
func (l *LabelService) CreateLabel(req *dto.LabelDto) (uint, error) {
|
||||
label := &model.Label{
|
||||
Key: req.Key,
|
||||
Value: req.Value,
|
||||
Color: req.Color,
|
||||
TailwindClassName: req.TailwindClassName,
|
||||
}
|
||||
return label.ID, repo.Label.CreateLabel(label)
|
||||
}
|
||||
|
||||
func (l *LabelService) UpdateLabel(req *dto.LabelDto) (uint, error) {
|
||||
label := &model.Label{
|
||||
Model: gorm.Model{ID: req.ID},
|
||||
Key: req.Key,
|
||||
Value: req.Value,
|
||||
Color: req.Color,
|
||||
TailwindClassName: req.TailwindClassName,
|
||||
}
|
||||
return label.ID, repo.Label.UpdateLabel(label)
|
||||
}
|
||||
|
||||
func (l *LabelService) DeleteLabel(id string) error {
|
||||
return repo.Label.DeleteLabel(id)
|
||||
}
|
||||
|
||||
func (l *LabelService) GetLabelByKey(key string) (*dto.LabelDto, error) {
|
||||
label, err := repo.Label.GetLabelByKey(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &dto.LabelDto{
|
||||
ID: label.ID,
|
||||
Key: label.Key,
|
||||
Value: label.Value,
|
||||
Color: label.Color,
|
||||
TailwindClassName: label.TailwindClassName,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (l *LabelService) GetLabelByID(id string) (*dto.LabelDto, error) {
|
||||
label, err := repo.Label.GetLabelByID(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &dto.LabelDto{
|
||||
ID: label.ID,
|
||||
Key: label.Key,
|
||||
Value: label.Value,
|
||||
Color: label.Color,
|
||||
TailwindClassName: label.TailwindClassName,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (l *LabelService) ListLabels() ([]dto.LabelDto, error) {
|
||||
labels, err := repo.Label.ListLabels()
|
||||
var labelDtos []dto.LabelDto
|
||||
if err != nil {
|
||||
return labelDtos, err
|
||||
}
|
||||
for _, label := range labels {
|
||||
labelDtos = append(labelDtos, dto.LabelDto{
|
||||
ID: label.ID,
|
||||
Key: label.Key,
|
||||
Value: label.Value,
|
||||
Color: label.Color,
|
||||
TailwindClassName: label.TailwindClassName,
|
||||
})
|
||||
}
|
||||
return labelDtos, nil
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
"github.com/snowykami/neo-blog/internal/model"
|
||||
"github.com/snowykami/neo-blog/internal/repo"
|
||||
"github.com/snowykami/neo-blog/pkg/errs"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type PostService struct{}
|
||||
@ -15,10 +16,10 @@ func NewPostService() *PostService {
|
||||
return &PostService{}
|
||||
}
|
||||
|
||||
func (p *PostService) CreatePost(ctx context.Context, req *dto.CreateOrUpdatePostReq) error {
|
||||
currentUser := ctxutils.GetCurrentUser(ctx)
|
||||
if currentUser == nil {
|
||||
return errs.ErrUnauthorized
|
||||
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,
|
||||
@ -27,7 +28,7 @@ func (p *PostService) CreatePost(ctx context.Context, req *dto.CreateOrUpdatePos
|
||||
Labels: func() []model.Label {
|
||||
labelModels := make([]model.Label, 0)
|
||||
for _, labelID := range req.Labels {
|
||||
labelModel, err := repo.Label.GetLabelByID(labelID)
|
||||
labelModel, err := repo.Label.GetLabelByID(strconv.Itoa(int(labelID)))
|
||||
if err == nil {
|
||||
labelModels = append(labelModels, *labelModel)
|
||||
}
|
||||
@ -37,14 +38,14 @@ func (p *PostService) CreatePost(ctx context.Context, req *dto.CreateOrUpdatePos
|
||||
IsPrivate: req.IsPrivate,
|
||||
}
|
||||
if err := repo.Post.CreatePost(post); err != nil {
|
||||
return err
|
||||
return 0, err
|
||||
}
|
||||
return nil
|
||||
return post.ID, nil
|
||||
}
|
||||
|
||||
func (p *PostService) DeletePost(ctx context.Context, id string) error {
|
||||
currentUser := ctxutils.GetCurrentUser(ctx)
|
||||
if currentUser == nil {
|
||||
currentUser, ok := ctxutils.GetCurrentUser(ctx)
|
||||
if !ok {
|
||||
return errs.ErrUnauthorized
|
||||
}
|
||||
if id == "" {
|
||||
@ -64,8 +65,8 @@ func (p *PostService) DeletePost(ctx context.Context, id string) error {
|
||||
}
|
||||
|
||||
func (p *PostService) GetPost(ctx context.Context, id string) (*dto.PostDto, error) {
|
||||
currentUser := ctxutils.GetCurrentUser(ctx)
|
||||
if currentUser == nil {
|
||||
currentUser, ok := ctxutils.GetCurrentUser(ctx)
|
||||
if !ok {
|
||||
return nil, errs.ErrUnauthorized
|
||||
}
|
||||
if id == "" {
|
||||
@ -92,20 +93,20 @@ func (p *PostService) GetPost(ctx context.Context, id string) (*dto.PostDto, err
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p *PostService) UpdatePost(ctx context.Context, id string, req *dto.CreateOrUpdatePostReq) error {
|
||||
currentUser := ctxutils.GetCurrentUser(ctx)
|
||||
if currentUser == nil {
|
||||
return errs.ErrUnauthorized
|
||||
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 errs.ErrBadRequest
|
||||
return 0, errs.ErrBadRequest
|
||||
}
|
||||
post, err := repo.Post.GetPostByID(id)
|
||||
if err != nil {
|
||||
return errs.New(errs.ErrNotFound.Code, "post not found", err)
|
||||
return 0, errs.New(errs.ErrNotFound.Code, "post not found", err)
|
||||
}
|
||||
if post.UserID != currentUser.ID {
|
||||
return errs.ErrForbidden
|
||||
return 0, errs.ErrForbidden
|
||||
}
|
||||
post.Title = req.Title
|
||||
post.Content = req.Content
|
||||
@ -113,7 +114,7 @@ func (p *PostService) UpdatePost(ctx context.Context, id string, req *dto.Create
|
||||
post.Labels = func() []model.Label {
|
||||
labelModels := make([]model.Label, len(req.Labels))
|
||||
for _, labelID := range req.Labels {
|
||||
labelModel, err := repo.Label.GetLabelByID(labelID)
|
||||
labelModel, err := repo.Label.GetLabelByID(strconv.Itoa(int(labelID)))
|
||||
if err == nil {
|
||||
labelModels = append(labelModels, *labelModel)
|
||||
}
|
||||
@ -121,14 +122,14 @@ func (p *PostService) UpdatePost(ctx context.Context, id string, req *dto.Create
|
||||
return labelModels
|
||||
}()
|
||||
if err := repo.Post.UpdatePost(post); err != nil {
|
||||
return errs.ErrInternalServer
|
||||
return 0, errs.ErrInternalServer
|
||||
}
|
||||
return nil
|
||||
return post.ID, nil
|
||||
}
|
||||
|
||||
func (p *PostService) ListPosts(ctx context.Context, req *dto.ListPostReq) (*dto.ListPostResp, error) {
|
||||
func (p *PostService) ListPosts(ctx context.Context, req *dto.ListPostReq) ([]dto.PostDto, error) {
|
||||
postDtos := make([]dto.PostDto, 0)
|
||||
currentUserID := ctxutils.GetCurrentUserID(ctx)
|
||||
currentUserID, _ := ctxutils.GetCurrentUserID(ctx)
|
||||
posts, err := repo.Post.ListPosts(currentUserID, req.Keywords, req.Page, req.Size, req.OrderedBy, req.Reverse)
|
||||
if err != nil {
|
||||
return nil, errs.New(errs.ErrInternalServer.Code, "failed to list posts", err)
|
||||
@ -136,10 +137,5 @@ func (p *PostService) ListPosts(ctx context.Context, req *dto.ListPostReq) (*dto
|
||||
for _, post := range posts {
|
||||
postDtos = append(postDtos, post.ToDto())
|
||||
}
|
||||
return &dto.ListPostResp{
|
||||
Posts: postDtos,
|
||||
Total: uint64(len(posts)),
|
||||
OrderedBy: req.OrderedBy,
|
||||
Reverse: req.Reverse,
|
||||
}, nil
|
||||
return postDtos, nil
|
||||
}
|
||||
|
@ -140,7 +140,7 @@ func (s *UserService) RequestVerifyEmail(req *dto.VerifyEmailReq) (*dto.VerifyEm
|
||||
return &dto.VerifyEmailResp{Success: true}, nil
|
||||
}
|
||||
|
||||
func (s *UserService) ListOidcConfigs() (*dto.ListOidcConfigResp, error) {
|
||||
func (s *UserService) ListOidcConfigs() ([]dto.UserOidcConfigDto, error) {
|
||||
enabledOidcConfigs, err := repo.Oidc.ListOidcConfigs(true)
|
||||
if err != nil {
|
||||
return nil, errs.ErrInternalServer
|
||||
@ -187,9 +187,7 @@ func (s *UserService) ListOidcConfigs() (*dto.ListOidcConfigResp, error) {
|
||||
LoginUrl: loginUrl,
|
||||
})
|
||||
}
|
||||
return &dto.ListOidcConfigResp{
|
||||
OidcConfigs: oidcConfigsDtos,
|
||||
}, nil
|
||||
return oidcConfigsDtos, nil
|
||||
}
|
||||
|
||||
func (s *UserService) OidcLogin(req *dto.OidcLoginReq) (*dto.OidcLoginResp, error) {
|
||||
|
Reference in New Issue
Block a user