Exemplo n.º 1
0
        public static Song GetOrCreateSong(Song song, MusicStoreDb musicStoreDbContext)
        {
            Song existingSong = null;

            song.Artist = GetOrCreateArtist(song.Artist, musicStoreDbContext);

            if (song.Id != 0)
            {
                existingSong = musicStoreDbContext.Songs.FirstOrDefault(s => s.Id == song.Id);
            }
            else if (song.Title != null)
            {
                existingSong = musicStoreDbContext.Songs.FirstOrDefault(s => s.Title == song.Title);
            }

            if (existingSong != null)
            {
                return existingSong;
            }

            musicStoreDbContext.Songs.Add(song);
            musicStoreDbContext.SaveChanges();

            return song;
        }
Exemplo n.º 2
0
        public static void AddSongXml(HttpClient client)
        {
            client.DefaultRequestHeaders.Accept.Add(new
                MediaTypeWithQualityHeaderValue("application/xml"));

            Console.WriteLine("Enter song title");
            string title = Console.ReadLine();
            Console.WriteLine("Enter song year");
            int year = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter song genre code");
            Genre genre = (Genre)int.Parse(Console.ReadLine());
            Console.WriteLine("Enter song description");
            string description = Console.ReadLine();
            Console.WriteLine("Enter song artist id");
            int id = int.Parse(Console.ReadLine());
            Song song = new Song { SongTitle = title, SongYear = year, SongGenre = genre, Description = description, ArtistId = id };
            HttpResponseMessage response =
                client.PostAsXmlAsync("api/songs", song).Result;

            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine("Song added");
            }
            else
            {
                Console.WriteLine("{0} ({1})",
                    (int)response.StatusCode, response.ReasonPhrase);
            }
        }
Exemplo n.º 3
0
        private static void AddTwoSongs(HttpClient client, Random rand, Artist firstArtist)
        {
            var song1 = new Song { Title = "Song-" + GetRandomSuffix(rand), Artist = firstArtist, Year = rand.Next(1980,2013) };
            var song2 = new Song { Title = "Song-" + GetRandomSuffix(rand), Artist = firstArtist, Year = rand.Next(1980, 2013) };

            Console.WriteLine("\nTwo new songs");
            SongsController.Add(song1, client);
            SongsController.Add(song2, client);
            Console.WriteLine();
        }
Exemplo n.º 4
0
 public SongApiModel(Song song)
 {
     if (song != null)
     {
         this.Id = song.Id;
         this.Title = song.Title;
         this.Year = song.Year;
         this.Genre = song.Genre;
     }
 }
Exemplo n.º 5
0
        public static void Delete(Song song, HttpClient client)
        {
            var postArtist = client.DeleteAsync("Songs/" + song.Id).Result;

            if (postArtist.IsSuccessStatusCode)
            {
                Console.WriteLine(postArtist.Headers.Location);
            }
            else
            {
                Console.WriteLine(postArtist.ReasonPhrase);
            }
        }
Exemplo n.º 6
0
        public SongFullApi(Song song)
            : base(song)
        {
            if (song != null)
            {
                if (song.Album != null)
                {
                    this.Album = new AlbumApi(song.Album);                    
                }

                if (song.Artist != null)
                {
                    this.Artist = new ArtistApi(song.Artist);                    
                }
            }
        }
Exemplo n.º 7
0
        public SongApi(Song song)
            : base(song)
        {
            if (song != null)
            {
                if (song.Album != null)
                {
                    this.Album = song.Album.Title;                    
                }

                if (song.Artist != null)
                {
                    this.Artist = song.Artist.Name;                    
                }
            }
        }
Exemplo n.º 8
0
        public static void Update(Song song, HttpClient client)
        {
            var serializedContent = JsonConvert.SerializeObject(song);

            var content = new StringContent(serializedContent, Encoding.UTF8, "application/json");

            var putArtist = client.PutAsync("Songs/" + song.Id, content).Result;

            if (putArtist.IsSuccessStatusCode)
            {
                Console.WriteLine(putArtist.Headers.Location);
            }
            else
            {
                Console.WriteLine(putArtist.ReasonPhrase);
            }
        }
Exemplo n.º 9
0
        public static SongModel Convert(Song song)
        {
            SongModel model = new SongModel
            {
                SongId = song.SongId,
                SongTitle = song.SongTitle,
                SongYear = song.SongYear,
                SongGenre = song.SongGenre,
                Description = song.Description,
                Artist = new ArtistModel
                {
                    ArtistId = song.SongArtist.ArtistId,
                    Name = song.SongArtist.Name,
                    DateOfBirth = song.SongArtist.DateOfBirth,
                    Country = song.SongArtist.Country
                }
            };

            return model;
        }
        public IHttpActionResult Post([FromBody]SongRequestModel song)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

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

            this.repository.Insert(newSong);
            this.repository.SaveChanges();

            return this.Ok(newSong);
        }
Exemplo n.º 11
0
        public IHttpActionResult Post([FromBody]SongDataModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            if (this.data.Genres.Find(model.GenreId) == null)
            {
                return this.BadRequest(GlobalContants.GenreNotFoundMessage);
            }

            var song = new Song
            {
                Title = model.Title,
                GenreId = model.GenreId,
            };

            this.data.Songs.Add(song);
            this.data.Savechanges();

            return this.Created(this.Url.ToString(), song);
        }
Exemplo n.º 12
0
        public static void UpdateSong(HttpClient client, int songId, string title, int year, string producer, int artistId)
        {
            var song = new Song()
            {
                Title = title,
                Year = year,
                Producer = producer,
                ArtistId = artistId
            };

            var content = new StringContent(JsonConvert.SerializeObject(song));
            content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(ContentType);

            var response = client.PutAsync("api/songs/update/" + songId, content).Result;

            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine("Song updated successfully.");
            }
            else
            {
                Console.WriteLine("Song have not been updated.");
            }
        }
        public IHttpActionResult Create(SongRequestModel song)
        {
            if (!this.ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var newSong= new Song
            {
                Genre = (GenreType)song.Genre,
                Title = song.Title,
                Year = song.Year
            };

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

            song.Id = newSong.Id;

            return Ok(song);
        }
        public List<Song> GenerateSongs(IRandomGenerator generator, int count)
        {
            var songs = new List<Song>();

            for (var i = 0; i < count; i++)
            {
                var song = new Song()
                {
                    Title = generator.GetRandomString(generator.GetRandomNumber(5, 20)),
                    Genre = generator.GetRandomString(generator.GetRandomNumber(5, 20)),
                    Year = generator.GetRandomNumber(DateTime.Now.AddYears(-50).Year, DateTime.Now.Year),
                    ArtistId = null,
                    AlbumId = null
                };
                songs.Add(song);
            }
            return songs;
        }
Exemplo n.º 15
0
        // POST api/Songs
        public HttpResponseMessage PostSong(Song song)
        {
            if (ModelState.IsValid)
            {
                Artist songArtist = null;

                if (song.Artist != null)
                {
                    songArtist = DataObjectsManager.GetOrCreateArtist(song.Artist, db);
                }

                song.Artist = songArtist;

                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);
            }
        }
Exemplo n.º 16
0
        // 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);
        }