Beispiel #1
0
        public HttpResponseMessage Put(int id, ServiceModel.UpdateTeamViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.ToJson().ToString()));
            }

            var existingTeam = context.GetAll <DomainModel.Team>()
                               .FirstOrDefault(u => u.Name.Equals(viewModel.Name) && u.Id != id);

            if (existingTeam != null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, "Team Name Already in Use"));
            }

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

            var editor = team.Members
                         .FirstOrDefault(tm => tm.Role == DomainModel.TeamUserRole.Administrator && tm.UserId == viewModel.UpdatedById);

            if (editor == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, "User does not have permissions to edit team"));
            }

            team.Name   = viewModel.Name;
            team.IsOpen = viewModel.IsPublic;

            context.SaveChanges();

            return(ResourceOkResponse(team.MapToServiceModel()));
        }
Beispiel #2
0
        public HttpResponseMessage Put(int id, ServiceModel.UpdateTeamViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                throw new HttpResponseException(ModelState.ToJson().ToString(), HttpStatusCode.BadRequest);
            }

            var existingTeam = context.GetAll <DomainModel.Team>()
                               .FirstOrDefault(u => u.Name.Equals(viewModel.Name) && u.Id != id);

            if (existingTeam != null)
            {
                ModelState.AddModelError("", "Team name already in use");
                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 edited");
                return(new HttpResponseMessage <JsonValue>(ModelState.ToJson(), HttpStatusCode.BadRequest));
            }

            var editor = team.TeamMembers
                         .FirstOrDefault(tm => tm.Role == DomainModel.TeamUserRole.Administrator && tm.UserId == viewModel.UpdatedById);

            if (editor == null)
            {
                ModelState.AddModelError("", "User does not have permissions to edit team");
                return(new HttpResponseMessage <JsonValue>(ModelState.ToJson(), HttpStatusCode.BadRequest));
            }

            team.Name   = viewModel.Name;
            team.IsOpen = viewModel.IsPublic;

            context.SaveChanges();

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

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