Esempio n. 1
0
 public Shortlink(ShortlinkRequest request, AkaContext context)
 {
     LinkId      = AkaHelper.GetNextAvailableIDFrom(context);
     PrettyName  = request.PrettyName;
     Destination = new Uri(request.DestinationUri);
     Owners      = OwnerHelper.CreateListFrom(request.Owners);
     LastUpdated = DateTime.Now;
 }
Esempio n. 2
0
        public void Get(string pretty)
        {
            _logger.LogInformation($"Shortlink name: {pretty} requested at {DateTime.Now}.");

            if (!string.IsNullOrWhiteSpace(pretty))
            {
                Shortlink sl = AkaHelper.GetShortlinkFromDb(_context, pretty);
                Response.Redirect(sl.Destination.ToString());
            }
            else
            {
                AkaHelper.RedirectToLinkManagement();
            }
        }
Esempio n. 3
0
        public async Task <ActionResult <Shortlink> > PostShortlink([FromForm] ShortlinkRequest?request)
        {
            if (request == null)
            {
                _logger.LogError($"Received a failed shortlink add/post @ {DateTime.Now}");
                return(BadRequest("Shortlink is null."));
            }

            _logger.LogInformation($"Received shortlink creation request: {request}.");

            // if doesn't exist
            Shortlink fromDb = AkaHelper.GetShortlinkFromDb(_context, request.PrettyName);

            if (fromDb == null)
            {
                await _context.Shortlinks.AddAsync(new Shortlink(request, _context));

                await _context.SaveChangesAsync();

                return(CreatedAtAction(nameof(Get), new { pretty = request.PrettyName }, request));
            }

            return(Unauthorized(fromDb));
        }