Exemplo n.º 1
0
        public IActionResult Create([FromBody] ShortUrl shortUrl)
        {
            if (!string.IsNullOrEmpty(shortUrl.Slug))
            {
                var url = _shortUrlService.Get(shortUrl.Slug);
                if (url != null)
                {
                    return(Conflict($"Slug '{url.Slug}' is in use"));
                }
            }

            if (string.IsNullOrEmpty(shortUrl.Slug))
            {
                var xeger = new Xeger("[a-z0-9]{5}", new Random());
                shortUrl.Slug = xeger.Generate();
            }

            shortUrl.Slug = shortUrl.Slug.ToLower();

            try
            {
                var res = _shortUrlService.Create(shortUrl);
                return(Ok(res));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(StatusCode(500));
            }
        }
Exemplo n.º 2
0
        public HttpResponseMessage Post(HttpRequestMessage request, ShortUrlDto dto)
        {
            var principal = (ClaimsPrincipal)User;
            var apiKeyId  = int.Parse(principal.Claims.First(x => x.Type == ClaimTypes.Sid).Value);

            dto.ApiKeyId = apiKeyId;

            try
            {
                return(request.CreateResponse(HttpStatusCode.Created, ShortUrlService.Create(dto)));
            }
            catch (Exception)
            {
                return(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }
        }
Exemplo n.º 3
0
        public ActionResult <ShortUrl> Create(ShortUrl shortUrl)
        {
            _shortUrlService.Create(shortUrl);

            return(CreatedAtRoute("GetShortUrl", new { url = shortUrl.Url.ToString() }, shortUrl));
        }