Skip to content
This repository has been archived by the owner on Oct 17, 2023. It is now read-only.

Commit

Permalink
完成了单条查询、更新和删除的权限校验
Browse files Browse the repository at this point in the history
  • Loading branch information
whxxxxxxxxxx committed Oct 7, 2023
1 parent 70ab4f8 commit da9ac92
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
9 changes: 6 additions & 3 deletions whxxxxxxxxxx/api/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ func CreateTask(c *gin.Context) {

func GetOneTask(c *gin.Context) {
var getOneTask service.GetOneTaskService
claim, _ := utils.ParseToken(c.GetHeader("Authorization"))
if err := c.ShouldBind(&getOneTask); err == nil {
res := getOneTask.GetOne(c.Param("id"))
res := getOneTask.GetOne(c.Param("id"), claim.Id)
c.JSON(200, res)

} else {
Expand All @@ -48,8 +49,9 @@ func GetAllTask(c *gin.Context) {

func UpdateTask(c *gin.Context) {
var updateTask service.UpdateTaskService
claim, _ := utils.ParseToken(c.GetHeader("Authorization"))
if err := c.ShouldBind(&updateTask); err == nil {
res := updateTask.Update(c.Param("id"))
res := updateTask.Update(c.Param("id"), claim.Id)
c.JSON(200, res)

} else {
Expand All @@ -73,8 +75,9 @@ func SearchTask(c *gin.Context) {

func DeleteTask(c *gin.Context) {
var deleteTask service.DeleteTaskService
claim, _ := utils.ParseToken(c.GetHeader("Authorization"))
if err := c.ShouldBind(&deleteTask); err == nil {
res := deleteTask.Delete(c.Param("id"))
res := deleteTask.Delete(c.Param("id"), claim.Id)
c.JSON(200, res)

} else {
Expand Down
23 changes: 13 additions & 10 deletions whxxxxxxxxxx/service/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ type CreateTaskService struct {
Title string `json:"title" form:"title"`
Content string `json:"content" form:"content"`
Status int `json:"status" form:"status"` //0未完成,1完成
EndTime int64 `json:"end_time" form:"end_time"`
}

type UpdateTaskService struct {
Title string `json:"title" form:"title"`
Content string `json:"content" form:"content"`
Status int `json:"status" form:"status"` //0未完成,1完成
EndTime int64 `json:"end_time" form:"end_time"`
}

type GetOneTaskService struct {
Expand Down Expand Up @@ -47,7 +49,7 @@ func (service *CreateTaskService) Create(id uint) serializer.Response {
Status: 0,
Content: service.Content,
StartTime: time.Now().Unix(),
EndTime: 0,
EndTime: service.EndTime,
}
err := model.DB.Create(&task).Error
if err != nil {
Expand All @@ -64,15 +66,15 @@ func (service *CreateTaskService) Create(id uint) serializer.Response {
}
}

func (service *GetOneTaskService) GetOne(tid string) serializer.Response {
func (service *GetOneTaskService) GetOne(tid string, uid uint) serializer.Response {
var task model.Task
code := 200
err := model.DB.First(&task, tid).Error
err := model.DB.Preload("User").Where("uid=?", uid).First(&task, tid).Error
if err != nil {
code = 500
return serializer.Response{
Status: code,
Msg: "获取对应task失败",
Msg: "获取对应task失败或者权限不足",
}
}
return serializer.Response{
Expand Down Expand Up @@ -104,20 +106,21 @@ func (service *GetAllTaskService) GetAll(uid uint) serializer.Response {
return serializer.BuildListResponse(serializer.BuildTasks(tasks), uint(count))
}

func (service *UpdateTaskService) Update(tid string) serializer.Response {
func (service *UpdateTaskService) Update(tid string, uid uint) serializer.Response {
var task model.Task
code := 200
err := model.DB.First(&task, tid).Error
err := model.DB.Preload("User").Where("uid=?", uid).First(&task, tid).Error
if err != nil {
code = 500
return serializer.Response{
Status: code,
Msg: "更新时获取对应task失败",
Msg: "更新时获取对应task失败或者权限不足",
}
}
task.Title = service.Title
task.Content = service.Content
task.Status = service.Status
task.EndTime = service.EndTime
err = model.DB.Save(&task).Error
if err != nil {
code = 500
Expand Down Expand Up @@ -147,15 +150,15 @@ func (service *SearchTaskService) Search(uid uint) serializer.Response {
return serializer.BuildListResponse(serializer.BuildTasks(tasks), uint(count))
}

func (service *DeleteTaskService) Delete(tid string) serializer.Response {
func (service *DeleteTaskService) Delete(tid string, uid uint) serializer.Response {
var task model.Task
code := 200
err := model.DB.First(&task, tid).Error
err := model.DB.Preload("User").Where("uid=?", uid).First(&task, tid).Error
if err != nil {
code = 500
return serializer.Response{
Status: code,
Msg: "删除时获取对应task失败",
Msg: "删除时获取对应task失败或者权限不足",
}
}
err = model.DB.Delete(&task).Error
Expand Down

0 comments on commit da9ac92

Please sign in to comment.