public EmployeeDto AddAmployee(EmployeeForAddDto emp)
        {
            Employee e = mapper.Map <Employee>(emp);

            this.employeeRepository.AddEmployee(e);
            this.employeeRepository.SaveChanges();
            EmployeeDto empdto = mapper.Map <EmployeeDto>(e);

            return(empdto);
        }
Esempio n. 2
0
 public static Employee MapFromEmployeeForAddDto(this EmployeeForAddDto emp)
 {
     if (emp != null)
     {
         return(new Employee
         {
             EmpCode = emp.EmpCode,
             Fname = emp.Fname,
             Lname = emp.Lname,
             IdCard = emp.IdCard,
             IsActive = 1
         });
     }
     return(null);
 }
        public async Task <IActionResult> AddEmployee(EmployeeForAddDto emp)
        {
            if (await _repo.EmployeeExists(emp.EntityCode, emp.EmpCode))
            {
                ModelState.AddModelError("Employee", "Employee is duplicate (EntityCode + EmpCode)");
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            Employee employeeToAdd = emp.MapFromEmployeeForAddDto();
            await _repo.AddEmployee(employeeToAdd);

            return(StatusCode(201));
        }
        public async Task <IActionResult> Add(EmployeeForAddDto employeeForAdd)
        {
            if (!int.TryParse(User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? string.Empty, out var userId))
            {
                return(Unauthorized());
            }

            var privilages = new List <int>()
            {
                (int)UserPrivileges.Administrator
            };

            int.TryParse(User.FindFirst(ClaimTypes.Role)?.Value, out var privilagesId);

            if (!privilages.Contains(privilagesId))
            {
                return(StatusCode((int)HttpStatusCode.Unauthorized));
            }

            if (await _authRepository.UserExists(employeeForAdd.Username))
            {
                return(BadRequest("Username already exists"));
            }

            if (await _authRepository.EmailExists(employeeForAdd.Email))
            {
                return(BadRequest("Email already registered"));
            }

            var employee = _mapper.Map <User>(employeeForAdd);

            employee.Privileges  = (int)UserPrivileges.Employee;
            employee.IsConfirmed = true;

            var addedEmployee = await _authRepository.Register(employee, employeeForAdd.Password);

            return(addedEmployee != null?Ok() : StatusCode((int)HttpStatusCode.InternalServerError));
        }
Esempio n. 5
0
 public IActionResult PostEmployee([FromBody] EmployeeForAddDto employee)
 {
     return(Created("", this.employeeService.AddAmployee(employee)));
 }