Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 내 맛집 리스트 기능 #26

Merged
merged 5 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions friends/migrations/0002_alter_friend_friend_alter_friend_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 4.2.14 on 2024-07-31 04:15

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("friends", "0001_initial"),
]

operations = [
migrations.AlterField(
model_name="friend",
name="friend",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="friend_of",
to=settings.AUTH_USER_MODEL,
),
),
migrations.AlterField(
model_name="friend",
name="user",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="friends",
to=settings.AUTH_USER_MODEL,
),
),
]
2 changes: 1 addition & 1 deletion friends/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.db import models
from users.models import User
from accounts.models import User

# Create your models here.

Expand Down
46 changes: 46 additions & 0 deletions restaurants/migrations/0004_userrestaurantslist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Generated by Django 4.2.14 on 2024-07-31 04:31

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("restaurants", "0003_searchhistory"),
]

operations = [
migrations.CreateModel(
name="UserRestaurantsList",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"restaurant",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="restaurants.restaurant",
),
),
(
"user",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
),
),
],
options={
"unique_together": {("user", "restaurant")},
},
),
]
8 changes: 8 additions & 0 deletions restaurants/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,11 @@ class SearchHistory(models.Model):

class Meta:
ordering = ["-timestamp"]


class UserRestaurantsList(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
restaurant = models.ForeignKey(Restaurant, on_delete=models.CASCADE)

class Meta:
unique_together = ("user", "restaurant")
10 changes: 9 additions & 1 deletion restaurants/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from rest_framework import serializers
from .models import Restaurant, SearchHistory
from .models import Restaurant, SearchHistory, UserRestaurantsList


class RestaurantSerializer(serializers.ModelSerializer):
Expand All @@ -23,3 +23,11 @@ class SearchHistorySerializer(serializers.ModelSerializer):
class Meta:
model = SearchHistory
fields = ["query", "timestamp"]


class UserRestaurantListSerializer(serializers.ModelSerializer):
restaurant = RestaurantSerializer()

class Meta:
model = UserRestaurantsList
fields = "__all__"
8 changes: 7 additions & 1 deletion restaurants/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@


urlpatterns = [
path("restaurants/", views.restaurant_list, name="restaurant-list"),
# path("restaurants/", views.restaurant_list, name="restaurant-list"),
path("api-auth/", include("rest_framework.urls", namespace="rest_framework")),
path("search/", views.search, name="search"),
path(
"restaurants/<int:pk>/",
views.add_remove_restaurant,
name="add-remove-restaurant",
),
path("restaurants/", views.user_restaurant_list, name="user-restaurant-list"),
]
44 changes: 43 additions & 1 deletion restaurants/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# from django.shortcuts import render
from rest_framework.decorators import api_view
from rest_framework.response import Response
from .models import Restaurant, SearchHistory
from rest_framework import status
from .models import Restaurant, SearchHistory, UserRestaurantsList
from .serializers import (
RestaurantSerializer,
RestaurantListSerializer,
SearchHistorySerializer,
UserRestaurantListSerializer,
)
from django.contrib.auth.decorators import login_required


@api_view(["GET"])
Expand Down Expand Up @@ -40,3 +43,42 @@ def search(request):

else:
return Response({"error": "Unsupported method"}, status=405)


@api_view(["GET"])
@login_required
def user_restaurant_list(request):
user_restaurants = UserRestaurantsList.objects.filter(user=request.user)
serializer = UserRestaurantListSerializer(user_restaurants, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)


@api_view(["POST", "DELETE"])
@login_required
def add_remove_restaurant(request, pk):
try:
restaurant = Restaurant.objects.get(pk=pk)
if request.method == "POST":
UserRestaurantsList.objects.create(user=request.user, restaurant=restaurant)
return Response(
{"message": "Restaurant added successfully"},
status=status.HTTP_201_CREATED,
)
elif request.method == "DELETE":
user_restaurant = UserRestaurantsList.objects.get(
user=request.user, restaurant=restaurant
)
user_restaurant.delete()
return Response(
{"message": "Restaurant deleted successfully"},
status=status.HTTP_204_NO_CONTENT,
)
except Restaurant.DoesNotExist:
return Response(
{"message": "Restaurant not found"}, status=status.HTTP_404_NOT_FOUND
)
except UserRestaurantsList.DoesNotExist:
return Response(
{"message": "Restaurant not found in your list"},
status=status.HTTP_404_NOT_FOUND,
)
29 changes: 29 additions & 0 deletions reviews/migrations/0002_alter_recommend_user_alter_review_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 4.2.14 on 2024-07-31 04:15

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("reviews", "0001_initial"),
]

operations = [
migrations.AlterField(
model_name="recommend",
name="user",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL
),
),
migrations.AlterField(
model_name="review",
name="user",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL
),
),
]
2 changes: 1 addition & 1 deletion reviews/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.db import models
from users.models import User
from accounts.models import User
from restaurants.models import Restaurant

# Create your models here.
Expand Down
Empty file removed users/__init__.py
Empty file.
3 changes: 0 additions & 3 deletions users/admin.py

This file was deleted.

6 changes: 0 additions & 6 deletions users/apps.py

This file was deleted.

31 changes: 0 additions & 31 deletions users/migrations/0001_initial.py

This file was deleted.

Empty file removed users/migrations/__init__.py
Empty file.
11 changes: 0 additions & 11 deletions users/models.py

This file was deleted.

8 changes: 0 additions & 8 deletions users/serializers.py

This file was deleted.

3 changes: 0 additions & 3 deletions users/tests.py

This file was deleted.

3 changes: 0 additions & 3 deletions users/views.py

This file was deleted.

Loading