group.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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, "deleted"),
  37. Tag: dto.SearchTag{Type: -1, Status: 3}.ToString(),
  38. },
  39. },
  40. },
  41. }
  42. retData = array.Merge(retData, group.GetGroupInfoList(ctx))
  43. response.NewSuccessResponse(retData).FPrint(w)
  44. }
  45. type addGroupRequest struct {
  46. Name string `json:"name"`
  47. ParentId int `json:"parent_id"`
  48. }
  49. func AddGroup(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
  50. var reqData *addGroupRequest
  51. reqBytes, err := io.ReadAll(req.Body)
  52. if err != nil {
  53. log.WithContext(ctx).Errorf("%+v", err)
  54. }
  55. err = json.Unmarshal(reqBytes, &reqData)
  56. if err != nil {
  57. log.WithContext(ctx).Errorf("%+v", err)
  58. }
  59. newGroup, err := group.CreateGroup(ctx, reqData.Name, reqData.ParentId)
  60. if err != nil {
  61. response.NewErrorResponse(response.ServerError, "DBError", err.Error()).FPrint(w)
  62. return
  63. }
  64. response.NewSuccessResponse(newGroup.ID).FPrint(w)
  65. }
  66. type delGroupRequest struct {
  67. Id int `json:"id"`
  68. }
  69. func DelGroup(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
  70. var reqData *delGroupRequest
  71. reqBytes, err := io.ReadAll(req.Body)
  72. if err != nil {
  73. log.WithContext(ctx).Errorf("%+v", err)
  74. }
  75. err = json.Unmarshal(reqBytes, &reqData)
  76. if err != nil {
  77. log.WithContext(ctx).Errorf("%+v", err)
  78. }
  79. succ, err := group.DelGroup(ctx, reqData.Id)
  80. if err != nil {
  81. response.NewErrorResponse(response.ServerError, "DBError", err.Error()).FPrint(w)
  82. return
  83. }
  84. response.NewSuccessResponse(succ).FPrint(w)
  85. }