123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- package admin
- import (
- "strconv"
- "github.com/dmitriygnatenko/internal/helpers"
- "github.com/dmitriygnatenko/internal/interfaces"
- "github.com/dmitriygnatenko/internal/mapper"
- "github.com/go-playground/validator/v10"
- "github.com/gofiber/fiber/v2"
- )
- func ArticleHandler(app interfaces.IApp) fiber.Handler {
- return func(ctx *fiber.Ctx) error {
- articles, err := app.GetArticleRepository().GetAll(ctx.Context())
- if err != nil {
- return err
- }
- articlesDTO, err := mapper.ConvertArticleModelsToDTO(articles)
- if err != nil {
- return err
- }
- return ctx.Render("admin/article", fiber.Map{
- "articles": articlesDTO,
- "section": "article",
- }, "admin/_layout")
- }
- }
- func AddArticleHandler(app interfaces.IApp) fiber.Handler {
- return func(ctx *fiber.Ctx) error {
- return nil
- }
- }
- func EditArticleHandler(app interfaces.IApp) fiber.Handler {
- return func(ctx *fiber.Ctx) error {
- var validate = validator.New()
- validateErrors := make(map[string]string)
- trans, err := helpers.GetDefaultTranslator(validate)
- if err != nil {
- return err
- }
- ID, err := strconv.Atoi(ctx.Params("id"))
- if err != nil {
- return err
- }
- article, err := app.GetArticleRepository().GetByID(ctx.Context(), ID)
- if err != nil {
- return err
- }
- if article == nil {
- return fiber.ErrNotFound
- }
- articleTags, err := app.GetTagRepository().GetByArticleID(ctx.Context(), ID)
- if err != nil {
- return err
- }
- form, err := mapper.ConvertArticleModelToForm(*article, articleTags)
- if err != nil {
- return err
- }
- tags, err := app.GetTagRepository().GetAll(ctx.Context())
- if err != nil {
- return err
- }
- tagsDTO, err := mapper.ConvertTagModelsToDTO(tags)
- if err != nil {
- return err
- }
- if ctx.Method() == fiber.MethodPost {
- if err = ctx.BodyParser(&form); err != nil {
- return err
- }
- if err = validate.Struct(form); err != nil {
- validateErrors = helpers.FormatValidateErrors(err, trans)
- }
- // TODO
- }
- return ctx.Render("admin/article_edit", fiber.Map{
- "form": form,
- "errors": validateErrors,
- "tags": tagsDTO,
- "section": "article",
- "title": "Редактирование статьи",
- }, "admin/_layout")
- }
- }
- func DeleteArticleHandler(app interfaces.IApp) fiber.Handler {
- return func(ctx *fiber.Ctx) error {
- return nil
- }
- }
|