Example #1
0
 public override void Save(EmployeeManageDto model)
 {
     if (model.Id == 0)
     {
         Create(model);
     }
     else
     {
         Update(model);
     }
 }
        public async System.Threading.Tasks.Task <IActionResult> Save(EmployeeManageDto model)
        {
            if (!ModelState.IsValid)
            {
                //დროპდაუნების ჩამატება თუ დამჭირდება
                //_employeeAppService.FillManageDtoWithInitialData(model);
                return(View("Manage", model));
            }
            else
            {
                _employeeAppService.Save(model);
            }

            return(RedirectToAction("index"));
        }
Example #3
0
        protected override void Update(EmployeeManageDto model)
        {
            var dboEmployee = _unitOfWork.EmployeeRepo.Set()
                              .First(a => a.Id == model.Id);

            dboEmployee.Name        = model.Name;
            dboEmployee.SurName     = model.SurName;
            dboEmployee.Nationality = model.Nationality;
            dboEmployee.BirthDate   = model.BirthDate;
            dboEmployee.PersonalId  = model.PersonalId;
            dboEmployee.Salary      = model.Salary;
            dboEmployee.Currency    = model.Currency;

            _unitOfWork.EmployeeRepo.Update(dboEmployee);
            _unitOfWork.Save();
        }
Example #4
0
        public override EmployeeManageDto GetViewModel(int id)
        {
            var model = new EmployeeManageDto();

            if (id == 0)
            {
                return(model);
            }

            var dboEmployee = _unitOfWork.EmployeeRepo.Set()
                              .First(a => a.Id == id);

            model.Id          = dboEmployee.Id;
            model.BirthDate   = dboEmployee.BirthDate;
            model.Currency    = dboEmployee.Currency;
            model.Name        = dboEmployee.Name;
            model.SurName     = dboEmployee.SurName;
            model.Salary      = dboEmployee.Salary;
            model.PersonalId  = dboEmployee.PersonalId;
            model.Nationality = dboEmployee.Nationality;

            return(model);
        }
Example #5
0
        protected override void Create(EmployeeManageDto model)
        {
            var employee = new HTO.EntityFrameworkCore.Entities.Employee
            {
                Name        = model.Name,
                SurName     = model.SurName,
                Nationality = model.Nationality,
                BirthDate   = model.BirthDate,
                PersonalId  = model.PersonalId,
                Salary      = model.Salary,
                Currency    = model.Currency
            };

            //foreach (var phone in model.PhoneNumbers)
            //{
            //    var number = new HTO.EntityFrameworkCore.Entities.Phone
            //    {
            //        Number = phone
            //    };
            //}

            _unitOfWork.EmployeeRepo.Insert(employee);
            _unitOfWork.Save();
        }
Example #6
0
 public override void FillManageDtoWithInitialData(EmployeeManageDto model)
 {
     //დროპდაუნების ჩამატება თუ დამჭირდება
     throw new NotImplementedException();
 }
        public async Task <IActionResult> EditEmployee([FromRoute] string userId, [FromBody] EmployeeManageDto dto)
        {
            EmployeeManageDtoValidator validator = new EmployeeManageDtoValidator();
            ValidationResult           result    = await validator.ValidateAsync(dto);

            if (result.IsValid)
            {
                var entity = await _dbContext.Employees
                             .Include(x => x.ApplicationUser)
                             .SingleOrDefaultAsync(x => x.ApplicationUserId == userId);

                #region 驗證重複

                if (entity.ApplicationUser.UserName != dto.UserName)
                {
                    if (await _dbContext.Users.AnyAsync(x => x.UserName == dto.UserName))
                    {
                        result.Errors.Add(new ValidationFailure("userName", "使用者名稱已經被使用"));
                        return(BadRequest(result.Errors));
                    }
                }

                if (entity.ApplicationUser.Email != dto.Email)
                {
                    if (await _dbContext.Users.AnyAsync(x => x.Email == dto.Email))
                    {
                        result.Errors.Add(new ValidationFailure("email", "電子郵件已經被使用"));
                        return(BadRequest(result.Errors));
                    }
                }

                if (entity.ApplicationUser.PhoneNumber != dto.PhoneNumber && !string.IsNullOrEmpty(dto.PhoneNumber))
                {
                    if (await _dbContext.Users.AnyAsync(x => x.PhoneNumber == dto.PhoneNumber))
                    {
                        result.Errors.Add(new ValidationFailure("phoneNumber", "手機號碼已經被使用"));
                        return(BadRequest(result.Errors));
                    }
                }

                if (entity.ApplicationUser.NationalId != dto.NationalId && !string.IsNullOrEmpty(dto.NationalId))
                {
                    if (await _dbContext.Users.AnyAsync(x => x.NationalId == dto.NationalId.ToUpper()))
                    {
                        result.Errors.Add(new ValidationFailure("nationalId", "身份證字號已經被使用"));
                        return(BadRequest(result.Errors));
                    }
                }

                if (entity.NetworkId != dto.NetworkId)
                {
                    if (await _dbContext.Administrators.AnyAsync(x => x.NetworkId == dto.NetworkId.ToUpper()))
                    {
                        result.Errors.Add(new ValidationFailure("networkId", "證號已經被使用"));
                        return(BadRequest(result.Errors));
                    }
                }

                #endregion

                var updateEntity = _mapper.Map(dto, entity);

                await using (var transaction = await _dbContext.Database.BeginTransactionAsync())
                {
                    try
                    {
                        if (entity.ApplicationUser.UserName != dto.UserName)
                        {
                            if (await _userManager.ReplaceClaimAsync(entity.ApplicationUser, new Claim(ClaimTypes.Name, entity.ApplicationUser.UserName), new Claim(ClaimTypes.Name, dto.UserName)) != IdentityResult.Success)
                            {
                                throw new DbUpdateException();
                            }
                        }

                        if (entity.ApplicationUser.Email != dto.Email)
                        {
                            if (await _userManager.ReplaceClaimAsync(entity.ApplicationUser, new Claim(ClaimTypes.Email, entity.ApplicationUser.Email), new Claim(ClaimTypes.Email, dto.Email)) != IdentityResult.Success)
                            {
                                throw new DbUpdateException();
                            }
                        }

                        #region UpdateSecurity

                        var oldSecurityStamp = entity.ApplicationUser.SecurityStamp;
                        if (await _userManager.UpdateSecurityStampAsync(entity.ApplicationUser) != IdentityResult.Success)
                        {
                            throw new DbUpdateException();
                        }
                        if (await _userManager.ReplaceClaimAsync(entity.ApplicationUser, new Claim(ClaimTypes.Sid, oldSecurityStamp), new Claim(ClaimTypes.Sid, entity.ApplicationUser.SecurityStamp)) != IdentityResult.Success)
                        {
                            throw new DbUpdateException();
                        }

                        #endregion

                        _dbContext.Employees.Update(updateEntity);
                        if (await _dbContext.SaveChangesAsync() < 0)
                        {
                            throw new DbUpdateException();
                        }

                        await transaction.CommitAsync();
                    }
                    catch (DbUpdateException)
                    {
                        await transaction.RollbackAsync();

                        throw;
                    }
                }

                var routeValues = new { userId = entity.ApplicationUserId };
                var returnDto   = _mapper.Map <EmployeeDto>(updateEntity);
                return(CreatedAtAction(nameof(GetEmployee), routeValues, returnDto));
            }
            return(BadRequest(result.Errors));
        }