From aa786d646b82e1128d3acf7b821eccf4592855c5 Mon Sep 17 00:00:00 2001 From: pahmiudahgede Date: Sun, 26 Jan 2025 11:03:28 +0700 Subject: [PATCH] fix: fixing validation input field in article feature --- dto/article.go | 114 +++++--------------------------- internal/controllers/article.go | 18 ++--- internal/services/article.go | 35 ++++------ 3 files changed, 35 insertions(+), 132 deletions(-) diff --git a/dto/article.go b/dto/article.go index 94bc738..c298ae0 100644 --- a/dto/article.go +++ b/dto/article.go @@ -1,97 +1,3 @@ -// package dto - -// import ( -// "fmt" -// "time" - -// "github.com/go-playground/validator/v10" -// ) - -// type ArticleRequest struct { -// Title string `json:"title" validate:"required"` -// CoverImage string `json:"coverImage" validate:"required"` -// Author string `json:"author" validate:"required"` -// Heading string `json:"heading" validate:"required"` -// Content string `json:"content" validate:"required"` -// } - -// type ArticleResponse struct { -// ID string `json:"id"` -// Title string `json:"title"` -// CoverImage string `json:"coverImage"` -// Author string `json:"author"` -// Heading string `json:"heading"` -// Content string `json:"content"` -// PublishedAt time.Time `json:"publishedAt"` -// UpdatedAt time.Time `json:"updatedAt"` -// PublishedAtFormatted string `json:"publishedAtt"` -// UpdatedAtFormatted string `json:"updatedAtt"` -// } - -// type FormattedResponse struct { -// ID string `json:"id"` -// Title string `json:"title"` -// CoverImage string `json:"coverImage"` -// Author string `json:"author"` -// Heading string `json:"heading"` -// Content string `json:"content"` -// PublishedAtFormatted string `json:"publishedAt"` -// UpdatedAtFormatted string `json:"updatedAt"` -// } -// type ArticleUpdateRequest struct { -// Title string `json:"title" validate:"required"` -// CoverImage string `json:"coverImage" validate:"required"` -// Author string `json:"author" validate:"required"` -// Heading string `json:"heading" validate:"required"` -// Content string `json:"content" validate:"required"` -// } - -// func (c *ArticleRequest) ValidatePostArticle() error { -// err := validate.Struct(c) -// if err != nil { - -// for _, e := range err.(validator.ValidationErrors) { - -// switch e.Field() { -// case "Title": -// return fmt.Errorf("judul harus diisi") -// case "CoverImage": -// return fmt.Errorf("gambar cover harus diisi") -// case "Author": -// return fmt.Errorf("penulis harus diisi") -// case "Heading": -// return fmt.Errorf("heading harus diisi") -// case "Content": -// return fmt.Errorf("konten artikel harus diisi") -// } -// } -// } -// return nil -// } - -// func (c *ArticleUpdateRequest) ValidateUpdateArticle() error { -// err := validate.Struct(c) -// if err != nil { - -// for _, e := range err.(validator.ValidationErrors) { - -// switch e.Field() { -// case "Title": -// return fmt.Errorf("judul harus diisi") -// case "CoverImage": -// return fmt.Errorf("gambar cover harus diisi") -// case "Author": -// return fmt.Errorf("penulis harus diisi") -// case "Heading": -// return fmt.Errorf("heading harus diisi") -// case "Content": -// return fmt.Errorf("konten artikel harus diisi") -// } -// } -// } -// return nil -// } - package dto type ArticleResponse struct { @@ -114,9 +20,19 @@ type ArticleCreateRequest struct { } type ArticleUpdateRequest struct { - Title *string `json:"title,omitempty" validate:"omitempty,min=1"` - CoverImage *string `json:"coverImage,omitempty" validate:"omitempty,url"` - Author *string `json:"author,omitempty" validate:"omitempty,min=1"` - Heading *string `json:"heading,omitempty" validate:"omitempty,min=1"` - Content *string `json:"content,omitempty" validate:"omitempty,min=1"` + Title string `json:"title" validate:"required"` + CoverImage string `json:"coverImage" validate:"required"` + Author string `json:"author" validate:"required"` + Heading string `json:"heading" validate:"required"` + Content string `json:"content" validate:"required"` +} + +func (p *ArticleCreateRequest) Validate() error { + validate := GetValidator() + return validate.Struct(p) +} + +func (p *ArticleUpdateRequest) Validate() error { + validate := GetValidator() + return validate.Struct(p) } diff --git a/internal/controllers/article.go b/internal/controllers/article.go index 6583fe8..1bbf5bd 100644 --- a/internal/controllers/article.go +++ b/internal/controllers/article.go @@ -58,8 +58,8 @@ func (ac *ArticleController) CreateArticle(c *fiber.Ctx) error { article, err := ac.service.CreateArticle(&request) if err != nil { - return c.Status(fiber.StatusInternalServerError).JSON(utils.ErrorResponse( - fiber.StatusInternalServerError, + return c.Status(fiber.StatusBadRequest).JSON(utils.ErrorResponse( + fiber.StatusBadRequest, err.Error(), )) } @@ -84,21 +84,15 @@ func (ac *ArticleController) UpdateArticle(c *fiber.Ctx) error { article, err := ac.service.UpdateArticle(id, &request) if err != nil { - if err.Error() == "article not found" { - return c.Status(fiber.StatusNotFound).JSON(utils.ErrorResponse( - fiber.StatusNotFound, - "Article not found", - )) - } - return c.Status(fiber.StatusInternalServerError).JSON(utils.ErrorResponse( - fiber.StatusInternalServerError, + return c.Status(fiber.StatusBadRequest).JSON(utils.ErrorResponse( + fiber.StatusBadRequest, err.Error(), )) } return c.Status(fiber.StatusOK).JSON(utils.FormatResponse( fiber.StatusOK, - "Article updated successfully", + "Point updated successfully", article, )) } @@ -125,4 +119,4 @@ func (ac *ArticleController) DeleteArticle(c *fiber.Ctx) error { "Article deleted successfully", nil, )) -} \ No newline at end of file +} diff --git a/internal/services/article.go b/internal/services/article.go index 5e9093f..4618512 100644 --- a/internal/services/article.go +++ b/internal/services/article.go @@ -93,9 +93,8 @@ func (s *ArticleService) GetArticleByID(id string) (*dto.ArticleResponse, error) func (s *ArticleService) CreateArticle(request *dto.ArticleCreateRequest) (*dto.ArticleResponse, error) { - if request.Title == "" || request.CoverImage == "" || request.Author == "" || - request.Heading == "" || request.Content == "" { - return nil, errors.New("invalid input data") + if err := request.Validate(); err != nil { + return nil, err } newArticle := &domain.Article{ @@ -130,8 +129,12 @@ func (s *ArticleService) CreateArticle(request *dto.ArticleCreateRequest) (*dto. func (s *ArticleService) UpdateArticle(id string, request *dto.ArticleUpdateRequest) (*dto.ArticleResponse, error) { - if err := dto.GetValidator().Struct(request); err != nil { - return nil, errors.New("invalid input data") + // if err := dto.GetValidator().Struct(request); err != nil { + // return nil, errors.New("invalid input data") + // } + + if err := request.Validate(); err != nil { + return nil, err } article, err := s.repo.GetByID(id) @@ -139,21 +142,11 @@ func (s *ArticleService) UpdateArticle(id string, request *dto.ArticleUpdateRequ return nil, errors.New("article not found") } - if request.Title != nil { - article.Title = *request.Title - } - if request.CoverImage != nil { - article.CoverImage = *request.CoverImage - } - if request.Author != nil { - article.Author = *request.Author - } - if request.Heading != nil { - article.Heading = *request.Heading - } - if request.Content != nil { - article.Content = *request.Content - } + article.Title = request.Title + article.CoverImage = request.CoverImage + article.Author = request.Author + article.Heading = request.Heading + article.Content = request.Content article.UpdatedAt = time.Now() err = s.repo.Update(article) @@ -196,4 +189,4 @@ func (s *ArticleService) DeleteArticle(id string) error { config.RedisClient.Del(ctx, "articles:"+id) return nil -} \ No newline at end of file +}