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) { ...@@ -77,8 +77,11 @@ func (d *Dao) GetGroup(id int) (g *dbModel.TaskGroup, err error) {
return g, err return g, err
} }
func (d *Dao) GetGroupTasks(gid int) (list []*dbModel.Task, err error) { func (d *Dao) GetGroupTasks(gid int, admin bool) (list []*dbModel.Task, err error) {
return list, d.db.Where("group_id = ?", gid).Find(&list).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) { func (d *Dao) CreateTask(gt *dbModel.Task) (err error) {
...@@ -95,6 +98,16 @@ func (d *Dao) GetTaskDetail(tid int) (t *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) { func (d *Dao) IsDailyTask(tid int) (ok bool, err error) {
var count int64 var count int64
err = d.db.Model(&dbModel.Task{}).Where("id = ? and daily = ?", tid, true).Count(&count).Error 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) { ...@@ -232,3 +245,20 @@ func (d *Dao) IsAdminUser(userId string) (ok bool, err error) {
func (d *Dao) GetUnstoppedTasks() { 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 { ...@@ -43,18 +43,18 @@ func JWTMiddleware(d *dao.Dao, secret string, needAdmin bool) gin.HandlerFunc {
return 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 { 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 { if !ok {
c.JSON(200, gin.H{ c.JSON(200, gin.H{
"code": 1, "code": 1,
...@@ -68,6 +68,7 @@ func JWTMiddleware(d *dao.Dao, secret string, needAdmin bool) gin.HandlerFunc { ...@@ -68,6 +68,7 @@ func JWTMiddleware(d *dao.Dao, secret string, needAdmin bool) gin.HandlerFunc {
log.WithField("userId", uid).Debug("jwt uid") log.WithField("userId", uid).Debug("jwt uid")
c.Set("userId", uid) c.Set("userId", uid)
c.Set("admin", ok)
c.Next() c.Next()
} }
} }
...@@ -58,6 +58,7 @@ type Task struct { ...@@ -58,6 +58,7 @@ type Task struct {
Daily bool `json:"daily" binding:"required"` Daily bool `json:"daily" binding:"required"`
Status string `json:"status"` Status string `json:"status"`
TelegramActiveThreshold int `json:"telegramActiveThreshold"` // 仅电报活跃可用 TelegramActiveThreshold int `json:"telegramActiveThreshold"` // 仅电报活跃可用
Enable bool `json:"enable"`
} }
type CreateTaskRequest struct { type CreateTaskRequest struct {
...@@ -79,7 +80,15 @@ type CreateTaskRequest struct { ...@@ -79,7 +80,15 @@ type CreateTaskRequest struct {
TelegramChatId int `json:"telegramChatId"` TelegramChatId int `json:"telegramChatId"`
UserId string `json:"-"` UserId string `json:"-"`
TaskId int `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 { type GetGroupResponse struct {
......
...@@ -82,6 +82,7 @@ type Task struct { ...@@ -82,6 +82,7 @@ type Task struct {
Daily bool `gorm:"type:bool;not null;comment:是否是每日任务"` Daily bool `gorm:"type:bool;not null;comment:是否是每日任务"`
TwitterTaskEndAt sql.NullTime `gorm:"index;comment:推特可用,推特任务中心已停止"` TwitterTaskEndAt sql.NullTime `gorm:"index;comment:推特可用,推特任务中心已停止"`
TelegramActiveThreshold int `gorm:"type:int;not null;comment:telegram群活跃阈值"` TelegramActiveThreshold int `gorm:"type:int;not null;comment:telegram群活跃阈值"`
Enable bool `gorm:"type:bool;not null;comment:是否启用"`
Config Config
gorm.Model gorm.Model
} }
......
...@@ -33,8 +33,8 @@ func getGroup(c *gin.Context) { ...@@ -33,8 +33,8 @@ func getGroup(c *gin.Context) {
return return
} }
userId := c.GetString("userId") userId := c.GetString("userId")
admin := c.GetBool("admin")
resp, err := srv.GetGroup(gid, userId) resp, err := srv.GetGroup(gid, userId, admin)
if err != nil { if err != nil {
c.JSON(200, withError(constant.InternalError)) c.JSON(200, withError(constant.InternalError))
return return
...@@ -52,8 +52,8 @@ func listGroup(c *gin.Context) { ...@@ -52,8 +52,8 @@ func listGroup(c *gin.Context) {
c.JSON(200, withError(constant.InvalidParam)) c.JSON(200, withError(constant.InvalidParam))
return return
} }
admin := c.GetBool("admin")
resp, err := srv.GetGroupList(page, pageSize) resp, err := srv.GetGroupList(page, pageSize, admin)
if err != nil { if err != nil {
c.JSON(200, withError(constant.InternalError)) c.JSON(200, withError(constant.InternalError))
return return
......
...@@ -20,9 +20,10 @@ func initRouter(e *gin.Engine) { ...@@ -20,9 +20,10 @@ func initRouter(e *gin.Engine) {
{ {
task := v1.Group("/task") task := v1.Group("/task")
task.POST("/create", createTask, middleware.JWTMiddleware(d, conf.Supabase.JWTSecret, true)) // 创建任务 task.POST("/create", middleware.JWTMiddleware(d, conf.Supabase.JWTSecret, true), createTask) // 创建任务
task.POST("/submit/:tid", submitTask, middleware.JWTMiddleware(d, conf.Supabase.JWTSecret, false)) // 提交任务 task.POST("/edit", middleware.JWTMiddleware(d, conf.Supabase.JWTSecret, true), editTask) // 编辑任务
task.GET("/check/:tid", checkTask, middleware.JWTMiddleware(d, conf.Supabase.JWTSecret, false)) // 检查任务是否完成 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) { ...@@ -34,6 +34,37 @@ func createTask(c *gin.Context) {
c.JSON(200, withSuccess(gin.H{"taskId": taskId})) 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) { func checkTask(c *gin.Context) {
_taskId := c.Param("tid") _taskId := c.Param("tid")
taskId, _ := strconv.Atoi(_taskId) taskId, _ := strconv.Atoi(_taskId)
......
...@@ -38,7 +38,7 @@ func (s *Service) CheckGroup(gid int, userId string) (exist bool, err error) { ...@@ -38,7 +38,7 @@ func (s *Service) CheckGroup(gid int, userId string) (exist bool, err error) {
return true, nil 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)} resp = &apiModel.GetGroupResponse{Tasks: make([]apiModel.Task, 0)}
g, err := s.d.GetGroup(gid) g, err := s.d.GetGroup(gid)
if err != nil { if err != nil {
...@@ -52,7 +52,7 @@ func (s *Service) GetGroup(gid int, userId string) (resp *apiModel.GetGroupRespo ...@@ -52,7 +52,7 @@ func (s *Service) GetGroup(gid int, userId string) (resp *apiModel.GetGroupRespo
resp.Description = g.Description resp.Description = g.Description
tasks, err := s.d.GetGroupTasks(g.Id) tasks, err := s.d.GetGroupTasks(g.Id, admin)
if err != nil { if err != nil {
log.WithError(err).Error("get group tasks error") log.WithError(err).Error("get group tasks error")
return return
...@@ -70,6 +70,7 @@ func (s *Service) GetGroup(gid int, userId string) (resp *apiModel.GetGroupRespo ...@@ -70,6 +70,7 @@ func (s *Service) GetGroup(gid int, userId string) (resp *apiModel.GetGroupRespo
End: task.End, End: task.End,
Daily: task.Daily, Daily: task.Daily,
TweetId: task.TweetId, TweetId: task.TweetId,
Enable: task.Enable,
} }
status, err := s.GetTaskResult(_task.TaskId, userId) status, err := s.GetTaskResult(_task.TaskId, userId)
...@@ -83,7 +84,7 @@ func (s *Service) GetGroup(gid int, userId string) (resp *apiModel.GetGroupRespo ...@@ -83,7 +84,7 @@ func (s *Service) GetGroup(gid int, userId string) (resp *apiModel.GetGroupRespo
return 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{} resp = &apiModel.GetGroupListResponse{}
list, count, err := s.d.GetGroupList(page, pageSize) list, count, err := s.d.GetGroupList(page, pageSize)
if err != nil { if err != nil {
...@@ -98,7 +99,7 @@ func (s *Service) GetGroupList(page, pageSize int) (resp *apiModel.GetGroupListR ...@@ -98,7 +99,7 @@ func (s *Service) GetGroupList(page, pageSize int) (resp *apiModel.GetGroupListR
Description: v.Description, Description: v.Description,
}) })
tasks, err := s.d.GetGroupTasks(v.Id) tasks, err := s.d.GetGroupTasks(v.Id, admin)
if err != nil { if err != nil {
log.WithError(err).Error("get group tasks error") log.WithError(err).Error("get group tasks error")
return resp, err return resp, err
...@@ -116,6 +117,7 @@ func (s *Service) GetGroupList(page, pageSize int) (resp *apiModel.GetGroupListR ...@@ -116,6 +117,7 @@ func (s *Service) GetGroupList(page, pageSize int) (resp *apiModel.GetGroupListR
End: task.End, End: task.End,
Daily: task.Daily, Daily: task.Daily,
TweetId: task.TweetId, TweetId: task.TweetId,
Enable: task.Enable,
}) })
} }
} }
......
...@@ -90,6 +90,22 @@ func (s *Service) CreateTask(req *apiModel.CreateTaskRequest) (taskId int, err e ...@@ -90,6 +90,22 @@ func (s *Service) CreateTask(req *apiModel.CreateTaskRequest) (taskId int, err e
return task.Id, nil 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) { func (s *Service) CheckTask(taskId int, userId string) (exist, done bool, expired bool, err error) {
exist = true exist = true
task, err := s.d.GetTaskDetail(taskId) 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