public IActionResult Create(
            string hostName,
            string path,
            string originalUrl,
            string subject,
            string letterLink,
            string note)
        {
            if (!IsUser())
            {
                return(Forbid());
            }

            var request = new ShortUrlCreationRequest
            {
                HostName    = hostName,
                Path        = path,
                OriginalUrl = originalUrl,
                Subject     = subject,
                LetterLink  = letterLink,
                Note        = note,
                UserName    = User.Identity.Name,
            };

            TryValidateModel(request);
            if (ModelState.IsValid)
            {
                int urlId = service.Save(request);

                return(RedirectToAction(actionName: nameof(Show), routeValues: new { id = urlId }));
            }

            return(View(request));
        }
Esempio n. 2
0
        public int Save(ShortUrlCreationRequest request)
        {
            ShortUrlHost host = HostByName(request.HostName);

            if (host == null)
            {
                throw new Exception($"Host name '{request.HostName}' does not exist!");
            }

            var url = new ShortUrl
            {
                HostId      = host.Id,
                Path        = request.Path,
                OriginalUrl = request.OriginalUrl,
                LetterLink  = request.LetterLink,
                Note        = request.Note,
                UserName    = request.UserName,
                Subject     = request.Subject,
                Created     = DateTime.Now
            };

            context.ShortUrls.Add(url);
            context.SaveChanges();

            return(url.Id);
        }
        public IActionResult Create()
        {
            if (!IsUser())
            {
                return(Forbid());
            }

            var request = new ShortUrlCreationRequest
            {
                HostList = service.GetHostList()
            };

            return(View(request));
        }