Skip to content

Commit 56d6bea

Browse files
committed
feat(promotions): add promotion search view and managers
1 parent 363d36d commit 56d6bea

File tree

5 files changed

+45
-4
lines changed

5 files changed

+45
-4
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ repos:
3030
rev: "7.0.0"
3131
hooks:
3232
- id: flake8
33-
args: ["--max-line-length=79", "--ignore=E501"]
33+
args: ["--max-line-length=79", "--ignore=E501,W503"]
3434

3535
ci:
3636
autoupdate_schedule: weekly

apps/promotions/managers.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Managers for Promotions App."""
22

3-
from django.db.models import Manager
3+
from django.db.models import Manager, Q
44

55

66
class PromotionManager(Manager):
@@ -17,3 +17,10 @@ def get_available(self):
1717
def get_unavailable(self):
1818
"""Return a queryset of unavailable promotions."""
1919
return self.get_queryset().filter(available=False)
20+
21+
def get_search(self, search_query):
22+
"""Filter promotions based on a search query."""
23+
return self.get_available().filter(
24+
Q(name__icontains=search_query)
25+
| Q(conditions__icontains=search_query)
26+
)

apps/promotions/urls.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from django.urls import path
44

5-
from .views import PromotionListView, PromotionDetailView
5+
from .views import PromotionListView, PromotionDetailView, PromotionSearchView
66

77

88
urlpatterns = [
@@ -13,5 +13,9 @@
1313
path(
1414
"api/v1/promotions/<uuid:promotion_id>/",
1515
PromotionDetailView.as_view()
16+
),
17+
path(
18+
"api/v1/promotions/search/",
19+
PromotionSearchView.as_view()
1620
)
1721
]

apps/promotions/views.py

+30
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,33 @@ def delete(self, request, promotion_id):
9292
promotion.available = False # Logical deletion
9393
promotion.save()
9494
return Response(status=status.HTTP_204_NO_CONTENT)
95+
96+
97+
class PromotionSearchView(APIView):
98+
"""
99+
View to search promotions.
100+
101+
Endpoints:
102+
- GET api/v1/promotions/?query=<search_query>
103+
"""
104+
105+
def get(self, request):
106+
# Search for promotions for name and conditions fields
107+
search_query = request.query_params.get("query", "")
108+
109+
if not search_query:
110+
return Response(
111+
{"details": "No search query provided"},
112+
status=status.HTTP_400_BAD_REQUEST
113+
)
114+
115+
promotions = Promotion.objects.get_search(search_query)
116+
117+
if not promotions.exists():
118+
return Response(
119+
{"details": "No results found."},
120+
status=status.HTTP_404_NOT_FOUND
121+
)
122+
123+
serializer = PromotionReadSerializer(promotions, many=True)
124+
return Response(serializer.data)

apps/restaurants/schemas.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
summary="Get category",
5757
description="pending",
5858
),
59-
"put": extend_schema(
59+
"patch": extend_schema(
6060
operation_id="category_detail_update",
6161
summary="Update category",
6262
description="pending",

0 commit comments

Comments
 (0)