task_controller.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package controller
  2. import (
  3. "Zythum/dao"
  4. "encoding/json"
  5. "github.com/gin-gonic/gin"
  6. "log"
  7. "net/http"
  8. "strconv"
  9. )
  10. func writeResponse(ctx *gin.Context, data map[string]interface{}) {
  11. ctx.JSON(http.StatusOK, data)
  12. }
  13. func NewTaskHandler(ctx *gin.Context) {
  14. newTask, err := dao.GetNewTask()
  15. if err != nil {
  16. return
  17. }
  18. taskJson, err := json.Marshal(newTask)
  19. ctx.Header("Content-Type", "application/json")
  20. _, err = ctx.Writer.Write(taskJson)
  21. if err != nil {
  22. log.Println("Failed to write response:", err)
  23. } else {
  24. //log.Println("Get /GetNewTask")
  25. }
  26. }
  27. func UpdateTaskHandler(ctx *gin.Context) {
  28. idString := ctx.Query("id")
  29. statusString := ctx.Query("status")
  30. data := map[string]interface{}{
  31. "update_status": 0,
  32. }
  33. id, err := strconv.Atoi(idString)
  34. if err != nil {
  35. writeResponse(ctx, data)
  36. return
  37. }
  38. status, err1 := strconv.Atoi(statusString)
  39. if err1 != nil {
  40. writeResponse(ctx, data)
  41. return
  42. }
  43. updateResult := dao.UpdateTaskStatus(id, status)
  44. if updateResult != 0 {
  45. writeResponse(ctx, data)
  46. return
  47. }
  48. data["update_status"] = 1
  49. writeResponse(ctx, data)
  50. }
  51. func GetTaskStatusHandler(ctx *gin.Context) {
  52. taskStatus := dao.GetTaskStatus()
  53. taskStatusJson, err := json.Marshal(taskStatus)
  54. ctx.Header("Content-Type", "application/json")
  55. _, err = ctx.Writer.Write(taskStatusJson)
  56. if err != nil {
  57. log.Println("Failed to write response:", err)
  58. } else {
  59. //log.Println("Get /GetTaskStatus")
  60. }
  61. }