def put(self, id): request_data = {**request.json, 'id': id} try: request_data = UpdateCustomerRequestResponse().load(request_data) except ValidationError as err: return create_validation_error(err) session = session_factory() repository = SQLACustomerRepository(session) try: response = UpdateCustomer(repository)(request_data) except exceptions.CustomerNotFound: return create_response_error( code='CUSTOMER_NOT_FOUND', message=f'Customer with id {id} not found', status_code=HTTPStatus.NOT_FOUND, ) except exceptions.CustomerAlreadyRegistered: email = request_data.get('email') return create_response_error( code='CUSTOMER_ALREADY_REGISTERED', message= f'Already exists a customer registered with email {email}', status_code=HTTPStatus.UNPROCESSABLE_ENTITY, ) return response, HTTPStatus.OK
def get(self, id): session = session_factory() customer = get_customer(session, id) if customer: return customer return create_response_error( code='CUSTOMER_NOT_FOUND', message=f'Customer with id {id} not found', status_code=HTTPStatus.NOT_FOUND, )
def delete(self, id): session = session_factory() repository = SQLACustomerRepository(session) try: DeleteCustomer(repository)({'customer_id': id}) except exceptions.CustomerNotFound: return create_response_error( code='CUSTOMER_NOT_FOUND', message=f'Customer with id {id} not found', status_code=HTTPStatus.NOT_FOUND, ) return '', HTTPStatus.NO_CONTENT
def post(self): try: request_data = CreateCustomerRequest().load(request.json) except ValidationError as err: return create_validation_error(err) session = session_factory() repository = SQLACustomerRepository(session) try: response = CreateCustomer(repository)(request_data) except exceptions.CustomerAlreadyRegistered: email = request.json.get('email') return create_response_error( code='CUSTOMER_ALREADY_REGISTERED', message= f'Already exists a customer registered with email {email}', status_code=HTTPStatus.UNPROCESSABLE_ENTITY, ) return response, HTTPStatus.CREATED
def post(self): try: request_data = LoginSchema().load(request.json) except ValidationError as err: return create_validation_error(err) email = request_data.get('email', None) password = request_data.get('password', None) session = session_factory() user = get_user(session, email) if not user or not user.check_password(password): return create_response_error( code='AUTHENTICATION_FAILED', message='Wrong email or password', status_code=HTTPStatus.UNAUTHORIZED, ) return {'token': create_access_token(identity=email)}, HTTPStatus.OK
def post(self, customer_id, product_id): session = session_factory() repository = SQLACustomerRepository(session) service = AddProductToWishlist(repository, product_api) try: response = service({'customer_id': customer_id, 'product_id': product_id}) except exceptions.ProductAlreadAddedToWishlist: return create_response_error( code='PRODUCT_ALREADY_ADDED', message=f'Product with id {product_id} already added to wishlist', status_code=HTTPStatus.UNPROCESSABLE_ENTITY, ) except exceptions.ProductNotFound: return create_response_error( code='PRODUCT_NOT_FOUND', message=f'Product with id {product_id} not found', status_code=HTTPStatus.NOT_FOUND, ) return response, HTTPStatus.CREATED
def post(self): try: request_data = LoginSchema().load(request.json) except ValidationError as err: return create_validation_error(err) email = request_data.get('email') session = session_factory() if email_already_registered(session, email): return create_response_error( code='EMAIL_ALREADY_REGISTERED', message=f'Email "{email}" already registered', status_code=HTTPStatus.UNPROCESSABLE_ENTITY, ) user = User.create(email=email, password=request_data.get('password')) session.add(user) session.commit() return {'message': 'User registered with success'}, HTTPStatus.CREATED
def delete(self, product_id, customer_id): session = session_factory() repository = SQLACustomerRepository(session) service = RemoveProductFromWishlist(repository) return service({'customer_id': customer_id, 'product_id': product_id})
def get(self): session = session_factory() return list_customers(session)