예제 #1
0
        public async Task <IActionResult> AddEmployee(AddEmployeeViewModel newEmployee)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Index"));
            }
            var currentUser = await _userManager.GetUserAsync(User);

            var employedUser = await _userManager.FindByIdAsync(newEmployee.UserId);

            if (currentUser == null)
            {
                return(Challenge());
            }
            var employee = new Employee
            {
                FirstName       = newEmployee.FirstName,
                Surname         = newEmployee.Surname,
                SecondName      = newEmployee.SecondName,
                User            = employedUser,
                EmploymentDate  = newEmployee.EmploymentDate,
                ContractEndDate = newEmployee.ContractEndDate,
                CreatedDate     = DateTimeOffset.Now,
                AdedBy          = currentUser,
                BaseMonthSalary = newEmployee.BaseMonthSalary,
                EditedBy        = currentUser
            };

            var successful = await _employeeService
                             .AddEmployeeAsync(employee);

            if (!successful)
            {
                return(BadRequest("Could not add employee."));
            }


            return(RedirectToAction("Index"));
        }
예제 #2
0
        public async Task <IActionResult> AddEmployee([FromBody] AddEmployeeViewModel newEmployee)
        {
            if (!ModelState.IsValid)
            {
                var modelErrors = new List <string>();
                foreach (var modelState in ModelState.Values)
                {
                    foreach (var modelError in modelState.Errors)
                    {
                        modelErrors.Add(modelError.ErrorMessage);
                    }
                }
                return(BadRequest(new AddingResult {
                    Successful = false, Errors = modelErrors
                }));
            }
            var employedUser = await _userManager.FindByIdAsync(userId);

            //if (currentUser == null) return Challenge();
            var employee = new Employee
            {
                Id              = Guid.NewGuid(),
                User            = employedUser,
                EmploymentDate  = newEmployee.EmploymentDate,
                ContractEndDate = newEmployee.ContractEndDate,
                CreatedDate     = DateTimeOffset.Now,
                AddedById       = userId,
                BaseMonthSalary = newEmployee.BaseMonthSalary,
            };

            var successful = await _employeeService.AddEmployeeAsync(employee);

            if (!successful)
            {
                return(BadRequest("Could not add employee."));
            }
            return(Ok(employee));
        }