Beispiel #1
0
        private async Task <string> CreateUrlMapping(UrlMappingEntity entity)
        {
            var beforeEntity = await _urlMappingRepository.FindAndExtend(entity);

            if (beforeEntity != null)
            {
                return(beforeEntity.ShortLink);
            }

            await _urlMappingRepository.AddAsync(entity);

            return(entity.ShortLink);
        }
Beispiel #2
0
        public async Task <string> Handle(CreateUrlMappingCommand request, CancellationToken cancellationToken)
        {
            ValidationResult results = _validator.Validate(request);

            if (!results.IsValid)
            {
                throw new CustomValidationException(results.Errors.First().ErrorMessage);
            }

            var    attempts = 0;
            string _out;

            do
            {
                var code = await _codeService.GenerateCodeAsync();

                var entity = new UrlMappingEntity(
                    code,
                    Path.Combine(clientEndpoint, code),
                    request.LongUrl,
                    _dateProvider.Now.AddMinutes(intervalMinutes));

                try
                {
                    attempts++;
                    _out = await CreateUrlMapping(entity);

                    break;
                }
                catch (Exception ex)
                {
                    if (attempts == 3)
                    {
                        throw new CreateUrlMappingException("Error: create url mapping service not available", ex);
                    }
                }
            } while (true);

            return(_out);
        }