public async Task Cancel_OkStatus()
        {
            // Arrange
            _userManager.Setup(x => x.GetUserAsync(It.IsAny <ClaimsPrincipal>())).ReturnsAsync(new User());
            _userManager.Setup(x => x.GetRolesAsync(It.IsAny <User>())).ReturnsAsync(new List <string>()
            {
                "Admin"
            });
            RegionAdministrationDTO admin = new RegionAdministrationDTO()
            {
                ID = 2
            };

            _logger.Setup(x => x.LogInformation(It.IsAny <string>()));

            int id = 0;

            // Act
            var result = await _regionController.Cancel(id);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(200, ((ObjectResult)result).StatusCode);
            Assert.AreEqual("{ message = Річний звіт округи скасовано }", ((ObjectResult)result).Value.ToString());
        }
        public void EditRegionAdministrator_DifferentAdminTypeID_ReturnsCorrect()
        {
            //Arrange
            RegionAdministrationDTO regionAdministrationDTO = new RegionAdministrationDTO();

            regionAdministrationDTO.AdminTypeId = 1;
            _repoWrapper
            .Setup(r => r.RegionAdministration.GetFirstOrDefaultAsync(It.IsAny <Expression <Func <RegionAdministration, bool> > >(),
                                                                      It.IsAny <Func <IQueryable <RegionAdministration>,
                                                                                      IIncludableQueryable <RegionAdministration, object> > >()))
            .ReturnsAsync(regionAdmSecretary);
            _adminTypeService
            .Setup(a => a.GetAdminTypeByIdAsync(It.IsAny <int>()))
            .Returns(() => Task <AdminTypeDTO> .Factory.StartNew(() => AdminTypeHead));
            _repoWrapper
            .Setup(r => r.RegionAdministration.Update(It.IsAny <RegionAdministration>()));
            _repoWrapper
            .Setup(r => r.SaveAsync());
            //Act
            var result = _servise.EditRegionAdministrator(regionAdministrationDTO);

            //Assert
            _repoWrapper.Verify();
            Assert.NotNull(result);
        }
        public async Task Delete_Status200OK()
        {
            // Arrange
            _userManager.Setup(x => x.GetUserAsync(It.IsAny <ClaimsPrincipal>())).ReturnsAsync(new User());
            _userManager.Setup(x => x.GetRolesAsync(It.IsAny <User>())).ReturnsAsync(new List <string>()
            {
                "Admin"
            });
            RegionAdministrationDTO admin = new RegionAdministrationDTO()
            {
                ID = 2
            };

            _logger.Setup(x => x.LogInformation(It.IsAny <string>()));

            _regionAnnualReportService.Setup(x => x.DeleteAsync(It.IsAny <int>()));

            // Act
            var result = await _regionController.Delete(1);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(200, ((ObjectResult)result).StatusCode);
            Assert.AreEqual("{ message = Річний звіт округи видалено }", ((ObjectResult)result).Value.ToString());
        }
        public async Task AddAdministrator_Null_ReturnsNoContentResult()
        {
            // Arrange
            RegionAdministrationDTO admin = null;
            // Act
            var result = await _regionController.AddAdministrator(admin);

            // Assert
            Assert.IsInstanceOf <NoContentResult>(result);
        }
        public async Task AddRegionAdministrator(RegionAdministrationDTO regionAdministrationDTO)
        {
            var adminType = await _adminTypeService.GetAdminTypeByIdAsync(regionAdministrationDTO.AdminTypeId);

            var newRegionAdmin = new RegionAdministration()
            {
                StartDate   = regionAdministrationDTO.StartDate ?? DateTime.Now,
                EndDate     = regionAdministrationDTO.EndDate,
                AdminTypeId = adminType.ID,
                RegionId    = regionAdministrationDTO.RegionId,
                UserId      = regionAdministrationDTO.UserId
            };

            var oldAdmin = await _repoWrapper.RegionAdministration.
                           GetFirstOrDefaultAsync(d => d.AdminTypeId == newRegionAdmin.AdminTypeId &&
                                                  d.RegionId == newRegionAdmin.RegionId && d.Status);

            var newUser = await _userManager.FindByIdAsync(newRegionAdmin.UserId);

            var role = adminType.AdminTypeName == "Голова Округу" ? "Голова Округу" : "Діловод Округу";
            await _userManager.AddToRoleAsync(newUser, role);

            if (oldAdmin != null)
            {
                if (DateTime.Now < newRegionAdmin.EndDate || newRegionAdmin.EndDate == null)
                {
                    newRegionAdmin.Status = true;
                    oldAdmin.Status       = false;
                    oldAdmin.EndDate      = DateTime.Now;
                }
                else
                {
                    newRegionAdmin.Status = false;
                }
                var oldUser = await _userManager.FindByIdAsync(oldAdmin.UserId);

                await _userManager.RemoveFromRoleAsync(oldUser, role);

                _repoWrapper.RegionAdministration.Update(oldAdmin);
                await _repoWrapper.SaveAsync();

                await _repoWrapper.RegionAdministration.CreateAsync(newRegionAdmin);

                await _repoWrapper.SaveAsync();
            }
            else
            {
                newRegionAdmin.Status = true;
                await _repoWrapper.SaveAsync();

                await _repoWrapper.RegionAdministration.CreateAsync(newRegionAdmin);

                await _repoWrapper.SaveAsync();
            }
        }
        public async Task<RegionAdministrationDTO> EditAdministratorAsync(RegionAdministrationDTO adminDTO)
        {
            var adminType = await _adminTypeService.GetAdminTypeByNameAsync(adminDTO.AdminType.AdminTypeName);
            adminDTO.AdminTypeId = adminType.ID;

            var admin = _mapper.Map<RegionAdministrationDTO, RegionAdministration>(adminDTO);

            _repositoryWrapper.RegionAdministration.Update(admin);
            await _repositoryWrapper.SaveAsync();

            return adminDTO;
        }
        public async Task EditAdministrator_Null_ReturnsNoContentResult()
        {
            // Arrange
            RegionAdministrationDTO admin = null;

            _regionAdministrationService.Setup(x => x.EditRegionAdministrator(admin));
            _logger.Setup(x => x.LogError(It.IsAny <string>()));
            // Act
            var result = await _regionController.EditAdministrator(admin);

            // Assert
            Assert.IsInstanceOf <NotFoundResult>(result);
        }
