コード例 #1
0
        public ActionResult <EmployeeReadDto> CreateEmployee(EmployeeCreateDto employeeCreateDto)
        {
            var employeeModel = _mapper.Map <EmployeeModel>(employeeCreateDto);

            _repository.CreateEmployee(employeeModel);
            _repository.SaveChanges();
            var employeeReadDto = _mapper.Map <EmployeeReadDto>(employeeModel);

            return(CreatedAtRoute(nameof(GetEmployeeById), new { Id = employeeReadDto.EmployeeId }, employeeReadDto));
        }
コード例 #2
0
        public async Task <ActionResult <EmployeeDetails> > CreateEmployee(EmployeeDetails employee)
        {
            try
            {
                await _repository.CreateEmployee(employee);

                return(CreatedAtAction(nameof(GetEmployeeById), new { id = employee.EmployeeId }, employee));
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status400BadRequest, "Error retrieving all employees data from the database"));
            }
        }
コード例 #3
0
        public async Task <IActionResult> AddAnEmployee([FromBody] EmployeeDTO model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var empId  = "";
            var lastId = 0;

            var dbResult = await _employeeRepo.GetAllEmployees();

            if (dbResult.Count < 1 || dbResult == null)
            {
                empId = "E00001";
            }

            else
            {
                lastId = int.Parse(dbResult[dbResult.Count - 1].EmployeeId.Replace("E0000", ""));
                empId  = $"E0000{++lastId}";
            }

            var newEmployee = new Employee
            {
                EmployeeId = empId,
                FirstName  = model.FirstName,
                LastName   = model.LastName,
                Age        = model.Age,
                Join_Date  = model.Join_Date.ToString("yyyy-MM-dd")
            };

            var result = await _employeeRepo.CreateEmployee(newEmployee);

            if (!result)
            {
                return(BadRequest("Could not add the employee. Try again"));
            }

            return(Ok("Employee has been added successfully"));
        }
コード例 #4
0
 public JsonResult CreateEmployee(EmployeeDTO employee)
 {
     return(Json(_employeeRepo.CreateEmployee(employee), JsonRequestBehavior.AllowGet));
 }