group.go 2.5 KB

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