Ejemplo n.º 1
0
        // GET: Account/Edit/420
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var currentUser = await _userManager.GetUserAsync(User);

            var url = await _context.Urls.FirstOrDefaultAsync(u => u.UrlId == id && u.User == currentUser);

            if (url == null)
            {
                return(NotFound());
            }

            var urlVM = new UrlEditViewModel
            {
                UrlId     = (int)id,
                TargetUrl = url.TargetUrl,
                Name      = url.Name
            };


            return(View(urlVM));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Edit([Bind("UrlId,TargetUrl,Name")] UrlEditViewModel urlEditVM)
        {
            var currentUser = await _userManager.GetUserAsync(User);

            var urlToUpdate = await _context.Urls.SingleOrDefaultAsync(u => u.UrlId == urlEditVM.UrlId && u.User == currentUser);

            var createdName = urlEditVM.Name ?? Helper.GenerateRandomUrlName();

            if (await _context.Urls.AnyAsync(u => u.Name == createdName))
            {
                ModelState.AddModelError("Name", $"{createdName} name is already taken. Try different one.");
                return(View());
            }

            try
            {
                if (ModelState.IsValid)
                {
                    urlToUpdate.Name      = createdName;
                    urlToUpdate.TargetUrl = urlEditVM.TargetUrl;

                    _context.Urls.Update(urlToUpdate);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(List)));
                }
                return(View(urlEditVM));
            }
            catch (DbUpdateException)
            {
                ModelState.AddModelError("", "Jakis przypal z baza :/");
            }

            return(RedirectToAction("Index", "Home", new { area = "" }));
        }
Ejemplo n.º 3
0
        public ActionResult UrlEdit(UrlEditViewModel u)
        {
            if (ModelState.IsValid)
            {
                var model = Mapper.Map <Url>(u);
                _mDB.Update(model);
                return(RedirectToAction("UrlIndex"));
            }

            return(View(u));
        }
Ejemplo n.º 4
0
        public virtual ActionResult Edit(string id, UrlEditViewModel model)
        {
            try
            {
                // TODO: Validate that the new key is not in use.

                _documentStore.DatabaseCommands.Patch(
                    model.Id,
                    new[]
                {
                    new PatchRequest
                    {
                        Type  = PatchCommandType.Set,
                        Name  = "Key",
                        Value = model.Key
                    },
                    new PatchRequest
                    {
                        Type  = PatchCommandType.Set,
                        Name  = "Url",
                        Value = model.Url.ToString()
                    },
                    new PatchRequest
                    {
                        Type  = PatchCommandType.Set,
                        Name  = "Updated",
                        Value = DateTimeOffset.UtcNow
                    }
                }
                    );

                return(RedirectToAction(Actions.Index()));
            }
            catch
            {
                return(View());
            }
        }
Ejemplo n.º 5
0
 public KeyValuePair <string, string> Put([FromBody] UrlEditViewModel url)
 {
     return(new KeyValuePair <string, string>(url.Key, url.Value));
 }