public async Task <ActionResult <EmployeeResponse> > PostEmployee(EmployeePostRequest request) { var Employee = _mapper.Map <Employee>(request); await _repository.Create(Employee); return(Ok(Employee.EmployeeId)); }
public ActionResult Post([FromBody] EmployeePostRequest request) { var employee = new Employee { Name = request.Name, Birthdate = request.Birthdate, Gender = request.Gender, JobTitle = request.JobTitle, HireDate = request.HireDate }; _mockEmployeeDatabase.Create(employee); return(new CreatedResult($"api/employees/{employee.Id}", employee)); }
/// <summary> /// Upload image handler /// </summary> /// <param name="model"></param> /// <returns></returns> private string UploadedFile(EmployeePostRequest model) { string uniqueFileName = null; if (model.PhotoPath != null) { string uploadsFolder = Path.Combine(_hostingEnvironment.WebRootPath, "uploads"); uniqueFileName = Guid.NewGuid().ToString() + "_" + model.PhotoPath.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { model.PhotoPath.CopyTo(fileStream); } } return(uniqueFileName); }
public IActionResult Index(EmployeePostRequest model, string id = "") { try { CheckNotNull(model); string photoPath = UploadedFile(model); photoPath = string.IsNullOrEmpty(photoPath) ? CatalogBLL.GetEmployee(model.EmployeeID).PhotoPath : photoPath; Employee employee = new Employee { EmployeeID = model.EmployeeID, LastName = model.LastName, FirstName = model.FirstName, BirthDate = model.BirthDate, Email = model.Email, Address = model.Address, City = model.City, Country = model.Country, HomePhone = model.HomePhone, PhotoPath = photoPath, }; CatalogBLL.UpdateEmployeeProfile(employee); return(RedirectToAction("Index")); } catch (System.Exception ex) { _logger.LogError(ex.Message + ": " + ex.StackTrace); Employee employee = new Employee { EmployeeID = model.EmployeeID, LastName = model.LastName, FirstName = model.FirstName, BirthDate = model.BirthDate, Email = model.Email, Address = model.Address, City = model.City, Country = model.Country, HomePhone = model.HomePhone, }; return(View(employee)); } // return View(employee); }
/// <summary> /// Validate update user profile post request is null /// </summary> /// <param name="model"></param> private void CheckNotNull(EmployeePostRequest model) { if (string.IsNullOrEmpty(model.LastName)) { ModelState.AddModelError("LastName", "Last name expected"); } if (string.IsNullOrEmpty(model.FirstName)) { ModelState.AddModelError("FirstName", "First name expected"); } if (model.BirthDate.Year < 1753 || model.BirthDate.Year > 9999) { ModelState.AddModelError("BirthDate", "BirthDate's year must be between 1753 and 9999"); } if (string.IsNullOrEmpty(model.Email)) { ModelState.AddModelError("Email", "Email expected"); } if (string.IsNullOrEmpty(model.Address)) { model.Address = ""; } if (string.IsNullOrEmpty(model.City)) { model.City = ""; } if (string.IsNullOrEmpty(model.Country)) { model.Country = ""; } if (string.IsNullOrEmpty(model.HomePhone)) { model.HomePhone = ""; } if (ModelState.ErrorCount > 0) { throw new MissingFieldException(); } }
public async Task <ActionResult> HireAnEmployee([FromBody] EmployeePostRequest employeeToHire) { // 1 Make sure they posted a good thing (validation) -> 400 Bad Request // For demo purpose only!!! Thread.Sleep(3000); // pause here for three seconds to simulate a slow(er) API if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // 2 Add it to the database. // a. Map our EmployeePostRequest -> Employee var employee = new Employee { FirstName = employeeToHire.FirstName, LastName = employeeToHire.LastName, Department = employeeToHire.Department, IsActive = true }; // b. Add the employee to the data context. Context.Employees.Add(employee); // c. Save it to the datbase await Context.SaveChangesAsync(); // 3. Create the response. // a. 201 Created Status Code // b. Add Location = url of the new resource (like Http://localhost:1337/employees/12) // c. Just attach a copy of whatever they would get if they called the Location Url var response = new EmployeeDetailsResponse { Id = employee.Id, FirstName = employee.FirstName, LastName = employee.LastName, Department = employee.Department }; return(CreatedAtRoute("employees#getanemployee", new { id = response.Id }, response)); }
public IActionResult Input(EmployeePostRequest model, string id = "") { try { if (string.IsNullOrEmpty(model.LastName)) { ModelState.AddModelError("LastName", "Last name expected"); } if (string.IsNullOrEmpty(model.FirstName)) { ModelState.AddModelError("FirstName", "First name expected"); } if (string.IsNullOrEmpty(model.Title)) { ModelState.AddModelError("Title", "Title expected"); } if (model.BirthDate.Year < 1753 || model.BirthDate.Year > 9999) { ModelState.AddModelError("BirthDate", "BirthDate's year must be between 1753 and 9999"); } if (model.HireDate.Year < 1753 || model.HireDate.Year > 9999) { ModelState.AddModelError("HireDate", "HireDate's year must be between 1753 and 9999"); } if (string.IsNullOrEmpty(model.Email)) { ModelState.AddModelError("Email", "Email expected"); } if (string.IsNullOrEmpty(model.Address)) { model.Address = ""; } if (string.IsNullOrEmpty(model.City)) { model.City = ""; } if (string.IsNullOrEmpty(model.Country)) { model.Country = ""; } if (string.IsNullOrEmpty(model.HomePhone)) { model.HomePhone = ""; } if (string.IsNullOrEmpty(model.Notes)) { model.Notes = ""; } string photoPath = UploadedFile(model); photoPath = string.IsNullOrEmpty(id) ? "" : string.IsNullOrEmpty(photoPath) ? CatalogBLL.GetEmployee(model.EmployeeID).PhotoPath : photoPath; Employee employee = new Employee { EmployeeID = model.EmployeeID, LastName = model.LastName, FirstName = model.FirstName, Title = model.Title, BirthDate = model.BirthDate, HireDate = model.HireDate, Email = model.Email, Address = model.Address, City = model.City, Country = model.Country, HomePhone = model.HomePhone, Notes = model.Notes, PhotoPath = photoPath, }; // TODO: Save input into DB if (model.EmployeeID == 0) { // Set default password & role for new employee employee.Password = _passwordHasher.Hash(LiteCommerce.Common.Constants.DefaultPassword); employee.Roles = WebUserRoles.SALEMAN; CatalogBLL.AddEmployee(employee); } else { CatalogBLL.UpdateEmployee(employee); } return(RedirectToAction("Index")); } catch (System.Exception ex) { _logger.LogError(ex.Message + ": " + ex.StackTrace); ViewData["HeaderTitle"] = string.IsNullOrEmpty(id) ? "Create new employee" : "Edit employee"; return(View(model)); } }