You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.5 KiB
54 lines
1.5 KiB
6 months ago
|
# Create your views here.
|
||
|
from rest_framework.decorators import api_view
|
||
|
|
||
|
from myapp.handler import APIResponse
|
||
|
from myapp.models import Record
|
||
|
from myapp.serializers import RecordSerializer
|
||
|
|
||
|
|
||
|
@api_view(['GET'])
|
||
|
def list_api(request):
|
||
|
if request.method == 'GET':
|
||
|
records = Record.objects.all()
|
||
|
serializer = RecordSerializer(records, many=True)
|
||
|
return APIResponse(code=0, msg='查询成功', data=serializer.data)
|
||
|
|
||
|
|
||
|
@api_view(['POST'])
|
||
|
def create(request):
|
||
|
|
||
|
serializer = RecordSerializer(data=request.data)
|
||
|
if serializer.is_valid():
|
||
|
serializer.save()
|
||
|
return APIResponse(code=0, msg='创建成功', data=serializer.data)
|
||
|
|
||
|
return APIResponse(code=1, msg='创建失败')
|
||
|
|
||
|
|
||
|
@api_view(['POST'])
|
||
|
def update(request):
|
||
|
try:
|
||
|
pk = request.GET.get('id', -1)
|
||
|
records = Record.objects.get(pk=pk)
|
||
|
except Record.DoesNotExist:
|
||
|
return APIResponse(code=1, msg='对象不存在')
|
||
|
|
||
|
serializer = RecordSerializer(records, data=request.data)
|
||
|
if serializer.is_valid():
|
||
|
serializer.save()
|
||
|
return APIResponse(code=0, msg='更新成功', data=serializer.data)
|
||
|
|
||
|
return APIResponse(code=1, msg='更新失败')
|
||
|
|
||
|
|
||
|
@api_view(['POST'])
|
||
|
def delete(request):
|
||
|
try:
|
||
|
ids = request.GET.get('ids')
|
||
|
ids_arr = ids.split(',')
|
||
|
Record.objects.filter(id__in=ids_arr).delete()
|
||
|
except Record.DoesNotExist:
|
||
|
return APIResponse(code=1, msg='对象不存在')
|
||
|
|
||
|
return APIResponse(code=0, msg='删除成功')
|