public IHttpActionResult Get(int id)
        {
            EMPLOYEE emp = empService.FindOne(id);

            if (emp == null)
            {
                return(NotFound());
            }
            var config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap <EMPLOYEE, EmpDetailModel>()
                .ForMember(x => x.ADVERTISEMENT, y => y.Ignore())
                .ForMember(x => x.ANNOUNCEMENT, y => y.Ignore())
                .ForMember(x => x.AUTHORITY, y => y.Ignore())
                .ForMember(x => x.SHOP, y => y.Ignore());
            });
            IMapper mapper = config.CreateMapper();
            var     dest   = mapper.Map <EMPLOYEE, EmpDetailModel>(emp);

            dest.EmpbirthStr     = dest.empbirth.ToString("yyyy/MM/dd");
            dest.EmphiredateStr  = dest.hiredate.ToString("yyyy/MM/dd");
            dest.EmpleavedateStr = dest.leavedate != null?dest.leavedate.Value.ToString("yyyy/MM/dd") : "N/A";

            return(Ok(dest));
        }
Example #2
0
        public async Task <ActionResult> DeleteEmp(int id)
        {
            CustomModel.ResultModel customModel = new CustomModel.ResultModel()
            {
                Message = "刪除失敗"
            };
            EmpService empService = new EmpService();
            EMPLOYEE   emp        = empService.FindOne(id);

            var user = await UserManager.FindByNameAsync(emp.empaccount);

            var logins       = user.Logins;
            var rolesForUser = await UserManager.GetRolesAsync(user.Id);

            var appDbContext = HttpContext.GetOwinContext().Get <ApplicationDbContext>();

            using (var transaction = appDbContext.Database.BeginTransaction())
            {
                try
                {
                    foreach (var login in logins.ToList())
                    {
                        await UserManager.RemoveLoginAsync(login.UserId, new UserLoginInfo(login.LoginProvider, login.ProviderKey));
                    }

                    if (rolesForUser.Count() > 0)
                    {
                        foreach (var item in rolesForUser.ToList())
                        {
                            // item should be the name of the role
                            var result = await UserManager.RemoveFromRoleAsync(user.Id, item);
                        }
                    }

                    await UserManager.DeleteAsync(user);

                    empService.Delete(id);

                    transaction.Commit();

                    customModel.Success = true;
                    customModel.Message = "刪除成功";
                    return(Json(customModel));
                }
                catch (DbEntityValidationException ex)
                {
                    logger.Error(GetEntityErrorMsg(ex));
                }
                catch (Exception ex)
                {
                    logger.Error(ex.Message);
                    transaction.Rollback();
                }
                return(Json(customModel));
            }
        }
Example #3
0
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            EmpService empService = new EmpService();
            EMPLOYEE   emp        = empService.FindOne(id);

            var viewModel = empService.GetEditModel(emp);

            return(View(viewModel));
        }
Example #4
0
        public async Task <ActionResult> Edit(EmpEditModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            else
            {
                //var user = UserManager.FindById(User.Identity.GetUserId());
                EmpService empService = new EmpService();
                EMPLOYEE   emp        = empService.FindOne(model.empno);

                var user = await UserManager.FindByNameAsync(emp.empaccount);

                user.Email       = model.EMPEMAIL;
                user.PhoneNumber = model.emptel;
                if (!string.IsNullOrWhiteSpace(model.Newpwd))
                {
                    user.PasswordHash = UserManager.PasswordHasher.HashPassword(model.Newpwd);
                    model.emppwd      = user.PasswordHash;
                }
                using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    try
                    {
                        var result = await UserManager.UpdateAsync(user);

                        if (result.Succeeded)
                        {
                            empService.Update(model);

                            scope.Complete();
                            return(RedirectToAction("Index", "Home", new { area = "Admin" }));
                        }
                    }
                    catch (DbEntityValidationException ex)
                    {
                        logger.Error(GetEntityErrorMsg(ex));
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex.Message);
                        scope.Dispose();
                    }
                }
            }
            return(View(model));
        }