Beispiel #1
0
        public async Task <IActionResult> Register(Users users)
        {
            if (HttpContext.Session.GetInt32("Login") != 1)
            {
                if (ModelState.IsValid)
                {
                    _context.Add(users);
                    try
                    {
                        await _context.SaveChangesAsync();
                    }
                    catch (Exception ex)
                    {
                        var sqlException = ex.InnerException as System.Data.SqlClient.SqlException;

                        if (sqlException.Number == 2601 || sqlException.Number == 2627)
                        {
                            ViewBag.Message = "Już istnieje taki użytkownik!";
                            return(View());
                        }
                        else
                        {
                            ViewBag.Message = "Bład podczas dodawania do bazy!";
                            return(View());
                        }
                    }
                    ViewBag.Message = users.Email + " Dodany to agregatora!";
                }
                return(View());
            }
            else
            {
                return(RedirectToAction(nameof(Index)));
            }
        }
Beispiel #2
0
        public async Task <IActionResult> Like([Bind("LikeId,UserID,LinkID")] Likes likes, int id)
        {
            var model = _context.Likes.Where(u => u.UserID == HttpContext.Session.GetInt32("UserID") && u.LinkID == id).Any();

            if (model == false)
            {
                likes.LinkID = id;
                likes.UserID = (int)HttpContext.Session.GetInt32("UserID");
                if (ModelState.IsValid)
                {
                    _context.Add(likes);
                    await _context.SaveChangesAsync();

                    return(NoContent());
                }
                ViewData["LinkID"] = new SelectList(_context.Links, "LinkId", "Link", likes.LinkID);
                ViewData["UserID"] = new SelectList(_context.Users, "UserId", "Email", likes.UserID);
                return(NoContent());
            }
            else
            {
                _context.Likes.Remove(await _context.Likes.Where(u => u.UserID == HttpContext.Session.GetInt32("UserID") && u.LinkID == id).FirstAsync());
                await _context.SaveChangesAsync();

                return(NoContent());
            }
        }
        public async Task <IActionResult> Create([Bind("LinkId,Name,Link,Date,UserId")] Links links)
        {
            if (ModelState.IsValid)
            {
                links.Date   = DateTime.Now;
                links.UserId = (int)HttpContext.Session.GetInt32("UserID");
                _context.Add(links);
                await _context.SaveChangesAsync();

                ViewBag.MessageLinkAdd = "Link dodany to agregatora!";
                return(View());
            }
            ViewData["UserId"] = new SelectList(_context.Users, "UserId", "Email", links.UserId);
            return(View());
        }