Exemple #1
0
        public IActionResult CreatePersonalRelationship([FromBody] CreatePersonalRelationshipRequest request)
        {
            var validator         = new CreatePersonalRelationshipRequestValidator();
            var validationResults = validator.Validate(request);

            if (!validationResults.IsValid)
            {
                return(BadRequest(validationResults.ToString()));
            }

            try
            {
                _personalRelationshipsUseCase.ExecutePost(request);

                return(CreatedAtAction("CreatePersonalRelationship", "Successfully created personal relationship."));
            }
            catch (Exception e) when(
                e is PersonNotFoundException ||
                e is PersonalRelationshipTypeNotFoundException ||
                e is PersonalRelationshipAlreadyExistsException ||
                e is WorkerNotFoundException
                )
            {
                return(BadRequest(e.Message));
            }
        }
Exemple #2
0
        public void WhenRequestIsNullReturnsErrorsWithMessagesApartFromDetails()
        {
            var badRequest = new CreatePersonalRelationshipRequest();

            var response = createPersonalRelationshipRequestValidator.Validate(badRequest);

            response.IsValid.Should().BeFalse();
            response.Errors.Should().HaveCount(4);
            response.Errors.Should().Contain(e => e.ErrorMessage == "'personId' must be provided.");
            response.Errors.Should().Contain(e => e.ErrorMessage == "'otherPersonId' must be provided.");
            response.Errors.Should().Contain(e => e.ErrorMessage == "'type' must be provided.");
            response.Errors.Should().Contain(e => e.ErrorMessage == "'createdBy' must be provided.");
        }
Exemple #3
0
        public void SetUp()
        {
            _mockDatabaseGateway          = new Mock <IDatabaseGateway>();
            _personalRelationshipsUseCase = new PersonalRelationshipsUseCase(_mockDatabaseGateway.Object);

            _mockDatabaseGateway.Setup(x => x.GetPersonalRelationshipTypeByDescription(_typeInRequest.Description))
            .Returns(_typeInRequest);

            _request = PersonalRelationshipsHelper.CreatePersonalRelationshipRequest(type: _typeInRequest.Description);

            _person      = TestHelpers.CreatePerson(_request.PersonId);
            _otherPerson = TestHelpers.CreatePerson(_request.OtherPersonId);

            _typeInExistingRelationship = PersonalRelationshipsHelper.CreatePersonalRelationshipType("partner");

            _relationship = PersonalRelationshipsHelper.CreatePersonalRelationship(_person, _otherPerson, _typeInExistingRelationship);
            _rel_details  = PersonalRelationshipsHelper.CreatePersonalRelationshipDetail(_relationship.Id, "some details for the relationship");
        }
        public void SetUp()
        {
            _mockDatabaseGateway          = new Mock <IDatabaseGateway>();
            _personalRelationshipsUseCase = new PersonalRelationshipsUseCase(_mockDatabaseGateway.Object);

            _mockDatabaseGateway.Setup(x => x.GetPersonalRelationshipTypeByDescription(_typeInRequest.Description))
            .Returns(_typeInRequest);

            var typeInExistingRelationship = PersonalRelationshipsHelper.CreatePersonalRelationshipType("partner");

            _mockDatabaseGateway.Setup(x => x.GetPersonalRelationshipTypeByDescription(typeInExistingRelationship.Description))
            .Returns(typeInExistingRelationship);

            _request = PersonalRelationshipsHelper.CreatePersonalRelationshipRequest(type: _typeInRequest.Description);

            _person      = TestHelpers.CreatePerson(_request.PersonId);
            _otherPerson = TestHelpers.CreatePerson(_request.OtherPersonId);

            var personalRelationship = PersonalRelationshipsHelper.CreatePersonalRelationship(_person, _otherPerson, typeInExistingRelationship);

            _person.PersonalRelationships = new List <PersonalRelationship>()
            {
                personalRelationship
            };

            _mockDatabaseGateway.Setup(x => x.GetPersonsByListOfIds(It.IsAny <List <long> >()))
            .Returns(new List <Person>()
            {
                _person, _otherPerson
            });

            _mockDatabaseGateway.Setup(x => x.GetPersonWithPersonalRelationshipsByPersonId(It.IsAny <long>(), It.IsAny <bool>()))
            .Returns(_person);

            _mockDatabaseGateway.Setup(x => x.CreatePersonalRelationship(It.IsAny <CreatePersonalRelationshipRequest>()))
            .Returns(personalRelationship);

            _mockDatabaseGateway.Setup(x => x.GetWorkerByEmail(It.IsAny <string>()))
            .Returns(TestHelpers.CreateWorker(email: _request.CreatedBy));
        }
