Exemplo n.º 1
0
        /// <summary>
        /// Lets the user loan a book
        /// </summary>
        private void LoanBookById()
        {
            Console.Clear();

            Member memberToLoan = SelectMember();
            Book   loanBook     = SelectBook();

            Console.Write("Enter a day you want to loan your book: ");
            string input = Console.ReadLine();

            DateTime.TryParse(input, out DateTime startDate);


            Console.Write("Enter when you want to return your book: ");
            input = Console.ReadLine();
            DateTime.TryParse(input, out DateTime endDate);

            List <Loan> loans = LoanRepository.GetLoans();

            bool loanAccepted       = false;
            int  startDateResultat  = DateTime.Compare(startDate, DateTime.Now.Date);
            int  numberOFBooksLoans = 0;

            try
            {
                for (int i = 0; i < loans.Count; i++)
                {
                    if (loans[i].BookRented != null)
                    {
                        if (loans[i].BookRented.Id == loanBook.Id)
                        {
                            //The following is not met: the new loan start date is later than the current loan end date OR the new loan end date is earlier than the current loan start date.
                            if (!(startDate > loans[i].EndDate || endDate < loans[i].StartDate))
                            {
                                numberOFBooksLoans++;
                            }
                        }
                    }
                }

                if (numberOFBooksLoans < loanBook.Copies)
                {
                    loanAccepted = true;
                }

                else
                {
                    throw new Exception("try another date");
                }

                if (startDateResultat < 0)
                {
                    Console.WriteLine($"You have to book {DateTime.Now.ToShortDateString()} or later ");
                    loanAccepted = false;
                }
            }
            catch (System.Exception)
            {
                Console.WriteLine("try another date");
            }

            if (loanAccepted == true)
            {
                BookRepository.UpdateBookById(loanBook.Id, loanBook);
                LoanRepository.LoanBook(loanBook, memberToLoan, startDate, endDate);
            }

            Console.WriteLine("Press enter");
            Console.ReadLine();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            GenreRepository  genreRepository  = new GenreRepository();
            BookRepository   bookRepository   = new BookRepository();
            AuthorRepository authorRepository = new AuthorRepository();
            GenresService    genresService    = new GenresService();
            AuthorsService   authorService    = new AuthorsService();
            BooksService     booksService     = new BooksService();
            UserService      userService      = new UserService();

            List <Genre> genres  = genreRepository.GetAll();
            var          books   = bookRepository.GetAll();
            var          book    = bookRepository.Get(1);
            var          authors = authorRepository.GetAll();

            //var authors = new List<AuthorDTO>();
            //authors.Add(authorService.FindById(1));
            //authors.Add(authorService.FindById(2));
            //var book = new BookDTO
            //{
            //    Title = "Book1",
            //    Pages = 100,
            //    Description = "New Book 1",
            //    Genre = genresService.FindById(2),
            //    Authors = authors
            //};
            //var list = book.Authors.Select(a => new { authorId = a.Id, bookId = 1 }).ToList();
            //booksService.Create(new BookDTO
            //{
            //    Title = "Book1",
            //    Pages = 100,
            //    Description = "New Book 1",
            //    Genre = genresService.FindById(2),
            //    Authors = authors
            //});

            //authorService.Create(new AuthorDTO
            //{
            //    FirstName = "Petya",
            //    LastName = "Ivanov",
            //    Birth = DateTime.Parse("2010-01-01")
            //});

            //AuthorDTO author = authorService.FindById(1);
            //List<AuthorDTO> authors = authorService.GetAll();

            //userService.Create(new UserDTO
            //{
            //    Login = "******",
            //    Password = "******",
            //    Email = "*****@*****.**"
            //});

            //UserDTO author = userService.FindById(1);
            //List<UserDTO> authors = userService.GetAll();


            //genresService.Delete(1);
            //GenreDTO genre = genresService.FindById(1);
            //List<GenreDTO> genres = genresService.GetAll();
            ;

            //WeatherAPIService weatherAPIService = new WeatherAPIService();
            //var weather = weatherAPIService.GetWeatherForCity("Dnepropetrovsk");

            //genresService.CreateGenre(new GenreDTO { Name = "Some genre" });
            //var genres = genresService.GetAllGenres();

            //genresService.CreateGenre(new GenreDTO { Name = "" });

            Console.Read();
        }