public async Task <Result> Handle(AddImageCommand request, CancellationToken cancellationToken)
        {
            var individual = await _individualsRepository.FindSingle(request.Id.Value);

            if (individual == null)
            {
                return(Result.NotFound("Couldn't find related resource with identifier"));
            }

            var oldFileName = Path.GetFileNameWithoutExtension(request.FileName);
            var newFileName = $"{individual.FirstName}-{individual.LastName}-{individual.Id}";

            var fileName = request.FileName.Replace(oldFileName, newFileName);

            var directory = Path.Combine(Environment.CurrentDirectory, "Images");
            var filePath  = Path.Combine(directory, fileName);

            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }


            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await request.FileStream.CopyToAsync(stream, cancellationToken);
            }


            individual.SetImage(filePath);
            _individualsRepository.Update(individual);

            return(Result.OK(ResultType.Created));
        }
        public async Task <Result> Handle(SetConnectedIndividualCommand request, CancellationToken cancellationToken)
        {
            var connectedFromIndividual = await _individualsRepository.FindSingle(request.ConnectedFromIndividualId.Value);

            if (connectedFromIndividual == null)
            {
                return(Result.NotFound("Couldn't find related resource with identifier "));
            }

            var connectedToIndividual = await _individualsRepository.FindSingle(request.ConnectedToIndividualId.Value);

            if (connectedToIndividual == null)
            {
                return(Result.NotFound("Couldn't find related resource with identifier "));
            }


            var verifyConnection = await _individualsRepository.VerifyConnection(request.ConnectedFromIndividualId.Value,
                                                                                 request.ConnectedToIndividualId.Value);

            if (verifyConnection != null)
            {
                return(Result.Error(ResultType.BadRequest, "Connection already exists"));
            }

            var individualConnection = new ConnectedIndividual();

            individualConnection.SetIndividualsConnection(connectedFromIndividual, connectedToIndividual, request.ConnectionType.Value);

            connectedFromIndividual.SetConnection(individualConnection);

            _individualsRepository.Update(connectedFromIndividual);

            return(Result.OK(ResultType.Created));
        }
        public async Task <Result> Handle(UpdateIndividualCommand request, CancellationToken cancellationToken)
        {
            var individual = await _individualsRepository.FindSingle(request.Id.Value);

            if (individual == null)
            {
                return(Result.NotFound("Couldn't find resource with provided identifier"));
            }

            var city = await _cityRepository.FindSingle(request.CityId.Value);

            if (city == null)
            {
                return(Result.NotFound($"Couldn't find resource with provided identifier {request.CityId}"));
            }

            individual.UpdateIndividual(request.FirstName, request.LastName, (GenderType)request.Gender, request.PersonalNumber, request.BirthDate, city);

            _individualsRepository.Update(individual);
            return(Result.OK(ResultType.NoContent));
        }
        public async Task <Result> Handle(DeleteImageCommand request, CancellationToken cancellationToken)
        {
            var individual = await _individualsRepository.FindSingle(request.Id.Value);

            if (individual == null)
            {
                return(Result.NotFound("Couldn't find related resource with identifier"));
            }

            var directory = new DirectoryInfo((Path.Combine(Environment.CurrentDirectory, "Images")));

            var files = directory.GetFiles("*" + $"{individual.FirstName}-{individual.LastName}-{individual.Id}" + "*.*");

            foreach (var file in files)
            {
                File.Delete(file.FullName);
            }

            individual.RemoveImage();

            _individualsRepository.Update(individual);

            return(Result.OK(ResultType.NoContent));
        }