group.go 2.5 KB

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