Ejemplo n.º 1
0
        public SongVM CreateSong(NewSongVM song) {
            //TODO: handle many authors separeted with coma case
            //List<int> authorIds = _db.AuthorSongs.Where(authorsong => authorsong.SongId == songId).Select(author => author.AuthorId).ToList();
            //List<String> authors = _db.Authors.Where(a => authorIds.Contains(a.Id)).Select(a => a.Name).ToList();
            //Tag genre = tags.Where(t => t.Name != song.Name && !authors.Contains(t.Name)).FirstOrDefault();

            Author author = _db.Authors.FirstOrDefault(a => a.Name == song.AuthorName);
            if (author == null)
                _db.Authors.Add(new Author
                {
                    Name = song.AuthorName
                });
            Song createdSong = new Song() { 
                Name = song.Name,
                Link = song.Link
            };
            _db.Songs.Add(createdSong);
            AuthorSong authorSong = new AuthorSong { 
                AuthorId = author.Id,
                SongId = createdSong.Id
            };
            _db.AuthorSongs.Add(authorSong);

            //New tag managing
            Tag genreTag = _db.Tags.FirstOrDefault(t => t.Id == song.GenreTagId);
            if (genreTag == null)
                //okay, this should never EVER happen
                throw new Exception("That genre doesn't exist in our tag database anymore.");
            Tag authorTag = _db.Tags.FirstOrDefault(t => t.Name == song.AuthorName && t.ParentId == genreTag.Id);
            if (authorTag == null)
                //we need to add another tag for this author for this genre for better search results
                authorTag = _db.Tags.Add(new Tag {
                    Name = song.AuthorName,
                    ParentId = genreTag.Id,
                    Popularity = 0
                });
            Tag songTag = _db.Tags.FirstOrDefault(t => t.Name == song.Name && t.ParentId == authorTag.Id);
            if (songTag == null)
                //we need to add another tag for this song name that is from author called AuthorName in specific genre of music
                _db.Tags.Add(new Tag {
                    Name = song.Name,
                    ParentTag = authorTag,
                    Popularity = 0
                });
            _db.SaveChanges();
                
            return new SongVM(createdSong);
        }
        public HttpResponseMessage CreateSong(NewSongVM song) {
            HttpResponseMessage responseMsg;
            if (!ModelState.IsValid)
            {
                return new HttpResponseMessage(HttpStatusCode.BadRequest);
            }
            try {
                SongVM createdSong = _mngr.CreateSong(song);

                responseMsg = _helper.CreateCustomResponseMsg(new List<SongVM>() { createdSong }, HttpStatusCode.OK);
            } catch (Exception e) {
                responseMsg = _helper.CreateErrorResponseMsg(new HttpError(e.Message), HttpStatusCode.InternalServerError);
            }

            return responseMsg;
        }