fix: fixing response format for about with id
This commit is contained in:
parent
2d035074a4
commit
550f56e27c
|
@ -7,7 +7,6 @@ import (
|
||||||
type RequestAboutDTO struct {
|
type RequestAboutDTO struct {
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
CoverImage string `json:"cover_image"`
|
CoverImage string `json:"cover_image"`
|
||||||
// AboutDetail []RequestAboutDetailDTO `json:"about_detail"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RequestAboutDTO) ValidateAbout() (map[string][]string, bool) {
|
func (r *RequestAboutDTO) ValidateAbout() (map[string][]string, bool) {
|
||||||
|
@ -43,11 +42,7 @@ func (r *RequestAboutDetailDTO) ValidateAboutDetail() (map[string][]string, bool
|
||||||
errors := make(map[string][]string)
|
errors := make(map[string][]string)
|
||||||
|
|
||||||
if strings.TrimSpace(r.AboutId) == "" {
|
if strings.TrimSpace(r.AboutId) == "" {
|
||||||
errors["about_id"] = append(errors["about_id"], "About ID is required")
|
errors["about_id"] = append(errors["about_id"], "about_id is required")
|
||||||
}
|
|
||||||
|
|
||||||
if strings.TrimSpace(r.ImageDetail) == "" {
|
|
||||||
errors["image_detail"] = append(errors["image_detail"], "Image detail is required")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.TrimSpace(r.Description) == "" {
|
if strings.TrimSpace(r.Description) == "" {
|
||||||
|
|
|
@ -12,6 +12,7 @@ type AboutRepository interface {
|
||||||
CreateAboutDetail(aboutDetail *model.AboutDetail) error
|
CreateAboutDetail(aboutDetail *model.AboutDetail) error
|
||||||
GetAllAbout() ([]model.About, error)
|
GetAllAbout() ([]model.About, error)
|
||||||
GetAboutByID(id string) (*model.About, error)
|
GetAboutByID(id string) (*model.About, error)
|
||||||
|
GetAboutByIDWithoutPrel(id string) (*model.About, error)
|
||||||
GetAboutDetailByID(id string) (*model.AboutDetail, error)
|
GetAboutDetailByID(id string) (*model.AboutDetail, error)
|
||||||
UpdateAbout(id string, about *model.About) (*model.About, error)
|
UpdateAbout(id string, about *model.About) (*model.About, error)
|
||||||
UpdateAboutDetail(id string, aboutDetail *model.AboutDetail) (*model.AboutDetail, error)
|
UpdateAboutDetail(id string, aboutDetail *model.AboutDetail) (*model.AboutDetail, error)
|
||||||
|
@ -60,6 +61,17 @@ func (r *aboutRepository) GetAboutByID(id string) (*model.About, error) {
|
||||||
return &about, nil
|
return &about, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *aboutRepository) GetAboutByIDWithoutPrel(id string) (*model.About, error) {
|
||||||
|
var about model.About
|
||||||
|
if err := r.DB.Where("id = ?", id).First(&about).Error; err != nil {
|
||||||
|
if err == gorm.ErrRecordNotFound {
|
||||||
|
return nil, fmt.Errorf("about with ID %s not found", id)
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("failed to fetch About by ID: %v", err)
|
||||||
|
}
|
||||||
|
return &about, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r *aboutRepository) GetAboutDetailByID(id string) (*model.AboutDetail, error) {
|
func (r *aboutRepository) GetAboutDetailByID(id string) (*model.AboutDetail, error) {
|
||||||
var aboutDetail model.AboutDetail
|
var aboutDetail model.AboutDetail
|
||||||
if err := r.DB.Where("id = ?", id).First(&aboutDetail).Error; err != nil {
|
if err := r.DB.Where("id = ?", id).First(&aboutDetail).Error; err != nil {
|
||||||
|
|
|
@ -272,6 +272,17 @@ func (s *aboutService) GetAboutByID(id string) (*dto.ResponseAboutDTO, error) {
|
||||||
return nil, fmt.Errorf("error formatting About response: %v", err)
|
return nil, fmt.Errorf("error formatting About response: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var responseDetails []dto.ResponseAboutDetailDTO
|
||||||
|
for _, detail := range about.AboutDetail {
|
||||||
|
formattedDetail, err := formatResponseAboutDetailDTO(&detail)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error formatting AboutDetail response: %v", err)
|
||||||
|
}
|
||||||
|
responseDetails = append(responseDetails, *formattedDetail)
|
||||||
|
}
|
||||||
|
|
||||||
|
response.AboutDetail = &responseDetails
|
||||||
|
|
||||||
return response, nil
|
return response, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -317,9 +328,8 @@ func (s *aboutService) CreateAboutDetail(request dto.RequestAboutDetailDTO, cove
|
||||||
return nil, fmt.Errorf("validation error: %v", errors)
|
return nil, fmt.Errorf("validation error: %v", errors)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := s.aboutRepo.GetAboutByID(request.AboutId)
|
_, err := s.aboutRepo.GetAboutByIDWithoutPrel(request.AboutId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
||||||
return nil, fmt.Errorf("about_id tidak ditemukan: %v", err)
|
return nil, fmt.Errorf("about_id tidak ditemukan: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,26 +12,24 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func AboutRouter(api fiber.Router) {
|
func AboutRouter(api fiber.Router) {
|
||||||
|
|
||||||
aboutRepo := repositories.NewAboutRepository(config.DB)
|
aboutRepo := repositories.NewAboutRepository(config.DB)
|
||||||
aboutService := services.NewAboutService(aboutRepo)
|
aboutService := services.NewAboutService(aboutRepo)
|
||||||
aboutHandler := handler.NewAboutHandler(aboutService)
|
aboutHandler := handler.NewAboutHandler(aboutService)
|
||||||
|
|
||||||
aboutRoutes := api.Group("/about")
|
aboutRoutes := api.Group("/about")
|
||||||
aboutRoute := api.Group("/about")
|
aboutRoutes.Use(middleware.AuthMiddleware)
|
||||||
aboutRoutes.Use(middleware.AuthMiddleware, middleware.RoleMiddleware(utils.RoleAdministrator))
|
|
||||||
|
|
||||||
aboutRoute.Get("/", aboutHandler.GetAllAbout)
|
aboutRoutes.Get("/", aboutHandler.GetAllAbout)
|
||||||
aboutRoute.Get("/:id", aboutHandler.GetAboutByID)
|
aboutRoutes.Get("/:id", aboutHandler.GetAboutByID)
|
||||||
aboutRoutes.Post("/", aboutHandler.CreateAbout)
|
aboutRoutes.Post("/", aboutHandler.CreateAbout) // admin
|
||||||
aboutRoutes.Put("/:id", aboutHandler.UpdateAbout)
|
aboutRoutes.Put("/:id", middleware.RoleMiddleware(utils.RoleAdministrator), aboutHandler.UpdateAbout)
|
||||||
aboutRoutes.Delete("/:id", aboutHandler.DeleteAbout)
|
aboutRoutes.Delete("/:id", aboutHandler.DeleteAbout) // admin
|
||||||
|
|
||||||
aboutDetailRoutes := api.Group("/about-detail")
|
aboutDetailRoutes := api.Group("/about-detail")
|
||||||
aboutDetailRoutes.Use(middleware.AuthMiddleware, middleware.RoleMiddleware(utils.RoleAdministrator))
|
aboutDetailRoutes.Use(middleware.AuthMiddleware)
|
||||||
aboutDetailRoute := api.Group("/about-detail")
|
aboutDetailRoute := api.Group("/about-detail")
|
||||||
aboutDetailRoute.Get("/:id", aboutHandler.GetAboutDetailById)
|
aboutDetailRoute.Get("/:id", aboutHandler.GetAboutDetailById)
|
||||||
aboutDetailRoutes.Post("/", aboutHandler.CreateAboutDetail)
|
aboutDetailRoutes.Post("/", aboutHandler.CreateAboutDetail) // admin
|
||||||
aboutDetailRoutes.Put("/:id", aboutHandler.UpdateAboutDetail)
|
aboutDetailRoutes.Put("/:id", middleware.RoleMiddleware(utils.RoleAdministrator), aboutHandler.UpdateAboutDetail)
|
||||||
aboutDetailRoutes.Delete("/:id", aboutHandler.DeleteAboutDetail)
|
aboutDetailRoutes.Delete("/:id", middleware.RoleMiddleware(utils.RoleAdministrator), aboutHandler.DeleteAboutDetail)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue