public ActionResult Create([Bind(Include = "Id,Name")] Catergory catergory) { if (ModelState.IsValid) { db.Catergories.Add(catergory); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(catergory)); }
public ActionResult Create([Bind(Include = "ID,Name")] Author author) { if (ModelState.IsValid) { db.Authors.Add(author); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(author)); }
public ActionResult Create([Bind(Include = "ID,Title,PublishedDate,Description,ISBN,CategotyID")] Book book) { if (ModelState.IsValid) { db.Books.Add(book); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.CategotyID = new SelectList(db.Categories, "ID", "Name", book.CategotyID); return(View(book)); }
/// <summary> /// Dodaj nową książkę do bazy danych. /// </summary> /// <param name="title">Tytuł ksiązki</param> /// <param name="purchaseDate">Data zakupu ksiązki</param> /// <param name="genere">Gatunek</param> /// <param name="shelf">Półka, na której znajduje się książka</param> /// <param name="authors">Lista autorów książki</param> public void AddBook(string title, DateTime purchaseDate, Genere genere, Shelf shelf, List <Author> authors) { title = title.Trim(); if (title == null || title == "" || purchaseDate == null || genere == null || shelf == null || authors == null || authors.Count == 0) { throw new ArgumentNullException(); } if (purchaseDate > DateTime.Now) { throw new ArgumentOutOfRangeException(); } Book bookToAdd = new Book() { Title = title, PurchaseDate = purchaseDate, GenereId = genere.GenereId, ShelfId = shelf.ShelfId }; using (BookshelfContext context = new BookshelfContext()) { context.Add(bookToAdd); context.SaveChanges(); } foreach (Author author in authors) { AddBookAuthor(bookToAdd, author); } }
public int Create(Book item) { var tags = item.Tags; item.Tags = new List <Tag>(); foreach (var booktag in tags) { Tag tag = db.Tags.SingleOrDefault(t => t.Name == booktag.Name); if (tag == null) { tag = new Tag { Name = booktag.Name }; db.Tags.Add(tag); } item.Tags.Add(tag); } var authors = item.Authors; item.Authors = new List <Author>(); foreach (var author in authors) { Author aut = db.Authors.SingleOrDefault(t => t.Name == author.Name); if (aut == null) { continue; } item.Authors.Add(aut); } db.Books.Add(item); db.SaveChanges(); return(item.Id); }
/// <summary> /// Usuwa istniejącą w bazie półkę. /// </summary> /// <param name="shelfToRemove">Półka, która ma zostać usunięta z bazy</param> public void RemoveShelf(Shelf shelfToRemove) { using (BookshelfContext context = new BookshelfContext()) { context.Remove(shelfToRemove); context.SaveChanges(); } }
/// <summary> /// Usuwa istniejący w bazie gatunek. /// </summary> /// <param name="genereToRemove">Gatunek, który ma zostać usunięty z bazy</param> public void RemoveGenere(Genere genereToRemove) { using (BookshelfContext context = new BookshelfContext()) { context.Remove(genereToRemove); context.SaveChanges(); } }
/// <summary> /// Usuwa istniejącego w bazie autora. /// </summary> /// <param name="authorToRemove">Autor, który ma zostać usunięty z bazy</param> public void RemoveAuthor(Author authorToRemove) { using (BookshelfContext context = new BookshelfContext()) { context.Remove(authorToRemove); context.SaveChanges(); } }
/// <summary> /// Resetuje licznik przeczytań książki /// </summary> /// <param name="book">Książka, której licznik ma zostać zresetowany</param> public void ResetBookReadCount(Book book) { if (book == null) { throw new ArgumentNullException(); } using (BookshelfContext context = new BookshelfContext()) { book.ReadCount = 0; context.Update(book); context.SaveChanges(); } }
/// <summary> /// Zwiększa licznik przeczytań książki o 1. /// </summary> /// <param name="bookRead">Przeczytana książka</param> public void IncrementBookReadCount(Book bookRead) { if (bookRead == null) { throw new ArgumentNullException(); } using (BookshelfContext context = new BookshelfContext()) { bookRead.ReadCount++; context.Update(bookRead); context.SaveChanges(); } }
/// <summary> /// Modyfikuje istniejącą w bazie półkę. /// </summary> /// <param name="shelfToModify">Pólka, która ma zostać zmodyfikowana</param> /// <param name="name">Nowa nazwa półki</param> public void ModifyShelf(Shelf shelfToModify, string name) { name = name.Trim(); if (shelfToModify == null || name == null || name == "") { throw new ArgumentNullException(); } shelfToModify.ShelfName = name; using (BookshelfContext context = new BookshelfContext()) { context.Update(shelfToModify); context.SaveChanges(); } }
/// <summary> /// Modyfikuje istniejący w bazie gatunek. /// </summary> /// <param name="genereToModify">Autor, który ma zostać zmodyfikowany</param> /// <param name="name">Nowa nazwa gatunku</param> public void ModifyGenere(Genere genereToModify, string name) { name = name.Trim(); if (genereToModify == null || name == null || name == "") { throw new ArgumentNullException(); } genereToModify.GenereName = name; using (BookshelfContext context = new BookshelfContext()) { context.Update(genereToModify); context.SaveChanges(); } }
/// <summary> /// Usuwa autora z danej książki. /// </summary> /// <param name="book">Książka, z której ma zostać usunięty dany autor</param> /// <param name="author">Autor, który ma zostać usunięty z danej książki</param> public void RemoveAuthorFromBook(Book book, Author author) { if (book == null || author == null) { throw new ArgumentNullException(); } using (BookshelfContext context = new BookshelfContext()) { context.Remove(new BookAuthor() { BookId = book.BookId, AuthorId = author.AuthorId }); context.SaveChanges(); } }
/// <summary> /// Usuwa istniejącą w bazie książkę. /// </summary> /// <param name="bookToRemove">Książka, która ma zostać usunieta</param> public void RemoveBook(Book bookToRemove) { if (bookToRemove == null) { throw new ArgumentNullException(); } foreach (Author bookAuthor in GetBookAuthors(bookToRemove)) { RemoveAuthorFromBook(bookToRemove, bookAuthor); } using (BookshelfContext context = new BookshelfContext()) { context.Remove(bookToRemove); context.SaveChanges(); } }
/// <summary> /// Dodaje nowy gatunek do bazy danych. /// </summary> /// <param name="name">Nazwa nowego gatunku</param> public void AddGenere(string name) { name = name.Trim(); if (name == null || name == "") { throw new ArgumentNullException(); } using (BookshelfContext context = new BookshelfContext()) { context.Add(new Genere() { GenereName = name }); context.SaveChanges(); } }
/// <summary> /// Modyfikuje dane instniejącego w bazie autora. /// </summary> /// <param name="authorToModify">Autor, który ma zostać zmodyfikowany</param> /// <param name="firstName">Nowe imię autora</param> /// <param name="lastName">Nowe nazwisko autora</param> public void ModifyAuthor(Author authorToModify, string firstName, string lastName) { firstName = firstName.Trim(); lastName = lastName.Trim(); if (authorToModify == null || firstName == "" || firstName == null || lastName == "" || lastName == null) { throw new ArgumentNullException(); } authorToModify.FirstName = firstName; authorToModify.LastName = lastName; using (BookshelfContext context = new BookshelfContext()) { context.Update(authorToModify); context.SaveChanges(); } }
/// <summary> /// Modyfikuje istniejącą w bazie książkę. /// </summary> /// <param name="bookToModify">Książka, która ma zostać zmodyfikowana</param> /// <param name="title">Nowy tytuł</param> /// <param name="purchaseDate">Nowa data zakupu</param> /// <param name="genere">Nowy gatunek</param> /// <param name="shelf">Nowa półka</param> public void ModifyBook(Book bookToModify, string title, DateTime purchaseDate, Genere genere, Shelf shelf, List <Author> authors) { title = title.Trim(); if (title == null || title == "" || purchaseDate == null || genere == null || shelf == null || authors == null || authors.Count == 0) { throw new ArgumentNullException(); } if (purchaseDate > DateTime.Now) { throw new ArgumentOutOfRangeException(); } bookToModify.Title = title; bookToModify.PurchaseDate = purchaseDate; bookToModify.Genere = genere; bookToModify.Shelf = shelf; using (BookshelfContext context = new BookshelfContext()) { context.Update(bookToModify); context.SaveChanges(); } List <Author> currentBookAuthors = GetBookAuthors(bookToModify); foreach (Author authorToRemove in currentBookAuthors) { if (!authors.Contains(authorToRemove)) { RemoveAuthorFromBook(bookToModify, authorToRemove); } } foreach (Author authorToAdd in authors) { if (!currentBookAuthors.Contains(authorToAdd)) { AddBookAuthor(bookToModify, authorToAdd); } } }
/// <summary> /// Dodaje nowego autora do bazy danych. /// </summary> /// <param name="firstName">Imię autora</param> /// <param name="lastName">Nazwisko autora</param> public void AddAuthor(string firstName, string lastName) { firstName = firstName.Trim(); lastName = lastName.Trim(); if (firstName == "" || firstName == null || lastName == "" || lastName == null) { throw new ArgumentNullException(); } using (BookshelfContext context = new BookshelfContext()) { context.Authors.Add(new Author() { FirstName = firstName, LastName = lastName }); context.SaveChanges(); } }
public int Create(Tag item) { db.Tags.Add(item); db.SaveChanges(); return(item.Id); }
public void Save() { db.SaveChanges(); }
public int Create(Author item) { db.Authors.Add(item); db.SaveChanges(); return(item.Id); }