Example #1
0
        public IActionResult CreateEmployeeCollections([FromBody] IEnumerable <EmployeeForCreationDto> employeeCollection)
        {
            if (employeeCollection == null)
            {
                return(BadRequest());
            }

            var employeeEntities = Mapper.Map <IEnumerable <Employee> >(employeeCollection);

            foreach (var employee in employeeEntities)
            {
                _appRepository.AddEmployee(employee);
            }

            if (!_appRepository.Save())
            {
                throw new Exception($"Creation of employees failed on save.");
            }

            //return dto
            var employeesToReturn = Mapper.Map <IEnumerable <EmployeeDto> >(employeeEntities);

            var empIds = string.Join(",", employeesToReturn.Select(e => e.EmpId));

            return(CreatedAtRoute("GetEmployeeCollection", new { empIds = empIds }, employeesToReturn));
        }
        public IActionResult CreateEmployee([FromBody] EmployeeForCreationDto employee)
        {
            if (employee == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }
            var employeeEntity = Mapper.Map <Employee>(employee);

            _appRepository.AddEmployee(employeeEntity);

            if (!_appRepository.Save())
            {
                throw new Exception("Employee creation failed on save.");
            }

            var employeeToReturn = Mapper.Map <EmployeeDto>(employeeEntity);

            return(CreatedAtRoute("GetEmployee", new { empId = employeeToReturn.EmpId }, employeeToReturn));
        }