随笔 - 35  文章 - 20 评论 - 0 阅读 - 3497
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

controllers->admin->role.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package admin
 
import (
    "fmt"
    "ginshop05/models"
    "net/http"
    "strings"
 
    "github.com/gin-gonic/gin"
)
 
type RoleController struct {
    BaseController
}
 
func (con RoleController) Index(c *gin.Context) {
    roleList := []models.Role{}
    models.DB.Find(&roleList)
    fmt.Println(roleList)
    c.HTML(http.StatusOK, "admin/role/index.html", gin.H{
        "roleList": roleList,
    })
 
}
func (con RoleController) Add(c *gin.Context) {
    c.HTML(http.StatusOK, "admin/role/add.html", gin.H{})
}
func (con RoleController) DoAdd(c *gin.Context) {
 
    title := strings.Trim(c.PostForm("title"), " ")
    description := strings.Trim(c.PostForm("description"), " ")
 
    if title == "" {
        con.Error(c, "角色的标题不能为空", "/admin/role/add")
        return
    }
    role := models.Role{}
    role.Title = title
    role.Description = description
    role.Status = 1
    role.AddTime = int(models.GetUnix())
 
    err := models.DB.Create(&role).Error
    if err != nil {
        con.Error(c, "增加角色失败 请重试", "/admin/role/add")
    } else {
        con.Success(c, "增加角色成功", "/admin/role")
    }
 
}
func (con RoleController) Edit(c *gin.Context) {
 
    id, err := models.Int(c.Query("id"))
    if err != nil {
        con.Error(c, "传入数据错误", "/admin/role")
    } else {
        role := models.Role{Id: id}
        models.DB.Find(&role)
        c.HTML(http.StatusOK, "admin/role/edit.html", gin.H{
            "role": role,
        })
    }
 
}
func (con RoleController) DoEdit(c *gin.Context) {
 
    id, err1 := models.Int(c.PostForm("id"))
    if err1 != nil {
        con.Error(c, "传入数据错误", "/admin/role")
        return
    }
    title := strings.Trim(c.PostForm("title"), " ")
    description := strings.Trim(c.PostForm("description"), " ")
 
    if title == "" {
        con.Error(c, "角色的标题不能为空", "/admin/role/edit")
    }
 
    role := models.Role{Id: id}
    models.DB.Find(&role)
    role.Title = title
    role.Description = description
 
    err2 := models.DB.Save(&role).Error
    if err2 != nil {
        con.Error(c, "修改数据失败", "/admin/role/edit?id="+models.String(id))
    } else {
        con.Success(c, "修改数据成功", "/admin/role/edit?id="+models.String(id))
    }
 
    //查询要修改的数据 然后 修改
 
    // c.String(http.StatusOK, "-执行修改")
}
func (con RoleController) Delete(c *gin.Context) {
    id, err := models.Int(c.Query("id"))
    if err != nil {
        con.Error(c, "传入数据错误", "/admin/role")
    } else {
        role := models.Role{Id: id}
        models.DB.Delete(&role)
        con.Success(c, "删除数据成功", "/admin/role")
    }
}

  

posted on   KOA2后端  阅读(25)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示