public ActionResult Create([Bind(Include = "Name")] Publisher publisher) { if (string.IsNullOrWhiteSpace(publisher.Name)) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } try { using (var db = new BookStoreDb()) { var existing = db.Publishers.All().FirstOrDefault(p => p.Name == publisher.Name); if (existing == null) { publisher = db.Publishers.Create(publisher); } } return(RedirectToAction("Index")); } catch { return(View()); } }
public ActionResult DeleteConfirmed(int id) { try { Publisher publisher = null; using (var db = new BookStoreDb()) { publisher = db.Publishers.Find(id); if (publisher == null) { return(HttpNotFound()); } else { db.Publishers.Delete(id); } } return(RedirectToAction("Index")); } catch { return(View()); } }
public ActionResult Edit([Bind(Include = "Id,Name")] Publisher publisher) { try { var success = false; using (var db = new BookStoreDb()) { success = db.Publishers.Update(publisher); } if (success) { return(RedirectToAction("Index")); } else { ModelState.AddModelError("publisher", "The publisher update failed"); return(View(publisher)); } } catch { return(View(publisher)); } }
public ActionResult Create() { var publishers = Enumerable.Empty <Publisher>(); using (var db = new BookStoreDb()) { publishers = db.Publishers.All(); } ViewBag.PublisherId = new SelectList(publishers, "Id", "Name"); return(View()); }
// GET: Publisher/Details/5 public ActionResult Details(int id) { Publisher publisher = null; IEnumerable <Book> books = null; using (var db = new BookStoreDb()) { publisher = db.Publishers.Find(id); books = db.GetPublisherBooks(publisher); } var model = new PublisherBooks(publisher, books); return(View(model)); }
public ActionResult Edit(int id) { var publishers = Enumerable.Empty <Publisher>(); Book book = null; using (var db = new BookStoreDb()) { publishers = db.Publishers.All(); book = db.Books.Find(id); } ViewBag.PublisherId = new SelectList(publishers, "Id", "Name"); return(View(book)); }
public ActionResult Create([Bind(Include = "Title,Author,PublicationYear")] Book book, int publisherId, HttpPostedFileBase CoverPhoto) { try { if (CoverPhoto != null) { var imageFileName = Path.GetFileName(CoverPhoto.FileName); var imageContentType = CoverPhoto.ContentType; var folderPath = Server.MapPath("~/Content/Photos"); var uploaded = _storageHelper.UploadImage(imageFileName, imageContentType, CoverPhoto.InputStream, folderPath); if (uploaded.Success) { book.CoverPhoto = uploaded.Result; } } else { book.CoverPhoto = "noimage.png"; // alternatively don't allow saving if a photo is not uploaded //ModelState.AddModelError("CoverPhoto", "No cover image uploaded"); //return View(book); } using (var db = new BookStoreDb()) { var publisher = db.Publishers.Find(publisherId); book.Publisher = publisher; db.Books.Create(book); } var hubContext = GlobalHost.ConnectionManager.GetHubContext <NotificationHub>(); hubContext?.Clients?.All?.onBookAdded(book); return(RedirectToAction("Index")); } catch (Exception e) { ModelState.AddModelError("create", e); using (var db = new BookStoreDb()) { ViewBag.PublisherId = new SelectList(db.Publishers.All(), "Id", "Name"); } return(View(book)); } }
public ActionResult DeleteConfirmed(int id) { try { using (var db = new BookStoreDb()) { db.Books.Delete(id); } return(RedirectToAction("Index")); } catch { return(View()); } }
public ActionResult Delete(int id) { Book book = null; using (var db = new BookStoreDb()) { book = db.Books.Find(id); } if (book == null) { return(HttpNotFound("Book not found")); } return(View(book)); }
public ActionResult Login(Account account) { User user = null; bool verified = false; using (var db = new BookStoreDb()) { user = db.Users.All().FirstOrDefault(u => u.Username == account.Username); if (user != null && db.CheckUserCredentials(account.Username, account.Password)) { verified = true; } } if (!verified) { ModelState.AddModelError("Username", "Invalid username or password"); return(RedirectToAction("Index")); } var claims = new List <Claim>(new[] { // adding following 2 claim just for supporting default antiforgery provider new Claim(ClaimTypes.NameIdentifier, user.Username), new Claim( "http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity", "http://www.w3.org/2001/XMLSchema#string"), new Claim(ClaimTypes.Name, user.Username), new Claim(ClaimTypes.UserData, user.Id.ToString()) }); foreach (var role in user.Roles) { claims.Add(new Claim(ClaimTypes.Role, role.Name)); } var identity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie); HttpContext.GetOwinContext().Authentication.SignIn( new AuthenticationProperties { IsPersistent = false }, identity); return(RedirectToAction("Index", "Home")); }
// GET: Books public ActionResult Index() { OperationResult <IEnumerable <Book> > result = null; using (var db = new BookStoreDb()) { var app = new BookStoreApp(db); result = app.GetBooks(); } if (!result.Success) { // display the error message if you wish return(View("Error")); } return(View(result.Result)); }
// GET: Publisher/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Publisher publisher = null; using (var db = new BookStoreDb()) { publisher = db.Publishers.Find(id.Value); } if (publisher == null) { return(new HttpStatusCodeResult(HttpStatusCode.NotFound)); } return(View(publisher)); }
public BooksRepository(BookStoreDb context) { this.context = context; }
public UnitOfWork(BookStoreDb context) { Context = context; }