public static BookIssuedViewModel MapToViewModel(this CheckoutBook book) { var selectItems = new List <SelectListItem>(); for (int i = 1; i <= 10; i++) { selectItems.Add(new SelectListItem { Text = i.ToString(), Value = i.ToString() }); } var vm = new BookIssuedViewModel(); vm.BookId = book.BookId.ToString(); vm.Author = book.Book.Author; vm.StartDate = book.CheckoutDate; vm.EndDate = book.DueDate; vm.ISBN = book.Book.ISBN; vm.Title = book.Book.Title; vm.Status = book.Book.Status.ToString(); vm.IsReserved = false; vm.RatingList = selectItems; return(vm); }
public async Task AddBookToCheckoutBooksAsync(string isbn, string userName) { var user = await _accountManager.GetUserByUsernameAsync(userName); var booksByIsbn = await _bookManager.GetBooksByIsbnAsync(isbn); var bookToTake = booksByIsbn.FirstOrDefault(b => b.Status == BookStatus.Available); if (bookToTake is null) { throw new ArgumentException(Constants.NoAvailableBooks); } var newBook = new CheckoutBook() { BookId = bookToTake.Id, UserId = user.Id, CheckoutDate = DateTime.Today, DueDate = DateTime.Today.AddDays(Constants.MaxCheckoutDays) }; await this.ChangeBookStatusAsync(bookToTake.Id.ToString(), BookStatus.CheckedOut); _context.CheckoutBooks.Add(newBook); await _context.SaveChangesAsync().ConfigureAwait(false); var message = string.Format(Constants.CheckoutBookNotification, user.Username, newBook.Book.Title, newBook.BookId); await this.AddNotificationAsync(message, await _accountManager.GetAdminAccountAsync()); }
public List <Checkout> GetWithCheckoutBooksByFilter(string[] filters, string[] filters_text) { HashSet <string> keys = new HashSet <string>(); HashSet <string> values = new HashSet <string>(); for (int i = 0; i < filters_text.Length; i++) { keys.Add($"@{filters[i].Replace(".", "")}"); values.Add(filters_text[i].ToString()); } string query = @"SELECT Checkouts.Id as 'Id',Clients.Id as 'ClientId',Clients.Name,Checkouts.Date, Checkouts.DeliveryDate,Checkouts.ExpectedDate FROM Checkouts inner join Clients on Clients.Id = Checkouts.ClientId Where" ; for (int i = 0; i < filters.Length; i++) { query += i == 0 ? $" {filters[i]}={keys.ToArray()[i]}" : $" AND {filters[i]}={keys.ToArray()[i]}"; } DataTable dataTableCheckouts = factory.SelectQuery(query, keys.ToArray(), values.ToArray()); List <Checkout> checkouts = new List <Checkout>(); if (dataTableCheckouts.Rows.Count > 0) { for (int i = 0; i < dataTableCheckouts.Rows.Count; i++) { Client client = new Client(); Checkout checkout = new Checkout(); client.Id = int.Parse(dataTableCheckouts.Rows[i]["ClientId"].ToString()); client.Name = dataTableCheckouts.Rows[i]["Name"].ToString(); checkout.Id = int.Parse(dataTableCheckouts.Rows[i]["Id"].ToString()); checkout.Date = DateTime.Parse(dataTableCheckouts.Rows[i]["Date"].ToString()); checkout.DeliveryDate = dataTableCheckouts.Rows[i]["DeliveryDate"].ToString() == string.Empty ? (DateTime?)null : DateTime.Parse(dataTableCheckouts.Rows[i]["DeliveryDate"].ToString()); checkout.ExpectedDate = DateTime.Parse(dataTableCheckouts.Rows[i]["ExpectedDate"].ToString()); checkout.Client = client; checkouts.Add(checkout); } } foreach (var checkout in checkouts) { keys = new HashSet <string>(); values = new HashSet <string>(); dataTableCheckouts.Dispose(); keys.Add("@CheckoutId"); values.Add(checkout.Id.ToString()); query = @"SELECT Books.Id as 'BookId', Books.Title as 'Title' FROM CheckoutBooks inner join Checkouts on Checkouts.Id = CheckoutBooks.CheckoutId inner join Books on Books.Id = CheckoutBooks.BookId Where CheckoutId=@CheckoutId"; dataTableCheckouts = factory.SelectQuery(query, keys.ToArray(), values.ToArray()); List <CheckoutBook> checkoutBooks = new List <CheckoutBook>(); for (int i = 0; i < dataTableCheckouts.Rows.Count; i++) { CheckoutBook checkoutBook = new CheckoutBook(); Book book = new Book(); book.Id = int.Parse(dataTableCheckouts.Rows[i]["BookId"].ToString()); book.Title = dataTableCheckouts.Rows[i]["Title"].ToString(); checkoutBook.Book = book; checkoutBooks.Add(checkoutBook); } checkout.CheckoutBooks = checkoutBooks; } return(checkouts); }