public HttpResponseMessage PostContact( ContactDto contactDto ) { if ( !ModelState.IsValid ) { return Request.CreateErrorResponse( HttpStatusCode.BadRequest, ModelState ); } var model = contactDto.ToEntity(); model.UserId = User.Identity.Name; db.Contacts.Add( model ); db.SaveChanges(); contactDto.ContactId = model.ContactId; HttpResponseMessage response = Request.CreateResponse( HttpStatusCode.Created, contactDto ); response.Headers.Location = new Uri( Url.Link( "DefaultApi", new { id = contactDto.ContactId } ) ); return response; }
public HttpResponseMessage DeleteContact( int id ) { var contact = db.Contacts.Find( id ); if ( contact == null ) { return Request.CreateResponse( HttpStatusCode.NotFound ); } if ( db.Entry( contact ).Entity.UserId != User.Identity.Name ) { return Request.CreateResponse( HttpStatusCode.Unauthorized ); } var contactDto = new ContactDto(contact); db.Contacts.Remove( contact ); try { db.SaveChanges(); } catch ( DbUpdateConcurrencyException ) { return Request.CreateResponse( HttpStatusCode.InternalServerError ); } return Request.CreateResponse( HttpStatusCode.OK, contactDto ); }
public HttpResponseMessage PutContact( int id, ContactDto contactDto ) { if ( !ModelState.IsValid ) { return Request.CreateErrorResponse( HttpStatusCode.BadRequest, ModelState ); } var model = contactDto.ToEntity(); if ( db.Entry( model ).Entity.UserId != User.Identity.Name ) { return Request.CreateResponse( HttpStatusCode.Unauthorized ); } else { model.UserId = User.Identity.Name; } db.Entry( model ).State = EntityState.Modified; try { db.SaveChanges(); } catch ( DbUpdateConcurrencyException ) { return Request.CreateResponse( HttpStatusCode.InternalServerError ); } return Request.CreateResponse( HttpStatusCode.OK ); }