private static bool PictureCanBeVotedUnvoted(User user, Picture dbPicture, Contest dbContest) { // 1. User is author if (dbPicture.AuthorId == user.Id) { return false; } // 2. User is contest owner if (dbContest.OwnerId == user.Id) { return false; } //3. Contest is not Active if (dbContest.Status != PhotoContest.Models.Enumerations.ContestStatus.Active) { return false; } // 4. Contest is voted by Jury and user is not member of the Jury if (dbContest.VotingType == PhotoContest.Models.Enumerations.VotingType.Closed && !dbContest.Jury.Members.Any(m => m.Id == user.Id)) { return false; } return true; }
public static bool IsAuthor(User user, Picture dbPicture) { if (dbPicture.Author.Id ==user.Id) { return true; } return false; }
public static bool HasVotedForPicture(User user, Picture dbPicture, Contest dbContest) { if (dbPicture.Votes.Any(v => v.VoterId == user.Id && v.ContestId == dbContest.Id)) { return true; } return false; }
public ActionResult Save(IEnumerable<HttpPostedFileBase> files, int contestId) { var contest = this.Data.Contests.Find(contestId); foreach (var file in files) { var paths = UploadImages.UploadImage(file, false); var photo = new Picture() { ContestId = contestId, UserId = this.CurrentUser.Id, CreatedOn = DateTime.Now, Url = Dropbox.Download(paths[0]) }; contest.Pictures.Add(photo); } this.Data.SaveChanges(); return Content(""); }
public ActionResult Create(PictureInputModel model, int id) { var contest = this.Data.Contests.GetById(id); if (contest.Type == ContestType.Private && !contest.Participants.Contains(this.UserProfile) && contest.Type == ContestType.Private && contest.Owner != this.UserProfile) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "The contest is private. You need to be invited by the owner."); } if (contest.IsDeleted || contest.IsClosedForSubmissions) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "The contest is closed for submissions."); } string path = Dropbox.Upload(model.Photo.FileName, this.UserProfile.UserName, model.Photo.InputStream); var picture = new Picture() { Title = model.Title, Url = path, Owner = this.UserProfile, CreatedOn = DateTime.Now, ContestId = id, IsDeleted = false }; this.Data.Pictures.Add(picture); if (!contest.Participants.Contains(this.UserProfile)) { contest.Participants.Add(this.UserProfile); } this.Data.SaveChanges(); return RedirectToAction("Index", new { id = picture.ContestId }); }
public static bool CanVoteForPicture(User user, Picture dbPicture, Contest dbContest) { return PictureCanBeVotedUnvoted(user, dbPicture, dbContest) && !HasVotedForPicture(user, dbPicture, dbContest); }
public ActionResult UploadPicture(UploadPictureBindingModel model) { if (!this.ModelState.IsValid) { return this.View(model); } var userId = this.User.Identity.GetUserId(); var user = this.Data.Users.Find(userId); if (user == null) { this.RedirectToAction("Index", "Home"); } string thumbnailImageData = ""; try { var image = PictureUtills.CreateImageFromBase64(model.PictureData); var thumbnail = PictureUtills.CreateThumbnailFromImage(image, 316); thumbnailImageData = PictureUtills.ConvertImageToBase64(thumbnail); } catch (Exception ex) { thumbnailImageData = model.PictureData; } var picture = new Picture() { PictureData = model.PictureData, ThumbnailImageData = thumbnailImageData, Title = model.Title, AuthorId = userId, PostedOn = DateTime.Now, }; this.Data.Pictures.Add(picture); this.Data.SaveChanges(); return this.RedirectToAction("Pictures"); }
private static void AddPicture() { var author = dbContext.Users.First(); var picture = new Picture() { Author = author, PictureData = "vse endo e base64 string", PostedOn = DateTime.Now }; dbContext.Pictures.Add(picture); dbContext.SaveChanges(); }
private void AddVoteToPicture(VoteInput model, Picture picture) { var oldVote = picture.Votes.FirstOrDefault(x => x.UserId == this.CurrentUser.Id); if (oldVote != null) { oldVote.Rating = model.Rating; } else { picture.Votes.Add(new Vote() { Rating = model.Rating, UserId = this.CurrentUser.Id }); } }
public bool JoinContest(int id, IEnumerable<HttpPostedFileBase> files, string userId) { var contest = this.Data.Contests.Find(id); if (contest == null) { throw new NotFoundException("Contest with such id does not exists"); } var user = this.Data.Users.Find(userId); if (contest.Committee.Contains(user) || contest.Participants.Contains(user) || contest.OrganizatorId == user.Id) { throw new BadRequestException("You are either organizator of this contest or in the committee or you already participate in it"); } var deadlineStrategy = StrategyFactory.GetDeadlineStrategy(contest.DeadlineStrategy.DeadlineStrategyType); deadlineStrategy.CheckDeadline(this.Data, contest, user); var participationStrategy = StrategyFactory.GetParticipationStrategy(contest.ParticipationStrategy.ParticipationStrategyType); participationStrategy.CheckPermission(this.Data, user, contest); if (contest.Participants.Contains(user)) { throw new BadRequestException("You already participate in this contest"); } if (contest.Committee.Contains(user)) { throw new UnauthorizedException("You cannot participate in this contest, you are in the committee"); } foreach (var file in files) { var result = this._pictureService.UploadImageToGoogleDrive(file, file.FileName, file.ContentType); if (result[0] != "success") { throw new BadRequestException(result[1]); } Picture picture = new Picture { UserId = user.Id, Url = GoogleDrivePicturesBaseLink + result[1], GoogleFileId = result[1], ContestId = contest.Id }; this.Data.Pictures.Add(picture); } contest.Participants.Add(user); this.Data.Contests.Update(contest); this.Data.SaveChanges(); return true; }
protected void DeletePicturetData(Picture picture) { var votes = picture.Votes.ToList(); for (int i = 0; i < votes.Count; i++) { var vote = votes[i]; this.Data.Votes.Delete(vote); } this.Data.Pictures.Delete(picture); }