예제 #1
0
        public async Task DeleteByIdAsyncWithWrongIdReturnsError()
        {
            var id   = Guid.NewGuid().ToString();
            var user = new CarpetUser
            {
                Id          = id,
                FirstName   = "Иван",
                LastName    = "Иванов",
                PhoneNumber = "0888777444",
                Email       = "*****@*****.**",
                UserName    = "******",
            };
            var roleAdminId = Guid.NewGuid().ToString();
            var role        = new CarpetRole {
                Id = roleAdminId, Name = GlobalConstants.AdministratorRoleName
            };
            var roleOperatorId = Guid.NewGuid().ToString();
            var roleOperator   = new CarpetRole {
                Id = roleOperatorId, Name = GlobalConstants.OperatorRoleName
            };

            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "DeleteByIdAsyncWithWrongIdReturnsError")
                          .Options;
            var dbContext = new ApplicationDbContext(options);

            dbContext.Users.Add(user);
            dbContext.Roles.Add(role);
            dbContext.Roles.Add(roleOperator);
            await dbContext.SaveChangesAsync();

            var repository = new EfDeletableEntityRepository <Employee>(dbContext);
            var service    = new EmployeesService(repository, dbContext);

            var wrongId = Guid.NewGuid().ToString();

            var employee = new EmployeeCreateInputModel
            {
                Id          = id,
                FirstName   = "Иван",
                LastName    = "Иванов",
                PhoneNumber = "0888777444",
                Salary      = 1000m,
                RoleName    = GlobalConstants.OperatorRoleName,
            };

            var employeeDelete = new EmployeeDeleteInputModel
            {
                Id = wrongId,
            };

            var result = await service.CreateAsync(employee, string.Empty);

            var exception = await Assert.ThrowsAsync <NullReferenceException>(() => service.DeleteByIdAsync(employeeDelete.Id));

            Assert.Equal(string.Format(string.Format(EmployeeConstants.NullReferenceId, wrongId)), exception.Message);
        }
예제 #2
0
        public async Task <IActionResult> Delete(string id, EmployeeDeleteInputModel employeeDelete)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.RedirectToAction(nameof(this.Index)));
            }

            var employee = await this.employeesService.DeleteByIdAsync(employeeDelete.Id);

            return(this.RedirectToAction(nameof(this.Index)));
        }
예제 #3
0
        public async Task <IActionResult> Delete(EmployeeDeleteInputModel input)
        {
            if (this.ModelState.IsValid == false)
            {
                return(this.View(input));
            }

            var companyNameCurrent = this.companiesService.GetById <CompanyViewModel>(input.CompanyId).Name;

            await this.employeesService.Delete(input.Id);

            this.TempData["InfoMessage"] = "Employee successfully deleted!";
            return(this.RedirectToRoute("TaskDotNetCompany", new { name = companyNameCurrent }));
        }