async def get_market_by_registry(self, request): try: LOGGER.debug('Getting market action') record = request.path_params.get('market_registry') market = self.service.get_market_by_registry(registry=record) return generate_response(data=market, status_code=200) except JSONDecodeError: LOGGER.debug("Invalid payload") return generate_response(message=[INVALID_PAYLOAD], status_code=400) except NotFoundException: LOGGER.debug('Market not found') return generate_response(status_code=404) except InvalidRecordException as exception: LOGGER.debug("Invalid record param") return generate_response(message=exception.messages, status_code=422) except Exception as exception: LOGGER.debug(traceback.format_exc()) LOGGER.critical(f"Unknown error: {exception}") return generate_response(status_code=500, message=[CRITICAL_ERROR])
def test_generate_response(self): response = generate_response() json = JSONResponse({ "message": [], "notice": [], "data": {} }, status_code=200) self.assertEqual(response.body, json.body)
async def create_market(self, request): try: LOGGER.debug('Creating market action') payload = await request.json() market = self.service.create_market(data=payload) return generate_response(data=market, status_code=201) except JSONDecodeError: LOGGER.debug("Invalid payload") return generate_response(message=[INVALID_PAYLOAD], status_code=400) except InvalidRecordException as exception: LOGGER.debug("Invalid record param") return generate_response(message=exception.messages, status_code=422) except MissingRequiredFieldsException as exception: return generate_response(message=exception.messages, status_code=400) except DataError: LOGGER.debug("Invalid value informed") return generate_response(message=[INVALID_FIELD_VALUE], status_code=422) except IntegrityError: return generate_response(message=[REGISTRY_ALREADY_USED], status_code=422) except Exception as exception: LOGGER.debug(traceback.format_exc()) LOGGER.critical(f"Unknown error: {exception}") return generate_response(status_code=500, message=[CRITICAL_ERROR])
async def remove_favorite(self, request): try: LOGGER.debug('Removing customer favorite product action') customer_id = request.path_params.get('customer_id') product_id = request.path_params.get('product_id') customer = FavoritesService().remove_favorite( product_id=product_id, customer_id=customer_id) return generate_response(data=customer, status_code=201) except JSONDecodeError: LOGGER.debug("Invalid payload") return generate_response(message=[INVALID_PAYLOAD], status_code=400) except MissingRequiredFieldsException as exception: return generate_response(message=exception.messages, status_code=400) except DuplicateKeyError: return generate_response(message=[{ 'message': 'Duplicated email' }], status_code=400) except (InvalidFieldsValuesException, EmailAlreadyUsedException) as exception: return generate_response(message=exception.messages, status_code=422) except Exception as exception: LOGGER.debug(traceback.format_exc()) LOGGER.critical(f"Unknown error: {exception}") return generate_response(status_code=500, message=[CRITICAL_ERROR])
async def delete_customer(self, request): try: LOGGER.debug('Delete customer action') customer_id = request.path_params.get('customer_id') CustomerService().delete_customer(customer_id=customer_id) return generate_response(data={'_id': customer_id}) except JSONDecodeError: LOGGER.debug("Invalid payload") return generate_response(message=[INVALID_PAYLOAD], status_code=400) except MissingRequiredFieldsException as exception: return generate_response(message=exception.messages, status_code=400) except NotFoundException: return generate_response(status_code=404) except InvalidFieldsValuesException as exception: return generate_response(message=exception.messages, status_code=422) except Exception as exception: LOGGER.debug(traceback.format_exc()) LOGGER.critical(f"Unknown error: {exception}") return generate_response(status_code=500, message=[CRITICAL_ERROR])
async def create_customer(self, request): try: LOGGER.debug('Creating customer action') payload = await request.json() customer = CustomerService().create_customer(data=payload) return generate_response(data=customer, status_code=201) except JSONDecodeError: LOGGER.debug("Invalid payload") return generate_response(message=[INVALID_PAYLOAD], status_code=400) except MissingRequiredFieldsException as exception: return generate_response(message=exception.messages, status_code=400) except DuplicateKeyError: return generate_response(message=[{ 'message': 'Duplicated email' }], status_code=400) except (InvalidFieldsValuesException, EmailAlreadyUsedException) as exception: return generate_response(message=exception.messages, status_code=422) except Exception as exception: LOGGER.debug(traceback.format_exc()) LOGGER.critical(f"Unknown error: {exception}") return generate_response(status_code=500, message=[CRITICAL_ERROR])
async def update_customer(self, request): try: LOGGER.debug('Update customer action') customer_id = request.path_params.get('customer_id') payload = await request.json() customer = CustomerService().update_customer( data=payload, customer_id=customer_id) return generate_response(data=customer) except JSONDecodeError: LOGGER.debug("Invalid payload") return generate_response(message=[INVALID_PAYLOAD], status_code=400) except MissingRequiredFieldsException as exception: return generate_response(message=exception.messages, status_code=400) except NotFoundException: return generate_response(status_code=404) except (InvalidFieldsValuesException, EmailAlreadyUsedException) as exception: return generate_response(message=exception.messages, status_code=422) except Exception as exception: LOGGER.debug(traceback.format_exc()) LOGGER.critical(f"Unknown error: {exception}") return generate_response(status_code=500, message=[CRITICAL_ERROR])
async def update_product(self, request): try: LOGGER.debug('Update product action') product_id = request.path_params.get('product_id') payload = await request.json() product = ProductService().update_product(data=payload, product_id=product_id) return generate_response(data=product) except JSONDecodeError: LOGGER.debug("Invalid payload") return generate_response(message=[INVALID_PAYLOAD], status_code=400) except MissingRequiredFieldsException as exception: return generate_response(message=exception.messages, status_code=400) except NotFoundException: return generate_response(status_code=404) except InvalidFieldsValuesException as exception: return generate_response(message=exception.messages, status_code=422) except Exception as exception: LOGGER.debug(traceback.format_exc()) LOGGER.critical(f"Unknown error: {exception}") return generate_response(status_code=500, message=[CRITICAL_ERROR])
async def get_products(self, request): try: LOGGER.debug('Getting products action') page = request.query_params.get('page') products = ProductService().get_products(page=page) return generate_response(data=products) except JSONDecodeError: LOGGER.debug("Invalid payload") return generate_response(message=[INVALID_PAYLOAD], status_code=400) except MissingRequiredFieldsException as exception: return generate_response(message=exception.messages, status_code=400) except NotFoundException: return generate_response(status_code=404) except InvalidFieldsValuesException as exception: return generate_response(message=exception.messages, status_code=422) except Exception as exception: LOGGER.debug(traceback.format_exc()) LOGGER.critical(f"Unknown error: {exception}") return generate_response(status_code=500, message=[CRITICAL_ERROR])