Skip to content

Commit

Permalink
Merge pull request #82 from qianqianzyk/main
Browse files Browse the repository at this point in the history
refactor(theme):重构主题存储和获取逻辑
  • Loading branch information
XiMo-210 authored Oct 16, 2024
2 parents b6f13a2 + 1848eff commit 54c0d30
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 50 deletions.
31 changes: 15 additions & 16 deletions app/controllers/adminController/theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,11 @@ func CreateTheme(c *gin.Context) {
Type: data.Type,
ThemeConfig: data.ThemeConfig,
}
themeID, err := themeServices.CreateTheme(record)
err = themeServices.CreateTheme(record)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}
if data.Type == "all" {
studentIDs, err := themeServices.GetAllStudentIDs()
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}

_, err = themeServices.AddThemePermission(themeID, studentIDs)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}
}

utils.JsonSuccessResponse(c, nil)
}
Expand Down Expand Up @@ -110,7 +97,13 @@ func DeleteTheme(c *gin.Context) {
return
}

err = themeServices.DeleteTheme(data.ID)
theme, err := themeServices.GetThemeByID(data.ID)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}

err = themeServices.DeleteTheme(data.ID, theme.Type)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
Expand Down Expand Up @@ -138,7 +131,13 @@ func AddThemePermission(c *gin.Context) {
return
}

invalidStudentIDs, err := themeServices.AddThemePermission(data.ThemeID, data.StudentID)
theme, err := themeServices.GetThemeByID(data.ThemeID)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}

invalidStudentIDs, err := themeServices.AddThemePermission(data.ThemeID, data.StudentID, theme.Type)
if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
Expand Down
94 changes: 69 additions & 25 deletions app/services/themeServices/themePermissionService.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ import (
"encoding/json"
"errors"
"gorm.io/gorm"
"strconv"
"wejh-go/app/config"
"wejh-go/app/models"
"wejh-go/config/database"
)

