コード例 #1
0
        // POST /api/team/5
        public HttpResponseMessage Post(ServiceModel.AddTeamViewModel addTeamViewModel)
        {
            if (!ModelState.IsValid)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.ToJson().ToString()));
            }

            var existingTeam = context.GetAll <DomainModel.Team>()
                               .FirstOrDefault(u => u.Name.Equals(addTeamViewModel.Name));

            if (existingTeam != null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, "Team name already in use"));
            }


            var teamCreator = context.GetAll <DomainModel.User>()
                              .FirstOrDefault(u => u.Id == addTeamViewModel.CreatedById);

            if (teamCreator == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, "Invalid Team Owner Specified"));
            }

            var team = new DomainModel.Team(addTeamViewModel.Name, teamCreator);

            team.IsOpen = addTeamViewModel.IsPublic;
            context.Add(team);
            context.SaveChanges();

            return(ResourceOkResponse(team.MapToBasicServiceModel()));
        }
コード例 #2
0
        public HttpResponseMessage Post(ServiceModel.AddTeamViewModel addTeamViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(new HttpResponseMessage <JsonValue>(ModelState.ToJson(), HttpStatusCode.BadRequest));
            }

            var existingTeam = context.GetAll <DomainModel.Team>()
                               .FirstOrDefault(u => u.Name.Equals(addTeamViewModel.Name));

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

            var teamCreator = context.GetAll <DomainModel.User>()
                              .FirstOrDefault(u => u.Id == addTeamViewModel.CreatedById);

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

            var team = new DomainModel.Team(addTeamViewModel.Name, teamCreator);

            team.IsOpen = addTeamViewModel.IsPublic;
            context.Add(team);
            context.SaveChanges();

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

            response.Headers.Location = new Uri(Request.RequestUri, "/api/team/" + sTeam.Id.ToString());
            return(response);
        }
コード例 #3
0
ファイル: TeamController.cs プロジェクト: jessemlay/TeamThing
        // POST /api/team/5
        public HttpResponseMessage Post(ServiceModel.AddTeamViewModel addTeamViewModel)
        {
            if (!ModelState.IsValid) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, ModelState.ToJson().ToString())); }

            var existingTeam = context.GetAll<DomainModel.Team>()
                                      .FirstOrDefault(u => u.Name.Equals(addTeamViewModel.Name));

            if (existingTeam != null) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, "Team name already in use")); }

            var teamCreator = context.GetAll<DomainModel.User>()
                                     .FirstOrDefault(u => u.Id == addTeamViewModel.CreatedById);

            if (teamCreator == null) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, "Invalid Team Owner Specified")); }

            var team = new DomainModel.Team(addTeamViewModel.Name, teamCreator);
            team.IsOpen = addTeamViewModel.IsPublic;
            context.Add(team);
            context.SaveChanges();

            return ResourceOkResponse(team.MapToBasicServiceModel());
        }