Beispiel #8
0
        public async Task <IActionResult> EditAdministrator(RegionAdministrationDTO admin)
        {
            if (admin != null)
            {
                await _regionAdministrationService.EditRegionAdministrator(admin);

                _logger.LogInformation($"Successful edit Admin: {admin.UserId}");
                return(NoContent());
            }
            _logger.LogError("Admin is null");

            return(NotFound());
        }
        public async Task <RegionAdministrationDTO> AddAdministratorAsync(RegionAdministrationDTO adminDTO)
        {
            var adminType = await _repositoryWrapper.AdminType.GetFirstAsync(r => r.ID == adminDTO.AdminTypeId);

            adminDTO.AdminType = _mapper.Map <AdminType, AdminTypeDTO>(adminType);

            var admin = _mapper.Map <RegionAdministrationDTO, RegionAdministration>(adminDTO);

            _repositoryWrapper.RegionAdministration.Create(admin);
            _repositoryWrapper.Save();

            return(adminDTO);
        }
        public async Task AddAdministrator_CorrectData_ReturnsNoContentResult()
        {
            // Arrange
            RegionAdministrationDTO admin = new RegionAdministrationDTO()
            {
                ID = 2
            };

            _regionAdministrationService.Setup(x => x.AddRegionAdministrator(admin));
            // Act
            var result = await _regionController.AddAdministrator(admin);

            // Assert
            Assert.IsInstanceOf <NoContentResult>(result);
        }
        public async Task EditAdministrator_CorrectData_ReturnsNoContentResult()
        {
            // Arrange
            RegionAdministrationDTO admin = new RegionAdministrationDTO()
            {
                ID = 2
            };

            _regionAdministrationService.Setup(x => x.EditRegionAdministrator(admin));
            _logger.Setup(x => x.LogInformation(It.IsAny <string>()));
            // Act
            var result = await _regionController.EditAdministrator(admin);

            // Assert
            Assert.IsInstanceOf <NoContentResult>(result);
        }
