// PUT api/Song/5
        public HttpResponseMessage PutSong(int id, Song song)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != song.SongId)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

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

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

            return Request.CreateResponse(HttpStatusCode.OK);
        }
        // PUT api/Songs/5
        public HttpResponseMessage PutSong(int id, Song song)
        {
            Song dbSong = DataMapper.CreateOrLoadSong(repository, song);
            song.Update(dbSong);

            repository.Update(id, dbSong);

            return Request.CreateResponse(HttpStatusCode.OK);
        }
예제 #3
0
 public void Update(Song song)
 {
     if (this.Title != null)
     {
         song.Title = this.Title;
     }
     if (this.Year != null)
     {
         song.Year = this.Year;
     }
     if (this.Genre != null)
     {
         song.Genre = this.Genre;
     }
 }
        // POST api/Song
        public HttpResponseMessage PostSong(Song song)
        {
            if (ModelState.IsValid)
            {
                db.Songs.Add(song);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, song);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = song.SongId }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }
예제 #5
0
        public static Song CreateOrLoadSong(IRepository<Song> repository, Song entity)
        {
            Song song = repository.Get(entity.SongId);
            if (song != null)
            {
                return song;
            }

            Song newSong = repository.Add(new Song()
            {
                Title = entity.Title,
                Year = entity.Year,
                Genre = entity.Genre,
                Album = entity.Album,
                Artist = entity.Artist
            });

            return newSong;
        } 
예제 #6
0
            public static void Edit(int id, Song song)
            {
                song.SongId = id;
                HttpResponseMessage responseMessage = client.PutAsXmlAsync("api/songs/" + id, song).Result;

                if (responseMessage.IsSuccessStatusCode)
                {
                    Console.WriteLine("Song with id: {0} edited!", id);
                }
                else
                {
                    Console.WriteLine("{0} ({1})", (int)responseMessage.StatusCode, responseMessage.ReasonPhrase);
                }
            }
예제 #7
0
            public static void Add(Song song)
            {
                Song newSong = new Song { Artist = song.Artist, Genre = song.Genre, Title = song.Title, Year = song.Year };
                HttpResponseMessage responseMessage = client.PostAsJsonAsync("api/song", newSong).Result;

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