group.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package controllers
  2. import (
  3. "encoding/json"
  4. log "github.com/sirupsen/logrus"
  5. "io"
  6. "net/http"
  7. "pmail/db"
  8. "pmail/dto"
  9. "pmail/dto/response"
  10. "pmail/i18n"
  11. "pmail/services/group"
  12. "pmail/utils/array"
  13. "pmail/utils/context"
  14. )
  15. func GetUserGroupList(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
  16. infos := group.GetGroupList(ctx)
  17. response.NewSuccessResponse(infos).FPrint(w)
  18. }
  19. func GetUserGroup(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
  20. retData := []*group.GroupItem{
  21. {
  22. Label: i18n.GetText(ctx.Lang, "all_email"),
  23. Children: []*group.GroupItem{
  24. {
  25. Label: i18n.GetText(ctx.Lang, "inbox"),
  26. Tag: dto.SearchTag{Type: 0, Status: -1, GroupId: 0}.ToString(),
  27. },
  28. {
  29. Label: i18n.GetText(ctx.Lang, "outbox"),
  30. Tag: dto.SearchTag{Type: 1, Status: -1}.ToString(),
  31. },
  32. {
  33. Label: i18n.GetText(ctx.Lang, "sketch"),
  34. Tag: dto.SearchTag{Type: 1, Status: 0}.ToString(),
  35. },
  36. {
  37. Label: i18n.GetText(ctx.Lang, "deleted"),
  38. Tag: dto.SearchTag{Type: -1, Status: 3}.ToString(),
  39. },
  40. },
  41. },
  42. }
  43. retData = array.Merge(retData, group.GetGroupInfoList(ctx))
  44. response.NewSuccessResponse(retData).FPrint(w)
  45. }
  46. type addGroupRequest struct {
  47. Name string `json:"name"`
  48. ParentId int `json:"parent_id"`
  49. }
  50. func AddGroup(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
  51. var reqData *addGroupRequest
  52. reqBytes, err := io.ReadAll(req.Body)
  53. if err != nil {
  54. log.WithContext(ctx).Errorf("%+v", err)
  55. }
  56. err = json.Unmarshal(reqBytes, &reqData)
  57. if err != nil {
  58. log.WithContext(ctx).Errorf("%+v", err)
  59. }
  60. res, err := db.Instance.Exec(db.WithContext(ctx, "insert into `group` (name,parent_id,user_id) values (?,?,?)"), reqData.Name, reqData.ParentId, ctx.UserID)
  61. if err != nil {
  62. response.NewErrorResponse(response.ServerError, "DBError", err.Error()).FPrint(w)
  63. return
  64. }
  65. id, err := res.LastInsertId()
  66. if err != nil {
  67. response.NewErrorResponse(response.ServerError, "DBError", err.Error()).FPrint(w)
  68. return
  69. }
  70. response.NewSuccessResponse(id).FPrint(w)
  71. }
  72. type delGroupRequest struct {
  73. Id int `json:"id"`
  74. }
  75. func DelGroup(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
  76. var reqData *delGroupRequest
  77. reqBytes, err := io.ReadAll(req.Body)
  78. if err != nil {
  79. log.WithContext(ctx).Errorf("%+v", err)
  80. }
  81. err = json.Unmarshal(reqBytes, &reqData)
  82. if err != nil {
  83. log.WithContext(ctx).Errorf("%+v", err)
  84. }
  85. succ, err := group.DelGroup(ctx, reqData.Id)
  86. if err != nil {
  87. response.NewErrorResponse(response.ServerError, "DBError", err.Error()).FPrint(w)
  88. return
  89. }
  90. response.NewSuccessResponse(succ).FPrint(w)
  91. }