コード例 #1
0
        public ActionResult AddPhoto(PhotoBindingModel model)
        {
            var contest = this.Data.Contests.GetAll().Where(c => c.Id == model.ContestId).ProjectTo<ContestViewModel>().FirstOrDefault();
            var deadlineStrategy = contest.GetDeadlineStrategy(this.Data.Contests.Find(contest.Id));
            bool hasExpired = deadlineStrategy.Expire();

            if (hasExpired)
            {
                this.Data.SaveChanges();
                this.hub.InfoExpiredContest(contest.Title, contest.Id);
                this.RedirectToAction("ParticipateContests", "Contests");
            }

            if (!this.ModelState.IsValid)
            {
                this.AddNotification("Invalid data entered.", NotificationType.ERROR);
                return this.RedirectToAction("AddPhoto", contest.Id);
            }

            string currentUserId = this.User.Identity.GetUserId();

            string[] uploadResults = this.UploadPhotoToGoogleDrive(model.PhotoData, model.ContestId);

            if (uploadResults[0] == "success")
            {
                if (model.ContestId != null)
                {
                    var newPhoto = new Photo()
                        {
                            Url = uploadResults[1],
                            Uploaded = DateTime.Now,
                            AuthorId = currentUserId,
                            ContestId = (int)model.ContestId
                        };

                    this.Data.Photos.Add(newPhoto);
                }

                this.Data.SaveChanges();

                this.AddNotification("Photo successfully added to contest", NotificationType.SUCCESS);
                return this.RedirectToAction("Details", "Contests", new { id = model.ContestId });
            }
            else
            {
                this.AddNotification("Photo did not upload. Please, try again later", NotificationType.ERROR);
                return this.RedirectToAction("AddPhoto", model.ContestId);
            }
        }
コード例 #2
0
 public ActionResult AddPhoto(int? contestId)
 {
     var model = new PhotoBindingModel { ContestId = contestId };
     return this.PartialView("~/Views/Photos/_AddPhoto.cshtml", model);
 }