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

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

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

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

            return Request.CreateResponse(HttpStatusCode.OK);
        }
Example #2
0
        private static void PrintArtist(Artist artist)
        {
            Console.WriteLine(new string('-', 20));
            Console.WriteLine("{0}\r\n{1}\r\n{2}\r\n", artist.name, artist.DateOfBirth, artist.Country);

            Console.WriteLine("List songs: ");
            if (artist.Songs.Count > 0)
            {
                foreach (var song in artist.Songs)
                {
                    Console.WriteLine(song.Genre.name);
                    Console.WriteLine(song.title);
                }
            }
            else
            {
                Console.WriteLine("No songs.");
            }

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

            Console.WriteLine(new string('-', 20));
        }
        // POST api/Artist
        public HttpResponseMessage PostArtist(Artist artist)
        {
            if (ModelState.IsValid)
            {
                db.Artists.Add(artist);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, artist);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = artist.Id }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }
        public IHttpActionResult Create(ArtistDataModel newArtist)
        {
            if (newArtist == null || newArtist.Name == null)
            {
                return this.BadRequest("You must provide artist name.");
            }

            var artist = new Artist
            {
                Name = newArtist.Name, 
                Country = newArtist.Country,
                Birthday = newArtist.Birthday
            };

            this.db.Artists.Add(artist);
            this.db.SaveChanges();

            return this.Ok(artist);
        }
        public IHttpActionResult Create(ArtistModel item)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var newItem = new Artist()
            {
                Name = item.Name,
                Country = item.Country,
                DateOfBirth = item.DateOfBirth,
            };

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

            item.Id = newItem.Id;

            return this.Ok(item);
        }
            public static void PutArtist(int id)
            {
                Artist artist = new Artist
                {
                    Name = "Bai Ivan!",
                    DateOfBirth = DateTime.Now,
                };

                // PUT api/Artist/5
                var result = client.PutAsXmlAsync(string.Format("api/Artist/{0}", id), artist).Result;

                if (result.IsSuccessStatusCode)
                {
                    Console.WriteLine("Song is put!", id);
                }
                else
                {
                    Console.WriteLine("{0} ({1})", (int)result.StatusCode, result.ReasonPhrase);
                }
            }
            public static void PostArtist(Artist postArtist)
            {
                // POST api/Artist
                HttpResponseMessage postResponse = client.PostAsXmlAsync("api/Artist", postArtist).Result;
                //.ContinueWith((postTask) => postTask.Result.EnsureSuccessStatusCode());

                if (postResponse.IsSuccessStatusCode)
                {
                    Console.WriteLine("Post ok!");
                }
                else
                {
                    Console.WriteLine("{0} ({1})", (int)postResponse.StatusCode, postResponse.ReasonPhrase);
                }
            }
            public static void PostArtist()
            {
                // POST api/Artist
                var postArtist = new Artist
                {
                    Name = "ArtistTEsting",
                    DateOfBirth = DateTime.Now,
                    Country = "Pesho"
                };

                HttpResponseMessage postResponse = client.PostAsXmlAsync("api/Artist", postArtist).Result;
                //.ContinueWith((postTask) => postTask.Result.EnsureSuccessStatusCode());

                if (postResponse.IsSuccessStatusCode)
                {
                    Console.WriteLine("Post ok!");
                }
                else
                {
                    Console.WriteLine("{0} ({1})", (int)postResponse.StatusCode, postResponse.ReasonPhrase);
                }
            }
 // POST api/Artist
 public HttpResponseMessage PostArtist(Artist artist)
 {
     artistRepo.Add(artist);
     return Request.CreateResponse(HttpStatusCode.OK);
 }