Beispiel #1
0
        public void GetListByDisplayAttr()
        {
            var funEnumList = BabouEnum <FunEnums> .GetListByDisplayAttr();

            var funStringList = new List <string>()
            {
                "HereDisplay", "Everywhere"
            };

            Assert.Equal(funStringList, funEnumList);
        }
        public async Task <IActionResult> Post([Required][FromHeader] string longUrl, [Optional][FromHeader] Domains?domain, [Optional][FromHeader] string token, [Optional][FromHeader] string emailAddress)
        {
            var userAuthKey = Request.Headers["AuthKey"].ToString();

            try
            {
                ApplicationUser applicationUser;

                if (emailAddress.IsNullOrWhiteSpace())
                {
                    applicationUser = await _context.ApplicationUsers.FirstOrDefaultAsync(x => x.ApiAuthKey == userAuthKey);
                }
                else
                {
                    applicationUser = await _context.ApplicationUsers.FirstOrDefaultAsync(x => x.Email == emailAddress)
                                      ?? await _userService.QuickCreateUser(emailAddress, null, emailAddress, null);
                }

                if (applicationUser == null)
                {
                    _logger.LogWarning("UrlShortenerController: Error finding user with the AutKey {AuthKey}", userAuthKey);
                    return(new BadRequestObjectResult($"Error finding user with the AuthKey {userAuthKey}."));
                }

                if (domain == null)
                {
                    domain = Domains.BabouIo;
                }

                var domainString = domain.Value.GetAttributeOfType <DescriptionAttribute>().Description;

                var allowedDomains = BabouEnum <Domains> .GetListByDescriptionAttr();

                if (!allowedDomains.Contains(domainString))
                {
                    return(new BadRequestObjectResult($"Domain not available. Please choose from {string.Join(", ", allowedDomains)}."));
                }

                var tokenAvailable = false;

                if (!token.IsNullOrWhiteSpace())
                {
                    try
                    {
                        tokenAvailable = await _urlShortenerService.CheckIfTokenIsAvailable(token, domainString);
                    }
                    catch (DuplicateNameException dne)
                    {
                        _logger.LogError(dne, "UrlShortenerController: Token {Token} is already being used by domain {Domain}. Requested by {UserId}.", token, domain, applicationUser.Id);
                        return(new BadRequestObjectResult(dne.Message));
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, "UrlShortenerController: Error while trying to create custom token {Token} for domain {Domain}. Requested by {UserId}.", token, domain, applicationUser.Id);
                        return(new BadRequestObjectResult(ex.Message));
                    }
                }

                var shortenedUrl = tokenAvailable
                    ? await _urlShortenerService.CreateByUserId(applicationUser.Id, longUrl, domainString, token)
                    : await _urlShortenerService.CreateByUserId(applicationUser.Id, longUrl, domainString);


                _logger.LogInformation("UrlShortenerController: New Shortened Url Created for {LongUrl} as {ShortUrl}. Requested by {UserId}.", shortenedUrl.LongUrl, shortenedUrl.ShortUrl, applicationUser.Id);

                return(new OkObjectResult(shortenedUrl.ShortUrl));
            }
            catch (Exception ex)
            {
                var domainString = domain.HasValue ? domain.Value.GetAttributeOfType <EnumMemberAttribute>().Value : "DomainError";
                _logger.LogError(ex, "UrlShortenerController: Error creating short url for {LongUrl}, {Domain} and {Token}. Requested by {AuthKey}", longUrl, domainString, token, userAuthKey);

                return(new BadRequestObjectResult(ex.Message));
            }
        }