Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using SamarpanInfotech.DataModel;
- using EntityFramework.Extensions;
- using System.Text.RegularExpressions;
- using SamarpanInfotech.Core.Paging;
- using System.Web;
- using System.Data.Entity;
- using System.Runtime.Remoting.Contexts;
- using System.Net;
- namespace SamarpanInfotech.Services
- {
- public class UserServices
- {
- /// <summary>
- /// Select All User with pagination and sorting
- /// </summary>
- /// <param name="pageNumber">PageNumber</param>
- /// <param name="pageRows">Total Number of Rows to Show in One Page</param>
- /// <param name="orderBy">Sorting ascending or descending</param>
- /// <param name="searchtext">Serch User by Specific Word or number or date</param>
- /// <returns>List of user with pagination and sorting</returns>
- public PagerList<User> GetUsers(int pageNumber, int pageRows, string orderBy, string searchtext)
- {
- using (SamarpanInfotechEntities db = new SamarpanInfotechEntities())
- {
- PagerList<User> paginationObject = null;
- var query = db.Users.Where(w => w.IsDeleted == false);
- if (!string.IsNullOrWhiteSpace(searchtext))
- {
- query = query.Where(w => w.FirstName.Contains(searchtext) || w.LastName.Contains(searchtext) || w.MobileNo.Contains(searchtext) || w.Address.Contains(searchtext) || w.EmailId.Contains(searchtext));
- }
- paginationObject = query.ToPagerListOrderBy(pageNumber, pageRows, orderBy);
- return paginationObject;
- }
- }
- /// <summary>
- /// Checking username and password in usertable while user login
- /// </summary>
- /// <param name="userName">username in usertable</param>
- /// <param name="password">passwprd in usertable</param>
- /// <returns>returns username and password if they mached with entered Username and Password</returns>
- public User Authentication(string userName, string password, string userAgent)
- {
- using (SamarpanInfotechEntities db = new SamarpanInfotechEntities())
- {
- var returnitem = db.Users.Include("Role").FirstOrDefault(f => f.UserName == userName && f.Password == password && f.IsDeleted == false);
- if (returnitem != null)
- {
- db.Users.Where(w => w.Id == returnitem.Id).Update(u => new User
- {
- LastLoginDate =DateTime.UtcNow,
- LastLoginHistory = userAgent
- });
- }
- return returnitem;
- }
- }
- /// <summary>
- /// Insert New User
- /// </summary>
- /// <param name="insert"></param>
- public void Insert(User insert)
- {
- using (SamarpanInfotechEntities db = new SamarpanInfotechEntities())
- {
- insert.Id = Guid.NewGuid();
- insert.CreatedDate = DateTime.Now;
- insert.LastLoginDate = DateTime.Now;
- insert.IsDeleted = false;
- db.Users.Add(insert);
- db.SaveChanges();
- }
- }
- /// <summary>
- /// Edit perticular User by Id
- /// </summary>
- /// <param name="update"></param>
- public void Update(User update)
- {
- using (SamarpanInfotechEntities db = new SamarpanInfotechEntities())
- {
- db.Users.Where(w => w.Id == update.Id).Update(u => new User
- {
- FirstName = update.FirstName,
- LastName = update.LastName,
- MiddleName = update.MiddleName,
- MobileNo = update.MobileNo,
- EmailId = update.EmailId,
- CompanyEmailId=update.CompanyEmailId,
- Address = update.Address,
- UserName = update.UserName,
- Password = update.Password,
- FkRoleId=update.FkRoleId
- });
- }
- }
- /// <summary>
- /// Select perticular User by Id from user table
- /// </summary>
- /// <param name="Id"></param>
- /// <returns></returns>
- public User GetUserById(Guid Id)
- {
- using (SamarpanInfotechEntities db = new SamarpanInfotechEntities())
- {
- return db.Users.FirstOrDefault(f => f.Id == Id);
- }
- }
- /// <summary>
- /// Delete user by Id from user table
- /// </summary>
- /// <param name="Id"></param>
- public void Delete(Guid Id)
- {
- using (SamarpanInfotechEntities db = new SamarpanInfotechEntities())
- {
- db.Users.Where(w => w.Id == Id).Update(u => new User
- {
- IsDeleted = true
- });
- // DbContext.SaveChanges();
- }
- }
- /// <summary>
- /// Select All Userlist from user table descending by firstname
- /// </summary>
- /// <returns>List of all user</returns>
- public List<User> GetAllUser()
- {
- using (SamarpanInfotechEntities db = new SamarpanInfotechEntities())
- {
- return db.Users.Where(w => w.IsDeleted == false).OrderByDescending(d => d.FirstName).ToList();
- }
- }
- /// <summary>
- /// Select All Userlist from user table descending by firstname 'Require some changes in method
- /// </summary>
- /// <returns>List of all user</returns>
- public List<User> GetAllUserForDropDown(bool onlyDisplayEmployee)
- {
- using (SamarpanInfotechEntities db = new SamarpanInfotechEntities())
- {
- if (onlyDisplayEmployee)
- {
- var employeeCode = SamarpanInfotech.Core.Enums.RoleCode.EMPLOYEE.ToString();
- return db.Users.Include("Role").Where(w => w.IsDeleted == false && w.Role.ParentId != null).OrderByDescending(d => d.FirstName).ToList();
- }
- else
- {
- return db.Users.Where(w => w.IsDeleted == false).OrderByDescending(d => d.FirstName).ToList();
- }
- }
- }
- public void PasswordUpdate(UserModel update)
- {
- using (SamarpanInfotechEntities db = new SamarpanInfotechEntities())
- {
- db.Users.Where(w => w.Id == update.Id).Update(U => new User
- {
- Password = update.ChangePassword
- });
- }
- }
- }
- }
- using SamarpanInfotech.Services;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using System.Data.Entity;
- using SamarpanInfotech.DataModel;
- using System.Net;
- using SamarpanInfotech.Core;
- using SamarpanInfotech.Areas.Admin.Controllers;
- namespace SamarpanInfotech.Controllers
- {
- [RoutePrefix("Admin")]
- public class AdminUserController : AdminBaseController
- {
- UserServices userServices = new UserServices();
- [Route("User")]
- public ActionResult Index()
- {
- var _list = userServices.GetUsers(1, 20, "FirstName descending", string.Empty);
- return View(_list);
- }
- [Route("User/Filter")]
- public PartialViewResult FilterList(int page = 1, int pagerow = 20, string sortby = "FirstName descending", string search = null)
- {
- var _list = userServices.GetUsers(page, pagerow, sortby, search);
- return PartialView("~/Areas/Admin/Views/AdminUser/DisplayTemplates/_List.cshtml", _list);
- }
- [Route("User/Add")]
- public ActionResult AddUser()
- {
- return View();
- }
- [HttpPost]
- [Route("User/Add")]
- public ActionResult AddUserPost(User model)
- {
- userServices.Insert(model);
- this.ShowMessage(MessageExtension.MessageType.Success, "Record successfully saved", true);
- return RedirectToAction("Index");
- }
- [Route("User/Edit/{Id}")]
- public ActionResult EditUser(Guid Id)
- {
- if (Id == null)
- {
- return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
- }
- User edituser = userServices.GetUserById(Id);
- if (edituser == null)
- {
- return HttpNotFound();
- }
- return View(edituser);
- }
- [HttpPost]
- [Route("User/Edit/{Id}")]
- public ActionResult EditUserPost(User model)
- {
- userServices.Update(model);
- this.ShowMessage(MessageExtension.MessageType.Success, "Successfully updated", true);
- return RedirectToAction("Index");
- }
- [Route("User/Delete/{Id}")]
- public ActionResult DeleteUser(Guid Id)
- {
- userServices.Delete(Id);
- this.ShowMessage(MessageExtension.MessageType.Success, "Successfully deleted", true);
- return RedirectToAction("Index");
- }
- }
- }
- @model SamarpanInfotech.Core.Paging.PagerList<SamarpanInfotech.DataModel.User>
- @{
- ViewBag.Title = "Index";
- }
- <!-- BEGIN PAGE BAR -->
- <div class="page-bar m-btm-20">
- <ul class="page-breadcrumb">
- <li>
- <a href="@Url.Action("Index", "AdminDashboard")">Home</a>
- <i class="fa fa-circle"></i>
- </li>
- <li>
- <span>User</span>
- </li>
- </ul>
- </div>
- <!-- END PAGE BAR -->
- <form method="post" class="form-horizontal">
- @Html.RenderMessages()
- <div class="row">
- <div class="col-md-12">
- <div class="table-toolbar">
- <div class="row">
- <div class="col-md-6 col-sm-6">
- <div class="btn-group">
- <a class="btn green" href="@Url.Action("AddUser","AdminUser")">
- Add User
- <i class="fa fa-plus"></i>
- </a>
- </div>
- </div>
- <div class="col-md-4 col-sm-4 col-md-offset-2">
- <div class="input-group">
- <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">
- <span class="input-group-btn">
- <button class="btn green in-sm-h-35" type="button" id="btnSearch" action-url="@Url.Action("FilterList", "AdminUser")" target-id="#target-load">
- Search
- </button>
- </span>
- </div>
- </div>
- </div>
- </div>
- <div class="target-load hide-x" id="target-load">
- @Html.Partial("~/Areas/Admin/Views/AdminUser/DisplayTemplates/_List.cshtml", Model)
- </div>
- </div>
- </div>
- </form>
- @section scripts
- {
- <script>
- $(document).on('click', ".delete", function (e) {
- if (confirm("Are you sure want to delete?")) {
- return true;
- }
- e.preventDefault();
- return false;
- });
- </script>
- }
- @model SamarpanInfotech.Core.Paging.PagerList<SamarpanInfotech.DataModel.User>
- <table class="table table-bordered dataTable" target=".target-load" id="sample_editable_1">
- <thead>
- <tr>
- @Html.Sorting(Url.Action("FilterList"), "FirstName", "First Name", Model.SortBy, new { item_page = Model.PageRows })
- @Html.Sorting(Url.Action("FilterList"), "LastName", "Last Name", Model.SortBy, new { item_page = Model.PageRows })
- @Html.Sorting(Url.Action("FilterList"), "MobileNo", "Mobile No.", Model.SortBy, new { item_page = Model.PageRows })
- @Html.Sorting(Url.Action("FilterList"), "Address", "Address", Model.SortBy, new { item_page = Model.PageRows })
- @Html.Sorting(Url.Action("FilterList"), "EmailId", "Email Id", Model.SortBy, new { item_page = Model.PageRows })
- <th>Action</th>
- </tr>
- </thead>
- <tbody>
- @foreach (var item in Model.PageList)
- {
- <tr class="gradeX">
- <td>@item.FirstName</td>
- <td>@item.LastName</td>
- <td>@item.MobileNo</td>
- <td>@item.Address</td>
- <td>@item.EmailId</td>
- <td>
- <a href="@Url.Action("EditUser", "AdminUser", new { Id = item.Id })" title="Edit" class="btn btn-outline btn-circle btn-sm purple">
- <i class="fa fa-edit"></i>
- </a>
- <a href="@Url.Action("DeleteUser", "AdminUser", new { Id = item.Id })" title="Delete" class="btn btn-outline btn-circle btn-sm red delete">
- <i class="fa fa-trash-o"></i>
- </a>
- </td>
- </tr>
- }
- </tbody>
- </table>
- <div class="row master-pager">
- @Html.PagerFrontend(Url.Action("FilterList", "AdminUser"), Model.PageNumber, Model.TotalRows, Model.PageRows, Model.SortBy)
- </div>
- ---------------------------------AddUser.cshtml----------------------------
- @model SamarpanInfotech.DataModel.User
- @{
- ViewBag.Title = "AddUser";
- }
- <div class="row">
- <div class="col-md-12">
- <!-- BEGIN VALIDATION STATES-->
- <div class="portlet light portlet-fit portlet-form bordered">
- <div class="portlet-title">
- <div class="caption">
- <span>Add User</span>
- </div>
- </div>
- <div class="portlet-body form">
- <!-- BEGIN FORM-->
- @using (Html.BeginForm("AddUserPost", "AdminUser", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
- {
- <div class="form-body">
- <div class="form-group">
- @Html.LabelFor(m => m.FirstName, new { @class = "control-label col-md-2" })
- <div class="col-md-4">
- @Html.TextBoxFor(model => model.FirstName, new { @class = "form-control", placeholder = "First Name" })
- @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(m => m.LastName, new { @class = "control-label col-md-2" })
- <div class="col-md-4">
- @Html.TextBoxFor(model => model.LastName, new { @class = "form-control", placeholder = "Last Name" })
- @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(m => m.MobileNo, new { @class = "control-label col-md-2" })
- <div class="col-md-4">
- @Html.TextBoxFor(model => model.MobileNo, new { @class = "form-control", placeholder = "Mobile Number" })
- @Html.ValidationMessageFor(model => model.MobileNo, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(m => m.Address, new { @class = "control-label col-md-2" })
- <div class="col-md-4">
- @Html.TextAreaFor(model => model.Address, new { @class = "form-control", placeholder = "Address" })
- @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(m => m.EmailId, new { @class = "control-label col-md-2" })
- <div class="col-md-4">
- @Html.TextBoxFor(model => model.EmailId, new { @class = "form-control", placeholder = "Email Id" })
- @Html.ValidationMessageFor(model => model.EmailId, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(m => m.CompanyEmailId, new { @class = "control-label col-md-2" })
- <div class="col-md-4">
- @Html.TextBoxFor(model => model.CompanyEmailId, new { @class = "form-control", placeholder = "Company Email Id" })
- @Html.ValidationMessageFor(model => model.CompanyEmailId, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(m => m.UserName, new { @class = "control-label col-md-2" })
- <div class="col-md-4">
- @Html.TextBoxFor(model => model.UserName, new { @class = "form-control", placeholder = "User Name" })
- @Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(m => m.Password, new { @class = "control-label col-md-2" })
- <div class="col-md-4">
- @Html.TextBoxFor(model => model.Password, new { @class = "form-control", type = "password", placeholder = "Password" })
- @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(m => m.FkRoleId, new { @class = "control-label col-md-2" })
- <div class="col-md-4">
- @Html.DropDownListFor(m => m.FkRoleId, Dropdownlist.GetAllRoles(), "Select Roles", new { @class = "form-control" })
- @Html.ValidationMessageFor(m => m.FkRoleId, "", new { @class = "text-danger" })
- </div>
- </div>
- </div>
- <div class="row m-btm-20">
- <div class="col-md-offset-2 col-md-9">
- <input type="submit" class="btn blue" value="Submit">
- <a type="button" class="btn grey-salsa btn-outline" href="@Url.Action("Index","AdminUser")">Cancel</a>
- </div>
- </div>
- }
- <!-- END FORM-->
- </div>
- </div>
- <!-- END VALIDATION STATES-->
- </div>
- </div>
- @model SamarpanInfotech.DataModel.User
- @{
- ViewBag.Title = "EditUser";
- }
- <div class="row">
- <div class="col-md-12">
- <div class="portlet light portlet-fit portlet-form bordered">
- <div class="portlet-title">
- <div class="caption">
- <span>Edit User</span>
- </div>
- </div>
- <div class="portlet-body form">
- @using (Html.BeginForm("EditUserPost", "AdminUser", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
- {
- @Html.HiddenFor(model => model.Id)
- <div class="clearfix"></div>
- <div class="form-body">
- <div class="form-group">
- @Html.LabelFor(m => m.FirstName, new { @class = "control-label col-md-2" })
- <div class="col-md-4">
- @Html.TextBoxFor(model => model.FirstName, new { @class = "form-control", placeholder = "First Name" })
- @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(m => m.LastName, new { @class = "control-label col-md-2" })
- <div class="col-md-4">
- @Html.TextBoxFor(model => model.LastName, new { @class = "form-control", placeholder = "Last Name" })
- @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(m => m.MobileNo, new { @class = "control-label col-md-2" })
- <div class="col-md-4">
- @Html.TextBoxFor(model => model.MobileNo, new { @class = "form-control", placeholder = "Mobile Number" })
- @Html.ValidationMessageFor(model => model.MobileNo, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(m => m.Address, new { @class = "control-label col-md-2" })
- <div class="col-md-4">
- @Html.TextAreaFor(model => model.Address, new { @class = "form-control", placeholder = "Address" })
- @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(m => m.EmailId, new { @class = "control-label col-md-2" })
- <div class="col-md-4">
- @Html.TextBoxFor(model => model.EmailId, new { @class = "form-control", placeholder = "Email Id" })
- @Html.ValidationMessageFor(model => model.EmailId, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(m => m.CompanyEmailId, new { @class = "control-label col-md-2" })
- <div class="col-md-4">
- @Html.TextBoxFor(model => model.CompanyEmailId, new { @class = "form-control", placeholder = "Company Email Id" })
- @Html.ValidationMessageFor(model => model.CompanyEmailId, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(m => m.UserName, new { @class = "control-label col-md-2" })
- <div class="col-md-4">
- @Html.TextBoxFor(model => model.UserName, new { @class = "form-control", placeholder = "User Name" })
- @Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(m => m.Password, new { @class = "control-label col-md-2" })
- <div class="col-md-4">
- @Html.TextBoxFor(model => model.Password, new { @class = "form-control", type = "password", placeholder = "Password" })
- @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(m => m.FkRoleId, new { @class = "control-label col-md-2" })
- <div class="col-md-4">
- @Html.DropDownListFor(m => m.FkRoleId, Dropdownlist.GetAllRoles(), "Select Roles", new { @class = "form-control" })
- @Html.ValidationMessageFor(m => m.FkRoleId, "", new { @class = "text-danger" })
- </div>
- </div>
- </div>
- <div class="row m-btm-20">
- <div class="col-md-offset-2 col-md-9">
- <input type="submit" class="btn blue" value="Save">
- <a type="button" class="btn grey-salsa btn-outline" href="@Url.Action("Index","AdminUser")">Cancel</a>
- </div>
- </div>
- }
- </div>
- </div>
- </div>
- </div>
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace SamarpanInfotech.DataModel
- {
- public class DropDownModel
- {
- public string Id { get; set; }
- public string Name { get; set; }
- }
- }
Add Comment
Please, Sign In to add comment