Beispiel #1
0
 def create_loan():
         record = json.loads(request.data)
         x=Accounts.objects(username=record['username'])
         y=x.count()
         if y>0:
             customer = Loan(loantype=record['loantype'],loanamount=record['loanamount'],date=record['date'],rateofinterest=record['rateofinterest'],durationofloan=record['durationofloan'],username=record['username'])
             return jsonify(customer.save())
         else:
             return jsonify({"output":"Username does not exists. please register to apply loan"}),404
Beispiel #2
0
 def delete_loan(customer_username):
     customer=Loan.objects(username=customer_username)
     y=customer.count()
     if y>0:
         #delete the details in mongodb
         customer = Loan.objects(username=customer_username)
         customer.delete()
         return jsonify({"output":"loan has been removed"})
     else:
         return jsonify({"output":"you does not have any loans to delete"}),404
Beispiel #3
0
 def update_loan(customer_username):
     customer=Loan.objects(username=customer_username)
     y=customer.count()
     if y>0:
         record = json.loads(request.data)
         customer = Loan.objects(username=customer_username)
         customer.update(loantype=record['loantype'],loanamount=record['loanamount'],date=record['date'],rateofinterest=record['rateofinterest'],durationofloan=record['durationofloan'],username=record['username'])
         return jsonify(customer)
     else:
         return jsonify({"output":"you does not have any loans to update"}),404
Beispiel #4
0
 def get_loan(customer_username):
     # try:
         customer=Loan.objects(username=customer_username)
         # return Response(customer, mimetype="application/json", status=200)
         y=customer.count()
         if y>0:
             return jsonify(customer)
         else:
             # return Response({"output":"you does not have any loans to display"}, mimetype="application/json", status=404)
             return '',404
Beispiel #5
0
 def deepCopy(self, other):
     '''
     Function to deepCopy another LibraryRepository to this (self) one
     It copies all the data from another Repository to this one with no references of the objects (so that the states do not
         depend at all)
     :param other: another LibraryRepository
     '''
     self._books = [
         Book(book.getId(), book.getTitle(), book.getDescription(),
              book.getAuthor()) for book in other.getBooks()
     ]
     self._clients = [
         Client(client.getCnp(), client.getName())
         for client in other.getClients()
     ]
     self._loans = [
         Loan(self.searchClient(loan.getClient().getCnp()),
              self.searchBook(loan.getBook().getId()))
         for loan in other.getLoans()
     ]
Beispiel #6
0
from model.user import User
from model.book import Book
from model.loan import Loan
import time
joao = User(123, "Joao", "Silva")

code = Book(12, "Clean Code", 2017, "Uncle Bob")

loan = Loan(1, joao, code, "Ribeirão Preto")

print(loan.begin_date)
print(loan.begin_time)

loan.finish_loan("Sertãozinho")
print(loan.end_date)
print(loan.end_time)

