using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Linq; using System.Net; using System.Web; using System.Web.Mvc; using Book_club.Models; using Microsoft.AspNet.Identity; namespace Book_club.Controllers { [Authorize] public class BooksController : Controller { private ApplicationDbContext db = new ApplicationDbContext(); // GET: Books public ActionResult Index() { var currentUserId = User.Identity.GetUserId(); return View(db.Books.Where(x=>x.UserId == currentUserId). ToList()); } // GET: Books/Details/5 public ActionResult Details(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Book book = db.Books.Find(id); if (book == null) { return HttpNotFound(); } return View(book); } // GET: Books/Create public ActionResult Create() { return View(); } // POST: Books/Create // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "Id,Name,YearProducktion,AgeLimit,IsChildBook,Genre,UserId")] Book book) { var currentUserId = User.Identity.GetUserId(); if (ModelState.IsValid) { book.UserId = currentUserId; db.Books.Add(book); db.SaveChanges(); return RedirectToAction("Index"); } return View(book); } // GET: Books/Edit/5 public ActionResult Edit(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Book book = db.Books.Find(id); if (book == null) { return HttpNotFound(); } return View(book); } // POST: Books/Edit/5 // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit([Bind(Include = "Id,Name,YearProducktion,AgeLimit,IsChildBook,Genre,UserId")] Book book) { if (ModelState.IsValid) { var currentUserId = User.Identity.GetUserId(); book.UserId = currentUserId; db.Entry(book).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(book); } // GET: Books/Delete/5 public ActionResult Delete(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Book book = db.Books.Find(id); if (book == null) { return HttpNotFound(); } return View(book); } // POST: Books/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public ActionResult DeleteConfirmed(int id) { Book book = db.Books.Find(id); db.Books.Remove(book); db.SaveChanges(); return RedirectToAction("Index"); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } } }