class Contact(): ''' Contact class ''' def __init__(self): self.class_name = type(self).__name__ self.logger = logging.getLogger(self.class_name) self.collection_name = snake_case(self.class_name) self.collection = DBUtil().get_collection(self.collection_name) def add_item(self, item): ''' Add an item to the database ''' self.collection.insert_one(item) def find_one_item(self, query): ''' find a item in collection ''' return self.collection.find_one(filter=query) def find_many_item(self, query): ''' find a item in collection ''' return self.collection.find_many(filter=query) def add_multiple(self, lists): '''insert multiple posts or documents''' return self.collection.insert_many(documents=lists) def find_all(self): '''returns all documents or a collection''' results = self.collection.find({}) for result in results: print(result) # def find_multiple(self, results, result): # '''finds multiple documents''' # results = self.collection.find(result) # for result in results: # return results def delete_one_item(self, delt): # what is the filter from '''delete one item''' return self.collection.delete_one(filter=delt) def delete_clear(self): '''clear database''' return self.collection.delete_many({}) def update_first_name(self, firstName): change = input('New name? ') return self.collection.update_one({'firstName': firstName}, {'$set': {'firstName': change}})
class Contact(): ''' Contact class ''' def __init__(self): self.class_name = type(self).__name__ # Contact self.logger = logging.getLogger(self.class_name) self.collection_name = snake_case(self.class_name) self.collection = DBUtil().get_collection(self.collection_name) def add_item(self, item): ''' Add an item to the database ''' if self.find_one(query={ 'firstName': item['firstName'], 'lastName': item['lastName']}): self.logger.error('Contact already exists for %s %s', item['firstName'], item['lastName']) return self.logger.info('Contact %s %s added successfully', item['firstName'], item['lastName']) self.collection.insert_one(item) def find_one(self, query): ''' find a item in collection ''' return self.collection.find_one(filter=query) def find_many(self, query): ''' find a item in collection ''' return self.collection.find(filter=query) def delete_one(self, query): ''' delete an item from collection ''' return self.collection.delete_one(filter=query) def delete_many(self, query): ''' delete an item from collection ''' return self.collection.delete_many(filter=query) def update(self, query, update_dict): ''' update a item in the collection ''' result = self.collection.update_one(filter=query, update=update_dict) if result.modified_count == 0: self.logger.warning('Nothing changed') else: self.logger.info('New -> %s', self.find_one(query=query))
class Movie(): ''' Contact class ''' #MIGHT HAVE TO MOVE THE CONSTRUCTORS TO A NEW py class def __init__(self, name=None, director=None, starring=None, category=None): self.class_name = type(self).__name__ self.logger = logging.getLogger(self.class_name) self.collection_name = snake_case(self.class_name) self.collection = DBUtil().get_collection(self.collection_name) self.name = name self.director = director self.starring = starring self.category = category # cd.collection.insert_one(item) def add_item(self, item): # how did we place the inputs here before? ''' Add an item to the database ''' if self.find_one(query={ #just change the contents? 'movieName': item['movieName'] }): self.logger.error('Movie already exists for %s', item['movieName']) return self.logger.info('Movie %s added successfully', item['movieName']) self.collection.insert_one(item) def find_one(self, query): ''' find a item in collection ''' return self.collection.find_one(filter=query) def find_many(self, query): ''' find a item in collection ''' return self.collection.find(filter=query) def delete_one(self, query): ''' delete an item from collection ''' return self.collection.delete_one(filter=query) def delete_many(self, query): ''' delete an item from collection ''' return self.collection.delete_many(filter=query) def update(self, query, update_dict): ''' update a item in the collection ''' result = self.collection.update_one(filter=query, update=update_dict) if result.modified_count == 0: self.logger.warning('Nothing changed') else: self.logger.info('New -> %s', self.find_one(query=query)) ###################sets_and_gets########################## # def get(self): # return {'movieName': self.name} #Movie({'movieName': input('What is the Movie Name? ').lower().strip(' ')}) def get(self): ''' returns address ''' return { 'name': self.name, 'starring': self.starring, 'director': self.director, 'category': self.category, } def set_name(self, name): self.name = name def get_name(self, name): return self.name def set_director(self, director): self.director = director def get_director(self, director): return self.director def set_category(self, category): self.category = category def get_category(self, category): return self.category
class Movies(): ''' Movies Class ''' def __init__(self): self.class_name = type(self).__name__ # Contact self.logger = logging.getLogger(self.class_name) self.collection_name = snake_case(self.class_name) self.collection = DBUtil().get_collection(self.collection_name) def get_all(self): ''' get all movies ''' self.logger.info(list(self.collection.find({}))) return { 'result': list( self.collection.find({}, {'_id': 0}) ) #'result': list(self.collection.find({}, {'_id': 0, 'genre': 1})) }, 200 def get_one(self, title): ''' get one movie by title ''' result = self.collection.find_one({'title': title}, {'_id': 0}) if result: return result, 200 return {'message': f'{title} not found'}, 404 def create_one(self, payload): ''' Create a movie ''' added_title = self.collection.find_one(payload) if added_title: return { 'message': f'Duplicate Error {payload} already in database' }, 409 self.collection.insert_one(payload) return { 'message': f'Created movie: {payload}', 'movieName': f'{payload["title"]}', 'movieGenre': f'{payload["genre"]}', }, 201 def update_one(self, title, payload): ''' update one movie by title ''' result = self.collection.find_one({'title': title}, {'_id': 0}) if result: self.collection.update_one({'title': title}, {'$set': payload}) return {'message': f'Updated {title}'}, 200 return {'message': f' {title} not found'}, 404 def delete_one(self, title): ''' delete one movie by title ''' result = self.collection.find_one({'title': title}, {'_id': 0}) if result: self.collection.delete_one(result) return {'message': f' {title}- successfully deleted from db'}, 200 return {'message': f' {title} not found'}, 404 def delete_many(self, title): ''' delete multiple movies of the same title by title ''' # result = self.collection.find({'title': title}, {'_id': 0}) #NO '_d'? use get_all logger? find_many? or get_all result = self.collection.find({'title': title}, {'_id': 0}) if result: self.collection.delete_many(result) return {'message': f'All titles by {title} have been deleted'}, 200 return {f'{title} not found'}, 404