public static void IncreasePrices(BookShopContext context) { var books = context.Books .Where(b => b.ReleaseDate.Value.Year < 2010) .Update(x => new Book() { Price = x.Price + 5 }); context.SaveChanges(); }
public static int RemoveBooks(BookShopContext context) { var books = context.Books.Where(b => b.Copies < 4200).ToList(); context.BooksCategories.Where(b => books.Contains(b.Book)).ToList().ForEach(b => context.Remove(b)); books.ForEach(b => context.Books.Remove(b)); context.SaveChanges(); return(books.Count); }
/// <summary> /// Task 15. Removes all books which have less than 4200 copies. /// </summary> /// <param name="context"></param> /// <returns>Returns the number of deleted books as int.</returns> public static int RemoveBooks(BookShopContext context) { var booksToDelete = context.Books.Where(x => x.Copies < 4200); context.Books.RemoveRange(booksToDelete); int affectedRows = context.SaveChanges(); return(affectedRows); }
//P16. Removebooks public static string Removebook(BookShopContext db) { int minCopies = 4200; var booksToRemove = db.Books.Where(b => b.Copies < minCopies).ToArray(); db.Books.RemoveRange(booksToRemove); int booksRemovedCount = db.SaveChanges(); return($"{booksRemovedCount} books were deleted"); }
public static void IncreasePrices(BookShopContext context) { //context.Database.ExecuteSqlRaw("UPDATE Books SET Price = Price + 5 WHERE YEAR(ReleaseDate) < 2010"); var books = context.Books.Where(x => x.ReleaseDate.HasValue && x.ReleaseDate.Value.Year < 2010).ToList(); foreach (var book in books) { book.Price += 5; } context.SaveChanges(); }
//14 public static void IncreasePrices(BookShopContext db) { var books = db.Books.Where(b => b.ReleaseDate.Value.Year < 2010).ToArray(); foreach (var book in books) { book.Price += 5m; } db.SaveChanges(); }
//Problem: 14 public static void IncreasePrices(BookShopContext context) { var pricesBooks = context.Books.Where(x => x.ReleaseDate.Value.Year < 2010).ToList(); foreach (var prices in pricesBooks) { prices.Price += 5; } context.SaveChanges(); }
public static int RemoveBooks(BookShopContext context) { var books = context.Books.Where(x => x.Copies < 4200).ToList(); foreach (var book in books) { context.Books.Remove(book); } context.SaveChanges(); return(books.Count()); }
public static string ImportBooks(BookShopContext context, string xmlString) { var sb = new StringBuilder(); var xmlSerializer = new XmlSerializer(typeof(List <ImportBookDto>), new XmlRootAttribute("Books")); var reader = new StringReader(xmlString); using (reader) { var bookDtos = (List <ImportBookDto>)xmlSerializer.Deserialize(reader); var booksToAdd = new List <Book>(); foreach (var bookDto in bookDtos) { if (!IsValid(bookDto)) { sb.AppendLine(ErrorMessage); continue; } DateTime publishedOn; var isDateValid = DateTime.TryParseExact(bookDto.PublishedOn, "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out publishedOn);; if (!isDateValid) { sb.AppendLine(ErrorMessage); continue; } var validBook = new Book() { Name = bookDto.Name, Genre = (Genre)bookDto.Genre, Price = bookDto.Price, Pages = bookDto.Pages, PublishedOn = publishedOn }; booksToAdd.Add(validBook); sb.AppendLine(string.Format(SuccessfullyImportedBook, validBook.Name, validBook.Price)); } context.Books.AddRange(booksToAdd); context.SaveChanges(); } return(sb.ToString().Trim()); }
public static int RemoveBooks(BookShopContext context) { var books = context.Books .Where(b => b.Copies < 4200) .ToArray(); context.Books.RemoveRange(books); var rowsAffected = context.SaveChanges(); return(books.Length); }
public static int RemoveBooks(BookShopContext context) { Book[] books = context.Books.Where(b => b.Copies < 4200) .ToArray(); context.RemoveRange(books); context.SaveChanges(); return(books.Count()); }
//14 Increase Prices: public static void IncreasePrices(BookShopContext context) { var booksToIncrease = context .Books .Where(b => b.ReleaseDate.Value.Year < 2010) .ToList(); booksToIncrease.ForEach(x => x.Price += 5); context.SaveChanges(); }
public static int RemoveBooks(BookShopContext context) { var booksToDelete = context.Books .Where(x => x.Copies < 4200) .ToList(); context.RemoveRange(booksToDelete); context.SaveChanges(); return(booksToDelete.Count()); }
//Problem 14 public static void IncreasePrices(BookShopContext context) { var books = context.Books.Where(x => x.ReleaseDate.Value.Year < 2010).ToList(); for (int i = 0; i < books.Count(); i++) { books[i].Price += 5; } context.SaveChanges(); }
public static void IncreasePrices(BookShopContext context) { var books = context.Books.Where(b => b.ReleaseDate.Value.Year < 2010); foreach (var book in books) { book.Price += 5; } context.SaveChanges(); }
public static int RemoveBooks(BookShopContext context) { var books = context.Books .Where(b => b.Copies < 4200) .ToList(); context.Books.RemoveRange(books); context.SaveChanges(); return(books.Count); }
private static void RemoveBooks(BookShopContext context) { Console.Write("Please enter minimal number of copies: "); int minNumberOfCopies = int.Parse(Console.ReadLine()); var booksWithLowerNumberOfCopies = context.Books.Where(book => book.Copies < minNumberOfCopies); Console.WriteLine($"{booksWithLowerNumberOfCopies.Count()} books were deleted"); context.Books.Delete(booksWithLowerNumberOfCopies); context.SaveChanges(); }
// 14. Increase Prices public static void IncreasePrices(BookShopContext context) { var books = context .Books .Where(rd => rd.ReleaseDate.Value.Year < 2010) .ToList(); books.ForEach(p => p.Price += 5); context.SaveChanges(); }
public static int RemoveBooks(BookShopContext context) { var books = context.Books .Where(c => c.Copies < 4200) .ToArray(); context.Books.RemoveRange(books); int affectedRows = context.SaveChanges(); //return affectedRows; return(books.Length); }
public static void IncreasePrices(BookShopContext context) { decimal IncreasePricesWith = 5; context.Books .Where(x => x.ReleaseDate.Value.Year < 2015) .ToList() .ForEach(b => b.Price += IncreasePricesWith);; context.SaveChanges(); }
public static void IncreasePrices(BookShopContext context) { var books = context.Books .Where(x => x.ReleaseDate.GetValueOrDefault().Year < 2010); foreach (var book in books) { book.Price += 5.0m; } context.SaveChanges(); }
public static int RemoveBooks(BookShopContext dbContext) { Book[] books = dbContext .Books .Where(b => b.Copies < 4200) .ToArray(); dbContext.Books.RemoveRange(books); dbContext.SaveChanges(); return(books.Length); }
public static int RemoveBooks(BookShopContext db) { var deletedBooks = db.Books.Where(x => x.Copies < 4200).ToList(); var bookCount = deletedBooks.Count; db.RemoveRange(deletedBooks); db.SaveChanges(); return(bookCount); }
public static int RemoveBooks(BookShopContext context) { var booksForRemoving = context.Books .Where(x => x.Copies < 4200); var removedCount = booksForRemoving.Count(); context.RemoveRange(booksForRemoving); context.SaveChanges(); return(removedCount); }
public static void IncreasePrices(BookShopContext context) { var books = context.Books .Where(e => e.ReleaseDate.Value.Year < 2010).ToList(); foreach (var x in books) { x.Price += 5m; } context.SaveChanges(); }
public static void IncreasePrices(BookShopContext db) { var update = db.Books.Where(b => b.ReleaseDate.Value.Year < 2010 && b.ReleaseDate != null); foreach (var book in update) { book.Price += 5; } db.SaveChanges(); }
public static void IncreasePrices(BookShopContext context) { var books = context.Books .Where(x => x.ReleaseDate.Value.Year < 2010) .ToArray(); foreach (var item in books) { item.Price += 5; } context.SaveChanges(); }
public static int RemoveBooks(BookShopContext context) { var books = context.Books .Where(x => x.Copies < 4200); int count = books.Count(); context.RemoveRange(books); context.SaveChanges(); return(count); }
public static int RemoveBooks(BookShopContext context) { var booksToDelete = context.Books.Where(b => b.Copies < 4200); var countOfBooksToDelete = booksToDelete.Count(); context.Books.RemoveRange(booksToDelete); context.SaveChanges(); return(countOfBooksToDelete); }
public static int RemoveBooks(BookShopContext db) { var books = db.Books .Where(x => x.Copies < 4200); var count = books.Count(); db.Books.RemoveRange(books); db.SaveChanges(); return(count); }
public IHttpActionResult DeleteBookById(int id) { var context = new BookShopContext(); var book = context.Books.FirstOrDefault(b => b.Id == id); if (book == null) { return this.NotFound(); } context.Books.Remove(book); context.SaveChanges(); return this.Ok(); }
public IHttpActionResult DeleteCategoriesById(int id) { var context = new BookShopContext(); var category = context.Categories .FirstOrDefault(c => c.Id == id); if (category == null) { return this.NotFound(); } context.Categories.Remove(category); context.SaveChanges(); return this.Ok(); }
public IHttpActionResult PutBook([FromUri] int id, [FromBody] BookPutBindingModel bookModel) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var context = new BookShopContext(); var book = context.Books.FirstOrDefault(b => b.Id == id); if (book == null) { return this.NotFound(); } book.Title = bookModel.Title; book.Decription = bookModel.Decription; book.Price = bookModel.Price; book.Copies = bookModel.Copies; book.EditionType = bookModel.EditionType; book.AgeRestriction = bookModel.AgeRestriction; book.ReleaseDate = bookModel.ReleaseDate; book.AuthorId = bookModel.AuthorId; context.SaveChanges(); return this.Ok(bookModel); }
private static void RelatedBooks(BookShopContext context) { var books = context.Books .Take(3) .ToList(); books[0].RelatedBooks.Add(books[1]); books[1].RelatedBooks.Add(books[0]); books[0].RelatedBooks.Add(books[2]); books[2].RelatedBooks.Add(books[0]); context.SaveChanges(); var booksFromQuery = context.Books .Take(3) .Select(b => new { b.Title, RelatedBooks = b.RelatedBooks.Select(rb => rb.Title) }); Console.WriteLine(Separator); foreach (var book in booksFromQuery) { Console.WriteLine("--{0}", book.Title); foreach (var relatedBook in book.RelatedBooks) { Console.WriteLine(relatedBook); } } Console.WriteLine(Separator); }
public IHttpActionResult PutBuyBook(int id) { var context = new BookShopContext(); var book = context.Books.FirstOrDefault(b => b.Id == id); if (book == null) { return this.NotFound(); } if (book.Copies <= 0) { return this.BadRequest("No copies left"); } book.Copies--; var purchase = new Purchase() { Book = book, ApplicationUser = context.Users.FirstOrDefault(u => u.UserName == this.User.Identity.Name), Price = book.Price, DateOfPurchase = DateTime.Now, IsRecalled = false }; context.Purchases.Add(purchase); context.SaveChanges(); var purchaseView = new PurchaseViewModel() { BookName = book.Title, Price = book.Price, User = purchase.ApplicationUser.UserName }; return this.Ok(purchaseView); }
public static void Main() { var context = new BookShopContext(); var booksReleaseDate = context.Books.Where(b => b.ReleaseDate.Value.Year > 2000).Select(b => b.Title); context.SaveChanges(); //foreach (string book in booksReleaseDate) //{ // Console.WriteLine(book); //} var authors = context.Authors .Where(a => a.Books.Any(b => b.ReleaseDate.Value.Year < 1990)).Select(a => new { a.FirstName, a.LastName }); //foreach (var author in authors) //{ // Console.WriteLine("{0} {1}",author.FirstName,author.LastName); //} var authorsBookCount = context.Authors .OrderByDescending(a => a.Books.Count) .Select(a => new { a.FirstName, a.LastName, BookCount = a.Books.Count }); //foreach (var author in authorsBookCount) //{ // Console.WriteLine("{0} {1} - Books: {2}", author.FirstName, author.LastName, author.BookCount); //} var booksGeorge = context.Books .Where(b => b.Author.FirstName == "George" && b.Author.LastName == "Powell") .OrderByDescending(b => b.ReleaseDate) .ThenBy(b => b.Title) .Select(b => new { b.Title, b.ReleaseDate, b.Copies }); //foreach (var book in booksGeorge) //{ // Console.WriteLine("{0}, released on: {1} - Copies: {2}", book.Title, book.ReleaseDate.Value.ToString("d"), book.Copies); //} var booksByCategory = context.Categories.OrderByDescending(c => c.Books.Count).Select(c => new { c.Name, BookCount = c.Books.Count, Books = c.Books.OrderByDescending(b => b.ReleaseDate).ThenBy(b => b.Title).Select(b => new { b.Title, b.ReleaseDate }).Take(3) }); //foreach (var categoy in booksByCategory) //{ // Console.WriteLine("--{0}: {1} books{2}{3}", // categoy.Name, categoy.BookCount, // Environment.NewLine, // string.Join(Environment.NewLine, categoy.Books.Select(b => string.Format("{0} ({1})", b.Title, b.ReleaseDate.Value.Year)))); //} //var books = context.Books //.Take(3) //.ToList(); //books[0].RelatedBooks.Add(books[1]); //books[1].RelatedBooks.Add(books[0]); //books[0].RelatedBooks.Add(books[2]); //books[2].RelatedBooks.Add(books[0]); //context.SaveChanges(); var booksFromQuery = context.Books.Take(3).Select(b => new { b.Title, b.RelatedBooks }); foreach (var book in booksFromQuery) { Console.WriteLine("--{0}", book.Title); foreach (var relatedBook in book.RelatedBooks) { Console.WriteLine(relatedBook.Title); } } }
public IHttpActionResult PostAuthor([FromBody] AuthorBindingMethod model) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var author = new Author() { FirstName = model.FirstName, LastName = model.LastName }; var context = new BookShopContext(); context.Authors.Add(author); context.SaveChanges(); var authorView = new AuthorViewModel.AuthorInfoViewModel() { FirstName = author.FirstName, LastName = author.LastName }; return this.Ok(authorView); }
public IHttpActionResult PutRecallBook(int id) { var context = new BookShopContext(); var purchase = context.Purchases.FirstOrDefault(p => p.Id == id); if (purchase == null) { return this.NotFound(); } if (purchase.ApplicationUser.UserName != this.User.Identity.Name) { return this.BadRequest("You cannot return this purchase, you are not the buyer!"); } purchase.IsRecalled = true; purchase.Book.Copies++; context.SaveChanges(); return this.Ok("Refunded!"); }
public static void Main() { var context = new BookShopContext(); var migrationStrategy = new MigrateDatabaseToLatestVersion<BookShopContext, Configuration>(); Database.SetInitializer(migrationStrategy); Console.Write("Do you want output for step 6 y/n: "); string wannaSix = Console.ReadLine(); if (wannaSix == "y") { Console.Write("Please choise part from 1 to 5: "); string input = Console.ReadLine(); switch (input) { case "1": var bookTitles = context.Books .Where(b => b.ReleaseDate.Year > 2000) .Select(b => new { b.Title }); foreach (var bookTitle in bookTitles) { Console.WriteLine(bookTitle.Title); } break; case "2": var authorsByBook = context.Authors .Where(a => a.Books .Any(b => b.ReleaseDate.Year < 1990)) .Select(a => new { FirstName = a.FirstName, LastName = a.LastName }); foreach (var author in authorsByBook) { Console.WriteLine("{0} {1}", author.FirstName, author.LastName); } break; case "3": var authors = context.Authors .OrderByDescending(a => a.Books.Count) .Select(a => new { FirstName = a.FirstName, LastName = a.LastName, BookCount = a.Books.Count }); foreach (var author in authors) { Console.WriteLine("{0} {1} - {2}", author.FirstName, author.LastName, author.BookCount); } break; case "4": var georgesBooks = context.Books .Where(b => b.Author.FirstName == "George" && b.Author.LastName == "Powell") .OrderByDescending(b => b.ReleaseDate) .ThenBy(b => b.Title) .Select(b => new { Title = b.Title, ReleaseDate = b.ReleaseDate, Copies = b.Copies }); foreach (var georgesBook in georgesBooks) { Console.WriteLine("{0} - {1} - {2}", georgesBook.Title, georgesBook.ReleaseDate, georgesBook.Copies); } break; case "5": var booksByCategories = context.Categories .OrderBy(c => c.Books.Count) .Select(c => new { Books = c.Books .OrderByDescending(b => b.ReleaseDate) .ThenBy(b => b.Title) .Take(3) .Select(b => new { BookTitle = b.Title, ReleaseDate = b.ReleaseDate }), CategoryName = c.Name, BookCount = c.Books.Count }); foreach (var booksByCategory in booksByCategories) { Console.WriteLine("--{0}: {1} books", booksByCategory.CategoryName, booksByCategory.BookCount); foreach (var book in booksByCategory.Books) { Console.WriteLine("{0} ({1})", book.BookTitle, book.ReleaseDate.Year); } } break; default: Console.WriteLine("Incorect choise."); break; } } else { var books = context.Books .Take(3) .ToList(); if (books.Count == 0) { books[0].RelatedBooks.Add(books[1]); books[1].RelatedBooks.Add(books[0]); books[0].RelatedBooks.Add(books[2]); books[2].RelatedBooks.Add(books[0]); context.SaveChanges(); } var booksFromQuery = context.Books .Take(3) .Select(b => new { Title = b.Title, RelatedBooks = b.RelatedBooks.Select(rb => new { rb.Title }) }); foreach (var book in booksFromQuery) { Console.WriteLine("--{0}", book.Title); foreach (var relatedBook in book.RelatedBooks) { Console.WriteLine(relatedBook.Title); } } } }
public IHttpActionResult PostBook([FromBody] BookPostBindingModel bookPost) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var context = new BookShopContext(); var categories = bookPost.Categories.Split(' ').Select(c => c.Trim()).ToList(); var categoryList = new List<Category>(); categories.ForEach(c => { var categoryDb = context.Categories.FirstOrDefault(ca => ca.Name == c); if (categoryDb == null) { categoryList.Add(new Category() { Name = c }); } else { categoryList.Add(categoryDb); } }); var newBook = new Book() { Title = bookPost.Title, Decription = bookPost.Decription, Price = bookPost.Price, Copies = bookPost.Copies, EditionType = bookPost.EditionType, AgeRestriction = bookPost.AgeRestriction, ReleaseDate = bookPost.ReleaseDate, Categories = categoryList, AuthorId = 1 }; context.Books.Add(newBook); context.SaveChanges(); return this.Ok(bookPost); }
public IHttpActionResult PostCategory([FromBody] CategoryBindingModel model) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var context = new BookShopContext(); var duplicate = context.Categories .FirstOrDefault(c => c.Name == model.Name); if (duplicate != null) { var message = string.Format("Category with {0} name already exist!", model.Name); return this.BadRequest(message); } var category = new Category { Name = model.Name }; context.Categories.Add(category); context.SaveChanges(); var viewModel = new CategoryViewModel() { Name = category.Name }; return this.Ok(viewModel); }