public ActionResult CreateBook(AddBookViewModel form) { if (ModelState.IsValid) { var book = Books.AddBook(form); EditPageViewModel model = new EditPageViewModel(); model.BookID = book.ID; model.PageNumber = 1; return View("EditPage", model); } else { return View(form); } }
public static Book AddBook(AddBookViewModel form) { try { var context = new ClanographyContext(); var book = new Book(); book.Name = form.BookName; book.Description = form.Description; book.OwnerEmail = HttpContext.Current.User.Identity.Name; book.CreationDate = DateTime.Now; book.ModifiedDate = DateTime.Now; context.Books.Add(book); context.SaveChanges(); BookPermission bp = new BookPermission(); //Add Self Permission bp.BookID = book.ID; bp.EmailAddress = HttpContext.Current.User.Identity.Name; bp.PermissionFlag = 1; context.BookPermissions.Add(bp); context.SaveChanges(); //Add Permissions For Others if (!string.IsNullOrWhiteSpace(form.PeopleWithAccess)) { string[] emails = form.PeopleWithAccess.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); foreach (var email in emails) { if (Helpers.Common.IsValidEmail(email)) { //Sends An Email bp = new BookPermission(); bp.BookID = book.ID; bp.EmailAddress = email; bp.PermissionFlag = 1; context.BookPermissions.Add(bp); } } context.SaveChanges(); } return book; } catch (Exception) { return null; } }