コード例 #1
0
        public ActionResult WritePost(FormalPostViewModel postmodel)
        {
            var             ctx         = new ApplicationDbContext();
            var             cat         = new CategoryModel();
            var             value       = Request["Category"];
            var             userId      = User.Identity.GetUserId();
            var             store       = new UserStore <ApplicationUser>(new ApplicationDbContext());
            var             userManager = new UserManager <ApplicationUser>(store);
            ApplicationUser user        = userManager.FindById(userId);
            var             post        = new FormalPostModel

            {
                Title      = postmodel.Title,
                Content    = postmodel.Content,
                Author     = User.Identity.GetUserName(),
                Date       = DateTime.Now,
                CategoryId = int.Parse(value)
            };
            var notification = new SendEmailController();

            if (ModelState.IsValid)
            {
                if (postmodel.File != null && postmodel.File.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(postmodel.File.FileName);
                    var path     = Path.Combine(Server.MapPath("~/UploadedFiles/"), fileName);
                    post.FilePath = "~/UploadedFiles/" + fileName;
                    postmodel.File.SaveAs(path);
                }
                ctx.Post.Add(post);
                ctx.SaveChanges();
                notification.EmailWhenPost(post);
            }
            else
            {
                postmodel.Posts = ctx.Post.OrderByDescending(p => p.Date).ToList();
                return(View("ShowPost", postmodel));
            }

            return(RedirectToAction("ShowPost"));
        }
コード例 #2
0
        public ActionResult CreateMeetingPoll([FromBody]MeetingData meetingData)
        {
            var ctx = new ApplicationDbContext();

            var userId = User.Identity.GetUserId();
            var author = ctx.Users.First(u => u.Id.Equals(userId));

            var mp = new MeetingPoll();
            mp.Title = meetingData.Title;
            mp.Content = meetingData.Content;
            mp.Author = author;
            mp.Participants = new List<ApplicationUser>();

            List<ApplicationUser> userList = new List<ApplicationUser>();
            foreach (var email in meetingData.Participants)
            {
                var user = ctx.Users.First(u => u.Email.Equals(email));
                mp.Participants.Add(user);
            }

            var pollOptions = new List<PollOption>();
            var notification = new SendEmailController();
            foreach(var time in meetingData.MeetingTimes)
            {
                var option = new PollOption() { MeetingTime = time, Votes = 0, MeetingPoll = mp };
                pollOptions.Add(option);
            }

            mp.PollOptions = pollOptions;

            ctx.MeetingPolls.Add(mp);
            ctx.SaveChanges();
            notification.EmailPollInvitation(mp);

            return RedirectToAction("Index");
        }