예제 #1
0
        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);
        }