public ActionResult Create([Bind(Include = "Id,Name,Description")] Event @event) { if (ModelState.IsValid) { db.Events.Add(@event); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(@event)); }
public ActionResult Create([Bind(Include = "Id,Name,Surname")] Author author) { if (ModelState.IsValid) { db.Authors.Add(author); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(author)); }
public ObjectResult Post([FromBody] Author author) { _logger.LogInformation($"Add author: {author.Name}"); string message; ObjectResult result; try { var Book = new Author { Name = author.Name }; if (!database.Authors.Where(x => x.Name == author.Name).Any()) { database.Authors.Add(Book); } database.SaveChanges(); message = "Succesful adding"; result = Ok(message); _logger.LogInformation(message); } catch { message = "Problem with database while adding"; result = StatusCode(500, message); _logger.LogError(message); } return(result); }
public ActionResult Create(Author author) { if (ModelState.IsValid) { db.Authors.Add(author); foreach (Book book in author.Books) { if (book.Id == -1) { db.Books.Add(book); } } db.SaveChanges(); return(RedirectToAction("Index")); } return(View(author)); }
public ActionResult Edit(Author author, int[] writtenEvents) { Author newAuthor = db.Authors.Find(author.Id); newAuthor.Name = author.Name; newAuthor.Surname = author.Surname; newAuthor.Events.Clear(); if (writtenEvents != null) { //получаем написанные события foreach (var c in db.Events.Where(co => writtenEvents.Contains(co.Id))) { newAuthor.Events.Add(c); } } db.Entry(newAuthor).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); }
public IActionResult Add(int id) { try { _context.Authors.Add(new Author() { Id = id, FirstName = "Eugene", LastName = "Lahansky" }); _context.SaveChanges(); var added = _context.Authors.FirstOrDefault(x => x.Id == id); return(new JsonResult(string.Format("Inserted author: {0} {1}", added.FirstName, added.LastName))); } catch (Exception ex) { return(new JsonResult(string.Format("Error: {0}\r\n {1}", ex.Message, ex.InnerException))); } }
protected void Save_OnClick(object sender, EventArgs e) { var db = new AuthorsContext(); int newAuthorID; var authors = db.Authors.Select(s => s).ToList(); var author = authors.FirstOrDefault(p => p.FirstName == FirstName.Text && p.LastName == LastName.Text && p.MiddleName == MiddleName.Text); if (author != null) { newAuthorID = author.AuthorID; } else { author = new Author { FirstName = FirstName.Text, LastName = LastName.Text, MiddleName = MiddleName.Text, Birthday = new DateTime(1950, 5, 5) }; db.Authors.AddOrUpdate(author); db.SaveChanges(); newAuthorID = db.Authors.Select(s => s).First(p => p.FirstName == author.FirstName && p.LastName == author.LastName && p.MiddleName == author.MiddleName).AuthorID; } int i = 0; for (;;) { i++; var ctrlBookName = Page.Request.Form["BookName" + i]; var ctrlGenre = Page.Request.Form["Genre" + i]; int ctrlPages; int.TryParse((Page.Request.Form["TotalPages" + i]), out ctrlPages); if (ctrlBookName == null && ctrlGenre == null && ctrlPages == 0) { Response.Redirect("Default.aspx"); return; } int newBookID; var books = db.Books.Select(s => s).ToList(); var book = books.FirstOrDefault(p => p.Name == ctrlBookName && p.Genre == ctrlGenre && p.TotalPages == ctrlPages); if (book != null) { newBookID = book.BookID; } else { book = new Book { Name = ctrlBookName, Genre = ctrlGenre, TotalPages = ctrlPages }; db.Books.AddOrUpdate(book); db.SaveChanges(); newBookID = db.Books.Select(s => s) .First( p => p.Name == ctrlBookName && p.Genre == ctrlGenre && p.TotalPages == ctrlPages) .BookID; } var newAuthBooks = db.AuthorBooks.Select(s => s).ToList(); var newAuthBook = newAuthBooks.FirstOrDefault(p => p.AuthorID == newAuthorID && p.BookID == newBookID); if (newAuthBook == null) { newAuthBook = new AuthorBook { AuthorID = author.AuthorID, BookID = book.BookID }; db.AuthorBooks.AddOrUpdate(newAuthBook); db.SaveChanges(); } } }