public async Task <IActionResult> AddTopic(AddTopicViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            User user = await _context.User.FirstOrDefaultAsync(t => t.Id.Equals(model.UserId));

            Topic topic = new Topic()
            {
                Title = model.Topic,
                Rate  = 0,
                Date  = DateTime.Now.ToString("dd/MM/yyyy HH:mm"),
                User  = user
            };

            Comment comment = new Comment()
            {
                Content = model.Comment,
                Date    = DateTime.Now.ToString("dd/MM/yyyy HH:mm"),
                User    = user,
                Topic   = topic
            };

            user.TopicsCount++;
            user.CommentsCount++;

            _context.Update(user);
            await _context.AddAsync(topic);

            await _context.AddAsync(comment);

            await _context.SaveChangesAsync();

            return(Ok());
        }
Пример #2
0
        public async Task <IActionResult> AddTopic(AddTopicViewModel model)
        {
            if (ModelState.IsValid)
            {
                User user = await _context.User.FirstOrDefaultAsync(t => t.Id.Equals(_userManager.GetUserId(User)));

                Topic topic = new Topic()
                {
                    Title = model.Topic,
                    Rate  = 0,
                    Date  = DateTime.Now.ToString("dd/MM/yyyy HH:mm"),
                    User  = user
                };

                Comment comment = new Comment()
                {
                    Content = model.Comment,
                    Date    = DateTime.Now.ToString("dd/MM/yyyy HH:mm"),
                    User    = user,
                    Topic   = topic
                };

                user.TopicsCount++;
                user.CommentsCount++;

                _context.Update(user);
                await _context.AddAsync(topic);

                await _context.AddAsync(comment);

                await _context.SaveChangesAsync();

                return(RedirectToAction("ShowTopic", new { id = topic.Id }));
            }
            return(View(model));
        }