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

            if (id != song.id)
            {
                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);
        }
        public IHttpActionResult Create(SongDataModel newSong)
        {
            if (newSong == null || newSong.Title == null)
            {
                return this.BadRequest("You must provide song title.");
            }

            if (newSong.ArtistId == null || !this.db.Artists.Any(a => a.Id == newSong.ArtistId))
            {
                return this.BadRequest("Invalid artist id.");
            }

            if (newSong.AlbumId != null && !this.db.Albums.Any(a => a.Id == newSong.AlbumId))
            {
                return this.BadRequest("Invalid album id.");
            }

            var song = new Song
            {
                Title = newSong.Title,
                Genre = newSong.Genre,
                Year = newSong.Year,
                AlbumId = newSong.AlbumId,
                ArtistId = newSong.ArtistId.Value
            };

            this.db.Songs.Add(song);
            this.db.SaveChanges();

            return this.Ok(song);
        }
Exemple #3
1
        // POST api/Songs
        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.id }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }
Exemple #4
1
        private static void PrintSong(Song song)
        {
            Console.WriteLine(new string('-', 20));
            Console.WriteLine("{0}\r\n{1}\r\n{2}\r\n{3}\r\n", song.title, song.year, song.Artist.name, song.Genre.name);

            Console.WriteLine("List albums: ");
            if (song.Albums.Count > 0)
            {
                foreach (var album in song.Albums)
                {
                    Console.WriteLine(album.title);
                }
            }
            else
            {
                Console.WriteLine("No albums.");
            }

            Console.WriteLine(new string('-', 20));
        }
        public IHttpActionResult Create(SongModel item)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var newItem = new Song()
            {
                Title = item.Title,
                Year = item.Year,
                Genre = item.Genre,
            };

            this.data.Songs.Add(newItem);
            this.data.Songs.SaveChanges();

            item.Id = newItem.Id;

            return this.Ok(item);
        }
 //// POST api/Song
 public HttpResponseMessage PostSong(Song song)
 {
     songRepo.Add(song);
     return Request.CreateResponse(HttpStatusCode.OK);
 }
        // PUT api/Song/5
        public HttpResponseMessage PutSong(int id, Song song)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

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

            try
            {
                songRepo.Update(id, song);
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }