作者 linmadan

完成任务发布接口

@@ -122,10 +122,14 @@ func (taskService *TaskService) ReleaseTask(releaseTaskCommand *command.ReleaseT @@ -122,10 +122,14 @@ func (taskService *TaskService) ReleaseTask(releaseTaskCommand *command.ReleaseT
122 } else { 122 } else {
123 releaseTaskService = value 123 releaseTaskService = value
124 } 124 }
  125 + if task, err := releaseTaskService.Release(releaseTaskCommand.TaskId, releaseTaskCommand.Operator); err != nil {
  126 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  127 + } else {
125 if err := transactionContext.CommitTransaction(); err != nil { 128 if err := transactionContext.CommitTransaction(); err != nil {
126 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 129 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
127 } 130 }
128 - return nil, nil 131 + return task, nil
  132 + }
129 } 133 }
130 134
131 // 关闭任务 135 // 关闭任务
@@ -44,7 +44,7 @@ func (service *ReleaseTaskService) Release(taskId int64, operatorUid int64) (*do @@ -44,7 +44,7 @@ func (service *ReleaseTaskService) Release(taskId int64, operatorUid int64) (*do
44 if task == nil { 44 if task == nil {
45 return nil, fmt.Errorf("无效的任务") 45 return nil, fmt.Errorf("无效的任务")
46 } 46 }
47 - if operator.EmployeeId != task.Sponsor.Uid { 47 + if operator.EmployeeInfo.Uid != task.Sponsor.Uid {
48 return nil, fmt.Errorf("无效的发布者") 48 return nil, fmt.Errorf("无效的发布者")
49 } 49 }
50 if err := task.Release(); err != nil { 50 if err := task.Release(); err != nil {
@@ -63,9 +63,13 @@ func (repository *EmployeeRepository) FindOne(queryOptions map[string]interface{ @@ -63,9 +63,13 @@ func (repository *EmployeeRepository) FindOne(queryOptions map[string]interface{
63 if uid, ok := queryOptions["uid"]; ok { 63 if uid, ok := queryOptions["uid"]; ok {
64 query = query.Where("employee.uid = ?", uid) 64 query = query.Where("employee.uid = ?", uid)
65 } 65 }
66 - if err := query.Limit(1).Select(); err != nil { 66 + if err := query.First(); err != nil {
  67 + if err.Error() == "pg: no rows in result set" {
  68 + return nil, fmt.Errorf("没有此资源")
  69 + } else {
67 return nil, err 70 return nil, err
68 } 71 }
  72 + }
69 if employeeModel.Id == 0 { 73 if employeeModel.Id == 0 {
70 return nil, nil 74 return nil, nil
71 } else { 75 } else {
@@ -71,8 +71,12 @@ func (repository *TaskRepository) FindOne(queryOptions map[string]interface{}) ( @@ -71,8 +71,12 @@ func (repository *TaskRepository) FindOne(queryOptions map[string]interface{}) (
71 71
72 } 72 }
73 if err := query.First(); err != nil { 73 if err := query.First(); err != nil {
  74 + if err.Error() == "pg: no rows in result set" {
  75 + return nil, fmt.Errorf("没有此资源")
  76 + } else {
74 return nil, err 77 return nil, err
75 } 78 }
  79 + }
76 if taskModel.Id == 0 { 80 if taskModel.Id == 0 {
77 return nil, nil 81 return nil, nil
78 } else { 82 } else {
@@ -30,8 +30,13 @@ var _ = Describe("发布任务", func() { @@ -30,8 +30,13 @@ var _ = Describe("发布任务", func() {
30 }, 30 },
31 }, "null", "", pg.Array([]string{}), 2499036607974745099, time.Now(), time.Now().Add(dayAfter)) 31 }, "null", "", pg.Array([]string{}), 2499036607974745099, time.Now(), time.Now().Add(dayAfter))
32 Expect(err).NotTo(HaveOccurred()) 32 Expect(err).NotTo(HaveOccurred())
  33 + _, err1 := pG.DB.QueryOne(
  34 + pg.Scan(),
  35 + "INSERT INTO employees (id, uid, employee_name, employee_account, su_money) VALUES (?, ?, ?, ?, ?)",
  36 + 1, 2499036607974745088, "testEmployeeName", "testEmployeeAccount", 0)
  37 + Expect(err1).NotTo(HaveOccurred())
33 }) 38 })
34 - FIt("发布任务成功", func() { 39 + It("发布任务成功", func() {
35 httpExpect := httpexpect.New(GinkgoT(), server.URL) 40 httpExpect := httpexpect.New(GinkgoT(), server.URL)
36 body := map[string]interface{}{ 41 body := map[string]interface{}{
37 "operator": 2499036607974745088, 42 "operator": 2499036607974745088,
@@ -47,6 +52,44 @@ var _ = Describe("发布任务", func() { @@ -47,6 +52,44 @@ var _ = Describe("发布任务", func() {
47 ContainsKey("data").Value("data").Object() 52 ContainsKey("data").Value("data").Object()
48 }) 53 })
49 }) 54 })
  55 + Context("任务发起者发布不是待发布的任务", func() {
  56 + BeforeEach(func() {
  57 + dayAfter, _ := time.ParseDuration("72h")
  58 + _, err := pG.DB.QueryOne(
  59 + pg.Scan(),
  60 + "INSERT INTO tasks (id, company_id, task_name, task_type, sponsor, task_status, reference_resource, customer_value, task_nature, su_money, acceptance_standard, task_description, task_picture_urls, is_reward_take, participators, task_percentage, solve_report, solve_picture_urls, receiver_uid, create_time, release_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
  61 + 1, 101, "抢单任务1",1, &domain.EmployeeInfo{
  62 + Uid: 2499036607974745088,
  63 + }, 3, "null", pg.Array([]string{"口感", "便利", "品牌", "售后服务"}), "面", 1000.00, "验收标准1", "任务描述1", pg.Array([]string{}), true, []*domain.EmployeeInfo{
  64 + {
  65 + Uid: 2499036607974745077,
  66 + },
  67 + {
  68 + Uid: 2499036607974745066,
  69 + },
  70 + }, "null", "", pg.Array([]string{}), 2499036607974745099, time.Now(), time.Now().Add(dayAfter))
  71 + Expect(err).NotTo(HaveOccurred())
  72 + _, err1 := pG.DB.QueryOne(
  73 + pg.Scan(),
  74 + "INSERT INTO employees (id, uid, employee_name, employee_account, su_money) VALUES (?, ?, ?, ?, ?)",
  75 + 1, 2499036607974745088, "testEmployeeName", "testEmployeeAccount", 0)
  76 + Expect(err1).NotTo(HaveOccurred())
  77 + })
  78 + It("发布任务失败", func() {
  79 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  80 + body := map[string]interface{}{
  81 + "operator": 2499036607974745088,
  82 + }
  83 + httpExpect.POST("/tasks/1/release").
  84 + WithJSON(body).
  85 + Expect().
  86 + Status(http.StatusOK).
  87 + JSON().
  88 + Object().
  89 + ContainsKey("code").ValueEqual("code", 501).
  90 + ContainsKey("msg").ValueEqual("msg", "内部服务出错:进行中的任务不允许重新发布")
  91 + })
  92 + })
50 }) 93 })
51 AfterEach(func() { 94 AfterEach(func() {
52 _, err := pG.DB.Exec("DELETE FROM tasks WHERE true") 95 _, err := pG.DB.Exec("DELETE FROM tasks WHERE true")
@@ -55,5 +98,7 @@ var _ = Describe("发布任务", func() { @@ -55,5 +98,7 @@ var _ = Describe("发布任务", func() {
55 Expect(err1).NotTo(HaveOccurred()) 98 Expect(err1).NotTo(HaveOccurred())
56 _, err2 := pG.DB.Exec("DELETE FROM bidder_infos WHERE true") 99 _, err2 := pG.DB.Exec("DELETE FROM bidder_infos WHERE true")
57 Expect(err2).NotTo(HaveOccurred()) 100 Expect(err2).NotTo(HaveOccurred())
  101 + _, err3 := pG.DB.Exec("DELETE FROM employees WHERE true")
  102 + Expect(err3).NotTo(HaveOccurred())
58 }) 103 })
59 }) 104 })