Esempio n. 1
0
        // PUT api/TimeCard/5
        public HttpResponseMessage PutTimeCard(int id, TimeCard timecard)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }

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

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

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

            return(Request.CreateResponse(HttpStatusCode.OK));
        }
Esempio n. 2
0
        // GET api/TimeCard/5
        public TimeCard GetTimeCard(int id)
        {
            TimeCard timecard = db.TimeCards.Find(id);

            if (timecard == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return(timecard);
        }
Esempio n. 3
0
        // POST api/TimeCard
        public HttpResponseMessage PostTimeCard([FromBody] TimeCard timecard)
        {
            if (ModelState.IsValid)
            {
                db.TimeCards.Add(timecard);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, timecard);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = timecard.Id }));
                return(response);
            }
            else
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }
        }
Esempio n. 4
0
        // DELETE api/TimeCard/5
        public HttpResponseMessage DeleteTimeCard(int id)
        {
            TimeCard timecard = db.TimeCards.Find(id);

            if (timecard == null)
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            db.TimeCards.Remove(timecard);

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

            return(Request.CreateResponse(HttpStatusCode.OK, timecard));
        }