doctor uchun crud qoshildi
This commit is contained in:
@@ -5,7 +5,7 @@ from django.db import transaction
|
|||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
||||||
# shared
|
# shared
|
||||||
from core.apps.shared.models import Doctor
|
from core.apps.shared.models import Doctor, District, Place
|
||||||
# accounts
|
# accounts
|
||||||
from core.apps.accounts.models import User
|
from core.apps.accounts.models import User
|
||||||
|
|
||||||
@@ -70,11 +70,11 @@ class DoctorCreateSerializer(serializers.Serializer):
|
|||||||
extra_location = serializers.JSONField()
|
extra_location = serializers.JSONField()
|
||||||
|
|
||||||
def validate(self, data):
|
def validate(self, data):
|
||||||
district = ...
|
district = District.objects.filter(id=data['district_id']).first()
|
||||||
if not district:
|
if not district:
|
||||||
raise serializers.ValidationError({"district_id": "Tuman topilmadi"})
|
raise serializers.ValidationError({"district_id": "Tuman topilmadi"})
|
||||||
|
|
||||||
place = ...
|
place = Place.objects.filter(id=data['place_id']).first()
|
||||||
if not place:
|
if not place:
|
||||||
raise serializers.ValidationError({'place_id': "Obyekt topilmadi"})
|
raise serializers.ValidationError({'place_id': "Obyekt topilmadi"})
|
||||||
|
|
||||||
|
|||||||
@@ -23,9 +23,9 @@ class DoctorViewSet(viewsets.GenericViewSet, ResponseMixin):
|
|||||||
queryset = Doctor.objects.all()
|
queryset = Doctor.objects.all()
|
||||||
|
|
||||||
def get_serializer_class(self):
|
def get_serializer_class(self):
|
||||||
if self.action == "POST":
|
if self.action == "post":
|
||||||
return serializers.DoctorCreateSerializer
|
return serializers.DoctorCreateSerializer
|
||||||
elif self.action in ("PATCH", "PUT"):
|
elif self.action in ("patch", "put"):
|
||||||
return serializers.DoctorUpdateSerializer
|
return serializers.DoctorUpdateSerializer
|
||||||
else:
|
else:
|
||||||
return serializers.DoctorListSerializer
|
return serializers.DoctorListSerializer
|
||||||
@@ -195,3 +195,83 @@ class DoctorViewSet(viewsets.GenericViewSet, ResponseMixin):
|
|||||||
data=str(e),
|
data=str(e),
|
||||||
message="xatolik"
|
message="xatolik"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@swagger_auto_schema(
|
||||||
|
tags=['Admin Doctors']
|
||||||
|
)
|
||||||
|
@action(detail=False, methods=['post'], url_path='create')
|
||||||
|
def post(self, request):
|
||||||
|
try:
|
||||||
|
serializer = self.get_serializer(data=request.data)
|
||||||
|
if serializer.is_valid():
|
||||||
|
obj = serializer.save()
|
||||||
|
return self.success_response(
|
||||||
|
data=serializers.DoctorListSerializer(obj).data,
|
||||||
|
message='malumot qoshildi'
|
||||||
|
)
|
||||||
|
return self.failure_response(
|
||||||
|
data=serializer.errors,
|
||||||
|
message='malumot qoshilmadi'
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
return self.error_response(
|
||||||
|
data=str(e),
|
||||||
|
message="xatolik"
|
||||||
|
)
|
||||||
|
|
||||||
|
@swagger_auto_schema(
|
||||||
|
tags=['Admin Doctors']
|
||||||
|
)
|
||||||
|
@action(detail=True, methods=['patch'], url_path='update')
|
||||||
|
def update_doctor(self, request, pk=None):
|
||||||
|
try:
|
||||||
|
doctor = Doctor.objects.filter(id=pk).first()
|
||||||
|
if not doctor:
|
||||||
|
return self.failure_response(
|
||||||
|
data={},
|
||||||
|
message="doctor topilmadi",
|
||||||
|
status_code=404
|
||||||
|
)
|
||||||
|
serializer = self.get_serializer(data=request.data, instance=doctor)
|
||||||
|
if serializer.is_valid():
|
||||||
|
obj = serializer.save()
|
||||||
|
return self.success_response(
|
||||||
|
data=serializers.DoctorListSerializer(obj).data,
|
||||||
|
message='malumot tahrirlandi'
|
||||||
|
)
|
||||||
|
return self.failure_response(
|
||||||
|
data=serializer.errors,
|
||||||
|
message='malumot tahrirlandi'
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
return self.error_response(
|
||||||
|
data=str(e),
|
||||||
|
message="xatolik"
|
||||||
|
)
|
||||||
|
|
||||||
|
@swagger_auto_schema(
|
||||||
|
tags=['Admin Doctors']
|
||||||
|
)
|
||||||
|
@action(detail=True, methods=['delete'], url_path='delete')
|
||||||
|
def delete(self, request, pk=None):
|
||||||
|
try:
|
||||||
|
doctor = Doctor.objects.filter(id=pk).first()
|
||||||
|
if not doctor:
|
||||||
|
return self.failure_response(
|
||||||
|
data={},
|
||||||
|
message="doctor topilmadi",
|
||||||
|
status_code=404
|
||||||
|
)
|
||||||
|
doctor.delete()
|
||||||
|
return self.success_response(
|
||||||
|
data={},
|
||||||
|
message='malumot ochirildi',
|
||||||
|
status_code=204
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
return self.error_response(
|
||||||
|
data=str(e),
|
||||||
|
message="xatolik"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user