place.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package mappers
  2. import (
  3. "database/sql"
  4. "sort"
  5. "git.dmitriygnatenko.ru/dima/homethings/internal/dto"
  6. "git.dmitriygnatenko.ru/dima/homethings/internal/models"
  7. )
  8. func ToPlaceResponse(req models.Place) dto.PlaceResponse {
  9. res := dto.PlaceResponse{
  10. ID: req.ID,
  11. Title: req.Title,
  12. CreatedAt: req.CreatedAt.Format(defaultDateTimeLayout),
  13. UpdatedAt: req.UpdatedAt.Format(defaultDateTimeLayout),
  14. }
  15. if req.ParentID.Valid {
  16. parentID := int(req.ParentID.Int64)
  17. res.ParentID = &parentID
  18. }
  19. return res
  20. }
  21. func ToAddPlaceRequest(req dto.AddPlaceRequest) models.AddPlaceRequest {
  22. res := models.AddPlaceRequest{
  23. Title: req.Title,
  24. }
  25. if req.ParentID != nil {
  26. res.ParentID = sql.NullInt64{Int64: int64(*req.ParentID), Valid: true}
  27. }
  28. return res
  29. }
  30. func ToUpdatePlaceRequest(id int, req dto.UpdatePlaceRequest) models.UpdatePlaceRequest {
  31. res := models.UpdatePlaceRequest{
  32. ID: id,
  33. Title: req.Title,
  34. }
  35. if req.ParentID != nil {
  36. res.ParentID = sql.NullInt64{Int64: int64(*req.ParentID), Valid: true}
  37. }
  38. return res
  39. }
  40. func ToPlacesResponse(req []models.Place) dto.PlacesResponse {
  41. res := make([]dto.PlaceResponse, 0, len(req))
  42. for _, p := range req {
  43. res = append(res, ToPlaceResponse(p))
  44. }
  45. sort.Slice(res, func(i, j int) bool {
  46. return res[i].Title < res[j].Title
  47. })
  48. return dto.PlacesResponse{
  49. Places: res,
  50. }
  51. }
  52. func ToPlaceTreeResponse(req []models.Place) dto.PlaceTreeResponse {
  53. var res []dto.PlaceTree
  54. parentMap := make(map[int][]models.Place, len(req))
  55. for _, p := range req {
  56. if !p.ParentID.Valid {
  57. res = append(res, dto.PlaceTree{
  58. Place: ToPlaceResponse(p),
  59. })
  60. continue
  61. }
  62. parentID := int(p.ParentID.Int64)
  63. parentMap[parentID] = append(parentMap[parentID], p)
  64. }
  65. sort.Slice(res, func(i, j int) bool {
  66. return res[i].Place.Title < res[j].Place.Title
  67. })
  68. for i := range res {
  69. recursiveFillNested(&res[i], parentMap)
  70. }
  71. return dto.PlaceTreeResponse{
  72. Places: res,
  73. }
  74. }
  75. func recursiveFillNested(item *dto.PlaceTree, parentMap map[int][]models.Place) {
  76. id := item.Place.ID
  77. if _, ok := parentMap[id]; ok {
  78. for _, childPlace := range parentMap[id] {
  79. nestedItem := dto.PlaceTree{
  80. Place: ToPlaceResponse(childPlace),
  81. }
  82. recursiveFillNested(&nestedItem, parentMap)
  83. item.NestedPlaces = append(item.NestedPlaces, nestedItem)
  84. }
  85. sort.Slice(item.NestedPlaces, func(i, j int) bool {
  86. return item.NestedPlaces[i].Place.Title < item.NestedPlaces[j].Place.Title
  87. })
  88. }
  89. }