Skip to content

Commit

Permalink
Merge pull request #104 from dudtlstm/main
Browse files Browse the repository at this point in the history
  • Loading branch information
jiyoon607 authored Oct 7, 2024
2 parents 1851b4f + 25a5931 commit 8491fd9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
51 changes: 43 additions & 8 deletions booth/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ class Meta:
model = BoothMenu
fields = ['name', 'price']

# 부스 리스트
class BoothListSerializer(serializers.ModelSerializer):
thumbnail = serializers.SerializerMethodField()
waiting_count = serializers.SerializerMethodField()
# 사용자 대기 관련
is_waiting = serializers.SerializerMethodField()
waiting_status = serializers.SerializerMethodField()
# 대기 팀 수 관련
total_waiting_teams = serializers.SerializerMethodField()

class Meta:
model = Booth
fields = ['id', 'name', 'description', 'location', 'is_operated', 'thumbnail', 'waiting_count', 'is_waiting', 'waiting_status']
fields = ['id', 'name', 'description', 'location', 'is_operated', 'thumbnail', 'total_waiting_teams', 'is_waiting', 'waiting_status']

def get_thumbnail(self, obj):
# 첫 번째 이미지가 썸네일 ! !
Expand All @@ -34,8 +36,11 @@ def get_thumbnail(self, obj):
return request.build_absolute_uri(thumbnail.image.url)
return ''

def get_waiting_count(self, obj):
return obj.waitings.count()
def get_total_waiting_teams(self, obj):
return Waiting.objects.filter(
booth=obj,
waiting_status__in=['waiting', 'ready_to_confirm', 'confirmed']
).count()

# 사용자 대기 관련
def get_is_waiting(self, obj):
Expand All @@ -62,20 +67,41 @@ class Meta:
model = BoothImage
fields = ['image']

# 부스 디테일
class BoothDetailSerializer(serializers.ModelSerializer):
menu = BoothMenuSerializer(many=True, source='menus')
images = BoothImageSerializer(source='boothimages', many=True)
waiting_count = serializers.SerializerMethodField()
# 사용자 대기 관련
is_waiting = serializers.SerializerMethodField()
waiting_status = serializers.SerializerMethodField()
waiting_id = serializers.SerializerMethodField()
# 대기 팀 수 관련
waiting_teams_ahead = serializers.SerializerMethodField()
total_waiting_teams = serializers.SerializerMethodField()

class Meta:
model = Booth
fields = ['id', 'name', 'description', 'location', 'caution', 'is_operated', 'images', 'menu', 'open_time', 'close_time', 'waiting_count', 'is_waiting', 'waiting_status']
fields = ['id', 'name', 'description', 'location', 'caution', 'is_operated', 'images', 'menu', 'open_time', 'close_time', 'total_waiting_teams', 'waiting_teams_ahead', 'is_waiting', 'waiting_id', 'waiting_status']

def get_waiting_count(self, obj):
return obj.waitings.count()
# 대기 팀 수 관련
def get_waiting_teams_ahead(self, obj):
request = self.context.get('request')
if request and request.user.is_authenticated:
current_waiting = Waiting.objects.filter(user=request.user, booth=obj).order_by('-registered_at').first()
if current_waiting:
# 현재 유저의 등록 시점보다 이전에 등록한 사람들 (waiting, ready_to_confirm, confirmed 상태만 체크)
return Waiting.objects.filter(
booth=obj,
waiting_status__in=['waiting', 'ready_to_confirm', 'confirmed'],
registered_at__lt=current_waiting.registered_at
).count()
return 0

def get_total_waiting_teams(self, obj):
return Waiting.objects.filter(
booth=obj,
waiting_status__in=['waiting', 'ready_to_confirm', 'confirmed']
).count()

# 사용자 대기 관련
def get_is_waiting(self, obj):
Expand All @@ -88,6 +114,15 @@ def get_is_waiting(self, obj):
).exists()
return False

def get_waiting_id(self, obj):
request = self.context.get('request')
# is_waiting=true일 때만 waiting_id를 반환
if self.get_is_waiting(obj):
waiting = Waiting.objects.filter(user=request.user, booth=obj).order_by('-registered_at').first()
if waiting:
return waiting.id
return None

def get_waiting_status(self, obj):
request = self.context.get('request')
if request and request.user.is_authenticated:
Expand Down
9 changes: 9 additions & 0 deletions booth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ def get_booth_count(self, request):
return custom_response({'booth_count': booth_count}, message='Booth count fetched successfully')
except Exception as e:
return custom_response(data=None, message=str(e), code=status.HTTP_500_INTERNAL_SERVER_ERROR, success=False)

def retrieve(self, request, *args, **kwargs):
try:
booth = self.get_object()
serializer = self.get_serializer(booth)
return custom_response(data=serializer.data, message="Booth detail fetched successfully", code=status.HTTP_200_OK)
except Exception as e:
return custom_response(data=None, message=str(e), code=status.HTTP_500_INTERNAL_SERVER_ERROR, success=False)


# 에러 띄우기 위한 함수
@action(detail=False, methods=['get'], url_path='error')
Expand Down

0 comments on commit 8491fd9

Please sign in to comment.