示例#1
0
        public async Task <ActionResult> Create([FromForm] WebLinkModifyViewModel webLinkViewModel)
        {
            try
            {
                var webLink = webLinkViewModel.WebLink;
                webLink.Id = Guid.NewGuid();
                webLink.CreatedDateTime   = DateTime.Now;
                webLink.FaviconImageBytes = await LoadFavIcon(webLink.Url);

                _context.WebLinks.Add(webLink);
                _context.SaveChanges();

                //links
                foreach (var category in webLinkViewModel.CategoryItems)
                {
                    if (!category.IsSelected)
                    {
                        continue;
                    }

                    var webLinkCategory = new WebLinkCategory {
                        WebLinkId = webLink.Id, CategoryId = category.Id
                    };
                    _context.WebLinkCategories.Add(webLinkCategory);
                    _context.SaveChanges();
                }

                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(View());
            }
        }
        public ActionResult Create([FromForm] Category category)
        {
            try
            {
                category.Id = Guid.NewGuid();
                category.CreatedDateTime = DateTime.Now;

                _context.Categories.Add(category);
                _context.SaveChanges();

                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(View());
            }
        }
        public IActionResult Post([FromBody] WebLink webLink)
        {
            if (webLink == null)
            {
                return(BadRequest());
            }

            if (ModelState.IsValid)
            {
                if (webLink.Id == Guid.Empty)
                {
                    webLink.Id = new Guid();
                }

                _context.WebLinks.Add(webLink);
                _context.SaveChanges();

                Ok(webLink);
            }

            return(StatusCode(422));
        }
示例#4
0
        public IActionResult Redirect(Guid id)
        {
            var webLink = _context.WebLinks.FirstOrDefault(wl => wl.Id == id);

            if (webLink == null || string.IsNullOrEmpty(webLink.Url))
            {
                return(View("Index"));
            }

            //record the usage
            var usage = new Usage {
                Id = Guid.NewGuid(), CreatedDateTime = DateTime.Now, WebLinkId = id
            };

            _context.Usages.Add(usage);
            _context.SaveChanges();

            //redirect
            return(Redirect(webLink.Url));
        }