//--------------- METHODS ----------------- /// <summary> /// Creates a new <see cref="Employee"/> using the <see cref="CreateEmployeeServiceModel"/>. /// If such <see cref="Employee"/> already exists in the database, fetches it's (string)<c>Id</c> and returns it. /// If such <see cref="Employee"/> doesn't exist in the database, adds it and return it's (string)<c>Id</c>. /// </summary> /// <param name="model">Service model with <c>JobPositionId</c>, <c>OperatingLocationId</c>, <c>FirstName</c>, <c>MiddleName</c>, <c>LastName</c>, <c>Phone</c>, <c>Email</c>, <c>Town</c>, <c>Address</c> and <c>ImageUrl</c></param> /// <returns>Employee ID</returns> public async Task <string> CreateAsync(CreateEmployeeServiceModel model) { // TODO: Add EGN/Social Security Number to Employee table in Database and get employeeId by checking that number, not the names. string employeeId = this.dbContext.Employees.Where(x => x.FirstName == model.FirstName && x.MiddleName == model.MiddleName && x.LastName == model.LastName) .Select(x => x.Id) .FirstOrDefault(); if (employeeId != null) // If employeeId is different than null (string default value), employee with such name already exists, so return it's id. { return(employeeId); } Employee employee = new Employee { JobPositionId = model.JobPositionId, OperatingLocationId = model.OperatingLocationId, FirstName = model.FirstName, MiddleName = model.MiddleName, LastName = model.LastName, PhoneNumber = model.Phone, Email = model.Email, Town = model.Town, Address = model.Address, ImageUrl = model.ImageUrl, }; await this.dbContext.Employees.AddAsync(employee); await this.dbContext.SaveChangesAsync(); return(employee.Id); }
public async Task <IActionResult> Create(EmployeeInputModel model) { this.FillEmployeeInputModel(); if (!this.ModelState.IsValid) { return(this.View("Create", this.employeeInputModel)); } var employee = new CreateEmployeeServiceModel { JobPositionId = model.JobPositionId, OperatingLocationId = model.OperatingLocationId, FirstName = model.FirstName, MiddleName = model.MiddleName, LastName = model.LastName, Phone = model.Phone, Email = model.Email, Town = model.Town, Address = model.Address, ImageUrl = model.ImageUrl, }; await this.employeesService.CreateAsync(employee); return(this.RedirectToAction("Create")); }
public void CreateAsync_ReturnsCorrectEmployeeId() { var options = new DbContextOptionsBuilder <ApplicationDbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString()).Options; using (var dbContext = new ApplicationDbContext(options)) { JobPosition jobPosition = new JobPosition() { Name = "newJobPos", }; dbContext.JobPositions.Add(jobPosition); dbContext.SaveChanges(); OperatingLocation operatingLocation = new OperatingLocation() { Town = "Sofia", Address = "test street", ImageUrl = "kgkkkgk", }; dbContext.OperatingLocations.Add(operatingLocation); dbContext.SaveChanges(); CreateEmployeeServiceModel employee = new CreateEmployeeServiceModel { FirstName = "Ivan", MiddleName = "Ivanov", LastName = "Ivanov", Phone = "0897924218", Email = "*****@*****.**", Town = "Sofia", Address = "address 1", ImageUrl = "aasdfag", OperatingLocationId = operatingLocation.Id, JobPositionId = jobPosition.Id, }; var employeesService = new EmployeesService(dbContext); var result = employeesService.CreateAsync(employee); var employeeObj = dbContext.Employees.FirstOrDefaultAsync(); Assert.Equal(employeeObj.Result.Id, result.Result); } }