Пример #1
0
        public ActionResult InviteUser(CreateInvitationBindingModel model)
        {
            if (model == null)
            {
                this.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(this.Json(new { ErrorMessage = "Missing data" }));
            }

            if (!this.ModelState.IsValid)
            {
                this.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(this.Json(new { ErrorMessage = this.ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage) }));
            }

            try
            {
                var invitationId = this._service.InviteUser(model, this.User.Identity.GetUserId());

                var hub = GlobalHost.ConnectionManager.GetHubContext <PhotoContestHub>();
                hub.Clients.User(model.Username).notificationReceived(invitationId);
            }
            catch (NotFoundException exception)
            {
                this.Response.StatusCode = (int)HttpStatusCode.NotFound;
                return(this.Json(new { ErrorMessage = exception.Message }));
            }
            catch (BadRequestException exception)
            {
                this.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(this.Json(new { ErrorMessage = exception.Message }));
            }

            this.Response.StatusCode = (int)HttpStatusCode.OK;
            return(this.Json(new { SuccessfulMessage = string.Format("User with username {0} successfully invited", model.Username) }));
        }
Пример #2
0
        public int InviteUser(CreateInvitationBindingModel model, string loggerUserId)
        {
            var contest = this.Data.Contests.Find(model.ContestId);

            if (contest == null)
            {
                throw new NotFoundException(string.Format("Contest with id {0} not found", model.ContestId));
            }

            var loggedUser = this.Data.Users.Find(loggerUserId);

            if (contest.OrganizatorId != loggedUser.Id)
            {
                throw new BadRequestException("Only the contest organizator can invite users.");
            }

            if (model.Type == InvitationType.Committee &&
                contest.VotingStrategy.VotingStrategyType != VotingStrategyType.Closed)
            {
                throw new BadRequestException("The contest voting strategy type is not 'CLOSED'.");
            }

            if (model.Type == InvitationType.ClosedContest &&
                contest.ParticipationStrategy.ParticipationStrategyType != ParticipationStrategyType.Closed)
            {
                throw new BadRequestException("The contest participation strategy type is not 'CLOSED'.");
            }

            if (!contest.IsOpenForSubmissions)
            {
                throw new BadRequestException("The contest is closed for submissions/registrations.");
            }

            var userToInvite = this.Data.Users.All().FirstOrDefault(u => u.UserName == model.Username);

            if (userToInvite == null)
            {
                throw new NotFoundException(string.Format("User with username {0} not found", model.Username));
            }

            if (contest.Participants.Contains(userToInvite))
            {
                throw new BadRequestException("You cannot invite user who already participates in the contest");
            }

            if (contest.Committee.Contains(userToInvite))
            {
                throw new BadRequestException("You cannot invite user who already is in the contest committee");
            }

            if (userToInvite.UserName == loggedUser.UserName)
            {
                throw new BadRequestException("Users cannot invite themselves.");
            }

            if (userToInvite.PendingInvitations.Any(i => i.ContestId == model.ContestId && i.Type == model.Type))
            {
                throw new BadRequestException(string.Format("User is already invited to contest with id {0}", contest.Id));
            }

            var invitation = new Invitation
            {
                ContestId        = model.ContestId,
                InviterId        = loggedUser.Id,
                InvitedId        = userToInvite.Id,
                DateOfInvitation = DateTime.Now,
                Type             = model.Type,
                Status           = InvitationStatus.Neutral
            };

            if (model.Type == InvitationType.ClosedContest)
            {
                contest.InvitedUsers.Add(userToInvite);
            }

            userToInvite.PendingInvitations.Add(invitation);
            loggedUser.SendedInvitations.Add(invitation);

            this.Data.SaveChanges();

            return(invitation.Id);
        }