SHARE
TWEET

Untitled

a guest Dec 7th, 2019 94 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.SqlClient;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Microsoft.AspNetCore.Authorization;
  7. using Microsoft.AspNetCore.Identity;
  8. using Microsoft.AspNetCore.Mvc;
  9. using Microsoft.AspNetCore.Mvc.Rendering;
  10. using Microsoft.EntityFrameworkCore;
  11. using mis.applicationcore.Interfaces;
  12. using mis.applicationcore.Interfaces.Repositories;
  13. using mis.common.Models;
  14.  
  15. namespace mis.web.Controllers
  16. {
  17.     public class EmployeeDetailsController : AuthenticatedController<EmployeeDetails>
  18.     {
  19.         private readonly IAsyncRepository _asyncRepository;
  20.         private readonly IAsyncRepository<EmployeeDetails> _employeeDetailsRepository;
  21.         private readonly IAsyncRepository<EmployeeGroupDetails> _employeeGroupDetailsRepository;
  22.         private readonly IAsyncRepository<EmployeeLevelDetails> _employeeLevelDetailsRepository;
  23.         private readonly IAsyncRepository<EmployeeSectorDetails> _employeeSectorDetailsRepository;
  24.         private readonly IAsyncRepository<EmployeeDesignationDetails> _employeeDesignationDetailsRepository;
  25.         private readonly IAsyncRepository<OfficeDetails> _officeDetailsRepository;
  26.         private readonly IAsyncRepository<BloodGroup> _bloodGroupRepository;
  27.         private readonly UserManager<IdentityUser> _userManager;
  28.         private readonly IEmployeeRepository _employeeRepository;      
  29.  
  30.         public EmployeeDetailsController(
  31.             IAsyncRepository asyncRepository,
  32.             UserManager<IdentityUser> userManager,
  33.             IAsyncRepository<EmployeeDetails> employeeDetailsRepository,
  34.             IAsyncRepository<EmployeeGroupDetails> employeeGroupDetailsRepository,
  35.             IAsyncRepository<EmployeeLevelDetails> employeeLevelDetailsRepository,
  36.             IAsyncRepository<EmployeeSectorDetails> employeeSectorDetailsRepository,
  37.             IAsyncRepository<EmployeeDesignationDetails> employeeDesignationDetailsRepository,
  38.             IAsyncRepository<OfficeDetails> officeDetailsRepository,
  39.             IAsyncRepository<BloodGroup> bloodGroupRepository,
  40.             IEmployeeRepository employeeRepository,
  41.             IAppLogger<EmployeeDetails> appLogger) : base(appLogger,asyncRepository)
  42.         {
  43.             _asyncRepository = asyncRepository;
  44.             _employeeDetailsRepository = employeeDetailsRepository;
  45.             _employeeDesignationDetailsRepository = employeeDesignationDetailsRepository;
  46.             _employeeGroupDetailsRepository = employeeGroupDetailsRepository;
  47.             _employeeLevelDetailsRepository = employeeLevelDetailsRepository;
  48.             _employeeSectorDetailsRepository = employeeSectorDetailsRepository;
  49.             _bloodGroupRepository = bloodGroupRepository;
  50.             _employeeRepository = employeeRepository;
  51.             _officeDetailsRepository = officeDetailsRepository;
  52.             _userManager = userManager;            
  53.         }
  54.  
  55.         // GET: EmployeeDetails
  56.         [Authorize(Roles = AuthConst.VIEW_EMPLOYEE)]
  57.         public async Task<IActionResult> Index()
  58.         {
  59.             ViewBag.OfficeId = new SelectList(await _officeDetailsRepository.ListAllAsync(), "Id", "Name");
  60.             ViewBag.BloodGroupId = new SelectList(await _bloodGroupRepository.ListAllAsync(), "Id", "Type");
  61.             ViewBag.EmployeeDesignationId = new SelectList(await _employeeDesignationDetailsRepository.ListAllAsync(), "Id", "DesignationName");
  62.             ViewBag.EmployeeGroupId = new SelectList(await _employeeGroupDetailsRepository.ListAllAsync(), "Id", "GroupName");
  63.             ViewBag.EmployeeLevelId = new SelectList(await _employeeLevelDetailsRepository.ListAllAsync(), "Id", "LevelName");
  64.             ViewBag.EmployeeSectorId = new SelectList(await _employeeSectorDetailsRepository.ListAllAsync(), "Id", "Name");
  65.             ViewBag.Pradesh = new SelectList(await _asyncRepository.ToListAsync<StateInfo>(), "Id", "StateNameUniCode");
  66.             ViewBag.DistrictId = new SelectList(await _asyncRepository.ToListAsync<DistrictInfo>(), "Id", "DistrictNameUniCode");
  67.             ViewBag.MunicipalId = new SelectList(await _asyncRepository.ToListAsync<MunicipalInfo>(), "Id", "MunicipalNameUnicode");
  68.             return View(await _employeeDetailsRepository.ListAllAsync());          
  69.         }
  70.  
  71.         [HttpGet]
  72.         [Route("/EmployeeDetails/EmployeeGetList")]
  73.         public JsonResult EmployeeGetList(EmployeeDetailsViewModel employeeDetailsViewModel)
  74.         {
  75.             var newindexlist = _asyncRepository.FromSqlViewModel<EmployeeDetailsViewModel>($"usp_EmployeeList").ToList();
  76.             return Json(newindexlist);
  77.         }
  78.  
  79.         public JsonResult GetPradeshDistrict(int record)
  80.         {
  81.             var DistrictList = _asyncRepository.Where<DistrictInfo>(p => p.StateId == record).ToList();
  82.             return Json(DistrictList);
  83.         }
  84.  
  85.         public IActionResult Search(EmployeeDetails employee)
  86.         {        
  87.             var employeename = _asyncRepository.FromSqlModel<EmployeeDetails>($"usp_EmployeeSearch @FullName", new SqlParameter("@StoreId", employee.FullName)).SingleOrDefault();
  88.             //ViewBag.employee = employeename;
  89.             return View(nameof(Index), employee);
  90.         }
  91.  
  92.         // GET: EmployeeDetails/Create
  93.         public async Task<IActionResult> Create()
  94.         {
  95.             ViewBag.OfficeId = new SelectList(await _officeDetailsRepository.ListAllAsync(), "Id", "Name");
  96.             ViewBag.BloodGroupId = new SelectList(await _bloodGroupRepository.ListAllAsync(), "Id", "Type");
  97.             ViewBag.EmployeeDesignationId = new SelectList(await _employeeDesignationDetailsRepository.ListAllAsync(), "Id", "DesignationName");
  98.             ViewBag.EmployeeGroupId = new SelectList(await _employeeGroupDetailsRepository.ListAllAsync(), "Id", "GroupName");
  99.             ViewBag.EmployeeLevelId = new SelectList(await _employeeLevelDetailsRepository.ListAllAsync(), "Id", "LevelName");
  100.             ViewBag.EmployeeSectorId = new SelectList(await _employeeSectorDetailsRepository.ListAllAsync(), "Id", "Name");
  101.             ViewBag.Pradesh = new SelectList(await _asyncRepository.ToListAsync<StateInfo>(), "Id", "StateNameUniCode");
  102.             ViewBag.DistrictId = new SelectList(await _asyncRepository.ToListAsync<DistrictInfo>(), "Id", "DistrictNameUniCode");
  103.             ViewBag.MunicipalId = new SelectList(await _asyncRepository.ToListAsync<MunicipalInfo>(), "Id", "MunicipalNameUnicode");
  104.             ViewData["Type"] = "Create";
  105.             return View("AddEdit");
  106.         }
  107.  
  108.         // POST: EmployeeDetails/Create
  109.         // To protect from overposting attacks, please enable the specific properties you want to bind to, for
  110.         // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
  111.         [HttpPost]
  112.         [Authorize(Roles = AuthConst.ADD_EDIT_EMPLOYEE)]
  113.         [ValidateAntiForgeryToken]
  114.         public async Task<IActionResult> Create([Bind("FullName,CitizenshipNo,EmployeeDesignationId,EmployeeSectorId,EmployeeGroupId," +
  115.             "EmployeeLevelId,OfficeId,EmployeeCode,AppointedDate,EnrolledDate,EmploymentDuration,DateOfBirth,TemporaryAddress," +
  116.             "PermanentAddress,PhoneNumber1,PhoneNumber2,Pradesh,DistrictId,MunicipalId,WardNumber,StreetName,BloodGroupId,EmailAddress,Id,IsActive,OldOfficeId")] EmployeeDetails employeeDetails)
  117.         {
  118.             if (ModelState.IsValid)
  119.             {              
  120.                 try
  121.                 {
  122.                     employeeDetails.CreatedBy = _userManager.GetUserId(HttpContext.User) ?? "ADMIN";
  123.                     employeeDetails.AccountStatus = true;
  124.                     await _employeeDetailsRepository.AddAsync(employeeDetails);
  125.                 }
  126.                 catch(Exception ex)
  127.                 {
  128.                     if (ex.InnerException.ToString().Contains("EmployeeDetails_FullName_DateOfBirth_CitizenshipNo"))                      
  129.                     {
  130.                         ViewBag.OfficeId = new SelectList(await _officeDetailsRepository.ListAllAsync(), "Id", "Name");
  131.                         ViewBag.BloodGroupId = new SelectList(await _bloodGroupRepository.ListAllAsync(), "Id", "Type");
  132.                         ViewBag.EmployeeDesignationId = new SelectList(await _employeeDesignationDetailsRepository.ListAllAsync(), "Id", "DesignationName");
  133.                         ViewBag.EmployeeGroupId = new SelectList(await _employeeGroupDetailsRepository.ListAllAsync(), "Id", "GroupName");
  134.                         ViewBag.EmployeeLevelId = new SelectList(await _employeeLevelDetailsRepository.ListAllAsync(), "Id", "LevelName");
  135.                         ViewBag.EmployeeSectorId = new SelectList(await _employeeSectorDetailsRepository.ListAllAsync(), "Id", "Name");
  136.                         ViewBag.Pradesh = new SelectList(await _asyncRepository.ToListAsync<StateInfo>(), "Id", "StateNameUniCode");
  137.                         ViewBag.DistrictId = new SelectList(await _asyncRepository.ToListAsync<DistrictInfo>(), "Id", "DistrictNameUniCode");
  138.                         ViewBag.MunicipalId = new SelectList(await _asyncRepository.ToListAsync<MunicipalInfo>(), "Id", "MunicipalNameUnicode");
  139.                         ViewBag.Message = "कर्मचारीको विवरण पहिले राखिसक्नु भयो";
  140.                         return View("AddEdit", employeeDetails);
  141.                     }
  142.                     else if (ex.InnerException.ToString().Contains("EmployeeDetails_CitizenshipNo"))
  143.                     {
  144.                         ViewBag.OfficeId = new SelectList(await _officeDetailsRepository.ListAllAsync(), "Id", "Name");
  145.                         ViewBag.BloodGroupId = new SelectList(await _bloodGroupRepository.ListAllAsync(), "Id", "Type");
  146.                         ViewBag.EmployeeDesignationId = new SelectList(await _employeeDesignationDetailsRepository.ListAllAsync(), "Id", "DesignationName");
  147.                         ViewBag.EmployeeGroupId = new SelectList(await _employeeGroupDetailsRepository.ListAllAsync(), "Id", "GroupName");
  148.                         ViewBag.EmployeeLevelId = new SelectList(await _employeeLevelDetailsRepository.ListAllAsync(), "Id", "LevelName");
  149.                         ViewBag.EmployeeSectorId = new SelectList(await _employeeSectorDetailsRepository.ListAllAsync(), "Id", "Name");
  150.                         ViewBag.Pradesh = new SelectList(await _asyncRepository.ToListAsync<StateInfo>(), "Id", "StateNameUniCode");
  151.                         ViewBag.DistrictId = new SelectList(await _asyncRepository.ToListAsync<DistrictInfo>(), "Id", "DistrictNameUniCode");
  152.                         ViewBag.MunicipalId = new SelectList(await _asyncRepository.ToListAsync<MunicipalInfo>(), "Id", "MunicipalNameUnicode");
  153.                         ViewBag.Message = "यो नागरिकता नं पहिले राखिसक्नु भयो";
  154.                         return View("AddEdit", employeeDetails);
  155.                     }
  156.                     else
  157.                     {
  158.                         throw ex;
  159.                     }
  160.                 }
  161.                 return RedirectToAction(nameof(Index));
  162.             }
  163.             return View("AddEdit", employeeDetails);
  164.         }
  165.  
  166.         // GET: EmployeeDetails/Edit/5
  167.         [Authorize(Roles = AuthConst.VIEW_EMPLOYEE)]
  168.         public async Task<IActionResult> Edit(int id)
  169.         {
  170.             if (id == 0)
  171.             {
  172.                 return NotFound();
  173.             }
  174.  
  175.             var employeeDetails = await _employeeDetailsRepository.GetByIdAsync(id);
  176.             if (employeeDetails == null)
  177.             {
  178.                 return NotFound();
  179.             }
  180.             ViewBag.OfficeId = new SelectList(await _officeDetailsRepository.ListAllAsync(), "Id", "Name");
  181.             ViewBag.BloodGroupId = new SelectList(await _bloodGroupRepository.ListAllAsync(), "Id", "Type");
  182.             ViewBag.EmployeeDesignationId = new SelectList(await _employeeDesignationDetailsRepository.ListAllAsync(), "Id", "DesignationName");
  183.             ViewBag.EmployeeGroupId = new SelectList(await _employeeGroupDetailsRepository.ListAllAsync(), "Id", "GroupName");
  184.             ViewBag.EmployeeLevelId = new SelectList(await _employeeLevelDetailsRepository.ListAllAsync(), "Id", "LevelName");
  185.             ViewBag.EmployeeSectorId = new SelectList(await _employeeSectorDetailsRepository.ListAllAsync(), "Id", "Name");
  186.             ViewBag.Pradesh = new SelectList(await _asyncRepository.ToListAsync<StateInfo>(), "Id", "StateNameUniCode");
  187.             ViewBag.DistrictId = new SelectList(await _asyncRepository.ToListAsync<DistrictInfo>(), "Id", "DistrictNameUniCode");
  188.             ViewBag.MunicipalId = new SelectList(await _asyncRepository.ToListAsync<MunicipalInfo>(), "Id", "MunicipalNameUnicode");
  189.             var roleGroups = _asyncRepository.Where<RoleGroup>(r => r.IsActive);
  190.             ViewBag.RoleGroups = new SelectList(roleGroups,"Id","Name");
  191.             ViewBag.RoleGroup = roleGroups.Where(r => r.Id == employeeDetails.RoleGroupId).FirstOrDefault()?.Name;
  192.             ViewData["Type"] = "Edit";
  193.             return View("AddEdit", employeeDetails);
  194.         }
  195.  
  196.         // POST: EmployeeDetails/Edit/5
  197.         // To protect from overposting attacks, please enable the specific properties you want to bind to, for
  198.         // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
  199.         [HttpPost]
  200.         [Authorize(Roles = AuthConst.ADD_EDIT_EMPLOYEE)]
  201.         [ValidateAntiForgeryToken]
  202.         public async Task<IActionResult> Edit(int id, [Bind("FullName,CitizenshipNo,EmployeeDesignationId,EmployeeSectorId,EmployeeGroupId," +
  203.             "EmployeeLevelId,OfficeId,EmployeeCode,AppointedDate,EnrolledDate,EmploymentDuration,DateOfBirth,TemporaryAddress," +
  204.             "PermanentAddress,PhoneNumber1,PhoneNumber2,BloodGroupId,EmailAddress,Pradesh,DistrictId,RoleGroupId,Username,MunicipalId,WardNumber,StreetName,CreatedBy,CreatedDate,Id,IsActive,OldOfficeId")] EmployeeDetails employeeDetails)
  205.         {
  206.             if (id != employeeDetails.Id)
  207.             {
  208.                 return NotFound();
  209.             }
  210.  
  211.             if (ModelState.IsValid)
  212.             {
  213.                 try
  214.                 {
  215.                     employeeDetails.ModifiedBy = _userManager.GetUserId(HttpContext.User) ?? "ADMIN";
  216.                     employeeDetails.ModifiedDate = DateTime.Now;
  217.                     await _employeeDetailsRepository.UpdateAsync(employeeDetails);
  218.                 }
  219.                 catch (Exception ex)
  220.                 {
  221.                     if (ex.InnerException.ToString().Contains("EmployeeDetails_FullName_DateOfBirth_CitizenshipNo"))
  222.                     {
  223.                         ViewBag.OfficeId = new SelectList(await _officeDetailsRepository.ListAllAsync(), "Id", "Name");
  224.                         ViewBag.BloodGroupId = new SelectList(await _bloodGroupRepository.ListAllAsync(), "Id", "Type");
  225.                         ViewBag.EmployeeDesignationId = new SelectList(await _employeeDesignationDetailsRepository.ListAllAsync(), "Id", "DesignationName");
  226.                         ViewBag.EmployeeGroupId = new SelectList(await _employeeGroupDetailsRepository.ListAllAsync(), "Id", "GroupName");
  227.                         ViewBag.EmployeeLevelId = new SelectList(await _employeeLevelDetailsRepository.ListAllAsync(), "Id", "LevelName");
  228.                         ViewBag.EmployeeSectorId = new SelectList(await _employeeSectorDetailsRepository.ListAllAsync(), "Id", "Name");
  229.                         ViewBag.Pradesh = new SelectList(await _asyncRepository.ToListAsync<StateInfo>(), "Id", "StateNameUniCode");
  230.                         ViewBag.DistrictId = new SelectList(await _asyncRepository.ToListAsync<DistrictInfo>(), "Id", "DistrictNameUniCode");
  231.                         ViewBag.MunicipalId = new SelectList(await _asyncRepository.ToListAsync<MunicipalInfo>(), "Id", "MunicipalNameUnicode");
  232.                         ViewBag.Message = "कर्मचारीको विवरण पहिले राखिसक्नु भयो";
  233.                         return View("AddEdit", employeeDetails);
  234.                     }
  235.                     else if (ex.InnerException.ToString().Contains("EmployeeDetails_CitizenshipNo"))
  236.                     {
  237.                         ViewBag.OfficeId = new SelectList(await _officeDetailsRepository.ListAllAsync(), "Id", "Name");
  238.                         ViewBag.BloodGroupId = new SelectList(await _bloodGroupRepository.ListAllAsync(), "Id", "Type");
  239.                         ViewBag.EmployeeDesignationId = new SelectList(await _employeeDesignationDetailsRepository.ListAllAsync(), "Id", "DesignationName");
  240.                         ViewBag.EmployeeGroupId = new SelectList(await _employeeGroupDetailsRepository.ListAllAsync(), "Id", "GroupName");
  241.                         ViewBag.EmployeeLevelId = new SelectList(await _employeeLevelDetailsRepository.ListAllAsync(), "Id", "LevelName");
  242.                         ViewBag.EmployeeSectorId = new SelectList(await _employeeSectorDetailsRepository.ListAllAsync(), "Id", "Name");
  243.                         ViewBag.Pradesh = new SelectList(await _asyncRepository.ToListAsync<StateInfo>(), "Id", "StateNameUniCode");
  244.                         ViewBag.DistrictId = new SelectList(await _asyncRepository.ToListAsync<DistrictInfo>(), "Id", "DistrictNameUniCode");
  245.                         ViewBag.MunicipalId = new SelectList(await _asyncRepository.ToListAsync<MunicipalInfo>(), "Id", "MunicipalNameUnicode");
  246.                         ViewBag.Message = "यो नागरिकता नं पहिले राखिसक्नु भयो";
  247.                         return View("AddEdit", employeeDetails);
  248.                     }
  249.                     else
  250.                     {
  251.                         throw ex;
  252.                     }
  253.                 }
  254.                 return RedirectToAction(nameof(Index));
  255.             }
  256.             return View("AddEdit", employeeDetails);
  257.         }
  258.  
  259.         public async Task<List<EmployeeSearchResult>> SearchEmployee(int id)
  260.         {
  261.             return await _employeeRepository.SearchEmployee(id);
  262.         }
  263.  
  264.         [HttpPost]
  265.         [Route("/EmployeeDetails/CheckAccountStatus")]
  266.         public async Task<IActionResult> CheckAccountStatus(int id, bool accountstatus)
  267.         {
  268.             try
  269.             {
  270.                 var employee = await _asyncRepository.FindAsync<EmployeeDetails>(id);
  271.  
  272.                 if (accountstatus)
  273.                 {
  274.                    employee.AccountStatus = false;
  275.                 }
  276.                 else
  277.                 {
  278.                     employee.AccountStatus = true;
  279.                 }
  280.                 await _asyncRepository.UpdateAsync(employee);
  281.  
  282.                 return RedirectToAction("Index", "EmployeeDetails");
  283.             }
  284.             catch (Exception ex)
  285.             {
  286.                 throw ex;
  287.             }
  288.  
  289.         }
  290.  
  291.         [HttpPost]
  292.         [Route("/EmployeeDetails/CheckLockedStatus")]
  293.         public async Task<IActionResult> CheckLockedStatus(string userName)
  294.         {
  295.             try
  296.             {
  297.                 var user = _userManager.FindByNameAsync(userName).Result;
  298.                 user.LockoutEnd = null;
  299.                 await _userManager.UpdateAsync(user);
  300.  
  301.                 return RedirectToAction("Index", "EmployeeDetails");
  302.             }
  303.             catch (Exception ex)
  304.             {
  305.                 throw ex;
  306.             }
  307.  
  308.         }
  309.         private bool EmployeeDetailsExists(int id)
  310.         {
  311.             var record = _employeeDetailsRepository.GetByIdAsync(id);
  312.             return record == null ? false : true;
  313.         }
  314.     }
  315. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top