Guest User

Untitled

a guest
Feb 24th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using SamarpanInfotech.DataModel;
  7. using EntityFramework.Extensions;
  8. using System.Text.RegularExpressions;
  9. using SamarpanInfotech.Core.Paging;
  10. using System.Web;
  11. using System.Data.Entity;
  12. using System.Runtime.Remoting.Contexts;
  13. using System.Net;
  14.  
  15.  
  16.  
  17. namespace SamarpanInfotech.Services
  18. {
  19. public class UserServices
  20. {
  21. /// <summary>
  22. /// Select All User with pagination and sorting
  23. /// </summary>
  24. /// <param name="pageNumber">PageNumber</param>
  25. /// <param name="pageRows">Total Number of Rows to Show in One Page</param>
  26. /// <param name="orderBy">Sorting ascending or descending</param>
  27. /// <param name="searchtext">Serch User by Specific Word or number or date</param>
  28. /// <returns>List of user with pagination and sorting</returns>
  29. public PagerList<User> GetUsers(int pageNumber, int pageRows, string orderBy, string searchtext)
  30. {
  31. using (SamarpanInfotechEntities db = new SamarpanInfotechEntities())
  32. {
  33. PagerList<User> paginationObject = null;
  34. var query = db.Users.Where(w => w.IsDeleted == false);
  35. if (!string.IsNullOrWhiteSpace(searchtext))
  36. {
  37. query = query.Where(w => w.FirstName.Contains(searchtext) || w.LastName.Contains(searchtext) || w.MobileNo.Contains(searchtext) || w.Address.Contains(searchtext) || w.EmailId.Contains(searchtext));
  38. }
  39. paginationObject = query.ToPagerListOrderBy(pageNumber, pageRows, orderBy);
  40. return paginationObject;
  41. }
  42.  
  43. }
  44. /// <summary>
  45. /// Checking username and password in usertable while user login
  46. /// </summary>
  47. /// <param name="userName">username in usertable</param>
  48. /// <param name="password">passwprd in usertable</param>
  49. /// <returns>returns username and password if they mached with entered Username and Password</returns>
  50. public User Authentication(string userName, string password, string userAgent)
  51. {
  52. using (SamarpanInfotechEntities db = new SamarpanInfotechEntities())
  53. {
  54.  
  55. var returnitem = db.Users.Include("Role").FirstOrDefault(f => f.UserName == userName && f.Password == password && f.IsDeleted == false);
  56. if (returnitem != null)
  57. {
  58. db.Users.Where(w => w.Id == returnitem.Id).Update(u => new User
  59. {
  60. LastLoginDate =DateTime.UtcNow,
  61. LastLoginHistory = userAgent
  62. });
  63. }
  64. return returnitem;
  65. }
  66. }
  67. /// <summary>
  68. /// Insert New User
  69. /// </summary>
  70. /// <param name="insert"></param>
  71. public void Insert(User insert)
  72. {
  73. using (SamarpanInfotechEntities db = new SamarpanInfotechEntities())
  74. {
  75. insert.Id = Guid.NewGuid();
  76. insert.CreatedDate = DateTime.Now;
  77. insert.LastLoginDate = DateTime.Now;
  78. insert.IsDeleted = false;
  79. db.Users.Add(insert);
  80. db.SaveChanges();
  81. }
  82. }
  83. /// <summary>
  84. /// Edit perticular User by Id
  85. /// </summary>
  86. /// <param name="update"></param>
  87. public void Update(User update)
  88. {
  89. using (SamarpanInfotechEntities db = new SamarpanInfotechEntities())
  90. {
  91. db.Users.Where(w => w.Id == update.Id).Update(u => new User
  92. {
  93. FirstName = update.FirstName,
  94. LastName = update.LastName,
  95. MiddleName = update.MiddleName,
  96. MobileNo = update.MobileNo,
  97. EmailId = update.EmailId,
  98. CompanyEmailId=update.CompanyEmailId,
  99. Address = update.Address,
  100. UserName = update.UserName,
  101. Password = update.Password,
  102. FkRoleId=update.FkRoleId
  103. });
  104. }
  105.  
  106. }
  107. /// <summary>
  108. /// Select perticular User by Id from user table
  109. /// </summary>
  110. /// <param name="Id"></param>
  111. /// <returns></returns>
  112. public User GetUserById(Guid Id)
  113. {
  114. using (SamarpanInfotechEntities db = new SamarpanInfotechEntities())
  115. {
  116. return db.Users.FirstOrDefault(f => f.Id == Id);
  117. }
  118.  
  119. }
  120. /// <summary>
  121. /// Delete user by Id from user table
  122. /// </summary>
  123. /// <param name="Id"></param>
  124. public void Delete(Guid Id)
  125. {
  126. using (SamarpanInfotechEntities db = new SamarpanInfotechEntities())
  127. {
  128. db.Users.Where(w => w.Id == Id).Update(u => new User
  129. {
  130. IsDeleted = true
  131. });
  132. // DbContext.SaveChanges();
  133. }
  134.  
  135. }
  136. /// <summary>
  137. /// Select All Userlist from user table descending by firstname
  138. /// </summary>
  139. /// <returns>List of all user</returns>
  140. public List<User> GetAllUser()
  141. {
  142. using (SamarpanInfotechEntities db = new SamarpanInfotechEntities())
  143. {
  144. return db.Users.Where(w => w.IsDeleted == false).OrderByDescending(d => d.FirstName).ToList();
  145. }
  146.  
  147. }
  148.  
  149. /// <summary>
  150. /// Select All Userlist from user table descending by firstname 'Require some changes in method
  151. /// </summary>
  152. /// <returns>List of all user</returns>
  153. public List<User> GetAllUserForDropDown(bool onlyDisplayEmployee)
  154. {
  155. using (SamarpanInfotechEntities db = new SamarpanInfotechEntities())
  156. {
  157. if (onlyDisplayEmployee)
  158. {
  159. var employeeCode = SamarpanInfotech.Core.Enums.RoleCode.EMPLOYEE.ToString();
  160. return db.Users.Include("Role").Where(w => w.IsDeleted == false && w.Role.ParentId != null).OrderByDescending(d => d.FirstName).ToList();
  161. }
  162. else
  163. {
  164. return db.Users.Where(w => w.IsDeleted == false).OrderByDescending(d => d.FirstName).ToList();
  165. }
  166. }
  167. }
  168.  
  169. public void PasswordUpdate(UserModel update)
  170. {
  171. using (SamarpanInfotechEntities db = new SamarpanInfotechEntities())
  172. {
  173. db.Users.Where(w => w.Id == update.Id).Update(U => new User
  174. {
  175. Password = update.ChangePassword
  176. });
  177. }
  178. }
  179. }
  180. }
  181.  
  182. using SamarpanInfotech.Services;
  183. using System;
  184. using System.Collections.Generic;
  185. using System.Linq;
  186. using System.Web;
  187. using System.Web.Mvc;
  188. using System.Data.Entity;
  189. using SamarpanInfotech.DataModel;
  190. using System.Net;
  191. using SamarpanInfotech.Core;
  192. using SamarpanInfotech.Areas.Admin.Controllers;
  193.  
  194.  
  195. namespace SamarpanInfotech.Controllers
  196. {
  197. [RoutePrefix("Admin")]
  198. public class AdminUserController : AdminBaseController
  199. {
  200. UserServices userServices = new UserServices();
  201. [Route("User")]
  202. public ActionResult Index()
  203. {
  204. var _list = userServices.GetUsers(1, 20, "FirstName descending", string.Empty);
  205. return View(_list);
  206. }
  207. [Route("User/Filter")]
  208. public PartialViewResult FilterList(int page = 1, int pagerow = 20, string sortby = "FirstName descending", string search = null)
  209. {
  210. var _list = userServices.GetUsers(page, pagerow, sortby, search);
  211. return PartialView("~/Areas/Admin/Views/AdminUser/DisplayTemplates/_List.cshtml", _list);
  212. }
  213. [Route("User/Add")]
  214. public ActionResult AddUser()
  215. {
  216. return View();
  217. }
  218. [HttpPost]
  219. [Route("User/Add")]
  220. public ActionResult AddUserPost(User model)
  221. {
  222. userServices.Insert(model);
  223. this.ShowMessage(MessageExtension.MessageType.Success, "Record successfully saved", true);
  224. return RedirectToAction("Index");
  225. }
  226. [Route("User/Edit/{Id}")]
  227. public ActionResult EditUser(Guid Id)
  228. {
  229. if (Id == null)
  230. {
  231. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  232. }
  233. User edituser = userServices.GetUserById(Id);
  234. if (edituser == null)
  235. {
  236. return HttpNotFound();
  237. }
  238. return View(edituser);
  239. }
  240. [HttpPost]
  241. [Route("User/Edit/{Id}")]
  242. public ActionResult EditUserPost(User model)
  243. {
  244. userServices.Update(model);
  245. this.ShowMessage(MessageExtension.MessageType.Success, "Successfully updated", true);
  246. return RedirectToAction("Index");
  247. }
  248. [Route("User/Delete/{Id}")]
  249. public ActionResult DeleteUser(Guid Id)
  250. {
  251. userServices.Delete(Id);
  252. this.ShowMessage(MessageExtension.MessageType.Success, "Successfully deleted", true);
  253. return RedirectToAction("Index");
  254. }
  255.  
  256. }
  257. }
  258.  
  259. @model SamarpanInfotech.Core.Paging.PagerList<SamarpanInfotech.DataModel.User>
  260. @{
  261. ViewBag.Title = "Index";
  262. }
  263.  
  264. <!-- BEGIN PAGE BAR -->
  265. <div class="page-bar m-btm-20">
  266. <ul class="page-breadcrumb">
  267. <li>
  268. <a href="@Url.Action("Index", "AdminDashboard")">Home</a>
  269. <i class="fa fa-circle"></i>
  270. </li>
  271. <li>
  272. <span>User</span>
  273. </li>
  274. </ul>
  275. </div>
  276. <!-- END PAGE BAR -->
  277. <form method="post" class="form-horizontal">
  278. @Html.RenderMessages()
  279. <div class="row">
  280. <div class="col-md-12">
  281. <div class="table-toolbar">
  282. <div class="row">
  283. <div class="col-md-6 col-sm-6">
  284. <div class="btn-group">
  285. <a class="btn green" href="@Url.Action("AddUser","AdminUser")">
  286. Add User
  287. <i class="fa fa-plus"></i>
  288. </a>
  289. </div>
  290. </div>
  291. <div class="col-md-4 col-sm-4 col-md-offset-2">
  292. <div class="input-group">
  293. <input type="text" placeholder="Searching..." name="search" class="form-control input-sm in-sm-h-35 input-inline input-lg input-lg-pd5" id="extendBox">
  294. <span class="input-group-btn">
  295. <button class="btn green in-sm-h-35" type="button" id="btnSearch" action-url="@Url.Action("FilterList", "AdminUser")" target-id="#target-load">
  296. Search
  297. </button>
  298. </span>
  299. </div>
  300. </div>
  301. </div>
  302. </div>
  303. <div class="target-load hide-x" id="target-load">
  304. @Html.Partial("~/Areas/Admin/Views/AdminUser/DisplayTemplates/_List.cshtml", Model)
  305. </div>
  306. </div>
  307. </div>
  308. </form>
  309. @section scripts
  310. {
  311. <script>
  312. $(document).on('click', ".delete", function (e) {
  313. if (confirm("Are you sure want to delete?")) {
  314. return true;
  315. }
  316. e.preventDefault();
  317. return false;
  318. });
  319. </script>
  320. }
  321.  
  322. @model SamarpanInfotech.Core.Paging.PagerList<SamarpanInfotech.DataModel.User>
  323.  
  324. <table class="table table-bordered dataTable" target=".target-load" id="sample_editable_1">
  325. <thead>
  326. <tr>
  327. @Html.Sorting(Url.Action("FilterList"), "FirstName", "First Name", Model.SortBy, new { item_page = Model.PageRows })
  328. @Html.Sorting(Url.Action("FilterList"), "LastName", "Last Name", Model.SortBy, new { item_page = Model.PageRows })
  329. @Html.Sorting(Url.Action("FilterList"), "MobileNo", "Mobile No.", Model.SortBy, new { item_page = Model.PageRows })
  330. @Html.Sorting(Url.Action("FilterList"), "Address", "Address", Model.SortBy, new { item_page = Model.PageRows })
  331. @Html.Sorting(Url.Action("FilterList"), "EmailId", "Email Id", Model.SortBy, new { item_page = Model.PageRows })
  332. <th>Action</th>
  333. </tr>
  334. </thead>
  335. <tbody>
  336. @foreach (var item in Model.PageList)
  337. {
  338. <tr class="gradeX">
  339. <td>@item.FirstName</td>
  340. <td>@item.LastName</td>
  341. <td>@item.MobileNo</td>
  342. <td>@item.Address</td>
  343. <td>@item.EmailId</td>
  344. <td>
  345. <a href="@Url.Action("EditUser", "AdminUser", new { Id = item.Id })" title="Edit" class="btn btn-outline btn-circle btn-sm purple">
  346. <i class="fa fa-edit"></i>
  347. </a>
  348. <a href="@Url.Action("DeleteUser", "AdminUser", new { Id = item.Id })" title="Delete" class="btn btn-outline btn-circle btn-sm red delete">
  349. <i class="fa fa-trash-o"></i>
  350. </a>
  351. </td>
  352. </tr>
  353. }
  354. </tbody>
  355. </table>
  356.  
  357. <div class="row master-pager">
  358. @Html.PagerFrontend(Url.Action("FilterList", "AdminUser"), Model.PageNumber, Model.TotalRows, Model.PageRows, Model.SortBy)
  359. </div>
  360.  
  361. ---------------------------------AddUser.cshtml----------------------------
  362.  
  363.  
  364.  
  365.  
  366. @model SamarpanInfotech.DataModel.User
  367. @{
  368. ViewBag.Title = "AddUser";
  369. }
  370. <div class="row">
  371. <div class="col-md-12">
  372. <!-- BEGIN VALIDATION STATES-->
  373. <div class="portlet light portlet-fit portlet-form bordered">
  374. <div class="portlet-title">
  375. <div class="caption">
  376. <span>Add User</span>
  377. </div>
  378. </div>
  379. <div class="portlet-body form">
  380. <!-- BEGIN FORM-->
  381. @using (Html.BeginForm("AddUserPost", "AdminUser", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
  382. {
  383.  
  384. <div class="form-body">
  385. <div class="form-group">
  386. @Html.LabelFor(m => m.FirstName, new { @class = "control-label col-md-2" })
  387. <div class="col-md-4">
  388. @Html.TextBoxFor(model => model.FirstName, new { @class = "form-control", placeholder = "First Name" })
  389. @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
  390. </div>
  391. </div>
  392. <div class="form-group">
  393. @Html.LabelFor(m => m.LastName, new { @class = "control-label col-md-2" })
  394. <div class="col-md-4">
  395. @Html.TextBoxFor(model => model.LastName, new { @class = "form-control", placeholder = "Last Name" })
  396. @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
  397. </div>
  398. </div>
  399. <div class="form-group">
  400. @Html.LabelFor(m => m.MobileNo, new { @class = "control-label col-md-2" })
  401. <div class="col-md-4">
  402. @Html.TextBoxFor(model => model.MobileNo, new { @class = "form-control", placeholder = "Mobile Number" })
  403. @Html.ValidationMessageFor(model => model.MobileNo, "", new { @class = "text-danger" })
  404. </div>
  405. </div>
  406. <div class="form-group">
  407. @Html.LabelFor(m => m.Address, new { @class = "control-label col-md-2" })
  408. <div class="col-md-4">
  409. @Html.TextAreaFor(model => model.Address, new { @class = "form-control", placeholder = "Address" })
  410. @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
  411. </div>
  412. </div>
  413. <div class="form-group">
  414. @Html.LabelFor(m => m.EmailId, new { @class = "control-label col-md-2" })
  415. <div class="col-md-4">
  416. @Html.TextBoxFor(model => model.EmailId, new { @class = "form-control", placeholder = "Email Id" })
  417. @Html.ValidationMessageFor(model => model.EmailId, "", new { @class = "text-danger" })
  418. </div>
  419. </div>
  420. <div class="form-group">
  421. @Html.LabelFor(m => m.CompanyEmailId, new { @class = "control-label col-md-2" })
  422. <div class="col-md-4">
  423. @Html.TextBoxFor(model => model.CompanyEmailId, new { @class = "form-control", placeholder = "Company Email Id" })
  424. @Html.ValidationMessageFor(model => model.CompanyEmailId, "", new { @class = "text-danger" })
  425. </div>
  426. </div>
  427. <div class="form-group">
  428. @Html.LabelFor(m => m.UserName, new { @class = "control-label col-md-2" })
  429. <div class="col-md-4">
  430. @Html.TextBoxFor(model => model.UserName, new { @class = "form-control", placeholder = "User Name" })
  431. @Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
  432. </div>
  433. </div>
  434. <div class="form-group">
  435. @Html.LabelFor(m => m.Password, new { @class = "control-label col-md-2" })
  436. <div class="col-md-4">
  437. @Html.TextBoxFor(model => model.Password, new { @class = "form-control", type = "password", placeholder = "Password" })
  438. @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
  439. </div>
  440. </div>
  441. <div class="form-group">
  442. @Html.LabelFor(m => m.FkRoleId, new { @class = "control-label col-md-2" })
  443. <div class="col-md-4">
  444. @Html.DropDownListFor(m => m.FkRoleId, Dropdownlist.GetAllRoles(), "Select Roles", new { @class = "form-control" })
  445. @Html.ValidationMessageFor(m => m.FkRoleId, "", new { @class = "text-danger" })
  446. </div>
  447. </div>
  448. </div>
  449. <div class="row m-btm-20">
  450. <div class="col-md-offset-2 col-md-9">
  451. <input type="submit" class="btn blue" value="Submit">
  452. <a type="button" class="btn grey-salsa btn-outline" href="@Url.Action("Index","AdminUser")">Cancel</a>
  453. </div>
  454. </div>
  455. }
  456. <!-- END FORM-->
  457. </div>
  458. </div>
  459. <!-- END VALIDATION STATES-->
  460. </div>
  461. </div>
  462.  
  463. @model SamarpanInfotech.DataModel.User
  464. @{
  465. ViewBag.Title = "EditUser";
  466. }
  467. <div class="row">
  468. <div class="col-md-12">
  469. <div class="portlet light portlet-fit portlet-form bordered">
  470. <div class="portlet-title">
  471. <div class="caption">
  472. <span>Edit User</span>
  473. </div>
  474. </div>
  475. <div class="portlet-body form">
  476. @using (Html.BeginForm("EditUserPost", "AdminUser", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
  477. {
  478. @Html.HiddenFor(model => model.Id)
  479. <div class="clearfix"></div>
  480. <div class="form-body">
  481. <div class="form-group">
  482. @Html.LabelFor(m => m.FirstName, new { @class = "control-label col-md-2" })
  483. <div class="col-md-4">
  484. @Html.TextBoxFor(model => model.FirstName, new { @class = "form-control", placeholder = "First Name" })
  485. @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
  486. </div>
  487. </div>
  488. <div class="form-group">
  489. @Html.LabelFor(m => m.LastName, new { @class = "control-label col-md-2" })
  490. <div class="col-md-4">
  491. @Html.TextBoxFor(model => model.LastName, new { @class = "form-control", placeholder = "Last Name" })
  492. @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
  493. </div>
  494. </div>
  495. <div class="form-group">
  496. @Html.LabelFor(m => m.MobileNo, new { @class = "control-label col-md-2" })
  497. <div class="col-md-4">
  498. @Html.TextBoxFor(model => model.MobileNo, new { @class = "form-control", placeholder = "Mobile Number" })
  499. @Html.ValidationMessageFor(model => model.MobileNo, "", new { @class = "text-danger" })
  500. </div>
  501. </div>
  502. <div class="form-group">
  503. @Html.LabelFor(m => m.Address, new { @class = "control-label col-md-2" })
  504. <div class="col-md-4">
  505. @Html.TextAreaFor(model => model.Address, new { @class = "form-control", placeholder = "Address" })
  506. @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
  507. </div>
  508. </div>
  509. <div class="form-group">
  510. @Html.LabelFor(m => m.EmailId, new { @class = "control-label col-md-2" })
  511. <div class="col-md-4">
  512. @Html.TextBoxFor(model => model.EmailId, new { @class = "form-control", placeholder = "Email Id" })
  513. @Html.ValidationMessageFor(model => model.EmailId, "", new { @class = "text-danger" })
  514. </div>
  515. </div>
  516. <div class="form-group">
  517. @Html.LabelFor(m => m.CompanyEmailId, new { @class = "control-label col-md-2" })
  518. <div class="col-md-4">
  519. @Html.TextBoxFor(model => model.CompanyEmailId, new { @class = "form-control", placeholder = "Company Email Id" })
  520. @Html.ValidationMessageFor(model => model.CompanyEmailId, "", new { @class = "text-danger" })
  521. </div>
  522. </div>
  523. <div class="form-group">
  524. @Html.LabelFor(m => m.UserName, new { @class = "control-label col-md-2" })
  525. <div class="col-md-4">
  526. @Html.TextBoxFor(model => model.UserName, new { @class = "form-control", placeholder = "User Name" })
  527. @Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
  528. </div>
  529. </div>
  530. <div class="form-group">
  531. @Html.LabelFor(m => m.Password, new { @class = "control-label col-md-2" })
  532. <div class="col-md-4">
  533. @Html.TextBoxFor(model => model.Password, new { @class = "form-control", type = "password", placeholder = "Password" })
  534. @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
  535. </div>
  536. </div>
  537. <div class="form-group">
  538. @Html.LabelFor(m => m.FkRoleId, new { @class = "control-label col-md-2" })
  539. <div class="col-md-4">
  540. @Html.DropDownListFor(m => m.FkRoleId, Dropdownlist.GetAllRoles(), "Select Roles", new { @class = "form-control" })
  541. @Html.ValidationMessageFor(m => m.FkRoleId, "", new { @class = "text-danger" })
  542. </div>
  543. </div>
  544. </div>
  545. <div class="row m-btm-20">
  546. <div class="col-md-offset-2 col-md-9">
  547. <input type="submit" class="btn blue" value="Save">
  548. <a type="button" class="btn grey-salsa btn-outline" href="@Url.Action("Index","AdminUser")">Cancel</a>
  549. </div>
  550. </div>
  551. }
  552. </div>
  553. </div>
  554. </div>
  555. </div>
  556.  
  557. using System;
  558. using System.Collections.Generic;
  559. using System.Linq;
  560. using System.Text;
  561. using System.Threading.Tasks;
  562.  
  563. namespace SamarpanInfotech.DataModel
  564. {
  565. public class DropDownModel
  566. {
  567. public string Id { get; set; }
  568. public string Name { get; set; }
  569. }
  570. }
Add Comment
Please, Sign In to add comment