group.go 2.9 KB

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