Пример #1
0
        // POST api/Rosters
        public HttpResponseMessage PostRoster(Roster roster)
        {
            var baller = db.Ballers
                        .Where(b => b.UserName == User.Identity.Name)
                        .Select(AsDto.BallerDTO)
                        .FirstOrDefault();
            roster.BallerId = baller.Id;

            if (ModelState.IsValid)
            {
                db.Rosters.Add(roster);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, roster);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = roster.Id }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }
Пример #2
0
        // PUT api/Rosters/5
        public HttpResponseMessage PutRoster(int id, Roster roster)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != roster.Id)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            db.Entry(roster).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }