public string GetFileName(string format, AlbumTrack track, string extension = null) { if (string.IsNullOrWhiteSpace(format)) { throw new ArgumentNullException("format"); } if (track == null) { throw new ArgumentNullException("track"); } format = format.Replace("<Year>", track.ReleaseDate); format = format.Replace("<Artist>", track.Artist); format = format.Replace("<Album>", track.Album); format = format.Replace("<Title>", track.Title); format = format.Replace("<Song>", track.Title); int trackNumber; int.TryParse(track.TrackNumber, out trackNumber); format = format.Replace("<Track>", string.Format("{0:00}", trackNumber)); format = ApplyFormattingRules(format); if (extension != null) { format += extension; } return(format); }
public bool CreateTrack(Track track, string albumId) { this.db.Tracks.Add(track); var trackId = track.Id; var albumIdGuid = this.db.Albums.FirstOrDefault(a => a.Id.ToString() == albumId).Id; var albumTrack = new AlbumTrack { AlbumId = albumIdGuid, TrackId = trackId }; this.db.AlbumsTracks.Add(albumTrack); try { this.db.SaveChanges(); } catch (Exception e) { //TODO: handle error Console.WriteLine(e.Message); return(false); } return(true); }
public string CreateTrack(Track track, string albumId) { track.Link = WebUtility.UrlDecode(track.Link); //decimal price = decimal.Parse(track.Price); string albumIdhiddenInTheHtml = albumId; bool trackExists = this.Context.Tracks.Any(x => x.Name == track.Name); if (!trackExists) { this.Context.Tracks.Add(track); this.Context.SaveChanges(); } var trackAlbum = new AlbumTrack { AlbumId = this.Context.Albums.Find(albumIdhiddenInTheHtml).Id, TrackId = this.Context.Tracks.First(x => x.Name == track.Name).Id }; bool trackAlbumExist = this.Context.AlbumsTracks.Any(x => x.AlbumId == trackAlbum.AlbumId && x.TrackId == trackAlbum.TrackId); if (!trackAlbumExist) { this.Context.Add(trackAlbum); this.Context.SaveChanges(); } return(track.Id); }
public bool Create(string name, string link, decimal price, string albumId) { if (!context.Albums.Any(p => p.Id == albumId)) { return(false); } var track = new Track { Name = name, Link = link, Price = price, }; var trackAlbum = new AlbumTrack { Track = track, Album = context.Albums.SingleOrDefault(p => p.Id == albumId) }; context.AlbumTracks.Add(trackAlbum); context.SaveChanges(); return(true); }
public bool Create(string albumId, string name, string link, decimal price) { using (RunesDbContext db = new RunesDbContext()) { Track track = new Track() { Id = Guid.NewGuid().ToString(), Name = name, Link = link, Price = price, }; if (!Validation.TryValidate(track)) { return(false); } AlbumTrack albumTrack = new AlbumTrack() { Track = track, AlbumId = albumId }; db.AlbumsTracks.Add(albumTrack); db.SaveChanges(); return(true); } }
public AlbumTrackViewModel Map(AlbumTrack a) { return(new AlbumTrackViewModel { Id = a.TrackId, DurationInSec = a.DurationInSeconds, TrackNumber = a.TrackNumber, Title = a.Title }); }
public IHttpResponse CreatePost(IHttpRequest request) { Guid albumId = Guid.Parse(request.QueryData["albumId"].ToString() .Split('=', StringSplitOptions.RemoveEmptyEntries)[1]); viewBag[Constants.AlbumIdPlaceholder] = albumId.ToString(); var album = Context.Albums.Find(albumId); string trackArtist = request.FormData["artist"].ToString().Split('=')[1]; string trackTitle = request.FormData["title"].ToString().Split('=')[1]; if (Context.Tracks.Any(t => t.Artist == trackArtist && t.Title == trackTitle)) { viewBag[Constants.PageErrorPlaceholder] = string.Format( Constants.ErrorMessage, $"Track '{trackArtist} - {trackTitle}' already exists!"); return(BuildView(request.Path, request)); } var genreDescription = request.FormData["genre"].ToString().Split('=')[1]; if (!Enum.TryParse(typeof(MusicGenre), genreDescription, true, out object genre)) { genre = typeof(MusicGenre).GetFields() .SelectMany(f => f.GetCustomAttributes(typeof(DescriptionAttribute), false), (f, a) => new { Field = f, Att = a }) .Where(a => ((DescriptionAttribute)a.Att).Description == genreDescription) .SingleOrDefault().Field.Name; } string trackUrl = request.FormData["link"].ToString().Split('=', 2)[1]; decimal trackPrice = decimal.Parse(request.FormData["price"].ToString().Split('=')[1]); var track = new Track() { Artist = trackArtist, Title = trackTitle, Genre = Enum.Parse <MusicGenre>(genre.ToString()), Link = trackUrl, Price = trackPrice }; Context.Tracks.Add(track); var albumTrack = new AlbumTrack() { AlbumId = albumId, TrackId = track.Id }; album.AlbumTracks.Add(albumTrack); album.Price += track.Price; Context.SaveChanges(); request.QueryData["trackId"] = $"trackId={track.Id.ToString()}"; BrowseOne(request); return(BuildView(Constants.TrackDetailsViewRoute, request, HttpResponseStatusCode.SeeOther)); }
public void AddAlbumTrack(Guid albumId, Guid trackId) { var album = Context.Albums.Find(albumId); var albumTrack = new AlbumTrack() { AlbumId = albumId, TrackId = trackId }; album.AlbumTracks.Add(albumTrack); var track = Context.Tracks.Find(trackId); album.Price += track.Price; Context.SaveChanges(); }
public IHttpResponse CreatePost(IHttpRequest request) { Guid albumId = Guid.Parse(request.QueryData["albumId"].ToString() .Split('=', StringSplitOptions.RemoveEmptyEntries)[1]); viewBag[Constants.AlbumIdPlaceholder] = albumId.ToString(); var album = Context.Albums.Find(albumId); string trackArtist = request.FormData["artist"].ToString().Split('=')[1]; string trackTitle = request.FormData["title"].ToString().Split('=')[1]; if (Context.Tracks.Any(t => t.Artist == trackArtist && t.Title == trackTitle)) { viewBag[Constants.ErrorMessagePlaceholder] = string .Format(Constants.TrackExistsError, trackArtist, trackTitle); return(BuildView(request.Path, request)); } string genreDisplayName = request.FormData["genre"].ToString().Split('=')[1]; var genre = Enumerator.ToEnumOrDefault <MusicGenre>(genreDisplayName); string trackUrl = request.FormData["link"].ToString().Split('=', 2)[1]; decimal trackPrice = decimal.Parse(request.FormData["price"].ToString().Split('=')[1]); var track = new Track() { Artist = trackArtist, Title = trackTitle, Genre = genre, Link = trackUrl, Price = trackPrice }; Context.Tracks.Add(track); var albumTrack = new AlbumTrack() { AlbumId = albumId, TrackId = track.Id }; album.AlbumTracks.Add(albumTrack); album.Price += track.Price; Context.SaveChanges(); viewBag[Constants.TrackIdPlaceholder] = track.Id.ToString(); return(BuildView(Constants.TrackDetailsViewRoute, request, HttpResponseStatusCode.SeeOther)); }
public override async Task <MAlbum> Update(int id, AlbumUpsertRequest request) { var entity = _context.Albums.Find(id); _context.Albums.Attach(entity); _context.Albums.Update(entity); foreach (var TrackID in request.Tracks) { var albumTrack = await _context.AlbumTracks .Where(i => i.TrackID == TrackID && i.AlbumID == id) .SingleOrDefaultAsync(); if (albumTrack == null) { var newAlbumTrack = new AlbumTrack() { AlbumID = id, TrackID = TrackID }; await _context.Set <AlbumTrack>().AddAsync(newAlbumTrack); } } foreach (var TrackID in request.TracksToDelete) { var albumTrack = await _context.AlbumTracks .Where(i => i.TrackID == TrackID && i.AlbumID == id) .SingleOrDefaultAsync(); if (albumTrack != null) { _context.Set <AlbumTrack>().Remove(albumTrack); } } _mapper.Map(request, entity); await _context.SaveChangesAsync(); return(_mapper.Map <MAlbum>(entity)); }
public IHttpResponse DoCreate(DoCreateTrackInputModel model) { if (!this.IsAuthenticated()) { return(this.Redirect("/Users/Login")); } var link = WebUtility.UrlDecode(model.Link).Replace("watch?v=", "embed/"); // src="https://www.youtube.com/watch?v=_avb2ikX-rQ<iframe width="640" height="480" src="https://www.youtube.com/embed/_avb2ikX-rQ" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>" var albumId = this.Request.QueryData["albumId"].ToString().ToUpper(); var track = new Track { Name = model.Name.Trim(), Link = link, Price = decimal.Parse(model.Price) }; this.Db.Tracks.Add(track); var trackId = track.Id; var albumIdGuid = this.Db.Albums.FirstOrDefault(a => a.Id.ToString() == albumId).Id; var albumTrack = new AlbumTrack { AlbumId = albumIdGuid, TrackId = trackId }; this.Db.AlbumsTracks.Add(albumTrack); try { this.Db.SaveChanges(); } catch (Exception e) { //TODO: log error return(this.ServerError(e.Message)); } return(this.Create(this.Request)); }
public override async Task <MAlbum> Insert(AlbumUpsertRequest request) { var entity = _mapper.Map <Album>(request); await _context.Albums.AddAsync(entity); await _context.SaveChangesAsync(); foreach (var trackID in request.Tracks) { var albumTrack = new AlbumTrack() { AlbumID = entity.AlbumID, TrackID = trackID }; _context.AlbumTracks.Add(albumTrack); } await _context.SaveChangesAsync(); return(_mapper.Map <MAlbum>(entity)); }
public string CreateTrack(IHttpRequest request) { string trackName = request.FormData[IRunesConstants.TrackFormNameHolder].ToString(); string trackLink = WebUtility.UrlDecode(request.FormData[IRunesConstants.TrackUrlFormHolder].ToString()); decimal price = decimal.Parse(request.FormData[IRunesConstants.TrackPriceFormHolder].ToString()); string albumIdhiddenInTheHtml = request.FormData[IRunesConstants.AlbumPlaceHolderTrackCreateForm].ToString(); var track = new Track { Name = trackName, Link = trackLink, Price = price }; bool trackExists = this.Context.Tracks.Any(x => x.Name == track.Name); if (!trackExists) { this.Context.Tracks.Add(track); this.Context.SaveChanges(); } var trackAlbum = new AlbumTrack { AlbumId = this.Context.Albums.Find(albumIdhiddenInTheHtml).Id, TrackId = this.Context.Tracks.First(x => x.Name == track.Name).Id }; bool trackAlbumExist = this.Context.AlbumsTracks.Any(x => x.AlbumId == trackAlbum.AlbumId && x.TrackId == trackAlbum.TrackId); if (!trackAlbumExist) { this.Context.Add(trackAlbum); this.Context.SaveChanges(); } return(albumIdhiddenInTheHtml); }
public string AddTrack(CreateTrackViewModel viewModel) { var track = new Track() { Name = viewModel.Name, Link = viewModel.Link, Price = viewModel.Price, }; var albumTrack = new AlbumTrack() { Track = track, AlbumId = viewModel.AlbumId }; this.dbContext .AlbumTracks .Add(albumTrack); this.dbContext .SaveChanges(); return(albumTrack.TrackId); }
protected override void GetAlbumInfoWithTimer() { log.Debug("LastFM: Looking up Album on LastFM"); Albums.Clear(); try { var lastfmAlbum = LastFMLibrary.GetAlbumInfo(SwitchArtist(ArtistName), AlbumName); var album = new Album(); album.Artist = lastfmAlbum.ArtistName; album.Title = lastfmAlbum.AlbumName; album.SmallImageUrl = lastfmAlbum.Images.First(image => image.ImageSize == LastFMImage.LastFMImageSize.Small).ImageURL; album.MediumImageUrl = lastfmAlbum.Images.First(image => image.ImageSize == LastFMImage.LastFMImageSize.Large).ImageURL; album.LargeImageUrl = lastfmAlbum.Images.First(image => image.ImageSize == LastFMImage.LastFMImageSize.Mega).ImageURL; var discs = new List<List<AlbumTrack>>(); var albumTracks = new List<AlbumTrack>(); var i = 0; foreach (var track in lastfmAlbum.Tracks) { i++; AlbumTrack albumtrack = new AlbumTrack(); albumtrack.Title = track.TrackTitle; albumtrack.Duration = track.Duration.ToString(); albumtrack.Number = i; albumTracks.Add(albumtrack); } discs.Add(albumTracks); album.Discs = discs; Albums.Add(album); log.Debug("LastFM: Found {0} albums", Albums.Count); } catch (Exception ex) { log.Debug("LastFM: Exception receiving Album Information. {0} {1}", ex.Message, ex.StackTrace); } }
private Track AddTestTrackToDb(SpotifyDbContext context, string path) { Author testAuthor = new Author { CreatedOn = DateTime.Now, UpdatedOn = DateTime.Now, Name = "Ionics", Description = "", Plays = 0, Avatar = "/data/author-avatar/0" }; context.Authors.Add(testAuthor); context.SaveChanges(); Album testAlbum = new Album { CreatedOn = DateTime.Now, UpdatedOn = DateTime.Now, PublishedOn = DateTime.Now, Description = "", Authors = new List <AlbumAuthor>(), CreatedById = 1, Title = "Awkward Mystery", Plays = 0, Cover = "/data/album-cover/0" }; context.Albums.Add(testAlbum); context.SaveChanges(); Track testTrack = new Track { CreatedOn = DateTime.Now, CreatedById = 1, AlbumId = testAlbum.AlbumId, Title = "Awkward Mystery", Duration = 2 * 60 + 6, Plays = 0, Path = path }; context.Tracks.Add(testTrack); context.SaveChanges(); TrackAuthor testTrackAuthor = new TrackAuthor { TrackId = testTrack.TrackId, AuthorId = testAuthor.AuthorId }; context.TrackAuthor.Add(testTrackAuthor); context.SaveChanges(); AlbumTrack testTrackAlbum = new AlbumTrack { TrackId = testTrack.TrackId, AlbumId = testAlbum.AlbumId }; context.AlbumTrack.Add(testTrackAlbum); context.SaveChanges(); AlbumAuthor albumAuthor = new AlbumAuthor { AlbumId = testAlbum.AlbumId, AuthorId = testAuthor.AuthorId }; context.AlbumAuthor.Add(albumAuthor); context.SaveChanges(); return(testTrack); }
private Album GetRelease(int releaseid) { var release = _discogs.GetMasterRelease(releaseid); var album = new Album(); album.LargeImageUrl = release.Aggregate.PrimaryImage.Uri; album.Artist = release.Aggregate.JoinedArtists; album.CoverHeight = release.Aggregate.PrimaryImage.Height.ToString(); album.CoverWidth = release.Aggregate.PrimaryImage.Width.ToString(); album.Title = release.Title; album.Year = release.Year.ToString(); album.DiscCount = 1; // Get the Tracks var discs = new List<List<AlbumTrack>>(); var albumTracks = new List<AlbumTrack>(); var numDiscs = 1; var lastPosOnAlbumSideA = 0; foreach (var track in release.Tracklist) { var pos = track.Position; var albumTrack = new AlbumTrack(); if (string.IsNullOrEmpty(track.Position) || string.IsNullOrEmpty(track.Title)) { continue; } // check for Multi Disc Album if (track.Position.Contains("-")) { album.DiscCount = Convert.ToInt16(track.Position.Substring(0, track.Position.IndexOf("-", StringComparison.Ordinal))); // Has the number of Discs changed? if (album.DiscCount != numDiscs) { numDiscs = album.DiscCount; discs.Add(new List<AlbumTrack>(albumTracks)); albumTracks.Clear(); } pos = track.Position.Substring(track.Position.IndexOf("-", StringComparison.Ordinal) + 1); } else if (!track.Position.Substring(0, 1).All(Char.IsDigit)) { // The Master Release returned was a Vinyl Album with side A and B. So we have tracks as "A1", "A2", ... "B1",.. pos = track.Position.Substring(1); if (track.Position.Substring(0, 1) == "A") { lastPosOnAlbumSideA = Convert.ToInt16(pos); } else { pos = (lastPosOnAlbumSideA + Convert.ToInt16(pos)).ToString(); } } albumTrack.Number = Convert.ToInt16(pos); albumTrack.Title = track.Title; albumTrack.Duration = track.Duration; albumTracks.Add(albumTrack); } discs.Add(albumTracks); album.Discs = discs; return album; }
public IHttpResponse PostCreateTrack(DoTrackViewModel model) { Regex trackNameRegex = new Regex(@"^\w{3,30}$"); Regex linkUrlRegex = new Regex(@"^\b((http|https):\/\/?)[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|\/?))$"); string albumId = this.Request.QueryData["albumId"].ToString(); string backToAlbumPath = StringExtensions.UrlDecode($"'/Albums/Details?id={albumId}'"); model.Link = StringExtensions.UrlDecode(model.Link); decimal trackPrice = 0; bool IsDecimal = decimal.TryParse(model.Price.ToString(), out trackPrice); Track track = new Track() { Id = Guid.NewGuid().ToString(), Name = model.Name, Price = trackPrice, Link = model.Link }; AlbumTrack albumTrack = new AlbumTrack() { AlbumId = albumId, TrackId = track.Id }; if (!IsDecimal || !linkUrlRegex.Match(model.Link).Success || !trackNameRegex.Match(model.Name).Success || track.Price < 0 ) { CreateTrackViewModel createTrackViewModel = new CreateTrackViewModel() { BackToAlbum = backToAlbumPath, ErrorMessage = InvalidTrackInformationError, CreatePath = WebUtility.UrlDecode($"/Tracks/Create?albumId={albumId}") }; return(this.View("createTrack", HttpResponseStatusCode.BadRequest, createTrackViewModel)); } else if (this.Context.AlbumTracks.Where(at => at.AlbumId == albumId).Any(tr => tr.Track.Name == model.Name)) { CreateTrackViewModel createTrackViewModel = new CreateTrackViewModel() { BackToAlbum = backToAlbumPath, ErrorMessage = TrackAlreadyExistsError, CreatePath = WebUtility.UrlDecode($"/Tracks/Create?albumId={albumId}") }; return(this.View("createTrack", HttpResponseStatusCode.BadRequest, createTrackViewModel)); } using (this.Context) { this.Context .Add(track); this.Context .Add(albumTrack); this.Context .Albums .First(album => album.Id == albumId) .Tracks .Add(albumTrack); this.Context .SaveChanges(); } TrackViewModel viewModel = new TrackViewModel() { Link = track.Link, Name = track.Name, Price = $"{track.Price.ToString(CultureInfo.InvariantCulture):f2}", BackToAlbum = backToAlbumPath, CreatePath = StringExtensions.UrlDecode($"/Tracks/Create?albumId={albumId}") }; return(this.View("track", HttpResponseStatusCode.Ok, viewModel)); }
private async Task<Album> GetAlbumQuery(string artistName, string albumName) { // If we have an artist in form "LastName, FirstName" change it to "FirstName LastName" to have both results var artistNameOriginal = _switchedArtist.IsMatch(artistName) ? string.Format(" OR {0}",SwitchArtist(artistName)) : ""; var query = new QueryParameters<Release>(); query.Add("artist", string.Format("{0} {1}", artistName, artistNameOriginal)); query.Add("release", albumName); var albums = await Release.SearchAsync(query); // First look for Albums from the selected country in AmazonSites var mbAlbum = albums.Items.FirstOrDefault(r => (r.Title != null && r.Title.ToLower() == albumName.ToLower()) && (r.Country != null && r.Country.ToLower() == Options.MainSettings.AmazonSite.ToLower())); if (mbAlbum == null) { // Look for European wide release mbAlbum = albums.Items.FirstOrDefault(r => (r.Title != null && r.Title.ToLower() == albumName.ToLower()) && (r.Country != null && r.Country.ToLower() == "xe")); if (mbAlbum == null) { // Look for US release mbAlbum = albums.Items.FirstOrDefault(r => (r.Title != null && r.Title.ToLower() == albumName.ToLower()) && (r.Country != null && r.Country.ToLower() == "us")); if (mbAlbum == null) { mbAlbum = albums.Items.Count > 0 ? albums.Items[0] : null; if (mbAlbum == null) { return null; } } } } var release = await Release.GetAsync(mbAlbum.Id, new[] { "recordings", "media", "artists", "discids" }); var album = new Album(); album.LargeImageUrl = release.CoverArtArchive != null && release.CoverArtArchive.Front ? string.Format(@"http://coverartarchive.org/release/{0}/front.jpg", release.Id) : ""; album.CoverHeight = "0"; album.CoverWidth = "0"; album.Artist = JoinArtists(release.Credits); album.Title = release.Title; album.Year = release.Date; album.DiscCount = release.MediumList.Items.Count; // Get the Tracks var discs = new List<List<AlbumTrack>>(); foreach (var medium in release.MediumList.Items) { var albumTracks = new List<AlbumTrack>(); foreach (var track in medium.Tracks.Items) { AlbumTrack albumtrack = new AlbumTrack(); albumtrack.Number = track.Position; TimeSpan duration = TimeSpan.FromMilliseconds(track.Recording.Length); albumtrack.Duration = string.Format("{0:mm\\:ss}", duration); albumtrack.Title = track.Recording.Title; albumTracks.Add(albumtrack); } discs.Add(albumTracks); } album.Discs = discs; return album; }
private Album FillAlbum(XmlNode node) { Album album = new Album(); foreach (XmlNode childNode in node) { if (childNode.Name == "ASIN") album.Asin = childNode.InnerText; if (childNode.Name == "SmallImage") { album.SmallImageUrl = GetNode(childNode, "URL"); album.CoverWidth = GetNode(childNode, "Width"); album.CoverHeight = GetNode(childNode, "Height"); } if (childNode.Name == "MediumImage") { album.MediumImageUrl = GetNode(childNode, "URL"); album.CoverWidth = GetNode(childNode, "Width"); album.CoverHeight = GetNode(childNode, "Height"); } if (childNode.Name == "LargeImage") { album.LargeImageUrl = GetNode(childNode, "URL"); album.CoverWidth = GetNode(childNode, "Width"); album.CoverHeight = GetNode(childNode, "Height"); } if (childNode.Name == "ItemAttributes") { foreach (XmlNode attributeNode in childNode) { if (attributeNode.Name == "Artist") album.Artist = attributeNode.InnerText; if (attributeNode.Name == "Title") album.Title = attributeNode.InnerText; if (attributeNode.Name == "ReleaseDate") album.Year = attributeNode.InnerText; if (attributeNode.Name == "Binding") album.Binding = attributeNode.InnerText; if (attributeNode.Name == "Label") album.Label = attributeNode.InnerText; } } if (childNode.Name == "Tracks") { // The node starts with a "<Disc Number Node" , we want all subnodes of it List<List<AlbumTrack>> discs = new List<List<AlbumTrack>>(); foreach (XmlNode discNode in childNode.ChildNodes) { List<AlbumTrack> tracks = new List<AlbumTrack>(); foreach (XmlNode trackNode in discNode) { AlbumTrack track = new AlbumTrack(); track.Number = Convert.ToInt32(trackNode.Attributes["Number"].Value); track.Title = trackNode.InnerText; tracks.Add(track); } discs.Add(tracks); } album.Discs = discs; } } return album; }