[BAGETTT] 제품상세 API

LILO Ghim·2021년 12월 12일
0

ProductsView

패키지 상품 하나를 클릭했을 때, 상세 페이지로 이동하는데,
이 페이지에서 크게 깨달았던 사실은, back-end에서 데이터를 더미로 줘버리면 front-end에서 알아서 쓴다는 것을 처음 알게 되었다는 것.
페이지 레이아웃 별로 다르게 로직을 짜야 한다고 생각했었던 것이다. 아주 편안,,,

path parameter

다른 API에서는 써보지 못했던 path parameter를 사용해봄으로써, query와 path의 차이를 명확하게 알 수 있었던!!!


class ProductsView(View):
    def get(self, request, package_id):
        try:
            products = Product.objects.filter(packages__id = package_id)
            package = Package.objects.get(id = package_id)
            
            result = [{
                "package_id"           : package_id,
                "package_name"         : package.name,
                "package_description"  : package.description,
                "package_thumbnail"    : package.thumbnail_image,
                "price"                : package.price,
                "product_details"      : [{   
                            "name"      : product.name,
                            "brand_name": product.brand_name,
                            "image"     : product.image_url,
                            "kcal"      : product.kcal,
                            "nutrition" : product.nutrition
                    } for product in products],
                }]
        
            return JsonResponse({'result':result}, status=200)
        
        except Package.DoesNotExist:
            return JsonResponse({'messages':'NOT_FOUND'}, status=404)
      
profile
킴릴로

0개의 댓글