gladyssann

UserController.cs(Controller)

Jan 18th, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using OT_lib.Interfaces;
  7. using OT_lib.BusinessLayer;
  8. using OT_lib.Entity;
  9. using OverTime.Models;
  10. using PagedList;
  11.  
  12.  
  13. namespace OverTime.Controllers
  14. {
  15. public class UserController : Controller {
  16.  
  17. [HttpGet]
  18. public ActionResult Pagination(int? page)
  19. {
  20. var dummyItems = Enumerable.Range(1, 150).Select(x => "Items" + x);
  21. var pager = new Pager(dummyItems.Count(), page);
  22.  
  23. var viewModel = new UserModel()
  24. {
  25. Items = dummyItems.Skip((pager.CurrentPage - 1) * pager.PageSize).Take(pager.PageSize),
  26. Pager = pager
  27. };
  28.  
  29. return View(viewModel);
  30. }
  31.  
  32. public ActionResult List(UserModel usermodel)
  33. {
  34. //UserBusinessLayer userService = new UserBusinessLayer("sqlconn");
  35.  
  36. IUserInterface userService = new UserBusinessLayer("sqlConn");
  37. List<UserEntity> list = userService.GetAllUsers();
  38. List<UserModel> listModel = new List<UserModel>();
  39.  
  40. foreach (var item in list)
  41. {
  42. listModel.Add(new UserModel()
  43. {
  44. email = item.email,
  45. password = item.password,
  46. username = item.username,
  47. usr_Id = item.usr_Id
  48.  
  49. });
  50. }
  51. return View(listModel);
  52. }
  53.  
  54. public ActionResult CreateUser()
  55. {
  56. return View();
  57. }
  58.  
  59. [HttpPost]
  60. public ActionResult CreateUser(UserModel userModel)
  61. {
  62. //UserBusinessLayer userService = new UserBusinessLayer("sqlConn");
  63.  
  64. IUserInterface userService = new UserBusinessLayer("sqlConn");
  65.  
  66. userService.CreateUser(new UserEntity()
  67. {
  68. username = userModel.username,
  69. email = userModel.email,
  70. password = userModel.password
  71. });
  72.  
  73. return RedirectToAction("List");
  74. }
  75.  
  76. public ActionResult EditUser(int id)
  77. {
  78. //UserBusinessLayer userService = new UserBusinessLayer("sqlconn");
  79.  
  80. IUserInterface userService = new UserBusinessLayer("sqlConn");
  81.  
  82. UserModel userModel = new UserModel();
  83.  
  84. try
  85. {
  86. UserEntity userEntity = userService.GetUserId(id);
  87.  
  88. userModel.usr_Id = userEntity.usr_Id;
  89. userModel.email = userEntity.email;
  90. userModel.username = userEntity.username;
  91. userModel.password = userEntity.password;
  92. }
  93. catch (Exception ex)
  94. {
  95. throw ex;
  96. }
  97.  
  98. return View(userModel);
  99. }
  100.  
  101. //Update user info
  102. [HttpPost]
  103. public ActionResult EditUser(UserModel userModel)
  104. {
  105. //UserBusinessLayer userService = new UserBusinessLayer("sqlConn");
  106.  
  107. IUserInterface userService = new UserBusinessLayer("sqlConn");
  108.  
  109. userService.EditUser(new UserEntity()
  110. {
  111. usr_Id = userModel.usr_Id,
  112. username = userModel.username,
  113. email = userModel.email,
  114. password = userModel.password
  115. });
  116.  
  117. return RedirectToAction("List");
  118. }
  119.  
  120. public ActionResult DeleteUser(int id)
  121. {
  122. //UserBusinessLayer userService = new UserBusinessLayer("sqlconn");
  123. IUserInterface userService = new UserBusinessLayer("sqlConn");
  124.  
  125. UserModel userModel = new UserModel();
  126.  
  127. try
  128. {
  129. UserEntity userEntity = userService.GetUserId(id);
  130.  
  131. userModel.usr_Id = userEntity.usr_Id;
  132. userModel.email = userEntity.email;
  133. userModel.username = userEntity.username;
  134. userModel.password = userEntity.password;
  135. }
  136. catch (Exception ex)
  137. {
  138. throw ex;
  139. }
  140.  
  141. return View(userModel);
  142. }
  143.  
  144. [HttpPost, ActionName("DeleteUser")]
  145. public ActionResult DeleteUserConfirmed(int id)
  146. {
  147. //UserBusinessLayer userService = new UserBusinessLayer("sqlconn");
  148. IUserInterface userService = new UserBusinessLayer("sqlConn");
  149.  
  150. try
  151. {
  152. userService.DeleteUser(id);
  153. }
  154. catch (Exception eDelete)
  155. {
  156. throw eDelete;
  157. }
  158.  
  159. return RedirectToAction("List");
  160. }
  161.  
  162. public ActionResult Details(int id)
  163. {
  164. //UserBusinessLayer userService = new UserBusinessLayer("sqlconn");
  165. IUserInterface userService = new UserBusinessLayer("sqlConn");
  166.  
  167. UserModel userModel = new UserModel();
  168.  
  169. try
  170. {
  171. UserEntity userEntity = userService.GetUserId(id);
  172.  
  173. userModel.usr_Id = userEntity.usr_Id;
  174. userModel.email = userEntity.email;
  175. userModel.username = userEntity.username;
  176. userModel.password = userEntity.password;
  177.  
  178. }
  179. catch (Exception ex)
  180. {
  181. throw ex;
  182. }
  183.  
  184. return View(userModel);
  185. }
  186. }
  187. }
Add Comment
Please, Sign In to add comment