|
| 1 | +from django.shortcuts import Http404 |
1 | 2 | from rest_framework import serializers
|
| 3 | +from rest_framework.response import Response |
2 | 4 | from rest_framework.views import APIView
|
3 | 5 |
|
4 | 6 | from styleguide_example.api.pagination import (
|
5 | 7 | LimitOffsetPagination,
|
6 | 8 | get_paginated_response,
|
7 | 9 | )
|
8 | 10 | from styleguide_example.users.models import BaseUser
|
9 |
| -from styleguide_example.users.selectors import user_list |
10 |
| - |
| 11 | +from styleguide_example.users.selectors import user_get, user_list |
| 12 | +from styleguide_example.users.services import user_create, user_update |
11 | 13 |
|
12 | 14 | # TODO: When JWT is resolved, add authenticated version
|
| 15 | + |
| 16 | + |
| 17 | +class UserDetailApi(APIView): |
| 18 | + class OutputSerializer(serializers.Serializer): |
| 19 | + id = serializers.IntegerField() |
| 20 | + email = serializers.CharField() |
| 21 | + |
| 22 | + def get(self, request, user_id): |
| 23 | + user = user_get(user_id) |
| 24 | + |
| 25 | + if user is None: |
| 26 | + raise Http404 |
| 27 | + |
| 28 | + data = self.OutputSerializer(user).data |
| 29 | + |
| 30 | + return Response(data) |
| 31 | + |
| 32 | + |
13 | 33 | class UserListApi(APIView):
|
14 | 34 | class Pagination(LimitOffsetPagination):
|
15 | 35 | default_limit = 1
|
@@ -38,3 +58,51 @@ def get(self, request):
|
38 | 58 | request=request,
|
39 | 59 | view=self,
|
40 | 60 | )
|
| 61 | + |
| 62 | + |
| 63 | +class UserCreateApi(APIView): |
| 64 | + class InputSerializer(serializers.Serializer): |
| 65 | + email = serializers.EmailField() |
| 66 | + password = serializers.CharField() |
| 67 | + |
| 68 | + def post(self, request): |
| 69 | + serializer = self.InputSerializer(data=request.data) |
| 70 | + serializer.is_valid(raise_exception=True) |
| 71 | + |
| 72 | + user = user_create( |
| 73 | + **serializer.validated_data |
| 74 | + ) |
| 75 | + |
| 76 | + # Note: This shows a possible reusability for serializers between APIs |
| 77 | + # Usually, this is how we approach things, when building APIs at first |
| 78 | + # But at the very moment when we need to make a change to the output, |
| 79 | + # that's specific to this API, we'll introduce a separate OutputSerializer just for this API |
| 80 | + data = UserDetailApi.OutputSerializer(user).data |
| 81 | + |
| 82 | + return Response(data) |
| 83 | + |
| 84 | + |
| 85 | +class UserUpdateApi(APIView): |
| 86 | + class InputSerializer(serializers.Serializer): |
| 87 | + # Note: Currently, those are not actual user fields, but rather an example |
| 88 | + first_name = serializers.CharField(required=True) |
| 89 | + last_name = serializers.CharField(required=True) |
| 90 | + |
| 91 | + def post(self, request, user_id): |
| 92 | + serializer = self.InputSerializer(data=request.data) |
| 93 | + serializer.is_valid(raise_exception=True) |
| 94 | + |
| 95 | + user = user_get(user_id) |
| 96 | + |
| 97 | + if user is None: |
| 98 | + raise Http404 |
| 99 | + |
| 100 | + user = user_update(user=user, data=serializer.validated_data) |
| 101 | + |
| 102 | + # Note: This shows a possible reusability for serializers between APIs |
| 103 | + # Usually, this is how we approach things, when building APIs at first |
| 104 | + # But at the very moment when we need to make a change to the output, |
| 105 | + # that's specific to this API, we'll introduce a separate OutputSerializer just for this API |
| 106 | + data = UserDetailApi.OutputSerializer(user).data |
| 107 | + |
| 108 | + return Response(data) |
0 commit comments