time.sleep(10)
loan.finish_loan("Rio Preto")
print(loan.end_date)
print(loan.end_time)
Beispiel #7
0
    def testRunningScenarios(self):
        #Running scenario 1
        books = []
        clients = []
        testrepo = LibraryRepository(False)
        controller = LibraryController(testrepo)
        cmd = Command(
            "addBook|Introduction to Algorithms|The Bible for every computer scientist|Thomas H Cormen"
        )
        controller.addBook(cmd.toAddBook(0))
        books.append(cmd.toAddBook(0))
        cmd = Command(
            "addBook|Learning Python - 3rd Edition|A very nice book for learning Python from scratch|Mark Lutz"
        )
        controller.addBook(cmd.toAddBook(1))
        books.append(cmd.toAddBook(1))
        cmd = Command("addClient|1960715060015|Rusu Cosmin")
        controller.addClient(cmd.toAddClient())
        clients.append(cmd.toAddClient())
        cmd = Command("addClient|2960715060015|Rusu Raluca")
        controller.addClient(cmd.toAddClient())
        clients.append(cmd.toAddClient())
        assert controller.getBooks() == books
        assert controller.getClients() == clients
        cmd = Command("updateName|1960715060015|Rusu Cosmin-Ionut")
        controller.updateClientName(int(cmd.getArg(1)), cmd.getArg(2))
        clients[0].setName("Rusu Cosmin-Ionut")
        assert controller.getClients() == clients
        cmd = Command("updateCnp|2960715060015|2020715060015")
        controller.updateClientCnp(int(cmd.getArg(1)), int(cmd.getArg(2)))
        clients[1].setCnp(2020715060015)
        assert controller.getClients() == clients
        cmd = Command("removeClient|2020715060015")
        controller.removeClient(int(cmd.getArg(1)))
        clients = clients[:-1]
        assert controller.getClients() == clients
        cmd = Command("removeBook|1")
        controller.removeBook(int(cmd.getArg(1)))
        books = books[:-1]
        cmd = Command(
            "updateDescription|0|The best book on algorithms and data structures"
        )
        controller.updateDescription(int(cmd.getArg(1)), cmd.getArg(2))
        books[0].setDescription(
            "The best book on algorithms and data structures")
        assert controller.getBooks() == books
        controller.undo()
        controller.undo()
        assert controller.getBooks() == [
            Book(0, "Introduction to Algorithms",
                 "The Bible for every computer scientist", "Thomas H Cormen"),
            Book(1, "Learning Python - 3rd Edition",
                 "A very nice book for learning Python from scratch",
                 "Mark Lutz")
        ]
        assert controller.getClients() == [
            Client(1960715060015, "Rusu Cosmin-Ionut")
        ]
        controller.redo()
        assert controller.getBooks() == [
            Book(0, "Introduction to Algorithms",
                 "The Bible for every computer scientist", "Thomas H Cormen")
        ]
        assert controller.getClients() == [
            Client(1960715060015, "Rusu Cosmin-Ionut")
        ]

        #tests exactly like the running scenario here: https://github.com/rusucosmin/courses/tree/master/fop/lab05-07

        books = []
        clients = []
        testrepo = LibraryRepository(False)
        controller = LibraryController(testrepo)
        cmd = Command(
            "addBook|Introduction to Algorithms|The Bible for every computer scientist|Thomas H Cormen"
        )
        controller.addBook(cmd.toAddBook(0))
        books.append(cmd.toAddBook(0))
        cmd = Command(
            "addBook|Learning Python - 3rd Edition|A very nice book for learning Python from scratch|Mark Lutz"
        )
        controller.addBook(cmd.toAddBook(1))
        books.append(cmd.toAddBook(1))
        cmd = Command("addClient|1960715060015|Rusu Cosmin")
        controller.addClient(cmd.toAddClient())
        clients.append(cmd.toAddClient())
        cmd = Command("addClient|2960715060015|Rusu Raluca")
        controller.addClient(cmd.toAddClient())
        clients.append(cmd.toAddClient())
        assert controller.getBooks() == books
        assert controller.getClients() == clients
        cmd = Command("updateName|1960715060015|Rusu Cosmin-Ionut")
        controller.updateClientName(int(cmd.getArg(1)), cmd.getArg(2))
        clients[0].setName("Rusu Cosmin-Ionut")
        assert controller.getClients() == clients
        cmd = Command("updateCnp|2960715060015|2020715060015")
        controller.updateClientCnp(int(cmd.getArg(1)), int(cmd.getArg(2)))
        clients[1].setCnp(2020715060015)
        assert controller.getClients() == clients
        cmd = Command("removeClient|2020715060015")
        controller.removeClient(int(cmd.getArg(1)))
        clients = clients[:-1]
        assert controller.getClients() == clients
        cmd = Command("removeBook|1")
        controller.removeBook(int(cmd.getArg(1)))
        books = books[:-1]
        cmd = Command(
            "updateDescription|0|The best book on algorithms and data structures"
        )
        controller.updateDescription(int(cmd.getArg(1)), cmd.getArg(2))
        books[0].setDescription(
            "The best book on algorithms and data structures")
        assert controller.getBooks() == books
        controller.undo()
        controller.undo()
        assert controller.getBooks() == [
            Book(0, "Introduction to Algorithms",
                 "The Bible for every computer scientist", "Thomas H Cormen"),
            Book(1, "Learning Python - 3rd Edition",
                 "A very nice book for learning Python from scratch",
                 "Mark Lutz")
        ]
        assert controller.getClients() == [
            Client(1960715060015, "Rusu Cosmin-Ionut")
        ]
        controller.redo()
        assert controller.getBooks() == [
            Book(0, "Introduction to Algorithms",
                 "The Bible for every computer scientist", "Thomas H Cormen")
        ]
        assert controller.getClients() == [
            Client(1960715060015, "Rusu Cosmin-Ionut")
        ]

        cmd = Command("addClient|2960715060015|Rusu Raluca-Rusu")
        controller.addClient(cmd.toAddClient())
        cmd = Command("addBook|Dorian Gray's Portret|Modern novel|Oscar Wilde")
        controller.addBook(cmd.toAddBook(1))
        cmd = Command(
            "addBook|The basis of Math|A nice book on Math for students|Dorin Andrica"
        )
        controller.addBook(cmd.toAddBook(2))
        print(controller.getClients())
        print(controller.getLoans())
        cmd = Command("rentBook|1960715060015|0")
        controller.rentBook(int(cmd.getArg(1)), int(cmd.getArg(2)))
        cmd = Command("rentBook|1960715060015|1")
        controller.rentBook(int(cmd.getArg(1)), int(cmd.getArg(2)))
        cmd = Command("returnBook|2960715060015|1")
        controller.returnBook(int(cmd.getArg(1)), int(cmd.getArg(2)))
        assert controller.getBooks() == [
            Book(0, "Introduction to Algorithms",
                 "The Bible for every computer scientist", "Thomas H Cormen"),
            Book(1, "Dorian Gray's Portret", "Modern novel", "Oscar Wilde"),
            Book(2, "The basis of Math", "A nice book on Math for students",
                 "Dorin Andrica")
        ]
        assert controller.getClients() == [
            Client(1960715060015, "Rusu Cosmin-Ionut"),
            Client(2960715060015, "Rusu Raluca-Rusu")
        ]
        assert controller.getLoans() == [
            Loan(
                Client(1960715060015, "Rusu Cosmin-Ionut"),
                Book(0, "Introduction to Algorithms",
                     "The Bible for every computer scientist",
                     "Thomas H Cormen"))
        ]
        '''
Beispiel #8
0
 def get_loans():
     return jsonify(Loan.objects())
Beispiel #9
0
 def rentBook(self, clientCNP, bookID):
     clt = self.searchClient(clientCNP)
     bk = self.searchBook(bookID)
     if bk in [rental.getBook() for rental in self.getLoans()]:
         raise LibraryException("Book already rented to somebody!")
     self._loans.append(Loan(clt, bk))