func AddThemePermission(themeID int, reqStudentIDs []string) ([]string, error) {
func AddThemePermission(themeID int, reqStudentIDs []string, themeType string) ([]string, error) {
if themeType == "all" {
return nil, nil
}

var studentIDs []string
var invalidStudentIDs []string

if len(reqStudentIDs) > 0 {
var existingUsers []models.User
err := database.DB.Select("student_id").Where("student_id IN ?", reqStudentIDs).Find(&existingUsers).Error
Expand All @@ -32,14 +37,10 @@ func AddThemePermission(themeID int, reqStudentIDs []string) ([]string, error) {
}
}
} else {
var users []models.User
err := database.DB.Select("student_id").Find(&users).Error
if err != nil {
return nil, err
}
for _, user := range users {
studentIDs = append(studentIDs, user.StudentID)
}
return nil, errors.New("reqStudentIDs is invalid")
}
if len(studentIDs) == 0 {
return invalidStudentIDs, nil
}

var permissions []models.ThemePermission
Expand Down Expand Up @@ -106,6 +107,18 @@ func UpdateCurrentTheme(id int, studentID string) error {
return err
}

var allThemes []models.Theme
err = database.DB.Where("type = ?", "all").Find(&allThemes).Error
if err != nil {
return err
}

for _, theme := range allThemes {
if !containThemeID(themePermissionData.ThemeIDs, theme.ID) {
themePermissionData.ThemeIDs = append(themePermissionData.ThemeIDs, theme.ID)
}
}

if !containThemeID(themePermissionData.ThemeIDs, id) {
return errors.New("the theme ID is not in the user's permission list")
}
Expand Down Expand Up @@ -133,11 +146,25 @@ func GetThemeNameByID(themePermission models.ThemePermission) ([]string, error)
if err != nil {
return nil, err
}

var themes []models.Theme
err = database.DB.Where("id IN ?", themePermissionData.ThemeIDs).Find(&themes).Error
if err != nil {
return nil, err
}

var allThemes []models.Theme
err = database.DB.Where("type = ?", "all").Find(&allThemes).Error
if err != nil {
return nil, err
}

for _, allTheme := range allThemes {
if !containThemeID(themePermissionData.ThemeIDs, allTheme.ID) {
themes = append(themes, allTheme)
}
}

var themeNames []string
for _, theme := range themes {
themeNames = append(themeNames, theme.Name)
Expand All @@ -151,11 +178,25 @@ func GetThemesByID(themePermission models.ThemePermission) ([]models.Theme, erro
if err != nil {
return nil, err
}

var themes []models.Theme
err = database.DB.Where("id IN ?", themePermissionData.ThemeIDs).Find(&themes).Error
if err != nil {
return nil, err
}

var allThemes []models.Theme
err = database.DB.Where("type = ?", "all").Find(&allThemes).Error
if err != nil {
return nil, err
}

for _, allTheme := range allThemes {
if !containThemeID(themePermissionData.ThemeIDs, allTheme.ID) {
themes = append(themes, allTheme)
}
}

return themes, nil
}

Expand All @@ -164,29 +205,32 @@ func AddDefaultThemePermission(studentID string) error {
err := database.DB.Where("student_id = ?", studentID).First(&existingPermission).Error
if err != nil {
if err == gorm.ErrRecordNotFound {
var themes []models.Theme
err := database.DB.Where("type = ?", "all").Find(&themes).Error
if err != nil {
return err
}
if len(themes) == 0 {
return errors.New("no themes found with type 'all'")
}

var themeIDs []int
for _, theme := range themes {
themeIDs = append(themeIDs, theme.ID)
themePermissionData := models.ThemePermissionData{
ThemeIDs: []int{},
}
var themePermissionData models.ThemePermissionData
themePermissionData.ThemeIDs = themeIDs
permission, err := json.Marshal(themePermissionData)
if err != nil {
return err
}

var defaultThemeID int
defaultThemeIDStr := config.GetDefaultThemeKey()
if defaultThemeIDStr != "" {
defaultThemeID, err = strconv.Atoi(defaultThemeIDStr)
if err != nil {
return err
}
} else {
var theme models.Theme
if err := database.DB.Model(models.Theme{}).Where("type = ?", "all").First(&theme).Error; err != nil {
return err
}
defaultThemeID = theme.ID
}

newPermission := models.ThemePermission{
StudentID: studentID,
CurrentThemeID: themeIDs[0],
CurrentThemeID: defaultThemeID,
ThemePermission: string(permission),
}

Expand Down
39 changes: 30 additions & 9 deletions app/services/themeServices/themeService.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package themeServices

import (
"encoding/json"
"strconv"
"wejh-go/app/config"
"wejh-go/app/models"
"wejh-go/config/database"
)
Expand All @@ -12,9 +14,9 @@ func CheckThemeExist(id int) error {
return result.Error
}

func CreateTheme(record models.Theme) (int, error) {
func CreateTheme(record models.Theme) error {
result := database.DB.Create(&record)
return record.ID, result.Error
return result.Error
}

func UpdateTheme(id int, record models.Theme) error {
Expand All @@ -34,7 +36,7 @@ func GetThemes() ([]models.Theme, error) {
return themes, result.Error
}

func DeleteTheme(id int) error {
func DeleteTheme(id int, themeType string) error {
tx := database.DB.Begin()
if err := tx.Delete(&models.Theme{}, id).Error; err != nil {
tx.Rollback()
Expand All @@ -47,20 +49,36 @@ func DeleteTheme(id int) error {
return err
}

var permissions []models.ThemePermission
if err := tx.Where("current_theme_id = ?", id).Find(&permissions).Error; err != nil {
tx.Rollback()
return err
var defaultThemeID int
defaultThemeIDStr := config.GetDefaultThemeKey()
if defaultThemeIDStr != "" {
defaultThemeID, _ = strconv.Atoi(defaultThemeIDStr)
if id == defaultThemeID {
defaultThemeID = theme.ID
err := config.SetDefaultThemeKey(strconv.Itoa(defaultThemeID))
if err != nil {
tx.Rollback()
return err
}
}
} else {
defaultThemeID = theme.ID
}

if err := tx.Model(&models.ThemePermission{}).
Where("current_theme_id = ?", id).
Update("current_theme_id", theme.ID).Error; err != nil {
Update("current_theme_id", defaultThemeID).Error; err != nil {
tx.Rollback()
return err
}

result := tx.Find(&permissions)
if themeType == "all" {
tx.Commit()
return nil
}

var permissions []models.ThemePermission
result := tx.Model(models.ThemePermission{}).Find(&permissions)
if result.Error != nil {
tx.Rollback()
return result.Error
Expand All @@ -77,6 +95,9 @@ func DeleteTheme(id int) error {
updatedThemeIDs := removeThemeID(themePermissionData.ThemeIDs, id)
if len(updatedThemeIDs) != len(themePermissionData.ThemeIDs) {
themePermissionData.ThemeIDs = updatedThemeIDs
if len(updatedThemeIDs) == 0 {
themePermissionData.ThemeIDs = []int{}
}
updatedPermissionMap[permission.StudentID] = themePermissionData
}
}
Expand Down

0 comments on commit 54c0d30

Please sign in to comment.