From 55c22e78cebce17e10856e94265fc0950e2bee6e Mon Sep 17 00:00:00 2001 From: summit45 Date: Sun, 14 Aug 2022 15:01:22 +0900 Subject: [PATCH] =?UTF-8?q?[FEAT]=20=EB=B0=B8=EB=9F=B0=EC=8A=A4=EA=B2=8C?= =?UTF-8?q?=EC=9E=84=20api=20content=ED=99=95=EC=9D=B8=EA=B0=80=EB=8A=A5?= =?UTF-8?q?=ED=95=98=EB=8F=84=EB=A1=9D=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rememB/balanceapp/models.py | 8 ++++- rememB/balanceapp/serializers.py | 57 +++++++++++++++++++++++++++++++- rememB/balanceapp/views.py | 51 ++++++++++++++-------------- 3 files changed, 88 insertions(+), 28 deletions(-) diff --git a/rememB/balanceapp/models.py b/rememB/balanceapp/models.py index 813299d..b4e96cb 100644 --- a/rememB/balanceapp/models.py +++ b/rememB/balanceapp/models.py @@ -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,) - \ No newline at end of file +# 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() \ No newline at end of file diff --git a/rememB/balanceapp/serializers.py b/rememB/balanceapp/serializers.py index 925eaa4..61a15c5 100644 --- a/rememB/balanceapp/serializers.py +++ b/rememB/balanceapp/serializers.py @@ -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: @@ -12,6 +14,59 @@ class Meta: fields='__all__' class BalanceSerializer(serializers.ModelSerializer): + qcontent=serializers.SerializerMethodField() + acontent=serializers.SerializerMethodField() + class Meta: model=Balance - fields='__all__' \ No newline at end of file + 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 + + + + + \ No newline at end of file diff --git a/rememB/balanceapp/views.py b/rememB/balanceapp/views.py index 63adfe5..2aff0a7 100644 --- a/rememB/balanceapp/views.py +++ b/rememB/balanceapp/views.py @@ -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]) @@ -74,10 +75,6 @@ def get(self, request, pk): #한 유저의 전체 질문&대답 fk 전해주기 # 2. 7일 이내라면 해당 질문과 답을 보여준다. / 밀린 게 있을 수도 있으니 다수 가능 # 3. 해당 답을 받아온다. -# balance// -# 1. balance 모델에서 해당 유저 아이디로 검색하고, 답이 null값이 아니라면 -# 2. 질문과 답을 api로 보여준다. - #실제 프론트와 전달할 api class myBalanceList(APIView): #user(pk)의 질문&대답 목록 def get(self, request, pk): @@ -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) +