Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

查询数据需要考虑数据归属问题 #4

Open
supuwoerc opened this issue Feb 20, 2023 · 0 comments
Open

查询数据需要考虑数据归属问题 #4

supuwoerc opened this issue Feb 20, 2023 · 0 comments

Comments

@supuwoerc
Copy link

supuwoerc commented Feb 20, 2023

查询时需要添加过滤条件,不然会查询到别人的数据。

// TaskDetail 待办详情
func TaskDetail(c *gin.Context) {
	var taskDetailDTO service.TaskDetailDTO
	claims, _ := utils.ParseToken(c.GetHeader(conf.TokenKey))
	if err := c.ShouldBindUri(&taskDetailDTO); err == nil {
		res := taskDetailDTO.TaskDetail(claims.ID)
		c.JSON(http.StatusOK, res)
	} else {
		c.JSON(http.StatusOK, serializer.Response{
			Status:  e.InvalidParams,
			Data:    nil,
			Message: e.GetMessage(e.InvalidParams),
			Error:   err.Error(),
		})
	}
}
type TaskDetailDTO struct {
	ID uint `uri:"tid" binding:"required"`
}
// TaskDetail 查询task详情
func (taskDetailDTO *TaskDetailDTO) TaskDetail(uid uint) serializer.Response {
	var task model.Task
	if err := model.DB.Model(&task).Where("id = ? and uid = ?", taskDetailDTO.ID, uid).First(&task).Error; err != nil {
		if gorm.IsRecordNotFoundError(err) {
			return serializer.Response{
				Status:  e.ErrorTaskNotFound,
				Data:    nil,
				Message: e.GetMessage(e.ErrorTaskNotFound),
				Error:   err.Error(),
			}
		} else {
			return serializer.Response{
				Status:  e.ErrorDatabase,
				Data:    nil,
				Message: e.GetMessage(e.ErrorDatabase),
				Error:   err.Error(),
			}
		}
	}
	return serializer.Response{
		Status:  e.SUCCESS,
		Data:    serializer.BuildTaskDetail(task),
		Message: e.GetMessage(e.SUCCESS),
		Error:   "",
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant