Advertisement
Guest User

Untitled

a guest
Feb 27th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.74 KB | None | 0 0
  1. This is the user controller
  2.  
  3. using ClassicCarAuction;
  4. using ClassicCarAuction.Models;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel.DataAnnotations;
  8. using System.Linq;
  9. using System.Web;
  10. using System.Web.Mvc;
  11. using System.Web.Security;
  12.  
  13. namespace ClassicCarAuction.Controllers
  14. {
  15. public class UserController : Controller
  16. {
  17. // Return Home page.
  18. public ActionResult Index()
  19. {
  20. return View();
  21. }
  22.  
  23. //Return Register view
  24. public ActionResult Register()
  25. {
  26. return View();
  27. }
  28.  
  29. //The form's data in Register view is posted to this method.
  30. //We have binded the Register View with Register ViewModel, so we can accept object of Register class as parameter.
  31. //This object contains all the values entered in the form by the user.
  32. [HttpPost]
  33. public ActionResult SaveRegisterDetails(User registerDetails)
  34. {
  35. //We check if the model state is valid or not. We have used DataAnnotation attributes.
  36. //If any form value fails the DataAnnotation validation the model state becomes invalid.
  37. if (ModelState.IsValid)
  38. {
  39. try
  40. {
  41. //create database context using Entity framework
  42. using (var databaseContext = new ClassicCarAuctionContext())
  43. {
  44. //If the model state is valid i.e. the form values passed the validation then we are storing the User's details in DB.
  45. User reglog = new User();
  46.  
  47. //Save all details in RegitserUser object
  48.  
  49. reglog.FirstName = registerDetails.FirstName;
  50. reglog.LastName = registerDetails.LastName;
  51. reglog.Password = registerDetails.Password;
  52.  
  53. var isValidEmail = IsValidEmail(registerDetails);
  54. if (isValidEmail != null)
  55. {
  56. ModelState.AddModelError("Failure", "This Email address has already been used !");
  57.  
  58. return View("Register");
  59. }
  60. else
  61. {
  62. reglog.Email = registerDetails.Email;
  63.  
  64. }
  65.  
  66. //Calling the SaveDetails method which saves the details.
  67. databaseContext.Users.Add(reglog);
  68. databaseContext.SaveChanges();
  69. }
  70.  
  71. return View("ThankYou");
  72. }
  73.  
  74. catch
  75. {
  76. //If the validation fails, we are returning the model object with errors to the view, which will display the error messages.
  77. ModelState.AddModelError("Failure", "All the information is required !");
  78. return View("Register");
  79. }
  80.  
  81. }
  82. return View("Register");
  83. }
  84.  
  85.  
  86. public ActionResult Login()
  87. {
  88. return View();
  89. }
  90.  
  91. //The login form is posted to this method.
  92. [HttpPost]
  93. public ActionResult Login(User model)
  94. {
  95. //Checking the state of model passed as parameter.
  96. if (ModelState.IsValid)
  97. {
  98.  
  99. //Validating the user, whether the user is valid or not.
  100. var isValidUser = IsValidUser(model);
  101.  
  102. //If user is valid & present in database, we are redirecting it to Welcome page.
  103. if (isValidUser != null)
  104. {
  105. FormsAuthentication.SetAuthCookie(model.Email, false);
  106. return RedirectToAction("Index");
  107. }
  108. else
  109. {
  110. //If the username and password combination is not present in DB then error message is shown.
  111. ModelState.AddModelError("Failure", "Wrong Username and password combination !");
  112. return View();
  113. }
  114. }
  115. else
  116. {
  117. //If model state is not valid, the model with error message is returned to the View.
  118. return View(model);
  119. }
  120. }
  121.  
  122. //function to check if User is valid or not
  123. public User IsValidUser(User model)
  124. {
  125. using (var dataContext = new ClassicCarAuctionContext())
  126. {
  127. //Retrieving the user details from DB based on email and password enetered by user.
  128. User user = dataContext.Users.Where(query => query.Email.Equals(model.Email) && query.Password.Equals(model.Password)).SingleOrDefault();
  129. //If user is not present false is returned.
  130. if (user == null)
  131. return null;
  132. //If user is present, then true is returned.
  133. else
  134. {
  135. return user;
  136. }
  137. }
  138. }
  139.  
  140.  
  141. public User IsValidEmail(User model)
  142. {
  143. using (var dataContext = new ClassicCarAuctionContext())
  144. {
  145.  
  146. //Retrieving the user details from DB based on Email entered by user.
  147. User user = dataContext.Users.Where(query => query.Email.Equals(model.Email)).SingleOrDefault();
  148. //If user is present, then true is returned.
  149. if (user == null)
  150. return null;
  151. //If user is not present false is returned.
  152. else
  153. return user;
  154. }
  155. }
  156.  
  157. public ActionResult Logout()
  158. {
  159. FormsAuthentication.SignOut();
  160. Session.Abandon(); // it will clear the session at the end of request
  161. return RedirectToAction("../Home/Index");
  162. }
  163. }
  164. }
  165.  
  166. using System;
  167. using System.Collections.Generic;
  168. using System.Data.Entity;
  169. using System.Linq;
  170. using System.Net;
  171. using System.Web;
  172. using System.Web.Mvc;
  173. using ClassicCarAuction;
  174. using ClassicCarAuction.Models;
  175. using ClassicCarAuction.Controllers;
  176.  
  177. namespace ClassicCarAuction.Controllers
  178. {
  179. public class CarAuctionController : Controller
  180. {
  181. // GET: CarAuction
  182. ClassicCarAuctionContext db = new ClassicCarAuctionContext();
  183. public ActionResult Index()
  184. {
  185. foreach (var item in db.CarLists)
  186. {
  187. DateTime ReturnDate = System.DateTime.Now;
  188. if (ReturnDate < item.AuctionEndDate)
  189. {
  190. if (item.HighestBid < item.ReserveBid)
  191. item.Status = "Reserve has not been met";
  192. else item.Status = "Reserve has been met";
  193.  
  194. }
  195. else
  196. {
  197. if (item.HighestBid < item.ReserveBid)
  198. item.Status = "Auction has ended. Reserve has not been met";
  199. else item.Status = "Auction has ended. Reserve has been met";
  200.  
  201. }
  202.  
  203. }
  204. db.SaveChanges();
  205. return View(db.CarLists.ToList());
  206.  
  207. }
  208.  
  209. // GET: CarAuction/Details/5
  210. public ActionResult Details(int id)
  211. {
  212. return View();
  213. }
  214.  
  215. // GET: CarAuction/Create
  216. [HttpGet]
  217. public ActionResult Create()
  218. {
  219. return View();
  220. }
  221. // POST: CarAuction/Create
  222. [HttpPost]
  223. public ActionResult Create(CarList carlist)
  224. {
  225. try
  226. {
  227. // TODO: Add insert logic here
  228. if (ModelState.IsValid)
  229. {
  230. db.CarLists.Add(carlist);
  231.  
  232. if (carlist.HighestBid < carlist.ReserveBid)
  233. carlist.Status = "Reserve has not been met";
  234. else carlist.Status = "Reserve has been met";
  235. db.SaveChanges();
  236. return RedirectToAction("Index");
  237. }
  238. return View();
  239. }
  240. catch
  241. {
  242. return View();
  243. }
  244. }
  245.  
  246. // GET: CarAuction/Edit/5
  247. public ActionResult Edit(int id)
  248. {
  249. CarList carList = db.CarLists.Find(id);
  250. carList.CheckifMore = carList.HighestBid;
  251. db.SaveChanges();
  252. return View(carList);
  253. }
  254.  
  255. // POST: CarAuction/Edit/5
  256. [HttpPost]
  257. public ActionResult Edit(CarList carlist)
  258. {
  259.  
  260. try
  261. {
  262. // TODO: Add update logic here
  263.  
  264. if ((carlist.Status == "Reserve has not been met") || (carlist.Status == "Reserve has been met"))
  265. {
  266. if (carlist.HighestBid > carlist.CheckifMore)
  267. {
  268. if (ModelState.IsValid)
  269. {
  270. db.Entry(carlist).State = EntityState.Modified;
  271. if (carlist.HighestBid >= carlist.ReserveBid)
  272. {
  273. carlist.Status = "Reserve has been met";
  274. }
  275. db.SaveChanges();
  276. return RedirectToAction("Index");
  277. }
  278. }
  279. else
  280. {
  281. carlist.HighestBid = carlist.CheckifMore;
  282. db.SaveChanges();
  283. return RedirectToAction("Index");
  284. }
  285. return View("Index");
  286. }
  287. return RedirectToAction("Index");
  288. }
  289. catch
  290. {
  291. return View("Index");
  292. }
  293. }
  294.  
  295. // GET: CarAuction/Delete/5
  296. public ActionResult Delete(int id)
  297. {
  298. return View();
  299. }
  300.  
  301. // POST: CarAuction/Delete/5
  302. [HttpPost]
  303. public ActionResult Delete(int id, FormCollection collection)
  304. {
  305. try
  306. {
  307. // TODO: Add delete logic here
  308.  
  309. return RedirectToAction("Index");
  310. }
  311. catch
  312. {
  313. return View();
  314. }
  315. }
  316. }
  317. }
  318.  
  319. @model ClassicCarAuction.Models.User
  320. @{
  321. ViewBag.Title = "Index";
  322. }
  323.  
  324. <h2>Index</h2>
  325.  
  326. @if (Request.IsAuthenticated)
  327. {
  328. <h2>
  329. <p>Thank you for logging in @Html.Encode(User.Identity.Name). Now you can create and bid on listings.</p>
  330.  
  331.  
  332. @Html.ActionLink("Listings", "Index", "CarAuction")<br />
  333. </h2>
  334. }
  335. else
  336. {
  337. <h2>
  338. <p>
  339. If you have already registered, Please login to create or bid on listings.<br />
  340. @Html.ActionLink("Login", "Login", "User")<br />
  341. </p>
  342. <p>
  343. If you have not registered yet, you will need to do so before you can log in.<br />
  344. @Html.ActionLink("Register", "Register", "User")
  345. </p>
  346.  
  347. </h2>
  348. }
  349.  
  350. @model ClassicCarAuction.Models.User
  351.  
  352. @{
  353.  
  354. ViewBag.Title = "Login";
  355.  
  356. }
  357.  
  358. <h2>Login</h2>
  359.  
  360. <div>
  361. @using (Html.BeginForm("Login", "User"))
  362. {
  363. <div style="color:red;">@Html.ValidationSummary()</div>
  364. <div class="row">
  365. <!--Show details are saved successfully message-->
  366. <div class="col-lg-12">@ViewBag.Message</div>
  367. </div><br />
  368.  
  369. <div class="row">
  370. <div class="col-lg-2">@Html.LabelFor(a => a.Email)</div>
  371. <div class="col-lg-10">@Html.TextBoxFor(a => a.Email, new { @class = "form-control" })</div>
  372. </div><br />
  373. <div class="row">
  374. <div class="col-lg-2">@Html.LabelFor(a => a.Password)</div>
  375. <div class="col-lg-10">@Html.TextBoxFor(a => a.Password, new { @class = "form-control", type = "Password" })</div>
  376. </div><br />
  377. <input type="submit" value="Login" class="btn btn-primary" />
  378. }
  379.  
  380. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement