def post(self): """ Creates a new trainer """ try: payload = request.get_json(force=True) trainer = TrainerDocument(**payload).save() return json.loads(trainer.to_json()), 201 except Exception as error: return {'message': error}
def delete(self, trainerId): """ Based on the trainerId, a trainer is deleted Args: trainerId (str) : unique id of the trainer """ if TrainerDocument.objects(id=trainerId): TrainerDocument.objects(id=trainerId).delete() return {'message': 'Trainer was deleted successful'}, 200 raise Exception('Trainer with this id does not exist')
def get(self, trainerId): """ Baded on the trainerId, details of the trainer is returned Args: trainerId (str) : unique id of the trainer """ if TrainerDocument.objects(id=trainerId): return json.loads( TrainerDocument.objects(id=trainerId).to_json()), 200 raise Exception('Trainer does not exist!')
def get(self, trainerId): """ List of all gym classes, specific trainer Args: trainerId (objectId) : unique identifier of the trainer """ gym_classes = [] document = TrainerDocument.objects(id=trainerId) if document: for gym_class in document: for gym in gym_class.gymclass: gym_classes.append(json.loads(gym.to_json())) return {'classes': gym_classes}, 200 return {'message': 'Gym class does not exist'}, 400
def put(self, trainerId): """ Based on the trainerId, details of the trainer are edited Args: trainerId (str) : unique id of the trainer """ try: payload = request.get_json(force=True) trainer = TrainerDocument.objects(id=trainerId) if trainer: trainer.update_one(**payload) return json.loads(trainer.to_json()), 200 except Exception as error: return {'message': error}
def delete(self, trainerId, classId): """ Deletes a specific gym class from a trainer Args: trainerId (objectId): unique identifier of the trainer classId (objectId): unique identifier of the gym class """ trainers = TrainerDocument.objects(id=trainerId) if trainers: for trainer in trainers: for gym_class in trainer.gymclass: if str(gym_class._id) == classId: TrainerDocument.objects.update(pul__gymclass___id=classId) return {'message' : 'Gym class was deleted!'}, 200 return {'message' : 'Gym class does not exist!'}, 400 return {'message' : 'Gym class does not exist!'}, 400
def post(self, trainerId): """ Create a gym class specific for the trainer Args: trainerId (objectId) : unique identifier of the trainer """ try: payload = request.get_json(force=True) document = TrainerDocument.objects(id=trainerId) if document: for trainer in document: gym_class_document = GymClassDocument(**payload) trainer.gymclass.append(gym_class_document) trainer.save() return json.loads(gym_class_document.to_json()), 200 return {'message': 'Gym class does not exist!'}, 400 except Exception as error: raise ValueError(error)
def put(self, trainerId, classId): """ Updates a specific membership based on the trainerId Args: trainerId (objectId) : unique identifier of the trainer classId (objectId) : unique identifier of the gym class """ try: payload = request.get_json(force=True) trainers = TrainerDocument.objects(id=trainerId) if trainers: for trainer in trainers: for gym_class in trainer.gymclass: if str(gym_class._id) == classId: gym_class.className = payload['className'] gym_class.classTime = payload['classTime'] gym_class.maxAvailable = payload['maxAvailable'] trainer.save() return payload, 200 except Exception as error: raise ValueError(error)
def get(self): """ Returns a list of trainers """ trainers = [] for trainer in TrainerDocument.objects(): trainers.append(json.loads(trainer.to_json())) return {'trainers': trainers}, 200