update curd & code logic
This commit is contained in:
@@ -100,7 +100,7 @@ func deleteUser(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func initDB() {
|
func initDB() {
|
||||||
db, err := sql.Open("sqlite3", "test.db")
|
db, err := sql.Open("sqlite3", "./db/cat.db")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,172 +1,46 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"chat/opencatd/router"
|
||||||
"strconv"
|
_ "chat/pkg/opencatd/db"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type User struct {
|
|
||||||
IsDelete bool `json:"IsDelete"`
|
|
||||||
ID int `json:"id"`
|
|
||||||
UpdatedAt string `json:"updatedAt"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
Token string `json:"token"`
|
|
||||||
CreatedAt string `json:"createdAt"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Key struct {
|
|
||||||
ID int `json:"id"`
|
|
||||||
Key string `json:"key"`
|
|
||||||
UpdatedAt string `json:"updatedAt"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
CreatedAt string `json:"createdAt"`
|
|
||||||
}
|
|
||||||
|
|
||||||
var users = []User{
|
|
||||||
{false, 1, "2023-03-28T12:36:03.907590825Z", "root", "4930f144-7ffc-469b-8f2d-71a267358d87", "2023-03-28T11:52:45.593940257Z"},
|
|
||||||
}
|
|
||||||
|
|
||||||
var keys = []Key{
|
|
||||||
{1, "1234567890", "2023-03-28T12:53:41.124319079Z", "key1", "2023-03-28T12:53:41.124318928Z"},
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
router := gin.Default()
|
|
||||||
|
|
||||||
// 初始化用户
|
r := gin.Default()
|
||||||
router.POST("/1/users/init", func(c *gin.Context) {
|
group := r.Group("/1")
|
||||||
resJSON := User{
|
{
|
||||||
false,
|
group.Use(router.AuthMiddleware())
|
||||||
1,
|
|
||||||
"2023-03-28T14:13:54.969089197Z",
|
|
||||||
"root",
|
|
||||||
"3260dd53-463f-45d0-afa4-57acd04ee036",
|
|
||||||
"2023-03-28T14:13:54.969089107Z",
|
|
||||||
}
|
|
||||||
|
|
||||||
c.JSON(http.StatusOK, resJSON)
|
|
||||||
})
|
|
||||||
|
|
||||||
// 获取当前用户信息
|
// 获取当前用户信息
|
||||||
router.GET("/1/me", func(c *gin.Context) {
|
group.GET("/me", router.HandleMe)
|
||||||
resJSON := users[0]
|
|
||||||
|
|
||||||
c.JSON(http.StatusOK, resJSON)
|
|
||||||
})
|
|
||||||
|
|
||||||
// 获取所有Key
|
// 获取所有Key
|
||||||
router.GET("/1/keys", func(c *gin.Context) {
|
group.GET("/keys", router.HandleKeys)
|
||||||
c.JSON(http.StatusOK, keys)
|
|
||||||
})
|
|
||||||
|
|
||||||
// 获取所有用户信息
|
// 获取所有用户信息
|
||||||
router.GET("/1/users", func(c *gin.Context) {
|
group.GET("/users", router.HandleUsers)
|
||||||
c.JSON(http.StatusOK, users)
|
|
||||||
})
|
|
||||||
|
|
||||||
// 添加Key
|
// 添加Key
|
||||||
router.POST("/1/keys", func(c *gin.Context) {
|
group.POST("/keys", router.HandleAddKey)
|
||||||
var body Key
|
|
||||||
if err := c.BindJSON(&body); err != nil {
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
keys = append(keys, body)
|
|
||||||
c.JSON(http.StatusCreated, body)
|
|
||||||
})
|
|
||||||
|
|
||||||
// 删除Key
|
// 删除Key
|
||||||
router.DELETE("/1/keys/:id", func(c *gin.Context) {
|
group.DELETE("/keys/:id", router.HandleDelKey)
|
||||||
id := c.Param("id")
|
|
||||||
var keyIdx int = -1
|
|
||||||
|
|
||||||
for idx, key := range keys {
|
|
||||||
if strconv.Itoa(key.ID) == id {
|
|
||||||
keyIdx = idx
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if keyIdx == -1 {
|
|
||||||
c.JSON(http.StatusNotFound, gin.H{"error": "Key not found"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
keys = append(keys[:keyIdx], keys[keyIdx+1:]...)
|
|
||||||
c.JSON(http.StatusOK, gin.H{"message": "ok"})
|
|
||||||
})
|
|
||||||
|
|
||||||
// 添加用户
|
// 添加用户
|
||||||
router.POST("/1/users", func(c *gin.Context) {
|
group.POST("/users", router.HandleAddUser)
|
||||||
var body User
|
|
||||||
if err := c.BindJSON(&body); err != nil {
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
newUser := User{
|
|
||||||
false,
|
|
||||||
len(users) + 1,
|
|
||||||
"2023-03-28T13:05:02.103135082Z",
|
|
||||||
body.Name,
|
|
||||||
GenerateToken(),
|
|
||||||
"2023-03-28T12:38:57.513105431Z",
|
|
||||||
}
|
|
||||||
|
|
||||||
users = append(users, newUser)
|
|
||||||
c.JSON(http.StatusCreated, newUser)
|
|
||||||
})
|
|
||||||
|
|
||||||
// 删除用户
|
// 删除用户
|
||||||
router.DELETE("/1/users/:id", func(c *gin.Context) {
|
group.DELETE("/users/:id", router.HandleDelUser)
|
||||||
id := c.Param("id")
|
|
||||||
var userIdx int = -1
|
|
||||||
|
|
||||||
for idx, user := range users {
|
|
||||||
if strconv.Itoa(user.ID) == id {
|
|
||||||
userIdx = idx
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if userIdx == -1 {
|
|
||||||
c.JSON(http.StatusNotFound, gin.H{"error": "User not found"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
users = append(users[:userIdx], users[userIdx+1:]...)
|
|
||||||
c.JSON(http.StatusOK, gin.H{"message": "ok"})
|
|
||||||
})
|
|
||||||
|
|
||||||
// 重置用户Token
|
// 重置用户Token
|
||||||
router.POST("/1/users/:id/reset", func(c *gin.Context) {
|
group.POST("/users/:id/reset", router.HandleResetUserToken)
|
||||||
id := c.Param("id")
|
|
||||||
var userIdx int = -1
|
|
||||||
|
|
||||||
for idx, user := range users {
|
|
||||||
if strconv.Itoa(user.ID) == id {
|
|
||||||
userIdx = idx
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if userIdx == -1 {
|
// 初始化用户
|
||||||
c.JSON(http.StatusNotFound, gin.H{"error": "User not found"})
|
r.POST("/1/users/init", router.Handleinit)
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
users[userIdx].Token = GenerateToken()
|
r.Run(":80")
|
||||||
c.JSON(http.StatusOK, users[userIdx])
|
|
||||||
})
|
|
||||||
|
|
||||||
router.Run(":8080")
|
|
||||||
}
|
|
||||||
|
|
||||||
func GenerateToken() string {
|
|
||||||
token := uuid.New()
|
|
||||||
return token.String()
|
|
||||||
}
|
}
|
||||||
|
|||||||
223
opencatd/router/router.go
Normal file
223
opencatd/router/router.go
Normal file
@@ -0,0 +1,223 @@
|
|||||||
|
package router
|
||||||
|
|
||||||
|
import (
|
||||||
|
"chat/pkg/opencatd/db"
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/Sakurasan/to"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
rootToken string
|
||||||
|
)
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
IsDelete bool `json:"IsDelete,omitempty"`
|
||||||
|
ID int `json:"id,omitempty"`
|
||||||
|
UpdatedAt string `json:"updatedAt,omitempty"`
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
Token string `json:"token,omitempty"`
|
||||||
|
CreatedAt string `json:"createdAt,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Key struct {
|
||||||
|
ID int `json:"id,omitempty"`
|
||||||
|
Key string `json:"key,omitempty"`
|
||||||
|
UpdatedAt string `json:"updatedAt,omitempty"`
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
CreatedAt string `json:"createdAt,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func AuthMiddleware() gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
if rootToken == "" {
|
||||||
|
u, err := db.GetUserByID(uint(1))
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
rootToken = u.Token
|
||||||
|
}
|
||||||
|
token := c.GetHeader("Authorization")
|
||||||
|
if token == "" || token[:7] != "Bearer " || token[7:] != rootToken {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 可以在这里对 token 进行验证并检查权限
|
||||||
|
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Handleinit(c *gin.Context) {
|
||||||
|
user, err := db.GetUserByID(1)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
u := db.User{Name: "root", Token: uuid.NewString()}
|
||||||
|
u.ID = 1
|
||||||
|
if err := db.CreateUser(&u); err != nil {
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"error": err.Error(),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
rootToken = u.Token
|
||||||
|
resJSON := User{
|
||||||
|
false,
|
||||||
|
int(u.ID),
|
||||||
|
u.UpdatedAt.Format(time.RFC3339),
|
||||||
|
u.Name,
|
||||||
|
u.Token,
|
||||||
|
u.CreatedAt.Format(time.RFC3339),
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, resJSON)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"error": err.Error(),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if user.ID == uint(1) {
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"error": "super user already exists, use cli to reset password",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleMe(c *gin.Context) {
|
||||||
|
u, err := db.GetUserByID(1)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"error": err.Error(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
resJSON := User{
|
||||||
|
false,
|
||||||
|
int(u.ID),
|
||||||
|
u.UpdatedAt.Format(time.RFC3339),
|
||||||
|
u.Name,
|
||||||
|
u.Token,
|
||||||
|
u.CreatedAt.Format(time.RFC3339),
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, resJSON)
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleKeys(c *gin.Context) {
|
||||||
|
keys, err := db.GetAllKeys()
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"error": err.Error(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, keys)
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleUsers(c *gin.Context) {
|
||||||
|
users, err := db.GetAllUsers()
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"error": err.Error(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, users)
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleAddKey(c *gin.Context) {
|
||||||
|
var body Key
|
||||||
|
if err := c.BindJSON(&body); err != nil {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := db.AddKey(body.Key, body.Name); err != nil {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
k, err := db.GetKeyrByName(body.Name)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusCreated, k)
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleDelKey(c *gin.Context) {
|
||||||
|
id := to.Int(c.Param("id"))
|
||||||
|
if id < 1 {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"error": "invalid key id"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := db.DeleteKey(uint(id)); err != nil {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"error": "invalid key id"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"message": "ok"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleAddUser(c *gin.Context) {
|
||||||
|
var body User
|
||||||
|
if err := c.BindJSON(&body); err != nil {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// if len(body.Name) == 0 {
|
||||||
|
// c.JSON(http.StatusOK, gin.H{"error": "invalid user name"})
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
|
||||||
|
if err := db.AddUser(body.Name, uuid.NewString()); err != nil {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
u, err := db.GetUserByName(body.Name)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusCreated, u)
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleDelUser(c *gin.Context) {
|
||||||
|
id := to.Int(c.Param("id"))
|
||||||
|
if id <= 1 {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"error": "invalid user id"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := db.DeleteUser(uint(id)); err != nil {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, gin.H{"message": "ok"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleResetUserToken(c *gin.Context) {
|
||||||
|
id := to.Int(c.Param("id"))
|
||||||
|
|
||||||
|
if err := db.UpdateUser(uint(id), uuid.NewString()); err != nil {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
u, err := db.GetUserByID(uint(id))
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, u)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenerateToken() string {
|
||||||
|
token := uuid.New()
|
||||||
|
return token.String()
|
||||||
|
}
|
||||||
@@ -1,6 +1,9 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
"gorm.io/driver/sqlite"
|
"gorm.io/driver/sqlite"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
@@ -8,6 +11,12 @@ import (
|
|||||||
var db *gorm.DB
|
var db *gorm.DB
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
if _, err := os.Stat("db"); os.IsNotExist(err) {
|
||||||
|
errDir := os.MkdirAll("db", 0755)
|
||||||
|
if errDir != nil {
|
||||||
|
log.Fatalln("Error creating directory:", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
var err error
|
var err error
|
||||||
db, err = gorm.Open(sqlite.Open("./db/cat.db"), &gorm.Config{})
|
db, err = gorm.Open(sqlite.Open("./db/cat.db"), &gorm.Config{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -15,7 +24,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 自动迁移 User 结构体
|
// 自动迁移 User 结构体
|
||||||
err = db.AutoMigrate(&User{})
|
err = db.AutoMigrate(&User{}, &Key{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,38 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
import "gorm.io/gorm"
|
import "time"
|
||||||
|
|
||||||
type Key struct {
|
type Key struct {
|
||||||
gorm.Model
|
ID uint `gorm:"primarykey" json:"id,omitempty"`
|
||||||
ApiKey string
|
Key string `gorm:"unique;not null" json:"key,omitempty"`
|
||||||
UserId string
|
Name string `gorm:"unique;not null" json:"name,omitempty"`
|
||||||
|
UserId string `json:"-,omitempty"`
|
||||||
|
CreatedAt time.Time `json:"createdAt,omitempty"`
|
||||||
|
UpdatedAt time.Time `json:"updatedAt,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetKeyrByName(name string) (*Key, error) {
|
||||||
|
var key Key
|
||||||
|
result := db.First(&key, "name = ?", name)
|
||||||
|
if result.Error != nil {
|
||||||
|
return nil, result.Error
|
||||||
|
}
|
||||||
|
return &key, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAllKeys() ([]Key, error) {
|
||||||
|
var keys []Key
|
||||||
|
if err := db.Find(&keys).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return keys, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加记录
|
// 添加记录
|
||||||
func AddKey(apiKey string, userId string) error {
|
func AddKey(apikey, name string) error {
|
||||||
key := Key{
|
key := Key{
|
||||||
ApiKey: apiKey,
|
Key: apikey,
|
||||||
UserId: userId,
|
Name: name,
|
||||||
}
|
}
|
||||||
if err := db.Create(&key).Error; err != nil {
|
if err := db.Create(&key).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -29,9 +49,9 @@ func DeleteKey(id uint) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 更新记录
|
// 更新记录
|
||||||
func UpdateKey(id uint, apiKey string, userId string) error {
|
func UpdateKey(id uint, apikey string, userId string) error {
|
||||||
key := Key{
|
key := Key{
|
||||||
ApiKey: apiKey,
|
Key: apikey,
|
||||||
UserId: userId,
|
UserId: userId,
|
||||||
}
|
}
|
||||||
if err := db.Model(&Key{}).Where("id = ?", id).Updates(key).Error; err != nil {
|
if err := db.Model(&Key{}).Where("id = ?", id).Updates(key).Error; err != nil {
|
||||||
|
|||||||
@@ -1,11 +1,25 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
import "gorm.io/gorm"
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
gorm.Model
|
IsDelete bool `gorm:"default:false" json:"IsDelete"`
|
||||||
Name string `gorm:"not null"`
|
ID uint `gorm:"primarykey" json:"id,omitempty"`
|
||||||
Token string `gorm:"unique;not null"`
|
Name string `gorm:"unique;not null" json:"name,omitempty"`
|
||||||
|
Token string `gorm:"unique;not null" json:"token,omitempty"`
|
||||||
|
CreatedAt time.Time `json:"createdAt,omitempty"`
|
||||||
|
UpdatedAt time.Time `json:"updatedAt,omitempty"`
|
||||||
|
// DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateUser(u *User) error {
|
||||||
|
result := db.Create(u)
|
||||||
|
if result.Error != nil {
|
||||||
|
return result.Error
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加用户
|
// 添加用户
|
||||||
@@ -28,11 +42,38 @@ func DeleteUser(id uint) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 修改用户
|
// 修改用户
|
||||||
func UpdateUser(id uint, name, token string) error {
|
func UpdateUser(id uint, token string) error {
|
||||||
user := &User{Name: name, Token: token}
|
user := &User{Token: token}
|
||||||
result := db.Model(&User{}).Where("id = ?", id).Updates(user)
|
result := db.Model(&User{}).Where("id = ?", id).Updates(user)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
return result.Error
|
return result.Error
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetUserByID(id uint) (*User, error) {
|
||||||
|
var user User
|
||||||
|
result := db.Where("id = ?", id).First(&user)
|
||||||
|
if result.Error != nil {
|
||||||
|
return nil, result.Error
|
||||||
|
}
|
||||||
|
return &user, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetUserByName(name string) (*User, error) {
|
||||||
|
var user User
|
||||||
|
result := db.Where(&User{Name: name}).First(&user)
|
||||||
|
if result.Error != nil {
|
||||||
|
return nil, result.Error
|
||||||
|
}
|
||||||
|
return &user, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAllUsers() ([]*User, error) {
|
||||||
|
var users []*User
|
||||||
|
result := db.Find(&users)
|
||||||
|
if result.Error != nil {
|
||||||
|
return nil, result.Error
|
||||||
|
}
|
||||||
|
return users, nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user