//Need books in Database to Run Search Function public ActionResult Search() { WordlessContext db = new WordlessContext(); string searchString = Request["searchString"]; var bookList = db.Books.ToList(); if (!String.IsNullOrEmpty(searchString)) { //checks if search text matches book title, description or author name irrespective of case var searchResult = bookList.Where(b => (b.Title.ToLower().Contains(searchString.ToLower())) || (b.BookText.ToLower().Contains(searchString.ToLower())) || (b.Author.Name.ToLower().Contains(searchString.ToLower()))).ToList(); //if match result is positive if (searchResult.Count() != 0) { //returns a view with search result return(View("Index", searchResult)); } } return(RedirectToAction("Index")); }
public ActionResult Test() { WordlessContext db = new WordlessContext(); db.Books.Count(); return(View()); }
public ActionResult BookDetails(int?bookId) { SetListsForViews(); WordlessContext db = new WordlessContext(); if ((bool)Session["loginStatus"]) { var purchasedbooks = db.PurchasedBooks.ToList(); var userId = (Int64)Session["currentUserId"]; var userPurchases = (from p in purchasedbooks where p.BuyerId == userId select p).ToList(); ViewBag.PurchaseList = userPurchases; } List <Book> bookList = new List <Book>(); if (bookId != 0) { bookList = (from b in db.Books where b.BookId == bookId select b).ToList(); } else { bookList = (from b in db.Books select b).ToList(); } return(View(bookList)); }
// GET: Book public ActionResult Index(string genre) { SetListsForViews(); WordlessContext db = new WordlessContext(); //kollar vilka böcker den aktuella användaren har köpt if ((bool)Session["loginStatus"]) { var purchasedbooks = db.PurchasedBooks.ToList(); var userId = (Int64)Session["currentUserId"]; var userPurchases = (from p in purchasedbooks where p.BuyerId == userId select p).ToList(); ViewBag.PurchaseList = userPurchases; } //if a genre was provided, return a list of books of the genre List <Book> bookList = db.Books.Include(b => b.Author).Include(c => c.Comments).ToList(); if (genre != null) { var categorySort = from c in bookList where c.Genre.ToString() == genre select c; return(View(categorySort)); } else { return(View(bookList)); } }
public ActionResult BookByGenre(int id) { SetListsForViews(); WordlessContext db = new WordlessContext(); var booklist = db.Books.Include(b => b.Author).Include(c => c.Comments).Where(b => b.Genre == (Genres)id).ToList(); return(View("Index", booklist)); }
public ActionResult FacebookCallback(string code) { var fb = new FacebookClient(); dynamic result = fb.Post("oauth/access_token", new { client_id = "1774392159446483", client_secret = "8f9a13caa407845dc68c0b1bf585ece6", redirect_uri = RedirectUri.AbsoluteUri, code = code }); var accessToken = result.access_token; // TODO: Authenticate User // Store the access token in the session Session["AccessToken"] = accessToken; Session["loginStatus"] = true; // update the facebook client with the access token so // we can make requests on behalf of the user fb.AccessToken = accessToken; // Get the user's information dynamic me = fb.Get("me?fields=first_name,last_name,id,email"); string email = me.email; string currentUsername = me.first_name; Session["currentUsername"] = currentUsername; string currentUserLastName = me.last_name; Session["currentUserLastName"] = currentUserLastName; string currentUserId = me.id; Session["currentUserId"] = Int64.Parse(currentUserId); //adds a new user on fb login in database User fbUser = new User { Username = currentUsername, Email = email, Funds = 0, Name = (currentUsername + " " + currentUserLastName) }; WordlessContext context = new WordlessContext(); if (context.Users.Where(e => e.Email == email).Count() == 0) { context.Users.Add(fbUser); context.SaveChanges(); } // Set the auth cookie FormsAuthentication.SetAuthCookie(email, false); return(RedirectToAction("Index", "Home")); }
public ActionResult BooksAPI(string searchstring) { WordlessContext context = new WordlessContext(); var booklist1 = context.Books.Where(b => b.Title.ToLower().Contains(searchstring.ToLower())); var booklist2 = context.Books.Where(b => b.Genre.ToString().ToLower().Contains(searchstring.ToLower())); var booklist3 = context.Books.Where(b => b.BookText.ToLower().Contains(searchstring.ToLower())); var booklist4 = context.Books.Where(b => b.Author.Name.ToLower().Contains(searchstring.ToLower())); var FinalBookList = booklist1.ToList(); foreach (var b in booklist2) { if (!FinalBookList.Any(v => v == b)) { FinalBookList.Add(b); } } foreach (var b in booklist3) { if (!FinalBookList.Any(v => v == b)) { FinalBookList.Add(b); } } foreach (var b in booklist4) { if (!FinalBookList.Any(v => v == b)) { FinalBookList.Add(b); } } if (searchstring == "") { FinalBookList = context.Books.ToList(); } string htmlstring = ""; foreach (Book b in FinalBookList) { htmlstring = htmlstring + "<div class='bodyDiv col-md-5 col-lg-5'>" + "<p class='bookTitle'>" + b.Title + "<button class='pull-right btn btn-default bookButton' type='button' onclick='location.href = @'/Book/BookDetails?bookId=" + b.BookId + "''><strong>Details</strong></button>" + "<p class='bookAuthor'>Written by: " + b.Author.Name + "</p>" + "<p class='bookAuthor'>Genre: " + b.Genre + "</p>" + "<p class='booktText'>Price: " + b.Price + "</p>" + "<p class='booktText'>" + b.BookText + " </p>" + "</div>"; } //'@Url.Action('BookDetails', 'Book', new { bookId = " + b.BookId + " })' return(Json(new { HtmlString = htmlstring }, JsonRequestBehavior.AllowGet)); }
//sparar kommentaren i databasen public ActionResult SaveComment(string comment, int bookId) { //gör om \n till br istället så det blir html istället var bookText = comment.Replace("\r\n", "<br />"); var userId = (Int64)Session["currentUserId"]; Comment newComment; WordlessContext db = new WordlessContext(); // om man iunte är inlogggad och försöker kommentera if (!(bool)Session["loginStatus"] || comment == null) { List <Book> bookReturn = (from b in db.Books select b).ToList(); return(View("Book", bookReturn)); } if ((bool)Session["loginStatus"]) { // om inloggad var findBook = db.Books.Where(b => b.BookId == bookId).FirstOrDefault(); //skapa nytt kommentars-objekt newComment = new Comment() { //hämtar bokID BookId = findBook.BookId, CommentText = bookText, Date = DateTime.Now, UserId = userId }; db.Comments.Add(newComment); db.SaveChanges(); } db = new WordlessContext(); var purchasedbooks = db.PurchasedBooks.ToList(); var userPurchases = (from p in purchasedbooks where p.BuyerId == userId select p).ToList(); ViewBag.PurchaseList = userPurchases; List <Book> book = (from b in db.Books where b.BookId == bookId select b).ToList(); return(View("BookDetails", book)); }
public ActionResult Register() { WordlessContext db = new WordlessContext(); SetListsForViews(); string name = Request["name"]; string username = Request["username"]; string password = Request["password"]; string email = Request["email"]; bool author = false; if (Request["isAuthor"] != null) { author = bool.Parse(Request["isAuthor"]); } if (name == null || username == null || password == null || email == null) { TempData["error"] = ""; return(View()); } if (db.Users.Where(n => n.Username.ToLower() == username.ToLower()).Count() == 0) { User user = new User { Name = name, Username = username, Password = password, Email = email, Author = author }; db.Users.Add(user); db.SaveChanges(); TempData["error"] = "Registered successfully"; return(View()); } else if (db.Users.Where(u => u.Username == username).Count() > 0 || db.Users.Where(e => e.Email == email).Count() > 0) { TempData["error"] = "User already exists"; return(View()); } else { return(View()); } }
public ActionResult UserHome() { if ((bool)Session["loginStatus"]) { ViewBag.Message = "Welcome " + Session["currentUsername"]; var userId = Convert.ToDouble(Session["currentUserId"]); WordlessContext db = new WordlessContext(); var userInfo = (db.Users .Include(b => b.WrittenBooks) .Include(c => c.Comments) .Include(p => p.PurchasedBooks) .Where(u => u.UserId == userId)).SingleOrDefault(); return(View(userInfo)); } else { return(Redirect("/user/Register")); } }
public ActionResult Login() { try { WordlessContext db = new WordlessContext(); string username = Request["username"]; string password = Request["password"]; var userList = db.Users.Where(u => u.Username.ToLower() == username.ToLower()).ToList(); if (userList.Count() == 1 && userList.First().Password == password) { ///Set session values Session["currentUserId"] = userList.First().UserId; Session["currentUsername"] = userList.First().Username; Session["loginStatus"] = true; if (username.ToLower() == "patrik") { Session["Admin"] = true; } else if (userList.Where(u => u.Username == username).First().Author) { Session["isAuthor"] = true; } TempData["error"] = "Welcome " + Session["currentUsername"]; return(Redirect("/Home/Index")); } else { TempData["error"] = "Check username and/or password"; return(RedirectToAction("Register")); // Redirect("/Default/Login"); } } catch { ///Return to login/register -view to try again TempData["error"] = "Database fail!"; return(View("Register")); // Redirect("/Default/Login"); } }
public void SetListsForViews() { WordlessContext db = new WordlessContext(); //sparar mest nedladdade i en lista ViewBag.MostDownloaded = (from d in db.Books orderby d.TimesPurchased descending select d).Take(4).ToList(); var listFromDb = db.PurchasedBooks.Include(u => u.Buyer).ToList(); List <PurchasedBook> purchasedList = new List <PurchasedBook>(); //räknar ut snittet på rating foreach (var item in listFromDb) { //hur många gånger en bok har blvit köpt var times = db.PurchasedBooks.Where(t => t.BookId == item.BookId).Count(); if (times > 1 && purchasedList.Any(f => f.BookId == item.BookId) == false) { //totala summan av alla ratings var sum = db.PurchasedBooks.Where(t => t.BookId == item.BookId).Sum(t => t.Rating); //summan delat på antalet nedladdingar(förutsatt att alla har gett en rating :) ) var avgRating = sum / times; item.Rating = avgRating; } if (purchasedList.Any(f => f.BookId == item.BookId) == false) { purchasedList.Add(item); } //sorterar listan var sortedList = (from x in purchasedList orderby x.Rating descending select x).Take(4).ToList(); ViewBag.BestRating = sortedList; //skapar en lista av antalet kommentarer ViewBag.MostCommented = (from b in db.Books orderby b.Comments.Count() descending select b).Take(4).ToList(); } }
public ActionResult RateBook(int bookId, int rating) { WordlessContext db = new WordlessContext(); var userId = (Int64)Session["currentUserId"]; var findPurchase = db.PurchasedBooks.Where(x => x.BookId == bookId && x.BuyerId == userId).First(); findPurchase.Rating = rating; var purchasedbooks = db.PurchasedBooks.ToList(); var userPurchases = (from p in purchasedbooks where p.BuyerId == userId select p).ToList(); ViewBag.PurchaseList = userPurchases; db.SaveChanges(); db = new WordlessContext(); var book = (from b in db.Books where b.BookId == bookId select b).ToList(); return(RedirectToAction("Index")); }
public ActionResult BuyBook(int bookId, bool?confirmed) { WordlessContext db = new WordlessContext(); //check if logged in if (!(bool)Session["loginStatus"] || bookId == 0) { return(RedirectToAction("Index")); } //if logged in and a bookId is provided if (bookId != 0 && (bool)Session["loginStatus"]) { //get userId var userId = (Int64)Session["currentUserId"]; //get book Book book = (from b in db.Books where b.BookId == bookId select b).First(); //get user User user = (from u in db.Users where u.UserId == userId select u).FirstOrDefault(); //if user confirmed if (confirmed != null && user != null) { if ((bool)confirmed) { //create new book-purchase var bookToBuy = new PurchasedBook { DateOfPurchase = DateTime.Now, BookId = book.BookId, BuyerId = userId, Rating = 0 }; //add a purchase db.PurchasedBooks.Add(bookToBuy); //give author cash book.Author.Funds += book.Price; //remove cash from user user.Funds -= book.Price; //incremenet TimesPurchase by 1 book.TimesPurchased++; //save changes db.SaveChanges(); return(RedirectToAction("Index")); } } List <Book> bookToBuyView = new List <Book>() { book }; return(View(bookToBuyView)); } else { var book = (from b in db.Books.Include(c => c.Comments) where b.BookId == bookId select b).ToList(); return(View("Index", book)); } }