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

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

            var user = context.GetAll <DomainModel.User>()
                       .FirstOrDefault(u => u.Id == joinTeamViewModel.UserId);

            if (user == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, "Invalid User"));
            }
            if (user.Id == team.OwnerId)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, "Owner can not leave team"));
            }

            var teamUser = user.Teams.FirstOrDefault(ut => ut.TeamId == team.Id);

            if (teamUser != null)
            {
                team.Members.Remove(teamUser);
                context.SaveChanges();
            }

            return(ResourceOkResponse(team.MapToBasicServiceModel()));
        }
예제 #2
0
        public HttpResponseMessage Join(int id, ServiceModel.JoinTeamViewModel joinTeamViewModel)
        {
            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.Id == joinTeamViewModel.UserId);

            if (user == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, "Invalid 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);

            if (team.IsOpen)
            {
                newTeamMember.Status = DomainModel.TeamUserStatus.Approved;
            }
            team.Members.Add(newTeamMember);
            context.SaveChanges();

            return(ResourceOkResponse(team.MapToBasicServiceModel()));
        }
예제 #3
0
        public HttpResponseMessage Join(int id, ServiceModel.JoinTeamViewModel joinTeamViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(new HttpResponseMessage <JsonValue>(ModelState.ToJson(), HttpStatusCode.BadRequest));
            }

            var team = context.GetAll <DomainModel.Team>()
                       .FirstOrDefault(u => u.Id == id);

            if (team == null)
            {
                ModelState.AddModelError("", "Invalid Team");
                return(new HttpResponseMessage <JsonValue>(ModelState.ToJson(), HttpStatusCode.BadRequest));
            }

            var user = context.GetAll <DomainModel.User>()
                       .FirstOrDefault(u => u.Id == joinTeamViewModel.UserId);

            if (user == null)
            {
                ModelState.AddModelError("", "Invalid User");
                return(new HttpResponseMessage <JsonValue>(ModelState.ToJson(), HttpStatusCode.BadRequest));
            }

            if (user.Teams.Any(ut => ut.TeamId == team.Id))
            {
                ModelState.AddModelError("", "User already added to team");
                return(new HttpResponseMessage <JsonValue>(ModelState.ToJson(), HttpStatusCode.BadRequest));
            }

            var newTeamMember = new DomainModel.TeamUser(team, user);

            if (team.IsOpen)
            {
                newTeamMember.Status = DomainModel.TeamUserStatus.Approved;
            }
            team.TeamMembers.Add(newTeamMember);
            context.SaveChanges();

            var sTeam    = team.MapToBasicServiceModel();
            var response = new HttpResponseMessage <ServiceModel.TeamBasic>(sTeam, HttpStatusCode.OK);

            response.Headers.Location = new Uri(Request.RequestUri, "/api/team/" + sTeam.Id.ToString());
            return(response);
        }