Пример #1
0
        public async Task <IActionResult> PutEntry(int id, Entry entry)
        {
            if (id != entry.Id)
            {
                return(BadRequest());
            }

            _context.Entry(entry).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!EntryExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            catch (DbUpdateException e)
            {
                return(ValidationProblem(new ValidationProblemDetails(new Dictionary <string, string[]>()
                {
                    { "Link", new string[] { "Model with this Shortcut already exists" } }
                })));
            }

            return(NoContent());
        }
Пример #2
0
        public async Task PopulateData(int amount)
        {
            if (_shortenerContext.Shortcuts.Count() >= amount)
            {
                throw new Exception("You have already populated data. Comment PopulateData method.");
            }

            var redirectFaker = new Faker <Redirect>()
                                .RuleFor(a => a.Url, f => f.Internet.Url());

            var redirectExtendedFaker = new Faker <RedirectExtended>()
                                        .RuleFor(a => a.Url, f => f.Internet.Url());

            var shortcutFakerOne = new Faker <Shortcut>()
                                   .RuleFor(a => a.Alias, f => f.Random.AlphaNumeric(new Random().Next(5, 30)))
                                   .RuleFor(a => a.TimesRedirect, f => f.Random.Long(min: 0, max: 999999999));


            for (var i = 0; i < amount; i++)
            {
                if (i % 2 == 0)
                {
                    Redirect redirect = redirectFaker.Generate();
                    Shortcut shortcut = shortcutFakerOne.Generate();

                    redirect.Shortcut = shortcut;
                    shortcut.Redirect = redirect;

                    await _shortenerContext.AddAsync(redirect);

                    await _shortenerContext.AddAsync(shortcut);
                }
                else
                {
                    RedirectExtended redirectExtended = redirectExtendedFaker.Generate();
                    Shortcut         shortcut         = shortcutFakerOne.Generate();

                    redirectExtended.Shortcut = shortcut;
                    shortcut.RedirectExtended = redirectExtended;

                    await _shortenerContext.AddAsync(redirectExtended);

                    await _shortenerContext.AddAsync(shortcut);
                }
            }

            await _shortenerContext.SaveChangesAsync();
        }
Пример #3
0
        public async Task <IActionResult> Shorten([FromBody] string url)
        {
            // TODO: Move validation somewhere else
            // Ensures URL is valid
            if (Uri.TryCreate(url, UriKind.Absolute, out Uri uri) &&
                uri.IsWellFormedOriginalString() &&
                (uri.Scheme == Uri.UriSchemeHttps || uri.Scheme == Uri.UriSchemeHttp))
            {
                string key = _keyGenerator.GetKey(6);

                _shortenerContext.Add(new ShortenedUrl()
                {
                    Key = key, Url = url
                });
                await _shortenerContext.SaveChangesAsync();

                return(Ok(new { key }));
            }

            return(BadRequest(new { error = "Invalid URL" }));
        }
Пример #4
0
 public async Task <int> SaveChangesAsync()
 {
     return(await _shortenerContext
            .SaveChangesAsync());
 }