Beispiel #12
0
        public async Task <IActionResult> AddAdministrator(RegionAdministrationDTO admin)
        {
            try
            {
                await _regionAdministrationService.AddAdministratorAsync(admin);

                _logger.LogInformation($"User {{{admin.UserId}}} became admin for region {{{admin.CityId}}}" +
                                       $" with role {{{admin.AdminType.AdminTypeName}}}.");

                return(Ok());
            }
            catch (Exception e)
            {
                _logger.LogError($"Exception :{e.Message}");

                return(BadRequest());
            }
        }
        public async Task GetRegionHead_Int_ReturnsOkObjResult()
        {
            // Arrange
            int id   = 2;
            var head = new RegionAdministrationDTO()
            {
                ID = 2
            };

            _regionAdministrationService.Setup(x => x.GetHead(id)).ReturnsAsync(head);
            // Act
            var result = await _regionController.GetRegionHead(id);

            var actual = (result as ObjectResult).Value as RegionAdministrationDTO;

            // Assert
            Assert.IsInstanceOf <OkObjectResult>(result);
            Assert.IsInstanceOf <RegionAdministrationDTO>((result as ObjectResult).Value);
            Assert.AreEqual(actual.ID, 2);
        }
        public async Task EditRegionAdministrator(RegionAdministrationDTO regionAdministrationDTO)
        {
            var admin = await _repoWrapper.RegionAdministration.GetFirstOrDefaultAsync(a => a.ID == regionAdministrationDTO.ID);

            if (admin != null)
            {
                var adminType = await _adminTypeService.GetAdminTypeByIdAsync(regionAdministrationDTO.AdminTypeId);

                if (adminType.ID == admin.AdminTypeId)
                {
                    admin.StartDate = regionAdministrationDTO.StartDate ?? DateTime.Now;
                    admin.EndDate   = regionAdministrationDTO.EndDate;

                    _repoWrapper.RegionAdministration.Update(admin);
                    await _repoWrapper.SaveAsync();
                }

                else
                {
                    await DeleteAdminByIdAsync(regionAdministrationDTO.ID);
                    await AddRegionAdministrator(regionAdministrationDTO);
                }
            }
        }
Beispiel #15
0
        public async Task <IActionResult> AddAdministrator(RegionAdministrationDTO admin)
        {
            await _regionAdministrationService.AddRegionAdministrator(admin);

            return(NoContent());
        }
Beispiel #16
0
        public async Task AddRegionAdministrator(RegionAdministrationDTO regionAdministrationDTO)
        {
            var adminType = await _adminTypeService.GetAdminTypeByIdAsync(regionAdministrationDTO.AdminTypeId);

            var newRegionAdmin = new RegionAdministration()
            {
                StartDate   = regionAdministrationDTO.StartDate ?? DateTime.Now,
                EndDate     = regionAdministrationDTO.EndDate,
                AdminTypeId = adminType.ID,
                RegionId    = regionAdministrationDTO.RegionId,
                UserId      = regionAdministrationDTO.UserId
            };

            var oldAdmin = await _repoWrapper.RegionAdministration.
                           GetFirstOrDefaultAsync(d => d.AdminTypeId == newRegionAdmin.AdminTypeId &&
                                                  d.RegionId == newRegionAdmin.RegionId && d.Status);

            var newUser = await _userManager.FindByIdAsync(newRegionAdmin.UserId);

            string role;

            switch (adminType.AdminTypeName)
            {
            case Roles.OkrugaHead:
                role = Roles.OkrugaHead;
                break;

            case Roles.OkrugaHeadDeputy:
                role = Roles.OkrugaHeadDeputy;
                break;

            default:
                role = Roles.OkrugaSecretary;
                break;
            }
            await _userManager.AddToRoleAsync(newUser, role);

            if (oldAdmin != null)
            {
                if (DateTime.Now < newRegionAdmin.EndDate || newRegionAdmin.EndDate == null)
                {
                    newRegionAdmin.Status = true;
                    oldAdmin.Status       = false;
                    oldAdmin.EndDate      = DateTime.Now;
                }
                else
                {
                    newRegionAdmin.Status = false;
                }
                var oldUser = await _userManager.FindByIdAsync(oldAdmin.UserId);

                await _userManager.RemoveFromRoleAsync(oldUser, role);

                _repoWrapper.RegionAdministration.Update(oldAdmin);
                await _repoWrapper.SaveAsync();

                await _repoWrapper.RegionAdministration.CreateAsync(newRegionAdmin);

                await _repoWrapper.SaveAsync();
            }
            else
            {
                newRegionAdmin.Status = DateTime.Now < newRegionAdmin.EndDate || newRegionAdmin.EndDate == null;
                await _repoWrapper.SaveAsync();

                await _repoWrapper.RegionAdministration.CreateAsync(newRegionAdmin);

                await _repoWrapper.SaveAsync();
            }
        }