Esempio n. 1
0
        public bool BorrowBook(string code, string client, DateTime date)
        {
            try
            {
                if (!authenticated)
                {
                    throw new FaultException <NotAuthenticatedException>(new NotAuthenticatedException($"Restricted Area"));
                }

                using (var cli = new LibraryManagerClient())
                {
                    cli.Open();

                    var book = cli.GetBooksByCode(code);

                    if (book == null)
                    {
                        throw new FaultException <BookNotFoundException>(new BookNotFoundException($"Book Code {code} Not Found"),
                                                                         new FaultReason($"Book Code {code} Not Found"));
                    }

                    lock (locker)
                    {
                        if (book.IsBorrowed)
                        {
                            throw new FaultException <BookBorrowedException>(new BookBorrowedException($"Book Code {code} already borrowed"),
                                                                             new FaultReason($"Book Code {code} already borrowed"));
                        }

                        var loans = cli.GetLoansFromClient(client);

                        if (loans.Count == MAX_BOOKS_ALLOWED)
                        {
                            throw new FaultException <MaximumExceededException>(new MaximumExceededException($"You cannot borrow more than {MAX_BOOKS_ALLOWED} books"),
                                                                                new FaultReason($"You cannot borrow more than {MAX_BOOKS_ALLOWED} books"));
                        }

                        cli.UpdateToBorrowed(book.Id, client, date);
                    }


                    cli.Close();
                }

                return(true);
            }
            catch { throw; }
        }
Esempio n. 2
0
        public bool ReturnBook(string code, string client, DateTime date)
        {
            try {
                if (!authenticated)
                {
                    throw new FaultException <NotAuthenticatedException>(new NotAuthenticatedException($"Restricted Area"));
                }


                using (var cli = new LibraryManagerClient())
                {
                    cli.Open();
                    var book = cli.GetBooksByCode(code);

                    if (book == null)
                    {
                        throw new FaultException <BookNotFoundException>(new BookNotFoundException($"Book Code {code} Not Found"));
                    }

                    if (!book.IsBorrowed)
                    {
                        throw new FaultException <BookReturnedException>(new BookReturnedException($"Book Code {code} already returned"));
                    }

                    var loan = cli.GetLoan(book.Id);

                    var delay = date - loan.LoanDate;

                    if (delay.Days > MAX_DAYS_ALLOWED)
                    {
                        throw new FaultException <FineException>(new FineException($"Fine of $ {(delay.Days - MAX_DAYS_ALLOWED) * FINE }"));
                    }

                    cli.UpdateToAvaible(book.Id, client, date);

                    cli.Close();
                }

                return(true);
            }
            catch { throw; }
        }