135 lines
3.3 KiB
Go
135 lines
3.3 KiB
Go
package discovery
|
|
|
|
import (
|
|
"furtuna-be/internal/entity"
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
|
|
"furtuna-be/internal/common/errors"
|
|
"furtuna-be/internal/handlers/request"
|
|
"furtuna-be/internal/handlers/response"
|
|
"furtuna-be/internal/services"
|
|
)
|
|
|
|
type Handler struct {
|
|
service services.DiscoverService
|
|
}
|
|
|
|
func (h *Handler) Route(group *gin.RouterGroup, jwt gin.HandlerFunc) {
|
|
route := group.Group("/discovery")
|
|
|
|
route.GET("/home", h.DisoveryHome)
|
|
route.GET("/search", h.DisoverySearch)
|
|
|
|
}
|
|
|
|
func NewHandler(service services.DiscoverService) *Handler {
|
|
return &Handler{
|
|
service: service,
|
|
}
|
|
}
|
|
|
|
func (h *Handler) DisoveryHome(c *gin.Context) {
|
|
var req request.DiscoveryHomeParam
|
|
if err := c.ShouldBindQuery(&req); err != nil {
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
return
|
|
}
|
|
|
|
res, err := h.service.Home(c.Request.Context(), req.ToEntity())
|
|
|
|
if err != nil {
|
|
response.ErrorWrapper(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, response.BaseResponse{
|
|
Success: true,
|
|
Status: http.StatusOK,
|
|
Data: ConvertEntityToResponse(res),
|
|
})
|
|
}
|
|
|
|
func (h *Handler) DisoverySearch(c *gin.Context) {
|
|
var req request.DiscoveryHomeParam
|
|
if err := c.ShouldBindQuery(&req); err != nil {
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
return
|
|
}
|
|
|
|
res, total, err := h.service.Search(c.Request.Context(), req.ToEntity())
|
|
|
|
if err != nil {
|
|
response.ErrorWrapper(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, response.BaseResponse{
|
|
Success: true,
|
|
Status: http.StatusOK,
|
|
Data: ConvertEntityToSearchResponse(res, total, req),
|
|
})
|
|
}
|
|
|
|
func ConvertEntityToResponse(entityResp *entity.DiscoverySearchResp) *response.ExploreResponse {
|
|
// Convert ExploreRegions
|
|
exploreRegions := make([]response.Region, len(entityResp.ExploreRegions))
|
|
for i, region := range entityResp.ExploreRegions {
|
|
exploreRegions[i] = response.Region{
|
|
Name: region.Name,
|
|
}
|
|
}
|
|
|
|
// Convert ExploreDestinations
|
|
exploreDestinations := make([]response.Destination, len(entityResp.ExploreDestinations))
|
|
for i, destination := range entityResp.ExploreDestinations {
|
|
exploreDestinations[i] = response.Destination{
|
|
Name: destination.Name,
|
|
ImageURL: destination.ImageURL,
|
|
}
|
|
}
|
|
|
|
mustVisit := make([]response.MustVisit, len(entityResp.MustVisit))
|
|
for i, mv := range entityResp.MustVisit {
|
|
mustVisit[i] = response.MustVisit{
|
|
Name: mv.Name,
|
|
Region: mv.Region,
|
|
Rating: mv.Rating,
|
|
ReviewCount: mv.ReviewCount,
|
|
Price: mv.Price,
|
|
ImageURL: mv.ImageURL,
|
|
SiteID: mv.SiteID,
|
|
Regency: mv.Regency,
|
|
}
|
|
}
|
|
|
|
return &response.ExploreResponse{
|
|
ExploreRegions: exploreRegions,
|
|
ExploreDestinations: exploreDestinations,
|
|
MustVisit: mustVisit,
|
|
}
|
|
}
|
|
|
|
func ConvertEntityToSearchResponse(entityResp *entity.DiscoverySearchResp, total int64, req request.DiscoveryHomeParam) *response.SearchResponse {
|
|
data := make([]response.SiteSeach, len(entityResp.MustVisit))
|
|
for i, mv := range entityResp.MustVisit {
|
|
data[i] = response.SiteSeach{
|
|
Name: mv.Name,
|
|
Region: mv.Region,
|
|
Rating: mv.Rating,
|
|
ReviewCount: mv.ReviewCount,
|
|
Price: mv.Price,
|
|
ImageURL: mv.ImageURL,
|
|
SiteID: mv.SiteID,
|
|
Regency: mv.Regency,
|
|
}
|
|
}
|
|
|
|
return &response.SearchResponse{
|
|
Data: data,
|
|
Total: int(total),
|
|
Limit: req.Limit,
|
|
Offset: req.Offset,
|
|
}
|
|
}
|