Guest User

Untitled

a guest
Apr 10th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. CREATE TABLE public.users
  2. (
  3. iduser uuid NOT NULL,
  4. username character varying,
  5. password character varying
  6. )
  7.  
  8. public class UserLogin : BaseEntity
  9. {
  10. public Guid IdUser { get; set; } = new Guid();
  11.  
  12. public String UserName { get; set; }
  13.  
  14. public String Password { get; set; }
  15. }
  16.  
  17.  
  18. public class UserLoginRepository : IRepository<UserLogin>
  19. {
  20. private string connectionString;
  21.  
  22. public UserLoginRepository(IConfiguration configuration)
  23. {
  24. connectionString = configuration.GetValue<string>("DBInfo:ConnectionString");
  25. }
  26.  
  27. internal IDbConnection Connection
  28. {
  29. get
  30. {
  31. return new NpgsqlConnection(connectionString);
  32. }
  33. }
  34.  
  35. public void Add(UserLogin item)
  36. {
  37. using (IDbConnection dbConnection = Connection)
  38. {
  39. dbConnection.Open();
  40. dbConnection.Execute("SELECT iduser from users where username = @UserName AND password = @Password", item);
  41. }
  42. }
  43.  
  44. public UserLogin FindByID(Guid id)
  45. {
  46. using (IDbConnection dbConnection = Connection)
  47. {
  48. dbConnection.Open();
  49. return dbConnection.Query<UserLogin>("SELECT * FROM users WHERE id = @Id", new { Id = id }).FirstOrDefault();
  50. }
  51. }
  52.  
  53. }
  54. }
  55.  
  56. public class HomeController : Controller
  57. {
  58. private readonly UserLoginRepository uRepository;
  59.  
  60. public HomeController(IConfiguration configuration)
  61. {
  62. uRepository = new UserLoginRepository(configuration);
  63. }
  64.  
  65. public IActionResult Index()
  66. {
  67. return View();
  68. }
  69.  
  70. public IActionResult Authorization()
  71. {
  72. return View();
  73. }
  74.  
  75. // POST: UserLogin/Authorization
  76. [HttpPost]
  77. public IActionResult Authorization(UserLogin user)
  78. {
  79. if (ModelState.IsValid)
  80. {
  81. uRepository.Add(user);
  82. return RedirectToAction("UserPage");
  83. }
  84. return View(user);
  85. }
  86.  
  87. public IActionResult UserPage()
  88. {
  89. return View();
  90. }
  91.  
  92. // GET: /UserLogin/UserPage/1
  93. [HttpGet]
  94. public IActionResult UserPage(Guid? id)
  95. {
  96. if (id == null)
  97. {
  98. return NotFound();
  99. }
  100. UserLogin obj = uRepository.FindByID(id.Value);
  101. if (obj == null)
  102. {
  103. return NotFound();
  104. }
  105. return View(obj);
  106.  
  107. }
  108. }
  109.  
  110. @model WebApplication4.Entites.UserLogin
  111.  
  112. @{
  113. ViewData["Title"] = "Create";
  114. }
  115.  
  116. <h2>Create</h2>
  117.  
  118. <form asp-action="Authorization">
  119. <div class="form-horizontal">
  120. <h4>Пожалуйста, войдите в систему: </h4>
  121. <hr />
  122. <div asp-validation-summary="ModelOnly" class="text-danger"></div>
  123. <div class="form-group">
  124. <label asp-for="UserName" class="col-md-2 control-label"></label>
  125. <div class="col-md-10">
  126. <input asp-for="UserName" class="form-control" />
  127. <span asp-validation-for="UserName" class="text-danger" />
  128. </div>
  129. </div>
  130. <div class="form-group">
  131. <label asp-for="Password" class="col-md-2 control-label"></label>
  132. <div class="col-md-10">
  133. <input asp-for="Password" class="form-control" />
  134. <span asp-validation-for="Password" class="text-danger" />
  135. </div>
  136. </div>
  137. <div class="form-group">
  138. <div class="col-md-offset-2 col-md-10">
  139. <input type="submit" value="Authorization" class="btn btn-default" />
  140. </div>
  141. </div>
  142. </div>
  143. </form>
  144.  
  145. <div>
  146. <a asp-action="Index">Back to List</a>
  147. </div>
  148.  
  149. @model IEnumerable<WebApplication4.Entites.UserLogin>
  150.  
  151. @{
  152. ViewData["Title"] = "Index";
  153. }
  154.  
  155. <h2>Index</h2>
  156.  
  157. <table class="table">
  158. <tr>
  159. <th>
  160. @Html.DisplayNameFor(model => model.UserName)
  161. </th>
  162. <th>
  163. @Html.DisplayNameFor(model => model.Password)
  164. </th>
  165. <th></th>
  166. </tr>
  167.  
  168. @foreach (var item in Model)
  169. {
  170. <tr>
  171. <td>
  172. @Html.DisplayFor(modelItem => item.UserName)
  173. </td>
  174. <td>
  175. @Html.DisplayFor(modelItem => item.Password)
  176. </td>
  177. <td>
  178. <a asp-action="Edit" asp-route-id="@item.IdUser">Edit</a> |
  179. <a asp-action="Delete" asp-route-id="@item.IdUser" onclick="return confirm('Are sure wants to delete?');">Delete</a>
  180. </td>
  181. </tr>
  182. }
  183. </table>
Add Comment
Please, Sign In to add comment