Add Customer Discovery

This commit is contained in:
aditya.siregar
2024-08-03 20:01:25 +07:00
parent aeed6fde7b
commit 600d42d529
14 changed files with 620 additions and 19 deletions
@@ -0,0 +1,134 @@
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,
}
}
+33
View File
@@ -0,0 +1,33 @@
package request
import (
"furtuna-be/internal/entity"
)
type DiscoveryHomeParam struct {
Lat float64 `form:"lat" json:"lat" example:"10"`
Long float64 `form:"long" json:"long" example:"0"`
Name string `form:"name" json:"name" example:"0"`
Region string `form:"region" json:"region" example:"0"`
Radius int `form:"radius" json:"radius" example:"0"`
Limit int `form:"limit" json:"limit" example:"0"`
Offset int `form:"offset" json:"offset" example:"0"`
Discover string `form:"discover" json:"discover" example:"0"`
}
func (d *DiscoveryHomeParam) ToEntity() *entity.DiscoverySearch {
if d.Limit == 0 {
d.Limit = 10
}
return &entity.DiscoverySearch{
Lat: d.Lat,
Long: d.Long,
Name: d.Name,
Region: d.Region,
Radius: d.Radius,
Limit: d.Limit,
Offset: d.Offset,
Discover: d.Discover,
}
}
+49
View File
@@ -0,0 +1,49 @@
package response
type ExploreResponse struct {
ExploreRegions []Region `json:"exploreRegions"`
ExploreDestinations []Destination `json:"exploreDestinations"`
MustVisit []MustVisit `json:"mustVisit"`
}
type CurrentLocation struct {
City string `json:"city"`
}
type Region struct {
Name string `json:"name"`
}
type Destination struct {
Name string `json:"name"`
ImageURL string `json:"image_url"`
}
type MustVisit struct {
SiteID int64 `json:"site_id"`
Name string `json:"name"`
Region string `json:"region"`
Rating float64 `json:"rating"`
ReviewCount int `json:"reviewCount"`
Price float64 `json:"price"`
ImageURL string `json:"imageUrl"`
Regency string `json:"regency"`
}
type SearchResponse struct {
Offset int `json:"offset"`
Total int `json:"total"`
Limit int `json:"limit"`
Data []SiteSeach `json:"data"`
}
type SiteSeach struct {
SiteID int64 `json:"site_id"`
Name string `json:"name"`
Region string `json:"region"`
Rating float64 `json:"rating"`
ReviewCount int `json:"reviewCount"`
Price float64 `json:"price"`
ImageURL string `json:"imageUrl"`
Regency string `json:"regency"`
}