Skip to content

Commit

Permalink
Added: recipes endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
capcom6 committed Jul 30, 2023
1 parent 595b259 commit 64e64d6
Show file tree
Hide file tree
Showing 17 changed files with 93 additions and 8 deletions.
7 changes: 7 additions & 0 deletions api.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@baseUrl=http://127.0.0.1:8000/api/v1

###
GET {{baseUrl}}/recipes HTTP/1.1

###
GET {{baseUrl}}/recipes/76 HTTP/1.1
Empty file added api/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions api/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
7 changes: 7 additions & 0 deletions api/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.apps import AppConfig


class ApiConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "api"
verbose_name = "REST API"
Empty file added api/migrations/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions api/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
5 changes: 5 additions & 0 deletions api/pagination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from rest_framework import pagination


class CreatedAtPagination(pagination.CursorPagination):
ordering = "-pk"
20 changes: 20 additions & 0 deletions api/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from rest_framework import serializers

from recipes import models


class RecipeSerializer(serializers.ModelSerializer):
class Meta:
model = models.Recipe
fields = (
"pk",
"name",
"crust",
"description",
"weight",
"thumbnail",
"category",
"program",
"created_at",
"updated_at",
)
4 changes: 4 additions & 0 deletions api/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
REST_FRAMEWORK = {
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.CursorPagination",
"PAGE_SIZE": 25,
}
3 changes: 3 additions & 0 deletions api/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
10 changes: 10 additions & 0 deletions api/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.urls import path
from rest_framework import routers

from api import views

router = routers.SimpleRouter(trailing_slash=False)
router.register("recipes", views.RecipesViewSet)


urlpatterns = router.urls
10 changes: 10 additions & 0 deletions api/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from rest_framework import generics, viewsets
from api import serializers

from recipes import repositories


# Create your views here.
class RecipesViewSet(viewsets.ReadOnlyModelViewSet):
queryset = repositories.RecipesRepository.select()
serializer_class = serializers.RecipeSerializer
9 changes: 8 additions & 1 deletion bread/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"recipes",
"recipes.apps.RecipesConfig",
"api.apps.ApiConfig",
"rest_framework",
]

if DEBUG:
Expand Down Expand Up @@ -198,3 +200,8 @@
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

REST_FRAMEWORK = {
"DEFAULT_PAGINATION_CLASS": "api.pagination.CreatedAtPagination",
"PAGE_SIZE": 25,
}
8 changes: 4 additions & 4 deletions recipes/apps.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.apps import AppConfig


class DefaultConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'recipes'
verbose_name = 'Книга рецептов'
class RecipesConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "recipes"
verbose_name = "Книга рецептов"
4 changes: 4 additions & 0 deletions recipes/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@


class RecipesRepository:
@classmethod
def select(cls):
return models.Recipe.objects.all()

@classmethod
def get_random(cls) -> typing.Union[models.Recipe, None]:
max_id = models.Recipe.objects.aggregate(max_id=djmodels.Max("id"))["max_id"]
Expand Down
3 changes: 2 additions & 1 deletion recipes/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from django.urls import path
from django.urls import include, path
from django.conf import settings
from django.conf.urls.static import static
from django.views.decorators.cache import cache_page
Expand All @@ -33,6 +33,7 @@
views.CommentAddView.as_view(),
name="comment_add",
),
path("api/v1/", include("api.urls")),
]

if settings.DEBUG:
Expand Down
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
Django==4.1.10
Pillow==9.4.0
azure-storage-blob==12.13.0
boto3==1.26.24
django-storages==1.12.3
Django==4.1.10
djangorestframework==3.14.0
mysqlclient==2.1.1
Pillow==9.4.0
python-dotenv==0.19.2
requests==2.31.0
whitenoise==6.0.0

0 comments on commit 64e64d6

Please sign in to comment.