Commit e7bdc9fd authored by 贾浩@五瓣科技's avatar 贾浩@五瓣科技

update task status

parent 99adfe02
......@@ -77,8 +77,11 @@ func (d *Dao) GetGroup(id int) (g *dbModel.TaskGroup, err error) {
return g, err
}
func (d *Dao) GetGroupTasks(gid int) (list []*dbModel.Task, err error) {
return list, d.db.Where("group_id = ?", gid).Find(&list).Error
func (d *Dao) GetGroupTasks(gid int, admin bool) (list []*dbModel.Task, err error) {
if admin {
return list, d.db.Where("group_id = ?", gid).Find(&list).Error
}
return list, d.db.Where("group_id = ? and enable = ?", gid, true).Find(&list).Error
}
func (d *Dao) CreateTask(gt *dbModel.Task) (err error) {
......@@ -95,6 +98,16 @@ func (d *Dao) GetTaskDetail(tid int) (t *dbModel.Task, err error) {
}
func (d *Dao) GetTaskOwner(tid, gid int) (userId string, err error) {
// 联表group task,判断是否是该用户创建的任务组
sql := fmt.Sprintf(
"SELECT t2.user_id FROM %s t1 LEFT JOIN %s t2 ON t1.group_id = t2.id WHERE t1.id = ? AND t2.id = ?",
(&dbModel.Task{}).TableName(), (&dbModel.TaskGroup{}).TableName(),
)
err = d.db.Raw(sql, tid, gid).Scan(&userId).Error
return
}
func (d *Dao) IsDailyTask(tid int) (ok bool, err error) {
var count int64
err = d.db.Model(&dbModel.Task{}).Where("id = ? and daily = ?", tid, true).Count(&count).Error
......@@ -232,3 +245,20 @@ func (d *Dao) IsAdminUser(userId string) (ok bool, err error) {
func (d *Dao) GetUnstoppedTasks() {
}
func (d *Dao) EditTask(taskId int, delete bool, enable bool) (err error) {
if delete {
err = d.db.Model(&dbModel.Task{}).Where("id = ?", taskId).Updates(map[string]interface{}{
"enable": false,
"updated_at": "now()",
"deleted_at": "now()",
}).Error
} else {
err = d.db.Model(&dbModel.Task{}).Where("id = ?", taskId).Updates(map[string]interface{}{
"enable": enable,
"updated_at": "now()",
}).Error
}
return
}
......@@ -43,18 +43,18 @@ func JWTMiddleware(d *dao.Dao, secret string, needAdmin bool) gin.HandlerFunc {
return
}
ok, err := d.IsAdminUser(uid)
if err != nil {
log.WithError(err).Error("auth is admin")
c.JSON(200, gin.H{
"code": 1,
"msg": constant.InternalError,
"data": "",
})
c.Abort()
return
}
if needAdmin {
ok, err := d.IsAdminUser(uid)
if err != nil {
log.WithError(err).Error("auth is admin")
c.JSON(200, gin.H{
"code": 1,
"msg": constant.InternalError,
"data": "",
})
c.Abort()
return
}
if !ok {
c.JSON(200, gin.H{
"code": 1,
......@@ -68,6 +68,7 @@ func JWTMiddleware(d *dao.Dao, secret string, needAdmin bool) gin.HandlerFunc {
log.WithField("userId", uid).Debug("jwt uid")
c.Set("userId", uid)
c.Set("admin", ok)
c.Next()
}
}
......@@ -58,6 +58,7 @@ type Task struct {
Daily bool `json:"daily" binding:"required"`
Status string `json:"status"`
TelegramActiveThreshold int `json:"telegramActiveThreshold"` // 仅电报活跃可用
Enable bool `json:"enable"`
}
type CreateTaskRequest struct {
......@@ -79,7 +80,15 @@ type CreateTaskRequest struct {
TelegramChatId int `json:"telegramChatId"`
UserId string `json:"-"`
TaskId int `json:"-"`
GroupId int `json:"groupId"`
GroupId int `json:"groupId" binding:"required"`
}
type EditTaskRequest struct {
TaskId int `json:"taskId" binding:"required"`
GroupId int `json:"groupId" binding:"required"`
Enable bool `json:"enable"`
Delete bool `json:"delete"`
UserId string `json:"-"`
}
type GetGroupResponse struct {
......
......@@ -82,6 +82,7 @@ type Task struct {
Daily bool `gorm:"type:bool;not null;comment:是否是每日任务"`
TwitterTaskEndAt sql.NullTime `gorm:"index;comment:推特可用,推特任务中心已停止"`
TelegramActiveThreshold int `gorm:"type:int;not null;comment:telegram群活跃阈值"`
Enable bool `gorm:"type:bool;not null;comment:是否启用"`
Config
gorm.Model
}
......
......@@ -33,8 +33,8 @@ func getGroup(c *gin.Context) {
return
}
userId := c.GetString("userId")
resp, err := srv.GetGroup(gid, userId)
admin := c.GetBool("admin")
resp, err := srv.GetGroup(gid, userId, admin)
if err != nil {
c.JSON(200, withError(constant.InternalError))
return
......@@ -52,8 +52,8 @@ func listGroup(c *gin.Context) {
c.JSON(200, withError(constant.InvalidParam))
return
}
resp, err := srv.GetGroupList(page, pageSize)
admin := c.GetBool("admin")
resp, err := srv.GetGroupList(page, pageSize, admin)
if err != nil {
c.JSON(200, withError(constant.InternalError))
return
......
......@@ -20,9 +20,10 @@ func initRouter(e *gin.Engine) {
{
task := v1.Group("/task")
task.POST("/create", createTask, middleware.JWTMiddleware(d, conf.Supabase.JWTSecret, true)) // 创建任务
task.POST("/submit/:tid", submitTask, middleware.JWTMiddleware(d, conf.Supabase.JWTSecret, false)) // 提交任务
task.GET("/check/:tid", checkTask, middleware.JWTMiddleware(d, conf.Supabase.JWTSecret, false)) // 检查任务是否完成
task.POST("/create", middleware.JWTMiddleware(d, conf.Supabase.JWTSecret, true), createTask) // 创建任务
task.POST("/edit", middleware.JWTMiddleware(d, conf.Supabase.JWTSecret, true), editTask) // 编辑任务
task.POST("/submit/:tid", middleware.JWTMiddleware(d, conf.Supabase.JWTSecret, false), submitTask) // 提交任务
task.GET("/check/:tid", middleware.JWTMiddleware(d, conf.Supabase.JWTSecret, false), checkTask) // 检查任务是否完成
}
}
......@@ -34,6 +34,37 @@ func createTask(c *gin.Context) {
c.JSON(200, withSuccess(gin.H{"taskId": taskId}))
}
func editTask(c *gin.Context) {
req := &apiModel.EditTaskRequest{}
if err := c.ShouldBindJSON(req); err != nil {
c.JSON(200, withError(constant.InvalidParam))
return
}
req.UserId = c.GetString("userId")
userId, err := srv.GetTaskOwner(req.TaskId, req.GroupId)
if err != nil {
c.JSON(200, withError(constant.InternalError))
return
}
if userId != req.UserId {
c.JSON(200, withError("permission denied"))
return
}
if req.Delete {
req.Enable = false
}
err = srv.EditTask(req)
if err != nil {
c.JSON(200, withError(constant.InternalError))
return
}
c.JSON(200, withSuccess(""))
}
func checkTask(c *gin.Context) {
_taskId := c.Param("tid")
taskId, _ := strconv.Atoi(_taskId)
......
......@@ -38,7 +38,7 @@ func (s *Service) CheckGroup(gid int, userId string) (exist bool, err error) {
return true, nil
}
func (s *Service) GetGroup(gid int, userId string) (resp *apiModel.GetGroupResponse, err error) {
func (s *Service) GetGroup(gid int, userId string, admin bool) (resp *apiModel.GetGroupResponse, err error) {
resp = &apiModel.GetGroupResponse{Tasks: make([]apiModel.Task, 0)}
g, err := s.d.GetGroup(gid)
if err != nil {
......@@ -52,7 +52,7 @@ func (s *Service) GetGroup(gid int, userId string) (resp *apiModel.GetGroupRespo
resp.Description = g.Description
tasks, err := s.d.GetGroupTasks(g.Id)
tasks, err := s.d.GetGroupTasks(g.Id, admin)
if err != nil {
log.WithError(err).Error("get group tasks error")
return
......@@ -70,6 +70,7 @@ func (s *Service) GetGroup(gid int, userId string) (resp *apiModel.GetGroupRespo
End: task.End,
Daily: task.Daily,
TweetId: task.TweetId,
Enable: task.Enable,
}
status, err := s.GetTaskResult(_task.TaskId, userId)
......@@ -83,7 +84,7 @@ func (s *Service) GetGroup(gid int, userId string) (resp *apiModel.GetGroupRespo
return
}
func (s *Service) GetGroupList(page, pageSize int) (resp *apiModel.GetGroupListResponse, err error) {
func (s *Service) GetGroupList(page, pageSize int, admin bool) (resp *apiModel.GetGroupListResponse, err error) {
resp = &apiModel.GetGroupListResponse{}
list, count, err := s.d.GetGroupList(page, pageSize)
if err != nil {
......@@ -98,7 +99,7 @@ func (s *Service) GetGroupList(page, pageSize int) (resp *apiModel.GetGroupListR
Description: v.Description,
})
tasks, err := s.d.GetGroupTasks(v.Id)
tasks, err := s.d.GetGroupTasks(v.Id, admin)
if err != nil {
log.WithError(err).Error("get group tasks error")
return resp, err
......@@ -116,6 +117,7 @@ func (s *Service) GetGroupList(page, pageSize int) (resp *apiModel.GetGroupListR
End: task.End,
Daily: task.Daily,
TweetId: task.TweetId,
Enable: task.Enable,
})
}
}
......
......@@ -90,6 +90,22 @@ func (s *Service) CreateTask(req *apiModel.CreateTaskRequest) (taskId int, err e
return task.Id, nil
}
func (s *Service) EditTask(req *apiModel.EditTaskRequest) (err error) {
err = s.d.EditTask(req.TaskId, req.Delete, req.Enable)
if err != nil {
log.WithError(err).Error("edit task error")
}
return
}
func (s *Service) GetTaskOwner(taskId, groupId int) (userId string, err error) {
userId, err = s.d.GetTaskOwner(taskId, groupId)
if err != nil {
log.WithError(err).Error("get task owner error")
}
return
}
func (s *Service) CheckTask(taskId int, userId string) (exist, done bool, expired bool, err error) {
exist = true
task, err := s.d.GetTaskDetail(taskId)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment