Skip to content

Commit

Permalink
[FEAT] 밸런스게임 api content확인가능하도록 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
summit45 committed Aug 14, 2022
1 parent 3e1a1cd commit 55c22e7
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 28 deletions.
8 changes: 7 additions & 1 deletion rememB/balanceapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ class Balance(models.Model):
answer_id=models.ForeignKey(Answer, on_delete=models.CASCADE, null=True,)
question_id=models.ForeignKey(Question, on_delete=models.CASCADE, null=True,)


# class QAContent(models.Model):
# q_id=models.IntegerField()
# a1_id=models.IntegerField()
# a2_id=models.IntegerField()
# q_content=models.CharField()
# a1_content=models.CharField()
# a2_content=models.CharField()
57 changes: 56 additions & 1 deletion rememB/balanceapp/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from rest_framework import serializers
from .models import *
from datetime import datetime
from userapp.models import User

class QuestionSerializer(serializers.ModelSerializer):
class Meta:
Expand All @@ -12,6 +14,59 @@ class Meta:
fields='__all__'

class BalanceSerializer(serializers.ModelSerializer):
qcontent=serializers.SerializerMethodField()
acontent=serializers.SerializerMethodField()

class Meta:
model=Balance
fields='__all__'
fields = (
'user',
'question_id',
'answer_id',
'qcontent',
'acontent'
)

#유효하지 않은 값이 들어왔을 때 오류처리해야함
def get_qcontent(self, obj):
return Question.objects.get(id=obj.question_id.id).question_content

def get_acontent(self, obj):
return Answer.objects.get(id=obj.answer_id.id).answer_content


class BAQSerializer(serializers.ModelSerializer):
a1id=serializers.SerializerMethodField()
a1content=serializers.SerializerMethodField()
a2id=serializers.SerializerMethodField()
a2content=serializers.SerializerMethodField()

class Meta:
model=Question
fields = (
'id',
'question_content',
'a1id',
'a1content',
'a2id',
'a2content',
)

#유효하지 않은 값이 들어왔을 때 오류처리해야함

def get_a1id(self, obj):
return Answer.objects.get(id=(obj.id*2-1)).id

def get_a1content(self, obj):
return Answer.objects.get(id=(obj.id*2-1)).answer_content

def get_a2id(self, obj):
return Answer.objects.get(id=(obj.id*2)).id

def get_a2content(self, obj):
return Answer.objects.get(id=(obj.id*2)).answer_content





51 changes: 25 additions & 26 deletions rememB/balanceapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
from rest_framework import status
from rest_framework.views import APIView

from userapp import serializers
from .models import Answer, Balance, Question
from .serializers import AnswerSerializer, QuestionSerializer, BalanceSerializer
from .serializers import AnswerSerializer, BAQSerializer, QuestionSerializer, BalanceSerializer
from userapp.models import User

from balanceapp import serializers

def getDayBefore(mybirthday):
mybirthdayList = mybirthday.split("-")
byear = int(mybirthdayList[0])
Expand Down Expand Up @@ -74,10 +75,6 @@ def get(self, request, pk): #한 유저의 전체 질문&대답 fk 전해주기
# 2. 7일 이내라면 해당 질문과 답을 보여준다. / 밀린 게 있을 수도 있으니 다수 가능
# 3. 해당 답을 받아온다.

# balance/<int:pk>/
# 1. balance 모델에서 해당 유저 아이디로 검색하고, 답이 null값이 아니라면
# 2. 질문과 답을 api로 보여준다.

#실제 프론트와 전달할 api
class myBalanceList(APIView): #user(pk)의 질문&대답 목록
def get(self, request, pk):
Expand All @@ -88,37 +85,39 @@ def get(self, request, pk):

class myBalanceGame(APIView):
def post(self, request, pk): #밸런스게임 질문-답 선택(질문 형식으로)
print(request.data)

#해당 유저 찾아서
user=User.objects.get(id=pk)
request.data['user'] = user.id
leftDay=getDayBefore(str(user.birth))
print(request.data)
#Balance모델에 userid, questionid, answerid저장
serializer = BalanceSerializer(data=request.data)

if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
if(leftDay <=7):
request.data['user'] = user.id
serializer = BalanceSerializer(data=request.data)

if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
else:
return Response("아직 응답 못함", status=status.HTTP_200_OK)


def get(self, request, pk): #밸런스게임 질문-답 조회
def get(self, request, pk): #밸런스게임 질문-답 선택할수있도록
user=User.objects.get(id=pk)
leftDay=getDayBefore(str(user.birth))
print("DDAY", leftDay)

if(leftDay <= 7): #생일 비교해보고 7일 이내라면
questions=Question.objects.filter(id__gt=(leftDay-1)) #쿼리셋
print(questions)
serializer = BAQSerializer(questions, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
# if serializer.is_valid():
# return Response(serializer.data, status=status.HTTP_200_OK) #질문이랑 데이터가 전달됨
# return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

for q in questions:
q_id = q.id
answer1_id=Answer.objects.get(id=(q_id*2-1)).id
answer2_id=Answer.objects.get(id=(q_id*2)).id
content = {'q_id': q_id, 'answer1_id' : answer1_id, 'answer2_id': answer2_id} #여러 개일 때 여러개가 보내지는지 확인해야함

return Response(content, status=status.HTTP_200_OK) #질문이랑 데이터가 전달됨



else: #7일 이내가 아니라면 아직 답을 확인할 수 없음
print("not yet")
return Response("not yet", status=status.HTTP_200_OK)


0 comments on commit 55c22e7

Please sign in to comment.