Exemple #5
0
        public Infrastructure.PersonalRelationship CreatePersonalRelationship(CreatePersonalRelationshipRequest request)
        {
            var personalRelationship = new Infrastructure.PersonalRelationship()
            {
                PersonId        = request.PersonId,
                OtherPersonId   = request.OtherPersonId,
                TypeId          = (long)request.TypeId,
                IsMainCarer     = request.IsMainCarer?.ToUpper(),
                IsInformalCarer = request.IsInformalCarer?.ToUpper(),
                StartDate       = _systemTime.Now,
                CreatedBy       = request.CreatedBy,
                Details         = new PersonalRelationshipDetail()
                {
                    Details   = request.Details,
                    CreatedBy = request.CreatedBy
                }
            };

            _databaseContext.PersonalRelationships.Add(personalRelationship);
            _databaseContext.SaveChanges();

            return(personalRelationship);
        }
Exemple #6
0
        public void ExecutePost(CreatePersonalRelationshipRequest request)
        {
            var persons = _databaseGateway.GetPersonsByListOfIds(new List <long>()
            {
                request.PersonId, request.OtherPersonId
            });

            var personDoesNotExist = persons.Find(person => person.Id == request.PersonId) == null;

            if (personDoesNotExist)
            {
                throw new PersonNotFoundException($"'personId' with '{request.PersonId}' was not found.");
            }

            var otherPersonDoesNotExist = persons.Find(person => person.Id == request.OtherPersonId) == null;

            if (otherPersonDoesNotExist)
            {
                throw new PersonNotFoundException($"'otherPersonId' with '{request.OtherPersonId}' was not found.");
            }

            var type = _databaseGateway.GetPersonalRelationshipTypeByDescription(request.Type);

            var typeDoesNotExist = type == null;

            if (typeDoesNotExist)
            {
                throw new PersonalRelationshipTypeNotFoundException($"'type' with '{request.Type}' was not found.");
            }

            var worker             = _databaseGateway.GetWorkerByEmail(request.CreatedBy);
            var workerDoesNotExist = worker == null;

            if (workerDoesNotExist)
            {
                throw new WorkerNotFoundException($"'createdBy' with '{request.CreatedBy}' was not found as a worker.");
            }

            var personWithPersonalRelationships = _databaseGateway.GetPersonWithPersonalRelationshipsByPersonId(request.PersonId);

            var personalRelationships             = personWithPersonalRelationships.PersonalRelationships;
            var personalRelationshipAlreadyExists = personalRelationships.Find(pr => pr.OtherPersonId == request.OtherPersonId && pr.Type.Description == request.Type) != null;

            if (personalRelationshipAlreadyExists)
            {
                throw new PersonalRelationshipAlreadyExistsException($"Personal relationship with 'type' of '{request.Type}' already exists.");
            }

            request.TypeId = type.Id;

            _databaseGateway.CreatePersonalRelationship(request);

            _databaseGateway.CreatePersonalRelationship(new CreatePersonalRelationshipRequest()
            {
                PersonId        = request.OtherPersonId,
                OtherPersonId   = request.PersonId,
                TypeId          = type.InverseTypeId,
                IsMainCarer     = null,
                IsInformalCarer = null,
                Details         = null,
                CreatedBy       = request.CreatedBy
            });
        }