fix: fixing validation input field in article feature
This commit is contained in:
parent
db99c1a441
commit
aa786d646b
114
dto/article.go
114
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
|
package dto
|
||||||
|
|
||||||
type ArticleResponse struct {
|
type ArticleResponse struct {
|
||||||
|
@ -114,9 +20,19 @@ type ArticleCreateRequest struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ArticleUpdateRequest struct {
|
type ArticleUpdateRequest struct {
|
||||||
Title *string `json:"title,omitempty" validate:"omitempty,min=1"`
|
Title string `json:"title" validate:"required"`
|
||||||
CoverImage *string `json:"coverImage,omitempty" validate:"omitempty,url"`
|
CoverImage string `json:"coverImage" validate:"required"`
|
||||||
Author *string `json:"author,omitempty" validate:"omitempty,min=1"`
|
Author string `json:"author" validate:"required"`
|
||||||
Heading *string `json:"heading,omitempty" validate:"omitempty,min=1"`
|
Heading string `json:"heading" validate:"required"`
|
||||||
Content *string `json:"content,omitempty" validate:"omitempty,min=1"`
|
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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,8 +58,8 @@ func (ac *ArticleController) CreateArticle(c *fiber.Ctx) error {
|
||||||
|
|
||||||
article, err := ac.service.CreateArticle(&request)
|
article, err := ac.service.CreateArticle(&request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.Status(fiber.StatusInternalServerError).JSON(utils.ErrorResponse(
|
return c.Status(fiber.StatusBadRequest).JSON(utils.ErrorResponse(
|
||||||
fiber.StatusInternalServerError,
|
fiber.StatusBadRequest,
|
||||||
err.Error(),
|
err.Error(),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
@ -84,21 +84,15 @@ func (ac *ArticleController) UpdateArticle(c *fiber.Ctx) error {
|
||||||
|
|
||||||
article, err := ac.service.UpdateArticle(id, &request)
|
article, err := ac.service.UpdateArticle(id, &request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err.Error() == "article not found" {
|
return c.Status(fiber.StatusBadRequest).JSON(utils.ErrorResponse(
|
||||||
return c.Status(fiber.StatusNotFound).JSON(utils.ErrorResponse(
|
fiber.StatusBadRequest,
|
||||||
fiber.StatusNotFound,
|
|
||||||
"Article not found",
|
|
||||||
))
|
|
||||||
}
|
|
||||||
return c.Status(fiber.StatusInternalServerError).JSON(utils.ErrorResponse(
|
|
||||||
fiber.StatusInternalServerError,
|
|
||||||
err.Error(),
|
err.Error(),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
|
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
|
||||||
fiber.StatusOK,
|
fiber.StatusOK,
|
||||||
"Article updated successfully",
|
"Point updated successfully",
|
||||||
article,
|
article,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
@ -125,4 +119,4 @@ func (ac *ArticleController) DeleteArticle(c *fiber.Ctx) error {
|
||||||
"Article deleted successfully",
|
"Article deleted successfully",
|
||||||
nil,
|
nil,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
|
@ -93,9 +93,8 @@ func (s *ArticleService) GetArticleByID(id string) (*dto.ArticleResponse, error)
|
||||||
|
|
||||||
func (s *ArticleService) CreateArticle(request *dto.ArticleCreateRequest) (*dto.ArticleResponse, error) {
|
func (s *ArticleService) CreateArticle(request *dto.ArticleCreateRequest) (*dto.ArticleResponse, error) {
|
||||||
|
|
||||||
if request.Title == "" || request.CoverImage == "" || request.Author == "" ||
|
if err := request.Validate(); err != nil {
|
||||||
request.Heading == "" || request.Content == "" {
|
return nil, err
|
||||||
return nil, errors.New("invalid input data")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
newArticle := &domain.Article{
|
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) {
|
func (s *ArticleService) UpdateArticle(id string, request *dto.ArticleUpdateRequest) (*dto.ArticleResponse, error) {
|
||||||
|
|
||||||
if err := dto.GetValidator().Struct(request); err != nil {
|
// if err := dto.GetValidator().Struct(request); err != nil {
|
||||||
return nil, errors.New("invalid input data")
|
// return nil, errors.New("invalid input data")
|
||||||
|
// }
|
||||||
|
|
||||||
|
if err := request.Validate(); err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
article, err := s.repo.GetByID(id)
|
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")
|
return nil, errors.New("article not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
if request.Title != nil {
|
article.Title = request.Title
|
||||||
article.Title = *request.Title
|
article.CoverImage = request.CoverImage
|
||||||
}
|
article.Author = request.Author
|
||||||
if request.CoverImage != nil {
|
article.Heading = request.Heading
|
||||||
article.CoverImage = *request.CoverImage
|
article.Content = request.Content
|
||||||
}
|
|
||||||
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.UpdatedAt = time.Now()
|
article.UpdatedAt = time.Now()
|
||||||
|
|
||||||
err = s.repo.Update(article)
|
err = s.repo.Update(article)
|
||||||
|
@ -196,4 +189,4 @@ func (s *ArticleService) DeleteArticle(id string) error {
|
||||||
config.RedisClient.Del(ctx, "articles:"+id)
|
config.RedisClient.Del(ctx, "articles:"+id)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue