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
|
||||
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
))
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue