article.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package admin
  2. import (
  3. "strconv"
  4. "github.com/dmitriygnatenko/internal/helpers"
  5. "github.com/dmitriygnatenko/internal/interfaces"
  6. "github.com/dmitriygnatenko/internal/mapper"
  7. "github.com/go-playground/validator/v10"
  8. "github.com/gofiber/fiber/v2"
  9. )
  10. func ArticleHandler(app interfaces.IApp) fiber.Handler {
  11. return func(ctx *fiber.Ctx) error {
  12. articles, err := app.GetArticleRepository().GetAll(ctx.Context())
  13. if err != nil {
  14. return err
  15. }
  16. articlesDTO, err := mapper.ConvertArticleModelsToDTO(articles)
  17. if err != nil {
  18. return err
  19. }
  20. return ctx.Render("admin/article", fiber.Map{
  21. "articles": articlesDTO,
  22. "section": "article",
  23. }, "admin/_layout")
  24. }
  25. }
  26. func AddArticleHandler(app interfaces.IApp) fiber.Handler {
  27. return func(ctx *fiber.Ctx) error {
  28. return nil
  29. }
  30. }
  31. func EditArticleHandler(app interfaces.IApp) fiber.Handler {
  32. return func(ctx *fiber.Ctx) error {
  33. var validate = validator.New()
  34. validateErrors := make(map[string]string)
  35. trans, err := helpers.GetDefaultTranslator(validate)
  36. if err != nil {
  37. return err
  38. }
  39. ID, err := strconv.Atoi(ctx.Params("id"))
  40. if err != nil {
  41. return err
  42. }
  43. article, err := app.GetArticleRepository().GetByID(ctx.Context(), ID)
  44. if err != nil {
  45. return err
  46. }
  47. if article == nil {
  48. return fiber.ErrNotFound
  49. }
  50. articleTags, err := app.GetTagRepository().GetByArticleID(ctx.Context(), ID)
  51. if err != nil {
  52. return err
  53. }
  54. form, err := mapper.ConvertArticleModelToForm(*article, articleTags)
  55. if err != nil {
  56. return err
  57. }
  58. tags, err := app.GetTagRepository().GetAll(ctx.Context())
  59. if err != nil {
  60. return err
  61. }
  62. tagsDTO, err := mapper.ConvertTagModelsToDTO(tags)
  63. if err != nil {
  64. return err
  65. }
  66. if ctx.Method() == fiber.MethodPost {
  67. if err = ctx.BodyParser(&form); err != nil {
  68. return err
  69. }
  70. if err = validate.Struct(form); err != nil {
  71. validateErrors = helpers.FormatValidateErrors(err, trans)
  72. }
  73. // TODO
  74. }
  75. return ctx.Render("admin/article_edit", fiber.Map{
  76. "form": form,
  77. "errors": validateErrors,
  78. "tags": tagsDTO,
  79. "section": "article",
  80. "title": "Редактирование статьи",
  81. }, "admin/_layout")
  82. }
  83. }
  84. func DeleteArticleHandler(app interfaces.IApp) fiber.Handler {
  85. return func(ctx *fiber.Ctx) error {
  86. return nil
  87. }
  88. }