Exemple #1
0
        public async Task <ActionResult> Post([FromBody] AddLinkModel model)
        {
            if (!Uri.TryCreate(model.Url, UriKind.Absolute, out _))
            {
                return(BadRequest());
            }

            if (!HttpContext.Request.Cookies.TryGetValue("userId", out var userId))
            {
                userId = Guid.NewGuid().ToString();
                HttpContext.Response.Cookies.Append("userId", userId);
            }

            var newLink = new Link
            {
                OriginalLink = model.Url,
                ShortLink    = GenerateShortLink(),
                UserId       = userId
            };

            await _linkService.CreateLink(newLink);

            return(Ok(new
            {
                ShortLink = $"{Request.Host.Value}{Request.Path.Value}/{newLink.ShortLink}"
            }));
        }
Exemple #2
0
 public IActionResult Create([Bind("Id,Order,Url,Description,UserId")] Link link, string tags)
 {
     if (ModelState.IsValid)
     {
         _linksService.AssignTags(link, tags);
         if (!link.LinkTags.Any())
         {
             _linksService.CreateLink(link);
         }
         return(RedirectToAction(nameof(Index)));
     }
     return(View(link));
 }
        public async Task <ActionResult <Link> > PostLink(Link link)
        {
            link = _linksService.CreateLink(link);

            return(CreatedAtAction("GetLink", new { id = link.Id }, link));
        }