Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- This is the user controller
- using ClassicCarAuction;
- using ClassicCarAuction.Models;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using System.Web.Security;
- namespace ClassicCarAuction.Controllers
- {
- public class UserController : Controller
- {
- // Return Home page.
- public ActionResult Index()
- {
- return View();
- }
- //Return Register view
- public ActionResult Register()
- {
- return View();
- }
- //The form's data in Register view is posted to this method.
- //We have binded the Register View with Register ViewModel, so we can accept object of Register class as parameter.
- //This object contains all the values entered in the form by the user.
- [HttpPost]
- public ActionResult SaveRegisterDetails(User registerDetails)
- {
- //We check if the model state is valid or not. We have used DataAnnotation attributes.
- //If any form value fails the DataAnnotation validation the model state becomes invalid.
- if (ModelState.IsValid)
- {
- try
- {
- //create database context using Entity framework
- using (var databaseContext = new ClassicCarAuctionContext())
- {
- //If the model state is valid i.e. the form values passed the validation then we are storing the User's details in DB.
- User reglog = new User();
- //Save all details in RegitserUser object
- reglog.FirstName = registerDetails.FirstName;
- reglog.LastName = registerDetails.LastName;
- reglog.Password = registerDetails.Password;
- var isValidEmail = IsValidEmail(registerDetails);
- if (isValidEmail != null)
- {
- ModelState.AddModelError("Failure", "This Email address has already been used !");
- return View("Register");
- }
- else
- {
- reglog.Email = registerDetails.Email;
- }
- //Calling the SaveDetails method which saves the details.
- databaseContext.Users.Add(reglog);
- databaseContext.SaveChanges();
- }
- return View("ThankYou");
- }
- catch
- {
- //If the validation fails, we are returning the model object with errors to the view, which will display the error messages.
- ModelState.AddModelError("Failure", "All the information is required !");
- return View("Register");
- }
- }
- return View("Register");
- }
- public ActionResult Login()
- {
- return View();
- }
- //The login form is posted to this method.
- [HttpPost]
- public ActionResult Login(User model)
- {
- //Checking the state of model passed as parameter.
- if (ModelState.IsValid)
- {
- //Validating the user, whether the user is valid or not.
- var isValidUser = IsValidUser(model);
- //If user is valid & present in database, we are redirecting it to Welcome page.
- if (isValidUser != null)
- {
- FormsAuthentication.SetAuthCookie(model.Email, false);
- return RedirectToAction("Index");
- }
- else
- {
- //If the username and password combination is not present in DB then error message is shown.
- ModelState.AddModelError("Failure", "Wrong Username and password combination !");
- return View();
- }
- }
- else
- {
- //If model state is not valid, the model with error message is returned to the View.
- return View(model);
- }
- }
- //function to check if User is valid or not
- public User IsValidUser(User model)
- {
- using (var dataContext = new ClassicCarAuctionContext())
- {
- //Retrieving the user details from DB based on email and password enetered by user.
- User user = dataContext.Users.Where(query => query.Email.Equals(model.Email) && query.Password.Equals(model.Password)).SingleOrDefault();
- //If user is not present false is returned.
- if (user == null)
- return null;
- //If user is present, then true is returned.
- else
- {
- return user;
- }
- }
- }
- public User IsValidEmail(User model)
- {
- using (var dataContext = new ClassicCarAuctionContext())
- {
- //Retrieving the user details from DB based on Email entered by user.
- User user = dataContext.Users.Where(query => query.Email.Equals(model.Email)).SingleOrDefault();
- //If user is present, then true is returned.
- if (user == null)
- return null;
- //If user is not present false is returned.
- else
- return user;
- }
- }
- public ActionResult Logout()
- {
- FormsAuthentication.SignOut();
- Session.Abandon(); // it will clear the session at the end of request
- return RedirectToAction("../Home/Index");
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Data.Entity;
- using System.Linq;
- using System.Net;
- using System.Web;
- using System.Web.Mvc;
- using ClassicCarAuction;
- using ClassicCarAuction.Models;
- using ClassicCarAuction.Controllers;
- namespace ClassicCarAuction.Controllers
- {
- public class CarAuctionController : Controller
- {
- // GET: CarAuction
- ClassicCarAuctionContext db = new ClassicCarAuctionContext();
- public ActionResult Index()
- {
- foreach (var item in db.CarLists)
- {
- DateTime ReturnDate = System.DateTime.Now;
- if (ReturnDate < item.AuctionEndDate)
- {
- if (item.HighestBid < item.ReserveBid)
- item.Status = "Reserve has not been met";
- else item.Status = "Reserve has been met";
- }
- else
- {
- if (item.HighestBid < item.ReserveBid)
- item.Status = "Auction has ended. Reserve has not been met";
- else item.Status = "Auction has ended. Reserve has been met";
- }
- }
- db.SaveChanges();
- return View(db.CarLists.ToList());
- }
- // GET: CarAuction/Details/5
- public ActionResult Details(int id)
- {
- return View();
- }
- // GET: CarAuction/Create
- [HttpGet]
- public ActionResult Create()
- {
- return View();
- }
- // POST: CarAuction/Create
- [HttpPost]
- public ActionResult Create(CarList carlist)
- {
- try
- {
- // TODO: Add insert logic here
- if (ModelState.IsValid)
- {
- db.CarLists.Add(carlist);
- if (carlist.HighestBid < carlist.ReserveBid)
- carlist.Status = "Reserve has not been met";
- else carlist.Status = "Reserve has been met";
- db.SaveChanges();
- return RedirectToAction("Index");
- }
- return View();
- }
- catch
- {
- return View();
- }
- }
- // GET: CarAuction/Edit/5
- public ActionResult Edit(int id)
- {
- CarList carList = db.CarLists.Find(id);
- carList.CheckifMore = carList.HighestBid;
- db.SaveChanges();
- return View(carList);
- }
- // POST: CarAuction/Edit/5
- [HttpPost]
- public ActionResult Edit(CarList carlist)
- {
- try
- {
- // TODO: Add update logic here
- if ((carlist.Status == "Reserve has not been met") || (carlist.Status == "Reserve has been met"))
- {
- if (carlist.HighestBid > carlist.CheckifMore)
- {
- if (ModelState.IsValid)
- {
- db.Entry(carlist).State = EntityState.Modified;
- if (carlist.HighestBid >= carlist.ReserveBid)
- {
- carlist.Status = "Reserve has been met";
- }
- db.SaveChanges();
- return RedirectToAction("Index");
- }
- }
- else
- {
- carlist.HighestBid = carlist.CheckifMore;
- db.SaveChanges();
- return RedirectToAction("Index");
- }
- return View("Index");
- }
- return RedirectToAction("Index");
- }
- catch
- {
- return View("Index");
- }
- }
- // GET: CarAuction/Delete/5
- public ActionResult Delete(int id)
- {
- return View();
- }
- // POST: CarAuction/Delete/5
- [HttpPost]
- public ActionResult Delete(int id, FormCollection collection)
- {
- try
- {
- // TODO: Add delete logic here
- return RedirectToAction("Index");
- }
- catch
- {
- return View();
- }
- }
- }
- }
- @model ClassicCarAuction.Models.User
- @{
- ViewBag.Title = "Index";
- }
- <h2>Index</h2>
- @if (Request.IsAuthenticated)
- {
- <h2>
- <p>Thank you for logging in @Html.Encode(User.Identity.Name). Now you can create and bid on listings.</p>
- @Html.ActionLink("Listings", "Index", "CarAuction")<br />
- </h2>
- }
- else
- {
- <h2>
- <p>
- If you have already registered, Please login to create or bid on listings.<br />
- @Html.ActionLink("Login", "Login", "User")<br />
- </p>
- <p>
- If you have not registered yet, you will need to do so before you can log in.<br />
- @Html.ActionLink("Register", "Register", "User")
- </p>
- </h2>
- }
- @model ClassicCarAuction.Models.User
- @{
- ViewBag.Title = "Login";
- }
- <h2>Login</h2>
- <div>
- @using (Html.BeginForm("Login", "User"))
- {
- <div style="color:red;">@Html.ValidationSummary()</div>
- <div class="row">
- <!--Show details are saved successfully message-->
- <div class="col-lg-12">@ViewBag.Message</div>
- </div><br />
- <div class="row">
- <div class="col-lg-2">@Html.LabelFor(a => a.Email)</div>
- <div class="col-lg-10">@Html.TextBoxFor(a => a.Email, new { @class = "form-control" })</div>
- </div><br />
- <div class="row">
- <div class="col-lg-2">@Html.LabelFor(a => a.Password)</div>
- <div class="col-lg-10">@Html.TextBoxFor(a => a.Password, new { @class = "form-control", type = "Password" })</div>
- </div><br />
- <input type="submit" value="Login" class="btn btn-primary" />
- }
- </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement