mirror of
https://github.com/snowykami/neo-blog.git
synced 2025-09-04 00:06:22 +00:00
⚡ implement OIDC configuration management with CRUD operations, add admin routes, and enhance error handling
This commit is contained in:
55
internal/repo/label.go
Normal file
55
internal/repo/label.go
Normal file
@ -0,0 +1,55 @@
|
||||
package repo
|
||||
|
||||
import "github.com/snowykami/neo-blog/internal/model"
|
||||
|
||||
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,
|
||||
}
|
||||
return GetDB().Create(label).Error
|
||||
}
|
||||
|
||||
func (l *labelRepo) GetLabelByKey(key string) (*model.Label, error) {
|
||||
var label model.Label
|
||||
if err := GetDB().Where("key = ?", key).First(&label).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &label, nil
|
||||
}
|
||||
|
||||
func (l *labelRepo) GetLabelByID(id uint) (*model.Label, error) {
|
||||
var label model.Label
|
||||
if err := GetDB().Where("id = ?", id).First(&label).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &label, nil
|
||||
}
|
||||
|
||||
func (l *labelRepo) ListLabels() ([]model.Label, error) {
|
||||
var labels []model.Label
|
||||
if err := GetDB().Find(&labels).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return labels, nil
|
||||
}
|
||||
|
||||
func (l *labelRepo) UpdateLabel(label *model.Label) error {
|
||||
if err := GetDB().Save(label).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *labelRepo) DeleteLabel(id uint) error {
|
||||
if err := GetDB().Where("id = ?", id).Delete(&model.Label{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
69
internal/repo/oidc_config.go
Normal file
69
internal/repo/oidc_config.go
Normal file
@ -0,0 +1,69 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
"github.com/snowykami/neo-blog/internal/model"
|
||||
"github.com/snowykami/neo-blog/pkg/errs"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type oidcRepo struct {
|
||||
}
|
||||
|
||||
var Oidc = &oidcRepo{}
|
||||
|
||||
func (o *oidcRepo) CreateOidcConfig(oidcConfig *model.OidcConfig) error {
|
||||
if err := GetDB().Create(oidcConfig).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o *oidcRepo) DeleteOidcConfig(id string) error {
|
||||
if id == "" {
|
||||
return errs.New(http.StatusBadRequest, "invalid OIDC config ID", nil)
|
||||
}
|
||||
if err := GetDB().Where("id = ?", id).Delete(&model.OidcConfig{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o *oidcRepo) ListOidcConfigs(onlyEnabled bool) ([]model.OidcConfig, error) {
|
||||
var configs []model.OidcConfig
|
||||
if onlyEnabled {
|
||||
if err := GetDB().Where("enabled = ?", true).Find(&configs).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
if err := GetDB().Find(&configs).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return configs, nil
|
||||
}
|
||||
|
||||
func (o *oidcRepo) GetOidcConfigByName(name string) (*model.OidcConfig, error) {
|
||||
var config model.OidcConfig
|
||||
if err := GetDB().Where("name = ?", name).First(&config).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
func (o *oidcRepo) GetOidcConfigByID(id string) (*model.OidcConfig, error) {
|
||||
var config model.OidcConfig
|
||||
if err := GetDB().Where("id = ?", id).First(&config).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
func (o *oidcRepo) UpdateOidcConfig(oidcConfig *model.OidcConfig) error {
|
||||
if oidcConfig.ID == 0 {
|
||||
return errs.New(http.StatusBadRequest, "invalid OIDC config ID", nil)
|
||||
}
|
||||
if err := GetDB().Updates(oidcConfig).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
54
internal/repo/post.go
Normal file
54
internal/repo/post.go
Normal file
@ -0,0 +1,54 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
"github.com/snowykami/neo-blog/internal/model"
|
||||
"github.com/snowykami/neo-blog/pkg/errs"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type postRepo struct{}
|
||||
|
||||
var Post = &postRepo{}
|
||||
|
||||
func (p *postRepo) CreatePost(post *model.Post) error {
|
||||
if err := GetDB().Create(post).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *postRepo) DeletePost(id string) error {
|
||||
if id == "" {
|
||||
return errs.New(http.StatusBadRequest, "invalid post ID", nil)
|
||||
}
|
||||
if err := GetDB().Where("id = ?", id).Delete(&model.Post{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *postRepo) GetPostByID(id string) (*model.Post, error) {
|
||||
var post model.Post
|
||||
if err := GetDB().Where("id = ?", id).Preload("User").First(&post).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &post, nil
|
||||
}
|
||||
|
||||
func (p *postRepo) UpdatePost(post *model.Post) error {
|
||||
if post.ID == 0 {
|
||||
return errs.New(http.StatusBadRequest, "invalid post ID", nil)
|
||||
}
|
||||
if err := GetDB().Save(post).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *postRepo) ListPosts(limit, offset int) ([]model.Post, error) {
|
||||
var posts []model.Post
|
||||
if err := GetDB().Limit(limit).Offset(offset).Find(&posts).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return posts, nil
|
||||
}
|
@ -68,28 +68,6 @@ func (user *userRepo) CheckEmailExists(email string) (bool, error) {
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
func (user *userRepo) ListOidcConfigs(onlyEnabled bool) ([]model.OidcConfig, error) {
|
||||
var configs []model.OidcConfig
|
||||
if onlyEnabled {
|
||||
if err := GetDB().Where("enabled = ?", true).Find(&configs).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
if err := GetDB().Find(&configs).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return configs, nil
|
||||
}
|
||||
|
||||
func (user *userRepo) GetOidcConfigByName(name string) (*model.OidcConfig, error) {
|
||||
var config model.OidcConfig
|
||||
if err := GetDB().Where("name = ?", name).First(&config).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
func (user *userRepo) CreateOrUpdateUserOpenID(userOpenID *model.UserOpenID) error {
|
||||
if err := GetDB().Save(userOpenID).Error; err != nil {
|
||||
return err
|
||||
|
Reference in New Issue
Block a user