Skip to content

Commit

Permalink
Add endpoint to list restaurant by ID
Browse files Browse the repository at this point in the history
Added a new method `listRestaurantById` in the handler, which retrieves a restaurant by its ID. Included the corresponding route in `routes.go` and implemented the `ListRestaurantById` method in the service layer. Updated the restaurant interface to include this new method.
  • Loading branch information
mukulmantosh committed Aug 21, 2024
1 parent 185f78d commit a712021
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/abstract/restaurant/restaurant.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ import (
type Restaurant interface {
Add(ctx context.Context, user *restaurant.Restaurant) (bool, error)
ListRestaurants(ctx context.Context) ([]restaurant.Restaurant, error)
ListRestaurantById(ctx context.Context, restaurantId int64) (restaurant.Restaurant, error)
DeleteRestaurant(ctx context.Context, restaurantId int64) (bool, error)
}
14 changes: 14 additions & 0 deletions pkg/handler/restaurant/restaurant.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ func (s *RestaurantHandler) listRestaurants(c *gin.Context) {
c.JSON(http.StatusOK, results)
}

func (s *RestaurantHandler) listRestaurantById(c *gin.Context) {
ctx, cancel := context.WithTimeout(c.Request.Context(), 5*time.Second)
defer cancel()
restaurantId := c.Param("id")
restaurantID, _ := strconv.ParseInt(restaurantId, 10, 64)

result, err := s.service.ListRestaurantById(ctx, restaurantID)
if err != nil {
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, result)
}

func (s *RestaurantHandler) deleteRestaurant(c *gin.Context) {
ctx, cancel := context.WithTimeout(c.Request.Context(), 5*time.Second)
defer cancel()
Expand Down
1 change: 1 addition & 0 deletions pkg/handler/restaurant/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func (s *RestaurantHandler) registerGroup() *gin.RouterGroup {
func (s *RestaurantHandler) routes() http.Handler {
s.router.POST("/", s.addRestaurant)
s.router.GET("/", s.listRestaurants)
s.router.GET("/:id", s.listRestaurantById)
s.router.DELETE("/:id", s.deleteRestaurant)
s.router.POST("/menu", s.addMenu)
s.router.GET("/menu", s.listMenus)
Expand Down
16 changes: 16 additions & 0 deletions pkg/service/restaurant/list_restaurant_by_id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package restaurant

import (
"Go_Food_Delivery/pkg/database/models/restaurant"
"context"
)

func (restSrv *RestaurantService) ListRestaurantById(ctx context.Context, restaurantId int64) (restaurant.Restaurant, error) {
var restro restaurant.Restaurant

err := restSrv.db.Select(ctx, &restro, "restaurant_id", restaurantId)
if err != nil {
return restaurant.Restaurant{}, err
}
return restro, nil
}

0 comments on commit a712021

Please sign in to comment.