public async Task <IActionResult> Create([Bind("Title, Content, UserId, CityId, Date")] Proposition proposition)
        {
            var cities = await _context.Cities.ToListAsync();

            ViewData["Cities"] = cities;

            var user = GetUser();

            try
            {
                if (ModelState.IsValid)
                {
                    proposition.UserId = user.Id;
                    proposition.Date   = DateTime.Now;
                    _context.Add(proposition);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction("Index", "Proposition"));
                }
            }
            catch (DbUpdateException)
            {
                ModelState.AddModelError("", "Unable to save changes. " +
                                         "Try again, and if the problem persists " +
                                         "see your system administrator.");
            }
            return(View(proposition));
        }
        public async Task <IActionResult> CreateComment(int propositionId, [Bind("Content, PublicationDate, UserId, PropositionId")] Comment comment)
        {
            var user = GetUser();

            try
            {
                if (ModelState.IsValid)
                {
                    comment.PublicationDate = DateTime.Now;
                    comment.UserId          = user.Id;
                    comment.PropositionId   = propositionId;
                    _context.Add(comment);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction("Details", "Proposition", new { id = propositionId }));
                }
            }
            catch (DbUpdateException)
            {
                ModelState.AddModelError("", "Unable to save changes. " +
                                         "Try again, and if the problem persists " +
                                         "see your system administrator.");
            }

            return(View(comment));
        }
Ejemplo n.º 3
0
        public IActionResult Index(int propositionId, Like like)
        {
            var user = _context.Users.FirstOrDefault(u => u.Email == User.Identity.Name);

            var isLiked = _context.Likes.FirstOrDefault(c => c.PropositionId == propositionId && c.UserId == user.Id);

            if (isLiked == null)
            {
                like.PropositionId = propositionId;
                like.UserId        = user.Id;

                _context.Add(like);
                _context.SaveChanges();

                return(RedirectToAction("Details", "Proposition", new { id = propositionId }));
            }

            return(RedirectToAction("Details", "Proposition", new { id = propositionId }));
        }