Пример #1
0
 public HttpResponseMessage Put(int id, [FromUri] artist artist)
 {
     using (artistsDBEntities entities = new artistsDBEntities())
     {
         try {
             var entity = entities.artists.FirstOrDefault(e => e.ID == id);
             if (entity == null)
             {
                 return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Artist with Id " + id.ToString() + " not found to update"));
             }
             else
             {
                 entity.FirstName      = artist.FirstName;
                 entity.LastName       = artist.LastName;
                 entity.Genre          = artist.Genre;
                 entity.NumberOfAlbums = artist.NumberOfAlbums;
                 entities.SaveChanges();
                 return(Request.CreateResponse(HttpStatusCode.OK, entity));
             }
         }
         catch (Exception e)
         {
             return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e));
         }
     }
 }
Пример #2
0
        public HttpResponseMessage Post([FromUri] artist artist)
        {
            try {
                using (artistsDBEntities entities = new artistsDBEntities())
                {
                    entities.artists.Add(artist);
                    entities.SaveChanges();

                    var message = Request.CreateResponse(HttpStatusCode.Created, artist);
                    message.Headers.Location = new Uri(Request.RequestUri + artist.ID.ToString());
                    return(message);
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
Пример #3
0
 public HttpResponseMessage Delete(int id)
 {
     try
     {
         using (artistsDBEntities entities = new artistsDBEntities())
         {
             var entity = entities.artists.FirstOrDefault(e => e.ID == id);
             if (entity == null)
             {
                 return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "The artist with the id " + id.ToString() + "not found to delete"));
             }
             else
             {
                 entities.artists.Remove(entity);
                 entities.SaveChanges();
                 return(Request.CreateResponse(HttpStatusCode.OK));
             }
         }
     }
     catch (Exception e)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e));
     }
 }