def get(self, customerId): """ Based on the customerId, details of the customer is returned Args: customerId (str) : unique id of the customer """ if CustomerDocument.objects(id=customerId): return json.loads( CustomerDocument.objects(id=customerId).to_json()), 200 raise Exception('Customer does not exist!')
def delete(self, customerId): """ Based on the customerId, customer is deleted Args: customerId (str) : unique id of the customer """ if CustomerDocument.objects(id=customerId): CustomerDocument.objects(id=customerId).delete() return {'message': 'Customer was deleted successful'}, 200 raise Exception('Customer with this id does not exist!')
def get(self, customerId): """ List of all memberships, specific customer """ memberships = [] document = CustomerDocument.objects(id=customerId) if document: for customer in document: for member_pass in customer.membership: memberships.append(json.loads(member_pass.to_json())) return {'membership': memberships}, 200 return {'message': 'Customer does not exist'}, 404
def put(self, customerId): """ Based on the customerId, details of the customer are edited Args: customerId (str) : unique id of the customer """ try: payload = request.get_json(force=True) customer = CustomerDocument.objects(id=customerId) if customer: customer.update_one(**payload) return json.loads(customer.to_json()), 200 except Exception as error: raise Exception(error)
def delete(self, customerId, membershipId): """ Deletes a specific membership based on the customerId Args: customerId (str): unique identifier of the customer membershipId (str): unique identifier of the membership """ customers = CustomerDocument.objects(id=customerId) if customers: for customer in customers: for membership in customer.membership: if str(membership._id) == membershipId: CustomerDocument.objects.update( pull__membership___id=membershipId) return {'message': 'Membership deleted'}, 200 return {'message': 'Membership does not exist!'}, 404 return {'message': 'Customer does not exist!'}, 404
def post(self, customerId): """ Create a new membership for the customer Args: customerId (str): unique identifier of the customer """ try: payload = request.get_json(force=True) document = CustomerDocument.objects(id=customerId) if document: for customer in document: membership = MembershipDocument(**payload) customer.membership.append(membership) customer.save() return json.loads(membership.to_json()), 200 return {'message': 'Customer does not exist!'}, 404 except Exception as error: raise ValueError(error)
def put(self, customerId, membershipId): """ Updates a specific memberhsip based on the customerId Args: customerId (str): unique identifier of the customer memberhsipId (str): unique identifier of the membership """ try: payload = request.get_json(force=True) customers = CustomerDocument.objects(id=customerId) if customers: for customer in customers: for membership in customer.membership: if str(membership._id) == membershipId: membership.passMembership = payload[ 'passMembership'] membership.price = payload['price'] membership.startDate = payload['startDate'] membership.endDate = payload['endDate'] customer.save() return payload, 200 except Exception as error: raise ValueError(error)
def get(self): """ List all the customers from Mongodb """ customers = [] for customer in CustomerDocument.objects(): customers.append(json.loads(customer.to_json())) return {'customers': customers}, 200