mirror of
https://github.com/snowykami/neo-blog.git
synced 2025-09-06 01:06:22 +00:00
⚡ implement OIDC configuration management with CRUD operations, add admin routes, and enhance error handling
This commit is contained in:
92
internal/controller/v1/console.go
Normal file
92
internal/controller/v1/console.go
Normal file
@ -0,0 +1,92 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
"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"
|
||||
)
|
||||
|
||||
type AdminController struct {
|
||||
service *service.AdminService
|
||||
}
|
||||
|
||||
func NewAdminController() *AdminController {
|
||||
return &AdminController{
|
||||
service: service.NewAdminService(),
|
||||
}
|
||||
}
|
||||
|
||||
func (cc *AdminController) CreateOidc(ctx context.Context, c *app.RequestContext) {
|
||||
var adminCreateOidcReq dto.AdminOidcConfigDto
|
||||
if err := c.BindAndValidate(&adminCreateOidcReq); err != nil {
|
||||
c.JSON(400, map[string]string{"error": "Invalid parameters"})
|
||||
return
|
||||
}
|
||||
err := cc.service.CreateOidcConfig(&adminCreateOidcReq)
|
||||
if err != nil {
|
||||
serviceErr := errs.AsServiceError(err)
|
||||
resps.Custom(c, serviceErr.Code, serviceErr.Message, nil)
|
||||
return
|
||||
}
|
||||
resps.Ok(c, resps.Success, nil)
|
||||
}
|
||||
|
||||
func (cc *AdminController) DeleteOidc(ctx context.Context, c *app.RequestContext) {
|
||||
id := c.Param("id")
|
||||
if id == "" {
|
||||
resps.BadRequest(c, resps.ErrParamInvalid)
|
||||
return
|
||||
}
|
||||
|
||||
err := cc.service.DeleteOidcConfig(id)
|
||||
if err != nil {
|
||||
serviceErr := errs.AsServiceError(err)
|
||||
resps.Custom(c, serviceErr.Code, serviceErr.Message, nil)
|
||||
return
|
||||
}
|
||||
resps.Ok(c, resps.Success, nil)
|
||||
}
|
||||
|
||||
func (cc *AdminController) GetOidcByID(ctx context.Context, c *app.RequestContext) {
|
||||
id := c.Param("id")
|
||||
if id == "" {
|
||||
resps.BadRequest(c, resps.ErrParamInvalid)
|
||||
return
|
||||
}
|
||||
|
||||
config, err := cc.service.GetOidcConfigByID(id)
|
||||
if err != nil {
|
||||
serviceErr := errs.AsServiceError(err)
|
||||
resps.Custom(c, serviceErr.Code, serviceErr.Message, nil)
|
||||
return
|
||||
}
|
||||
resps.Ok(c, resps.Success, config)
|
||||
}
|
||||
|
||||
func (cc *AdminController) ListOidc(ctx context.Context, c *app.RequestContext) {
|
||||
configs, err := cc.service.ListOidcConfigs(false)
|
||||
if err != nil {
|
||||
serviceErr := errs.AsServiceError(err)
|
||||
resps.Custom(c, serviceErr.Code, serviceErr.Message, nil)
|
||||
return
|
||||
}
|
||||
resps.Ok(c, resps.Success, configs)
|
||||
}
|
||||
|
||||
func (cc *AdminController) UpdateOidc(ctx context.Context, c *app.RequestContext) {
|
||||
var adminUpdateOidcReq dto.AdminOidcConfigDto
|
||||
if err := c.BindAndValidate(&adminUpdateOidcReq); err != nil {
|
||||
c.JSON(400, map[string]string{"error": "Invalid parameters"})
|
||||
return
|
||||
}
|
||||
err := cc.service.UpdateOidcConfig(&adminUpdateOidcReq)
|
||||
if err != nil {
|
||||
serviceErr := errs.AsServiceError(err)
|
||||
resps.Custom(c, serviceErr.Code, serviceErr.Message, nil)
|
||||
return
|
||||
}
|
||||
resps.Ok(c, resps.Success, nil)
|
||||
}
|
@ -3,28 +3,57 @@ package v1
|
||||
import (
|
||||
"context"
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
"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"
|
||||
)
|
||||
|
||||
type postType struct{}
|
||||
type PostController struct {
|
||||
service *service.PostService
|
||||
}
|
||||
|
||||
var Post = new(postType)
|
||||
func NewPostController() *PostController {
|
||||
return &PostController{
|
||||
service: service.NewPostService(),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *postType) Create(ctx context.Context, c *app.RequestContext) {
|
||||
func (p *PostController) Create(ctx context.Context, c *app.RequestContext) {
|
||||
var req dto.CreateOrUpdatePostReq
|
||||
if err := c.BindAndValidate(&req); err != nil {
|
||||
resps.BadRequest(c, resps.ErrParamInvalid)
|
||||
}
|
||||
if err := p.service.CreatePost(&req); err != nil {
|
||||
serviceErr := errs.AsServiceError(err)
|
||||
resps.Custom(c, serviceErr.Code, serviceErr.Message, nil)
|
||||
return
|
||||
}
|
||||
resps.Ok(c, resps.Success, nil)
|
||||
}
|
||||
|
||||
func (p *PostController) Delete(ctx context.Context, c *app.RequestContext) {
|
||||
id := c.Param("id")
|
||||
if id == "" {
|
||||
resps.BadRequest(c, resps.ErrParamInvalid)
|
||||
return
|
||||
}
|
||||
if err := p.service.DeletePost(ctx, id); err != nil {
|
||||
serviceErr := errs.AsServiceError(err)
|
||||
resps.Custom(c, serviceErr.Code, serviceErr.Message, nil)
|
||||
return
|
||||
}
|
||||
resps.Ok(c, resps.Success, nil)
|
||||
}
|
||||
|
||||
func (p *PostController) Get(ctx context.Context, c *app.RequestContext) {
|
||||
// TODO: Impl
|
||||
}
|
||||
|
||||
func (p *postType) Delete(ctx context.Context, c *app.RequestContext) {
|
||||
func (p *PostController) Update(ctx context.Context, c *app.RequestContext) {
|
||||
// TODO: Impl
|
||||
}
|
||||
|
||||
func (p *postType) Get(ctx context.Context, c *app.RequestContext) {
|
||||
// TODO: Impl
|
||||
}
|
||||
|
||||
func (p *postType) Update(ctx context.Context, c *app.RequestContext) {
|
||||
// TODO: Impl
|
||||
}
|
||||
|
||||
func (p *postType) List(ctx context.Context, c *app.RequestContext) {
|
||||
func (p *PostController) List(ctx context.Context, c *app.RequestContext) {
|
||||
// TODO: Impl
|
||||
}
|
||||
|
@ -12,15 +12,17 @@ import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type userType struct {
|
||||
type UserController struct {
|
||||
service *service.UserService
|
||||
}
|
||||
|
||||
var User = &userType{
|
||||
service: service.NewUserService(),
|
||||
func NewUserController() *UserController {
|
||||
return &UserController{
|
||||
service: service.NewUserService(),
|
||||
}
|
||||
}
|
||||
|
||||
func (u *userType) Login(ctx context.Context, c *app.RequestContext) {
|
||||
func (u *UserController) Login(ctx context.Context, c *app.RequestContext) {
|
||||
var userLoginReq dto.UserLoginReq
|
||||
if err := c.BindAndValidate(&userLoginReq); err != nil {
|
||||
resps.BadRequest(c, resps.ErrParamInvalid)
|
||||
@ -40,7 +42,7 @@ func (u *userType) Login(ctx context.Context, c *app.RequestContext) {
|
||||
})
|
||||
}
|
||||
|
||||
func (u *userType) Register(ctx context.Context, c *app.RequestContext) {
|
||||
func (u *UserController) Register(ctx context.Context, c *app.RequestContext) {
|
||||
var userRegisterReq dto.UserRegisterReq
|
||||
if err := c.BindAndValidate(&userRegisterReq); err != nil {
|
||||
resps.BadRequest(c, resps.ErrParamInvalid)
|
||||
@ -61,12 +63,12 @@ func (u *userType) Register(ctx context.Context, c *app.RequestContext) {
|
||||
})
|
||||
}
|
||||
|
||||
func (u *userType) Logout(ctx context.Context, c *app.RequestContext) {
|
||||
func (u *UserController) Logout(ctx context.Context, c *app.RequestContext) {
|
||||
ctxutils.ClearTokenAndRefreshTokenCookie(c)
|
||||
resps.Ok(c, resps.Success, nil)
|
||||
}
|
||||
|
||||
func (u *userType) OidcList(ctx context.Context, c *app.RequestContext) {
|
||||
func (u *UserController) OidcList(ctx context.Context, c *app.RequestContext) {
|
||||
resp, err := u.service.ListOidcConfigs()
|
||||
if err != nil {
|
||||
serviceErr := errs.AsServiceError(err)
|
||||
@ -78,7 +80,7 @@ func (u *userType) OidcList(ctx context.Context, c *app.RequestContext) {
|
||||
})
|
||||
}
|
||||
|
||||
func (u *userType) OidcLogin(ctx context.Context, c *app.RequestContext) {
|
||||
func (u *UserController) OidcLogin(ctx context.Context, c *app.RequestContext) {
|
||||
name := c.Param("name")
|
||||
code := c.Param("code")
|
||||
state := c.Param("state")
|
||||
@ -100,7 +102,7 @@ func (u *userType) OidcLogin(ctx context.Context, c *app.RequestContext) {
|
||||
})
|
||||
}
|
||||
|
||||
func (u *userType) GetUser(ctx context.Context, c *app.RequestContext) {
|
||||
func (u *UserController) GetUser(ctx context.Context, c *app.RequestContext) {
|
||||
userID := c.Param("id")
|
||||
if userID == "" {
|
||||
resps.BadRequest(c, resps.ErrParamInvalid)
|
||||
@ -121,7 +123,7 @@ func (u *userType) GetUser(ctx context.Context, c *app.RequestContext) {
|
||||
resps.Ok(c, resps.Success, resp.User)
|
||||
}
|
||||
|
||||
func (u *userType) UpdateUser(ctx context.Context, c *app.RequestContext) {
|
||||
func (u *UserController) UpdateUser(ctx context.Context, c *app.RequestContext) {
|
||||
userID := c.Param("id")
|
||||
if userID == "" {
|
||||
resps.BadRequest(c, resps.ErrParamInvalid)
|
||||
@ -156,7 +158,7 @@ func (u *userType) UpdateUser(ctx context.Context, c *app.RequestContext) {
|
||||
resps.Ok(c, resps.Success, resp)
|
||||
}
|
||||
|
||||
func (u *userType) VerifyEmail(ctx context.Context, c *app.RequestContext) {
|
||||
func (u *UserController) VerifyEmail(ctx context.Context, c *app.RequestContext) {
|
||||
var verifyEmailReq dto.VerifyEmailReq
|
||||
if err := c.BindAndValidate(&verifyEmailReq); err != nil {
|
||||
resps.BadRequest(c, resps.ErrParamInvalid)
|
||||
|
Reference in New Issue
Block a user