Exemple #1
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; }
        }