public IActionResult CreateEmployee(Employee employee) { if (ModelState.IsValid) { var newEmployee = _employeeRepository.AddEmployee(employee); //return RedirectToAction("Employee", new { id = newEmployee.Id }); } return(View()); }
public IActionResult Create(Employee emp) { if (ModelState.IsValid) { employeeRepository.AddEmployee(emp); return(RedirectToAction("Index")); } // else //return View("fail"); return(View()); }
public async Task <ActionResult <Employee> > CreateEmployee(Employee employee) { try { if (employee == null) { return(BadRequest()); } var emp = await employeeRepository.GetEmployeeByEmail(employee.Email); if (emp != null) { ModelState.AddModelError("email", "Employee email is already in use."); return(BadRequest()); } var createdEmployee = await employeeRepository.AddEmployee(employee); // return 201(successfully created), newly created resource // add location header (uri of newly created employee object) return(CreatedAtAction(nameof(GetEmployee), new { id = createdEmployee.EmployeeId }, createdEmployee)); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Error adding data to the database")); } }
public async Task <ActionResult <Employee> > CreateEmployee([FromBody] Employee employee) { try { if (employee == null) { return(BadRequest()); } //Check duplicate email var emailList = await employeeRepository.GetEmployeeByEmail(employee.Email); if (emailList != null) { ModelState.AddModelError("Email", "Email is already exist in database"); return(BadRequest(ModelState)); } //Add employee var result = await employeeRepository.AddEmployee(employee); return(CreatedAtAction(nameof(GetEmployeeById), new { id = result.EmployeeId }, result)); // return Ok(result); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Error in db")); } }
public async Task <ActionResult <Employee> > CreateEmployee(Employee employee) { try { if (employee == null) { return(BadRequest()); } var emp = await _employeeRepository.GetEmployeeByEmail(employee.Email); if (emp != null) { ModelState.AddModelError("email", "Employee email is already in use"); return(BadRequest(ModelState)); } var createdEmployee = await _employeeRepository.AddEmployee(employee); return(CreatedAtAction(nameof(GetEmployee), new { id = createdEmployee.EmployeeId }, createdEmployee)); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Error, unable to create user")); } }
public async Task <ActionResult <Employee> > CreateEmployee([FromBody] Employee employee) { try { if (employee == null) { return(BadRequest()); } var email = await empRepo.GetEmployeeByEmail(employee.Email); if (email != null) { ModelState.AddModelError("email", "Email alredy exist please try anather"); return(BadRequest(ModelState)); } var addedEmployee = await empRepo.AddEmployee(employee); return(CreatedAtAction(nameof(GetEmployee), new { id = addedEmployee.DepartmentId }, addedEmployee)); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Error retrieving data from the database")); } }
public async Task <ActionResult <Employee> > CreateEmployee(Employee employee) { try { if (employee == null) { return(BadRequest()); } else { var empEmail = employeeRepository.GetEmployeeByEmail(employee.Email); if (empEmail != null) { ModelState.AddModelError("email", "Employee email already in user"); return(BadRequest(ModelState)); } var empCreatedObj = await employeeRepository.AddEmployee(employee); return(CreatedAtAction(nameof(GetEmployeByID), new { Id = empCreatedObj.EmployeeId }, empCreatedObj)); } } catch (System.Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Error retrieving data from database")); } }
/// <summary> /// Method to invoke when the AddEmployee command is executed. /// </summary> private void OnAddEmployeeExecute() { var employee = new Employee() { Department = SelectedDepartment }; var typeFactory = TypeFactory.Default; var viewModel = typeFactory.CreateInstanceWithParametersAndAutoCompletion <EmployeeViewModel>(employee); if (!(_uiVisualizerService.ShowDialog(viewModel) ?? false)) { return; } _employeeRepository.AddEmployee(employee); if (employee.Department == SelectedDepartment) { Employees.Add(employee); } MessageMediator.SendMessage(employee.Department, "UpdateSelectedDepartmentFromEM"); Mediator.SendMessage(string.Format("Employee {0} {1} is added in department {2}", employee.FirstName, employee.LastName, employee.Department.Name), "UpdateNotification"); }
public async Task <ActionResult <Employee> > CreateEmployee(Employee employee) { try { if (employee == null) { return(BadRequest()); } //Employeeg email ni baigaa esehiig shalgah var emp = await employeeRepository.GetEmployeeByEmail(employee.Emial); if (emp != null) { ModelState.AddModelError("email", "The employee email is already in use"); return(BadRequest(ModelState)); } //buh yumm hewiin bol employeeg nemeh var result = await employeeRepository.AddEmployee(employee); return(CreatedAtAction(nameof(GetEmployee), new { id = result.EmployeeId }, result)); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Error retrieving data from the database ")); } }
public async Task <AddEmployeeResponse> Handle(AddEmployeeCommand command, CancellationToken cancellationToken) { if (!IsValid(command)) { return(null); } var employee = new Employee(); var password = SetPassword(command.Password); employee = employee.AddEmployee(command.Name, command.Email, password, _notification); if (_notification.HasNotification()) { return(null); } using (var uow = _unitOfWorkManager.Begin()) { await _employeeRepository.AddEmployee(employee); uow.Complete(); } return(EmployeeResponse(employee)); }
public async Task <ActionResult <Employee> > CreateEmployee(Employee employee) { try { if (employee is null) { return(BadRequest()); } var emailCheck = _employeeRepository.GetEmail(employee.Email); if (emailCheck is null) { var createdEmployee = await _employeeRepository.AddEmployee(employee); return(CreatedAtAction(nameof(GetEmployee), new { id = createdEmployee.EmployeeId }, createdEmployee)); } ModelState.AddModelError("Email", "Employee already in use"); return(BadRequest(employee)); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Error creating new employee record")); } }
public async Task <ActionResult <Employee> > CreateEmployee(Employee employee) { //Check if the Email is already used var emp = await employeeRepository.GetEmployeeByEmail(employee.Email); if (emp != null) { ModelState.AddModelError("email", "Employee Email already in use"); return(BadRequest(ModelState)); } if (!ModelState.IsValid) { return(StatusCode(StatusCodes.Status500InternalServerError, "Error Adding new Emp..")); } try { var result = await employeeRepository.AddEmployee(employee); //return StatusCode(StatusCodes.Status201Created, "A new Emp is added.."); return(CreatedAtAction(nameof(GetEmployeeById), new { id = result.EmployeeId }, result)); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Error Adding new Emp..")); } }
public IHttpActionResult PostEmployee(EmployeeDetail data) { var employee = new Employee(); if (!ModelState.IsValid) { return BadRequest(ModelState); } try { employee.Name = data.EmpName; employee.Salary = data.Salary; employee.TaxAmount = GetTaxAmount(data.Salary); employee.Address = data.Address; employee.EmailId = data.EmailId; employee.DateOfBirth = data.DateOfBirth; employee.Gender = data.Gender; employee.PinCode = data.PinCode; employeeRepository.AddEmployee(employee); } catch (Exception) { throw; } return Ok(employee); }
public async Task <ActionResult <Employee> > CreateEmployee(Employee employee) { try { if (employee == null) { return(BadRequest()); } // custom validation. var employeeByEmail = await employeeRepository.GetEmployeeByEmail(employee.Email); if (employeeByEmail != null) { ModelState.AddModelError(nameof(employee.Email), "Employee Email already in use"); return(BadRequest(ModelState)); } var createdEmployee = await employeeRepository.AddEmployee(employee); return(CreatedAtAction(nameof(GetEmployee), new { id = createdEmployee.EmployeeId }, createdEmployee)); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Error creating new employee record")); } }
public ActionResult Create(CreateEmployeeViewModel vm) { if (ModelState.IsValid) { string uniqueFileName = ProcessFileUpload(vm); //string uniqueFileName = null; //if (vm.Photo != null) //{ // string uploadFolder =Path.Combine(_hostingEnvironment.WebRootPath, "images"); // uniqueFileName = Guid.NewGuid().ToString() + "_" + vm.Photo.FileName; // string filePath = Path.Combine(uploadFolder, uniqueFileName); // vm.Photo.CopyTo(new FileStream(filePath, FileMode.Create)); //} Employee newEmployee = new Employee { Name = vm.Name, Email = vm.Email, Department = vm.Department, PhotoPath = uniqueFileName }; _employeeRepository.AddEmployee(newEmployee); return(RedirectToAction("Details", new { id = newEmployee.Id })); //Employee newEmployee = _employeeRepository.AddEmployee(obj); //return RedirectToAction("Details", new { id = newEmployee.Id }); } return(View()); }
public EmployeeMutation(IEmployeeRepository employeeRepository, IDbContextScopeFactory dbContextScope) { _authUtility = new AuthenticationUtility(); FieldAsync <EmployeeType>("addEmployee", arguments: new QueryArguments { new QueryArgument <NonNullGraphType <AddEmployeeType> > { Name = "employeeData" } }, resolve: async context => { if (!_authUtility.ValidateContext(context)) { throw new ExecutionError("ErrorCode: UNAUTHORIZED_USER, Message: 401 Unautherized error."); } AddEmployeeDataModel employeeData = context.GetArgument <AddEmployeeDataModel>("employeeData"); using (dbContextScope.Create(DbContextScopeOption.ForceCreateNew)) { return(await employeeRepository.AddEmployee(employeeData)); } }); }
public Employee AddEmployee(Employee newEmployee) { try { if (newEmployee.FirstName == null) { throw new NullReferenceException(); } var _employeedata = _mapper.Map <EmployeeData>(newEmployee); _employeedata = _employeeRepository.AddEmployee(_employeedata); return(_mapper.Map <Employee>(_employeedata)); } catch (NullReferenceException ex) { _logger.LogError("Employee FirstnameName is Null. ErrorCode: {0}, ExceptionMessage: {1}", 1011, ex.ToString()); throw new ServiceException("Employee FirstnameName is Null", ex, 1011); } catch (Exception ex) { _logger.LogError("Unable to Add Employee. ErrorCode: {0}, ExceptionMessage: {1}", 1010, ex.ToString()); throw new ServiceException("Unable to Add Employee.", ex, 1010); } }
public async Task <ActionResult <Employee> > CreateEmployee(Employee employee) { try { if (employee == null) { return(NotFound()); } var exisitingEmployee = await _employeeRepository.GetEmployeeByEmail(employee.Email); if (exisitingEmployee != null) { ModelState.AddModelError("Email", "Employee Email is already in use."); return(BadRequest(ModelState)); } var createdEmployee = await _employeeRepository.AddEmployee(employee); return(CreatedAtAction(nameof(GetEmployee), new { id = createdEmployee.EmployeeId }, createdEmployee)); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Error while creating the employee in the database.")); } }
public async Task <ActionResult <EmployeeDto> > AddEmployee([FromBody] EmployeeForCreationDto employeeForCreation) { var validationResults = new EmployeeForCreationDtoValidator().Validate(employeeForCreation); validationResults.AddToModelState(ModelState, null); if (!ModelState.IsValid) { return(BadRequest(new ValidationProblemDetails(ModelState))); //return ValidationProblem(); } var employee = _mapper.Map <Employee>(employeeForCreation); await _employeeRepo.AddEmployee(employee); var saveSuccessful = await _employeeRepo.SaveAsync(); if (saveSuccessful) { var employeeFromRepo = await _employeeRepo.GetEmployeeAsync(employee.Id); var employeeDto = _mapper.Map <EmployeeDto>(employeeFromRepo); var response = new Response <EmployeeDto>(employeeDto); return(CreatedAtRoute("GetEmployee", new { employeeDto.Id }, response)); } return(StatusCode(500)); }
public void SaveEmployee(EmployeeEntity employeeEntity) { try { if (employeeEntity.Id > 0) { EmployeeEntity employeeEntity_saved = EmployeeRepository.GetEmployeeById(employeeEntity.Id); employeeEntity_saved.FirstName = employeeEntity.FirstName; employeeEntity_saved.LastName = employeeEntity.LastName; employeeEntity_saved.Country = employeeEntity.Country; employeeEntity_saved.City = employeeEntity.City; employeeEntity.ModifiedBy = System.Environment.UserName; employeeEntity.ModifiedDate = DateTime.Now; } else { employeeEntity.CreatedBy = System.Environment.UserName; employeeEntity.ModifiedBy = System.Environment.UserName; employeeEntity.CreatedDate = DateTime.Now; employeeEntity.ModifiedDate = DateTime.Now; EmployeeRepository.AddEmployee(employeeEntity); } EmployeeRepository.SaveChanges(); } catch (Exception) { throw; } }
public async Task <ActionResult <Employee> > CreateEmployee(Employee employee) { try { if (employee == null) { return(BadRequest()); } // Add custom model validation error var emp = employeeRepository.GetEmployeeByEmail(employee.Email); if (emp != null) { ModelState.AddModelError("email", "Employee email already in use"); return(BadRequest(ModelState)); } var createdEmployee = await employeeRepository.AddEmployee(employee); return(CreatedAtAction(nameof(GetEmployee), new { id = createdEmployee.EmployeeId }, createdEmployee)); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Error retrieving data from the database")); } }
public void AddEmployee(Employee emp) { if (emp != null) { _employeeRepository.AddEmployee(emp); } }
public void AddEmployeeShouldAddNewEmployeeWhenDoesNotExist() { _employeeRepository.AddEmployee(new EmployeeEntity { FirstName = "New", LastName = "Employee", StartDate = new DateTime(2013, 01, 25) }); Assert.AreEqual(4, _employeeRepository.GetAllEmployees().Count); }
public IActionResult Create(EmployeeCreateViewModel model) { if (ModelState.IsValid) { string uniqueFileName = null; if (model.Photos != null && model.Photos.Count() > 0) { foreach (IFormFile Photo in model.Photos) { string uploadsFolder = Path.Combine(webHostEnvironment.WebRootPath, "images"); uniqueFileName = Guid.NewGuid().ToString() + "_" + Photo.FileName; string pathFile = Path.Combine(uploadsFolder, uniqueFileName); Photo.CopyTo(new FileStream(pathFile, FileMode.Create)); } } Employee newEmp = new Employee { Name = model.Name, Email = model.Email, Department = model.Department, PhotoPath = uniqueFileName }; _mockEmployeeRepository.AddEmployee(newEmp); return(RedirectToAction("Details", new { id = newEmp.Id })); } return(View()); }
public IActionResult Create(EmployeeCreateViewModel model) { if (ModelState.IsValid) { string uniqueFileName = null; { string uploadFolder = Path.Combine(_hostingEnvironment.WebRootPath, "images"); uniqueFileName = Guid.NewGuid().ToString() + "_" + model.Photo.FileName.Split(@"\").Last(); string filePath = Path.Combine(uploadFolder, uniqueFileName); model.Photo.CopyTo(new FileStream(filePath, FileMode.Create)); } Employee newEmployee = new Employee { Name = model.Name, Email = model.Email, Department = model.Department, Image = uniqueFileName }; newEmployee = _employeeRepository.AddEmployee(newEmployee); return(RedirectToAction("details", new { id = newEmployee.Id })); } return(View()); }
public async Task <IActionResult> Add([FromBody] Employee employee) { ISingleModelResponse <Employee> response = new SingleModelResponse <Employee>(); try { bool isresult = await _employeeRepository.isEmpCodeExists(employee.EmpCode); if (isresult) { response.IsError = true; response.Message = "Employee Code is already exist"; return(Ok(response)); } Employee emp = await _employeeRepository.AddEmployee(employee); response.Message = "Employee added successfully"; response.Model = employee; } catch (Exception ex) { _logger.LogError(entities.LoggingEvents.InsertItem, ex, "Error while adding a new employee, Request: {0}", employee); response.IsError = true; response.ErrorMessage = "Could not add employee"; return(BadRequest(response)); } return(Ok(response)); }
public IActionResult CreateEmployee([FromBody] Employee employee) { if (employee == null) return BadRequest(); if (employee.FirstName == string.Empty || employee.LastName == string.Empty) { ModelState.AddModelError("Name/FirstName", "The name or first name shouldn't be empty"); } if (!ModelState.IsValid) return BadRequest(ModelState); string currentUrl = _httpContextAccessor.HttpContext.Request.Host.Value; if(!string.IsNullOrEmpty(employee.ImageName)) { var path = $"{_webHostEnvironment.WebRootPath}{Path.DirectorySeparatorChar}uploads{Path.DirectorySeparatorChar}{employee.ImageName}"; using var fileStream = System.IO.File.Create(path); fileStream.Write(employee.ImageContent, 0, employee.ImageContent.Length); fileStream.Close(); employee.ImageName = $"https://{currentUrl}/uploads/{employee.ImageName}"; } var createdEmployee = _employeeRepository.AddEmployee(employee); return Created("employee", createdEmployee); }
/// <summary> /// Add product to the database on click /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void buttonAddProduct_Click(object sender, EventArgs e) { try { string firstName = textBox1.Text; string lastName = textBox2.Text; string email = textBox3.Text; string phoneNumber = textBox4.Text; decimal salary = decimal.Parse(textBox5.Text); string dept = textBox7.Text; DateTime hiredate = dateTimePicker1.Value; int deptid = employer.GetDeptId(dept); employer.AddEmployee(firstName, lastName, email, phoneNumber, hiredate, salary, deptid); textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; textBox4.Text = ""; textBox5.Text = ""; textBox7.Text = ""; var temp = employer.GetAllEmployee(); var temp1 = new List <string>(); foreach (var item in temp) { temp1.Add($"{item.First_Name}, {item.Last_Name}"); } listBoxProduct.DataSource = temp1; } catch (Exception ex) { MessageBox.Show("Incomplete Information Provided"); } }
public async Task <ActionResult <Employee> > CreateEmployee(Employee employee) { await Task.Run(() => empRepo.AddEmployee(employee)); //return CreatedAtAction("GetTodoItem", new { id = todoItem.Id }, todoItem); return(CreatedAtAction(nameof(CreateEmployee), new { id = employee.Id }, employee)); }
public IActionResult AddEmployee([FromBody] AddEmployeeRequest addEmployee) { addEmployeeValidator.ValidateAndThrow(addEmployee); var employeeId = employeeRepository.AddEmployee(addEmployee); return(Ok(employeeId)); }