public async Task <IActionResult> Edit(EmployeeInputModel model)
        {
            if (!this.employeesService.Exists(model.Id))
            {
                return(this.BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(this.RedirectToAction("Error", "Home"));
            }

            EditEmployeeServiceModel serviceModel = new EditEmployeeServiceModel
            {
                Id                  = model.Id,
                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.EditAsync(serviceModel);

            return(this.RedirectToAction("Details", "Employees", new { id = serviceModel.Id }));
        }
Пример #2
0
        public void EditAsync_ReturnsCorrectNumberOfModifiedEntries()
        {
            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();

                Employee employee = new Employee()
                {
                    FirstName           = "Ivan",
                    MiddleName          = "Ivanov",
                    LastName            = "Ivanov",
                    PhoneNumber         = "0897924218",
                    Email               = "*****@*****.**",
                    Town                = "Sofia",
                    Address             = "address 1",
                    ImageUrl            = "aasdfag",
                    OperatingLocationId = operatingLocation.Id,
                    JobPositionId       = jobPosition.Id,
                };

                var employeesService = new EmployeesService(dbContext);
                dbContext.Employees.Add(employee);
                dbContext.SaveChanges();
                var employeeObj = dbContext.Employees.FirstOrDefaultAsync();

                EditEmployeeServiceModel model = new EditEmployeeServiceModel
                {
                    Id        = employeeObj.Result.Id,
                    FirstName = "new name",
                };

                var result = employeesService.EditAsync(model);

                Assert.Equal(1, result.Result);
            }
        }
Пример #3
0
        /// <summary>
        /// Edits the <see cref="Employee"/> using <see cref="EditEmployeeServiceModel"/>.
        /// </summary>
        /// <param name="model">Number of modified entities.</param>
        /// <returns>Service model with <c>Id</c>, <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>.</returns>
        public async Task <int> EditAsync(EditEmployeeServiceModel model)
        {
            Employee employee = this.dbContext.Employees.Find(model.Id);

            employee.JobPositionId       = model.JobPositionId;
            employee.OperatingLocationId = model.OperatingLocationId;
            employee.FirstName           = model.FirstName;
            employee.MiddleName          = model.MiddleName;
            employee.LastName            = model.LastName;
            employee.PhoneNumber         = model.Phone;
            employee.Email    = model.Email;
            employee.Town     = model.Town;
            employee.Address  = model.Address;
            employee.ImageUrl = model.ImageUrl;

            int modifiedEntities = await this.dbContext.SaveChangesAsync();

            return(modifiedEntities);
        }