def search_unique(self, keyobj): for obj in self._entities: #if keyobj == obj: if keyobj.rentalID == obj.rentalID: raise RepositoryError(self._name + " Invalid ID!\n") elif keyobj.book.bookID == obj.book.bookID and obj.returnedDate is None: raise RepositoryError(self._name + " Book is already taken!\n")
def search(self, key): # Function that search an element (given as key) in the collection of entities if key not in self._entities: raise RepositoryError("The element (movie/client/rental) doesn't exist \n") for element in self._entities: if element == key: return element
def update(self, obj): #updates an object from the repo #raises a RepositoryError if the object does not exist for i in range(len(self._entities)): if self._entities[i] == obj: self._entities[i] = obj return raise RepositoryError(self._name + " Inexistend ID!\n")
def add(self, obj): #adds a new object (obj) to the repo #raises a RepositoryError if the id of the obj is invalid self.read_bin_all() if obj in self._entities: raise RepositoryError(self._name + " Existing ID!\n") self._entities.append(obj) self.write_bin_all()
def search(self, keyobj): #returns an object with a specific key (keyobj) from the repo #raises a RepositoryError if the id is not in the repo if keyobj not in self._entities: raise RepositoryError(self._name + " Inexistent ID!\n") for obj in self._entities: if obj == keyobj: return obj
def add(self, element): ''' Function that adds element to the list of entities * Raises exception if element is already in the list (verified by the ID) ''' if element in self._entities: raise RepositoryError('Id already exists \n') self._entities.append(element)
def searchClientId(self, clientId): c = [] for client in self._entities: if str(client.clientID).find(str(clientId)) != -1: c.append(client) if len(c) > 0: return c else: raise RepositoryError(self._name + " No matching books were found!\n")
def searchClientName(self, clientName): c = [] for client in self._entities: if client.clientName.lower().find(clientName.lower()) != -1: c.append(client) if len(c) > 0: return c else: raise RepositoryError(self._name + " No matching clients were found!\n")
def searchBookAuthor(self, bookAuthor): self.read_bin_all() l = [] for book in self._entities: if book.bookAuthor.lower().find(bookAuthor.lower()) != -1: l.append(book) if len(l) > 0: return l else: raise RepositoryError(self._name + " No matching books were found!\n")
def searchBookId(self, bookId): self.read_all() l = [] for book in self._entities: if str(book.bookID).find(str(bookId)) != -1: l.append(book) if len(l) > 0: return l else: raise RepositoryError(self._name + " No matching books were found!\n")
def search(self, keyobj): #returns an object with a specific key (keyobj) from the repo #raises a RepositoryError if the id is not in the repo #print(str(keyobj.bookID)) cmd = "SELECT * FROM " + self._table_name + " WHERE Id=" + str( keyobj.bookID) + ";" self._cursor.execute(cmd) line = self._cursor.fetchone() if line is None: raise RepositoryError(self._name + " Inexistent ID!\n") return Book(int(line[0]), line[1], line[2])
def searchBookId(self, bookId): def same_id(book): if str(book.bookID).find(str(bookId)) != -1: return True else: return False l = self._entities.filter(same_id) if len(l) > 0: return l else: raise RepositoryError(self._name + " No matching books were found!\n")
def add(self, obj): #adds a new object in the sql table cmd = "SELECT * FROM " + self._table_name + " WHERE Id=" + str( obj.clientID) + ";" self._cursor.execute(cmd) line = self._cursor.fetchone() if line is not None: raise RepositoryError(self._name + " Existing ID!\n") cmd = 'INSERT INTO ' + self._table_name + " VALUES (" + str( obj.clientID) + ",'" + obj.clientName + "');" self._cursor.execute(cmd) self._conn.commit()
def searchBookTitle(self, bookTitle): def same_title(book): if book.bookTitle.lower().find(bookTitle.lower()) != -1: return True else: return False l = self._entities.filter(same_title) if len(l) > 0: return l else: raise RepositoryError(self._name + " No matching books were found!\n")
def remove(self, obj): #removes an object from the repo #raises a RepositoryError if the obj does not exist i = 0 y = self.size() #print(self.size()) while i < self.size(): if self._entities[i] == obj: del self._entities[i] return i = i + 1 if y == self.size(): raise RepositoryError(self._name + " Inexistend ID!\n")
def add(self, obj): #adds a new object in the sql table cmd = "SELECT * FROM " + self._table_name + " WHERE Id=" + str( obj.bookID) + ";" #self.read_data() #if obj in self._entities: self._cursor.execute(cmd) line = self._cursor.fetchone() if line is not None: raise RepositoryError(self._name + " Existing ID!\n") cmd = 'INSERT INTO ' + self._table_name + " VALUES (" + str( obj.bookID) + ",'" + obj.bookTitle + "','" + obj.bookAuthor + "');" self._cursor.execute(cmd) self._conn.commit()
def search(self, keyobj): #returns an object with a specific key (keyobj) from the repo #raises a RepositoryError if the id is not in the repo #print(str(keyobj.bookID)) cmd = "SELECT * FROM " + self._table_name + " WHERE Id=" + str( keyobj.rentalID) + ";" self._cursor.execute(cmd) line = self._cursor.fetchone() if line is None: raise RepositoryError(self._name + " Inexistent ID!\n") if line[4] is not None: obj = Rental( int(line[0]), Book(int(line[1]), None, None), Client(int(line[2]), None), datetime.datetime.strptime(line[3], '%Y-%m-%d').date(), datetime.datetime.strptime(line[4], '%Y-%m-%d').date()) else: obj = Rental( int(line[0]), Book(int(line[1]), None, None), Client(int(line[2]), None), datetime.datetime.strptime(line[3], '%Y-%m-%d').date(), None) return obj
def add(self, obj): #adds a new object in the sql table cmd = "SELECT * FROM " + self._table_name + " WHERE Id=" + str( obj.rentalID) + ";" #self.read_data() #if obj in self._entities: self._cursor.execute(cmd) line = self._cursor.fetchone() if line is not None: raise RepositoryError(self._name + " Existing ID!\n") if obj.returnedDate is not None: cmd = 'INSERT INTO ' + self._table_name + " VALUES (" + str( obj.rentalID) + "," + str(obj.book.bookID) + "," + str( obj.client.clientID) + ",'" + str( obj.rentedDate) + "','" + str(obj.returnedDate) + "');" else: cmd = 'INSERT INTO ' + self._table_name + " VALUES (" + str( obj.rentalID) + "," + str(obj.book.bookID) + "," + str( obj.client.clientID) + ",'" + str( obj.rentedDate) + "', NULL " + ");" self._cursor.execute(cmd) self._conn.commit()