group.go 2.6 KB

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