Skip to content

Commit

Permalink
Edit response
Browse files Browse the repository at this point in the history
  • Loading branch information
moslehazizi committed Dec 23, 2023
1 parent 670e9f1 commit 0422d53
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
39 changes: 27 additions & 12 deletions api/landing.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package api

import (
"fmt"
"net/http"

"github.com/gin-gonic/gin"
db "github.com/moslehazizi/Elyasam_Restaurant/db/sqlc"
)

func (server *Server) getLanding(c *gin.Context) {


var responseCategories []db.Category
var responseServices []db.Service
var responseSlider []db.SliderImage

arg := db.ListCategoriesParams{
Limit: 19,
Offset: 0,
Expand All @@ -19,8 +22,8 @@ func (server *Server) getLanding(c *gin.Context) {
c.JSON(http.StatusInternalServerError, errorResponse(err_1))
return
}
c.JSON(http.StatusOK, gin.H{
"categories": categories})

responseCategories = append(responseCategories, categories...)

for _, category := range categories {
arg := db.ListServicesParams{
Expand All @@ -34,12 +37,13 @@ func (server *Server) getLanding(c *gin.Context) {
c.JSON(http.StatusInternalServerError, errorResponse(err))
return
}
c.JSON(http.StatusOK, gin.H{
fmt.Sprintf("Services for category number %d", arg.ServiceCategory): services_for_category})
responseServices = append(responseServices, services_for_category...)
// c.JSON(http.StatusOK, gin.H{
// fmt.Sprintf("Services for category number %d", arg.ServiceCategory): services_for_category})
}

arg_2 := db.ListSliderImagesParams {
Limit: 5,
arg_2 := db.ListSliderImagesParams{
Limit: 5,
Offset: 0,
}

Expand All @@ -48,9 +52,20 @@ func (server *Server) getLanding(c *gin.Context) {
c.JSON(http.StatusInternalServerError, errorResponse(err))
return
}
c.JSON(http.StatusOK, gin.H{
"Slider images": slider_images})


server.getRandomServices(c)
responseSlider = append(responseSlider, slider_images...)

// c.JSON(http.StatusOK, gin.H{
// "categories": categories})

// c.JSON(http.StatusOK, gin.H{
// "Slider images": slider_images})
// server.getRandomServices(c)

c.JSON(http.StatusOK, gin.H{
"categories": responseCategories,
"servicesForCategories": responseServices,
"sliderImages": responseSlider,
"discountOffers": server.getRandomServices(c),
})
}
16 changes: 8 additions & 8 deletions api/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (server *Server) putService(c *gin.Context) {
c.JSON(http.StatusOK, service)
}

func (server *Server) getRandomServices(c *gin.Context) {
func (server *Server) getRandomServices(c *gin.Context) []db.DiscountOffer {

// clean holder
discountOfferStruct = nil
Expand All @@ -136,7 +136,6 @@ func (server *Server) getRandomServices(c *gin.Context) {
discount_offers, err := server.store.ListDiscountOffers(c, arg)
if err != nil {
c.JSON(http.StatusInternalServerError, errorResponse(err))
return
}

discountOfferStruct = append(discountOfferStruct, discount_offers...)
Expand All @@ -145,14 +144,15 @@ func (server *Server) getRandomServices(c *gin.Context) {
if time.Now().Compare(discountOfferStruct[1].ExpiredAt) == 1 || time.Now().Compare(discountOfferStruct[1].ExpiredAt) == 0 {
server.deleteDiscountOffers(c)
server.makeListOfdiscounts(c)
c.JSON(http.StatusOK, gin.H{
"Discount offers": discountOfferStruct,
})
// c.JSON(http.StatusOK, gin.H{
// "Discount offers": discountOfferStruct,
// })
} else if time.Now().Compare(discountOfferStruct[1].ExpiredAt) == -1 {
c.JSON(http.StatusOK, gin.H{
"Discount offers": discountOfferStruct,
})
// c.JSON(http.StatusOK, gin.H{
// "Discount offers": discountOfferStruct,
// })
}
return discountOfferStruct
}

// create new items for discountOfferStruct.
Expand Down

0 comments on commit 0422d53

Please sign in to comment.