def books_list(request): if request.method == 'POST': status_codes = copy.deepcopy(STATUS_CODES) status_codes[res.ResponseSuccess.SUCCESS] = 201 initial_data = request.POST request_object = BookAddRequestObject.from_dict(initial_data) repo = DjangoORMBookRepository() use_case = BookAddUseCase(repo) response = use_case.execute(request_object) return HttpResponse(content=json.dumps(response.value, cls=BookEncoder), content_type='application/json', status=status_codes[response.type]) qrystr_params = { 'filters': {}, } for arg, values in request.GET.items(): if arg.startswith('filter_'): qrystr_params['filters'][arg.replace('filter_', '')] = values request_object = BookListRequestObject.from_dict(qrystr_params) repo = DjangoORMBookRepository() use_case = BookListUseCase(repo) response = use_case.execute(request_object) return HttpResponse(json.dumps(response.value, cls=BookEncoder), content_type='application/json', status=STATUS_CODES[response.type])
def book_detail(request, pk): if request.method == 'DELETE': status_codes = copy.deepcopy(STATUS_CODES) status_codes[res.ResponseSuccess.SUCCESS] = 204 request_object = BookDeleteRequestObject.from_dict({'pk': pk}) repo = DjangoORMBookRepository() use_case = BookDeleteUseCase(repo) response = use_case.execute(request_object) return HttpResponse(content=json.dumps(response.value, cls=BookEncoder), content_type='application/json', status=status_codes[response.type]) if request.method == 'PATCH': patch_data = request.POST repo = DjangoORMBookRepository() if "'action': 'update'" in str(patch_data): request_object = BookUpdateRequestObject.from_dict({ 'pk': pk, 'patch': patch_data }) use_case = BookUpdateUseCase(repo) elif "'action': 'give'" in str(patch_data): request_object = BookGiveRequestObject.from_dict({ 'pk': pk, 'patch': patch_data }) use_case = BookGiveUseCase(repo) elif "'action': 'take'" in str(patch_data): request_object = BookReturnRequestObject.from_dict({ 'pk': pk, 'patch': patch_data }) use_case = BookReturnUseCase(repo) response = use_case.execute(request_object) return HttpResponse(content=json.dumps(response.value, cls=BookEncoder), content_type='application/json', status=STATUS_CODES[response.type]) request_object = BookDetailsRequestObject.from_dict({'pk': pk}) repo = DjangoORMBookRepository() use_case = BookDetailsUseCase(repo) response = use_case.execute(request_object) return HttpResponse(json.dumps(response.value, cls=BookEncoder), content_type='application/json', status=STATUS_CODES[response.type])
def setUp(self): self.repo = DjangoORMBookRepository() self.repo_readers = DjangoORMReaderRepository() self.reader = self.repo_readers.create( code='r2rwr3re-bdfc-e2ww-5644-hd94id04kd9r', full_name='John Smith', reg_date=datetime.date(2016, 1, 1)) self.patch = {'reader': self.reader} self.repo.create(code='f853578c-fc0f-4e65-81b8-566c5dffa35a', title='1984', author='George Orwell', year=1984, language='English', is_available=True, reader=None) self.repo.create(code='f853578c-fc0f-4e65-81b8-566c5dffa35b', title='1984', author='George Orwell', year=1984, language='English', is_available=False, reader=None) self.updated_book = \ DomainBook(code='f853578c-fc0f-4e65-81b8-566c5dffa35a', title='1984', author='George Orwell', year=1984, language='English', is_available=False, reader=self.reader)
def setUp(self): self.repo = DjangoORMBookRepository() self.repo.create(code='f853578c-fc0f-4e65-81b8-566c5dffa35a', title='1984', author='George Orwell', year=1984, language='English', is_available=True, reader=None)
def setUp(self): self.repo = DjangoORMBookRepository() self.repo.create(code='f853578c-fc0f-4e65-81b8-566c5dffa35a', title='1984', author='George Orwell', year=1984, language='English', is_available=True, reader=None) self.updated_book = \ DomainBook(code='f853578c-fc0f-4e65-81b8-566c5dffa35a', title='Fahrenheit 451', author='Ray Bradbury', year=1984, language='English', is_available=True, reader=None)
def setUp(self): self.repo = DjangoORMBookRepository() self.repo_readers = DjangoORMReaderRepository() self.reader = None self.patch = {'reader': self.reader} self.repo.create(code='f853578c-fc0f-4e65-81b8-566c5dffa35a', title='1984', author='George Orwell', year=1984, language='English', is_available=True, reader=None) self.repo.create(code='f853578c-fc0f-4e65-81b8-566c5dffa35b', title='1984', author='George Orwell', year=1984, language='English', is_available=False, reader=self.reader) self.updated_book = \ DomainBook(code='f853578c-fc0f-4e65-81b8-566c5dffa35b', title='1984', author='George Orwell', year=1984, language='English', is_available=True, reader=None)
def setUp(self): self.repo = DjangoORMBookRepository() self.repo.create(code='f853578c-fc0f-4e65-81b8-566c5dffa35a', title='1984', author='George Orwell', year=1984, language='English', is_available=True, reader=None) self.repo.create(code='fe2c3195-aeff-487a-a08f-e0bdc0ec6e9a', title='The Lord of the Rings', author='J.R.R. Tolkien', year=2000, language='English', is_available=False, reader=None) self.repo.create(code='913694c6-435a-4366-ba0d-da5334a611b2', title='The Master and Margarita', author='Mikhail Bulgakov', year=2005, language='Russian', is_available=False, reader=None) self.repo.create(code='eed76e77-55c1-41ce-985d-ca49bf6c0585', title='The Dark Tower', author='Stephen King', year=2015, language='English', is_available=True, reader=None) self.expected_result = [ DomainBook(code='f853578c-fc0f-4e65-81b8-566c5dffa35a', title='1984', author='George Orwell', year=1984, language='English', is_available=True, reader=None), DomainBook(code='fe2c3195-aeff-487a-a08f-e0bdc0ec6e9a', title='The Lord of the Rings', author='J.R.R. Tolkien', year=2000, language='English', is_available=False, reader=None), DomainBook(code='913694c6-435a-4366-ba0d-da5334a611b2', title='The Master and Margarita', author='Mikhail Bulgakov', year=2005, language='Russian', is_available=False, reader=None), DomainBook(code='eed76e77-55c1-41ce-985d-ca49bf6c0585', title='The Dark Tower', author='Stephen King', year=2015, language='English', is_available=True, reader=None) ]