コード例 #1
0
        public async Task <IActionResult> EditNews(int id, [Bind("Id,Title,Body")] CourseNews news)
        {
            if (id != news.Id)
            {
                return(NotFound());
            }

            var newsWithOwner = await _context.CourseNews
                                .Include(x => x.Course)
                                .ThenInclude(x => x.Members)
                                .ThenInclude(x => x.Member)
                                .ThenInclude(x => x.PushTokens)
                                .Where(x => x.Course.Owner.Id == GetUserId())
                                .SingleOrDefaultAsync(x => x.Id == id);

            if (newsWithOwner == null)
            {
                return(Forbid());
            }

            if (ModelState.IsValid)
            {
                newsWithOwner.Title = news.Title;
                newsWithOwner.Body  = news.Body;

                _context.Update(newsWithOwner);
                await _context.SaveChangesAsync();

                _fireBaseService.SendPushNotificationMessage(newsWithOwner.Course.Members.Select(x => x.Member).ToList(),
                                                             $"Change - {newsWithOwner.Course.Title}: {news.Title}", news.Body);

                return(RedirectToAction(nameof(Details), new { id = newsWithOwner.CourseId }));
            }
            return(View(newsWithOwner));
        }
コード例 #2
0
        public async Task <IActionResult> CreateNews([Bind("Title,Body,CourseId")] CourseNews news)
        {
            if (news == null)
            {
                return(NotFound());
            }

            Course course = await _context.Courses
                            .Include(x => x.Members)
                            .ThenInclude(x => x.Member)
                            .ThenInclude(x => x.PushTokens)
                            .Where(x => x.Owner.Id == GetUserId())
                            .SingleOrDefaultAsync(x => x.Id == news.CourseId);

            if (course == null)
            {
                return(Forbid());
            }

            news.Course = course;

            if (ModelState.IsValid)
            {
                _context.Add(news);
                await _context.SaveChangesAsync();

                _fireBaseService.SendPushNotificationMessage(course.Members.Select(x => x.Member).ToList(),
                                                             $"{course.Title}: {news.Title}", news.Body, new Dictionary <string, string>
                {
                    { "newsId", news.Id.ToString() }
                });

                return(RedirectToAction("Details", new { id = news.CourseId }));
            }
            return(View(news));
        }