예제 #1
0
        public HttpResponseMessage AddMember(int id, ServiceModel.AddMemberViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.ToJson()));
            }

            //get team
            var team = GetTeam(id);

            var user = context.GetAll <DomainModel.User>()
                       .FirstOrDefault(u => u.EmailAddress == viewModel.EmailAddress);

            if (user == null)
            {
                user = new DomainModel.User(viewModel.EmailAddress);
                context.Add(user);
            }

            if (user.Teams.Any(ut => ut.TeamId == team.Id))
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, "User already added to team"));
            }

            var newTeamMember = new DomainModel.TeamUser(team, user);
            var inviter       = team.Members.FirstOrDefault(x => x.UserId == viewModel.AddedByUserId);

            if (inviter == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, "User Not Allowed to Invite Members to this Team"));
            }

            if (team.IsOpen || (inviter != null && inviter.Role == DomainModel.TeamUserRole.Administrator))
            {
                newTeamMember.Status = DomainModel.TeamUserStatus.Approved;
            }

            emailService.InvitedToTeam(user, inviter.User, team).Send();

            team.Members.Add(newTeamMember);
            context.SaveChanges();

            return(ResourceOkResponse(team.MapToBasicServiceModel()));
        }