コード例 #1
0
        public async Task AddIdentifierToPerson_NotFoundPerson()
        {
            //Arrange
            var resultGuid = Guid.NewGuid();
            var testModel  = new IdentifierModel {
                Value    = "test",
                PersonId = Guid.NewGuid()
            };
            var identifierRepository = new Mock <IIdentifiersRepository>();

            identifierRepository.Setup(x => x.Add(testModel)).Returns(Task.FromResult(resultGuid));
            var personsRepository = new Mock <IPersonsRepository>();

            personsRepository.Setup(x => x.Get(testModel.PersonId)).Throws(new PersonNotFoundException());
            var unitOfWork = new Mock <IUnitOfWork>();

            unitOfWork.SetupGet(x => x.IdentifiersRepository).Returns(identifierRepository.Object);
            unitOfWork.SetupGet(x => x.PersonsRepository).Returns(personsRepository.Object);
            var dependencyResolver = new Mock <IDependencyResolver>();

            dependencyResolver.Setup(x => x.Resolve <IUnitOfWork>()).Returns(unitOfWork.Object);
            var identifierService = new IdentifierService(dependencyResolver.Object);

            //Act and Assert
            await Assert.ThrowsAsync <PersonNotFoundException>(() => identifierService.AddIdentifierToPerson(testModel));
        }
コード例 #2
0
        public async Task <IActionResult> AddIdentifier(Guid id, [FromBody] IdentifierModel dto)
        {
            if (dto == null)
            {
                return(BadRequest());
            }

            var Identifier = _mapper.Map <Identifier>(dto);
            var person     = _personRepository.Get(id).FirstOrDefault();

            if (person != null)
            {
                //if (_personRepository.IsIdentifierExist(Identifier.IdentifierType.GetEnumDisplayName()))
                if (_personRepository.IsIdentifierExistToPerson(id, Convert.ToInt32(Identifier.IdentifierType)))
                {
                    return(NotFound(new ApiResponse(404, $"IDENTIFIER is already added to person")));
                }

                _personRepository.AddIdentifierToPerson(id, Identifier);
                await _personRepository.SaveChangesAsync();

                return(CreatedAtRoute("GetPerson", new { ID = person.ID }, _mapper.Map <PersonModel>(person)));
            }
            return(BadRequest());
        }
コード例 #3
0
        public async Task AddIdentifierToPerson_Success()
        {
            //Arrange
            var resultGuid = Guid.NewGuid();
            var testModel  = new IdentifierModel {
                Value    = "test",
                PersonId = Guid.NewGuid()
            };
            var identifierRepository = new Mock <IIdentifiersRepository>();

            identifierRepository.Setup(x => x.Add(testModel)).Returns(Task.FromResult(resultGuid));
            var personsRepository = new Mock <IPersonsRepository>();

            personsRepository.Setup(x => x.Get(testModel.PersonId)).Returns(Task.FromResult(new PersonModel()));
            var unitOfWork = new Mock <IUnitOfWork>();

            unitOfWork.SetupGet(x => x.IdentifiersRepository).Returns(identifierRepository.Object);
            unitOfWork.SetupGet(x => x.PersonsRepository).Returns(personsRepository.Object);
            var dependencyResolver = new Mock <IDependencyResolver>();

            dependencyResolver.Setup(x => x.Resolve <IUnitOfWork>()).Returns(unitOfWork.Object);
            var identifierService = new IdentifierService(dependencyResolver.Object);

            //Act
            var id = await identifierService.AddIdentifierToPerson(testModel);

            //Assert
            Assert.Equal(resultGuid, id);
        }
コード例 #4
0
        public IActionResult CreateIdentifier([FromBody] IdentifierModel identifier)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var person = _db.Persons.Include(x => x.Identifiers).FirstOrDefault(x => x.Id.Equals(identifier.PersonId));

            if (person == null)
            {
                return(BadRequest("Person not found"));
            }

            var identifierToAdd = new Identifier
            {
                Value = identifier.Value,
                Type  = identifier.Type
            };


            person.Identifiers.Add(identifierToAdd);
            _db.SaveChanges();

            return(Ok(identifierToAdd));
        }
コード例 #5
0
        public async Task <Guid> Add(IdentifierModel identifier)
        {
            var entity = Mapper.Map <Identifier>(identifier);
            await _dbContext.Identifiers.AddAsync(entity);

            await _dbContext.SaveChangesAsync( );

            return(entity.Id);
        }
コード例 #6
0
        public void TestCreateIdentifier()
        {
            IdentifierModel identifier = new IdentifierModel
            {
                Type     = IdentifierTypes.Email,
                Value    = "Test Value",
                PersonId = _db.Persons.FirstOrDefault().Id
            };

            var result = _controller.CreateIdentifier(identifier);

            ProcessResult(result);
        }
コード例 #7
0
        public async Task <Guid> AddIdentifierToPerson(IdentifierModel identifier)
        {
            using (var unitOfWork = DependencyResolver.Resolve <IUnitOfWork>()) {
                var personExists = await unitOfWork.PersonsRepository.Get(identifier.PersonId) != null;

                if (!personExists)
                {
                    throw new PersonNotFoundException();
                }
                var id = await unitOfWork.IdentifiersRepository.Add(identifier);

                await unitOfWork.SaveChangesAsync();

                return(id);
            }
        }