コード例 #1
0
        // PUT api/Album/5
        public HttpResponseMessage PutAlbum(int id, Album album)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != album.AlbumId)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

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

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

            return Request.CreateResponse(HttpStatusCode.OK);
        }
コード例 #2
0
        // PUT api/Albums/5
        public HttpResponseMessage PutAlbum(int id, Album album)
        {
            Album dbAlbum = DataMapper.CreateOrLoadAlbum(repository, album);
            album.Update(dbAlbum);
            
            repository.Update(id, dbAlbum);

            return Request.CreateResponse(HttpStatusCode.OK);
        }
コード例 #3
0
 public void Update(Album album)
 {
     if (this.Title != null)
     {
         album.Title = this.Title;
     }
     if (this.Producer != null)
     {
         album.Producer = this.Producer;
     }
     if (this.Year != null)
     {
         album.Year = this.Year;
     }
 }
コード例 #4
0
        // POST api/Album
        public HttpResponseMessage PostAlbum(Album album)
        {
            if (ModelState.IsValid)
            {
                db.Albums.Add(album);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, album);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = album.AlbumId }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }
コード例 #5
0
        public static Album CreateOrLoadAlbum(IRepository<Album> repository, Album entity)
        {
            Album album = repository.Get(entity.AlbumId);
            if (album != null)
            {
                return album;
            }

            Album newAlbum = repository.Add(new Album()
                {
                    Title = entity.Title,
                    Producer = entity.Producer,
                    Year = entity.Year,
                    Artists = entity.Artists,
                    Songs = entity.Songs
                });

            return newAlbum;
        }
コード例 #6
0
 public void Update(Album album)
 {
     if (this.Title != null)
     {
         album.Title = this.Title;
     }
     if (this.Producer != null)
     {
         album.Producer = this.Producer;
     }
     if (this.Year != null)
     {
         album.Year = this.Year;
     }
     if (this.Songs != null)
     {
         album.Songs = this.Songs;
     }
     if (this.Artists != null)
     {
         album.Artists = this.Artists;
     }
 }
コード例 #7
0
            public static void Edit(int id, Album album)
            {
                album.AlbumId = id;
                HttpResponseMessage responseMessage = client.PutAsXmlAsync("api/albums/" + id, album).Result;

                if (responseMessage.IsSuccessStatusCode)
                {
                    Console.WriteLine("Album with id: {0} edited!", id);
                }
                else
                {
                    Console.WriteLine("{0} ({1})", (int)responseMessage.StatusCode, responseMessage.ReasonPhrase);
                }
            }
コード例 #8
0
            public static void Add(string title, string producer, DateTime year, ICollection<Artist> artists,
                                   ICollection<Song> songs)
            {
                Album newAlbum = new Album
                    {
                        Title = title,
                        Year = year,
                        Artists = artists,
                        Songs = songs,
                        Producer = producer
                    };
                HttpResponseMessage responseMessage = client.PostAsJsonAsync("api/album", newAlbum).Result;

                if (responseMessage.IsSuccessStatusCode)
                {
                    Console.WriteLine("Album added: {0}", newAlbum.Title);
                }
                else
                {
                    Console.WriteLine("{0} ({1})", (int)responseMessage.StatusCode, responseMessage.ReasonPhrase);
                }
            }