tag.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package mappers
  2. import (
  3. "sort"
  4. "git.dmitriygnatenko.ru/dima/homethings/internal/dto"
  5. "git.dmitriygnatenko.ru/dima/homethings/internal/models"
  6. )
  7. func ToAddTagRequest(req dto.AddTagRequest) models.AddTagRequest {
  8. return models.AddTagRequest{
  9. Title: req.Title,
  10. Style: req.Style,
  11. }
  12. }
  13. func ToUpdateTagRequest(id uint64, req dto.UpdateTagRequest) models.UpdateTagRequest {
  14. return models.UpdateTagRequest{
  15. ID: id,
  16. Title: req.Title,
  17. Style: req.Style,
  18. }
  19. }
  20. func ToAddThingTagRequest(tagID uint64, thingID uint64) models.AddThingTagRequest {
  21. return models.AddThingTagRequest{
  22. ThingID: thingID,
  23. TagID: tagID,
  24. }
  25. }
  26. func ToDeleteThingTagRequest(tagID uint64, thingID uint64) models.DeleteThingTagRequest {
  27. return models.DeleteThingTagRequest{
  28. ThingID: thingID,
  29. TagID: tagID,
  30. }
  31. }
  32. func ToTagResponse(req models.Tag) dto.TagResponse {
  33. return dto.TagResponse{
  34. ID: req.ID,
  35. Title: req.Title,
  36. Style: req.Style,
  37. CreatedAt: req.CreatedAt.Format(defaultDateTimeLayout),
  38. UpdatedAt: req.UpdatedAt.Format(defaultDateTimeLayout),
  39. }
  40. }
  41. func ThingTagToTagResponse(req models.ThingTag) dto.TagResponse {
  42. return dto.TagResponse{
  43. ID: req.ID,
  44. Title: req.Title,
  45. Style: req.Style,
  46. CreatedAt: req.CreatedAt.Format(defaultDateTimeLayout),
  47. UpdatedAt: req.UpdatedAt.Format(defaultDateTimeLayout),
  48. }
  49. }
  50. func ToTagsResponse(req []models.Tag) dto.TagsResponse {
  51. res := make([]dto.TagResponse, 0, len(req))
  52. for _, p := range req {
  53. res = append(res, ToTagResponse(p))
  54. }
  55. sort.Slice(res, func(i, j int) bool {
  56. return res[i].Title < res[j].Title
  57. })
  58. return dto.TagsResponse{
  59. Tags: res,
  60. }
  61. }