public async Task <OperationResult <PlayActivityList> > CreatePlayActivity(User roadieUser, TrackStreamInfo streamInfo) { try { var sw = Stopwatch.StartNew(); var track = this.DbContext.Tracks .Include(x => x.ReleaseMedia) .Include(x => x.ReleaseMedia.Release) .Include(x => x.ReleaseMedia.Release.Artist) .Include(x => x.TrackArtist) .FirstOrDefault(x => x.RoadieId == SafeParser.ToGuid(streamInfo.Track.Value)); if (track == null) { return(new OperationResult <PlayActivityList>($"CreatePlayActivity: Unable To Find Track [{ streamInfo.Track.Value }]")); } if (!track.IsValid) { return(new OperationResult <PlayActivityList>($"CreatePlayActivity: Invalid Track. Track Id [{streamInfo.Track.Value}], FilePath [{track.FilePath}], Filename [{track.FileName}]")); } data.UserTrack userTrack = null; var now = DateTime.UtcNow; try { var user = roadieUser != null?this.DbContext.Users.FirstOrDefault(x => x.RoadieId == roadieUser.UserId) : null; if (user != null) { userTrack = this.DbContext.UserTracks.FirstOrDefault(x => x.UserId == user.Id && x.TrackId == track.Id); if (userTrack == null) { userTrack = new data.UserTrack(now) { UserId = user.Id, TrackId = track.Id }; this.DbContext.UserTracks.Add(userTrack); } userTrack.LastPlayed = now; userTrack.PlayedCount = (userTrack.PlayedCount ?? 0) + 1; this.CacheManager.ClearRegion(user.CacheRegion); await this.DbContext.SaveChangesAsync(); } } catch (Exception ex) { this.Logger.LogError(ex, $"Error in CreatePlayActivity, Creating UserTrack: User `{ roadieUser}` TrackId [{ track.Id }"); } track.PlayedCount = (track.PlayedCount ?? 0) + 1; track.LastPlayed = now; var release = this.DbContext.Releases.Include(x => x.Artist).FirstOrDefault(x => x.RoadieId == track.ReleaseMedia.Release.RoadieId); release.LastPlayed = now; release.PlayedCount = (release.PlayedCount ?? 0) + 1; var artist = this.DbContext.Artists.FirstOrDefault(x => x.RoadieId == release.Artist.RoadieId); artist.LastPlayed = now; artist.PlayedCount = (artist.PlayedCount ?? 0) + 1; data.Artist trackArtist = null; if (track.ArtistId.HasValue) { trackArtist = this.DbContext.Artists.FirstOrDefault(x => x.Id == track.ArtistId); trackArtist.LastPlayed = now; trackArtist.PlayedCount = (trackArtist.PlayedCount ?? 0) + 1; this.CacheManager.ClearRegion(trackArtist.CacheRegion); } this.CacheManager.ClearRegion(track.CacheRegion); this.CacheManager.ClearRegion(track.ReleaseMedia.Release.CacheRegion); this.CacheManager.ClearRegion(track.ReleaseMedia.Release.Artist.CacheRegion); var pl = new PlayActivityList { Artist = new DataToken { Text = track.ReleaseMedia.Release.Artist.Name, Value = track.ReleaseMedia.Release.Artist.RoadieId.ToString() }, TrackArtist = track.TrackArtist == null ? null : new DataToken { Text = track.TrackArtist.Name, Value = track.TrackArtist.RoadieId.ToString() }, Release = new DataToken { Text = track.ReleaseMedia.Release.Title, Value = track.ReleaseMedia.Release.RoadieId.ToString() }, Track = new DataToken { Text = track.Title, Value = track.RoadieId.ToString() }, User = new DataToken { Text = roadieUser.UserName, Value = roadieUser.UserId.ToString() }, PlayedDateDateTime = userTrack?.LastPlayed, ReleasePlayUrl = $"{ this.HttpContext.BaseUrl }/play/release/{ track.ReleaseMedia.Release.RoadieId}", Rating = track.Rating, UserRating = userTrack?.Rating, TrackPlayUrl = $"{ this.HttpContext.BaseUrl }/play/track/{ track.RoadieId}.mp3", ArtistThumbnail = this.MakeArtistThumbnailImage(track.TrackArtist != null ? track.TrackArtist.RoadieId : track.ReleaseMedia.Release.Artist.RoadieId), ReleaseThumbnail = this.MakeReleaseThumbnailImage(track.ReleaseMedia.Release.RoadieId), UserThumbnail = this.MakeUserThumbnailImage(roadieUser.UserId) }; if (!roadieUser.IsPrivate) { try { await this.PlayActivityHub.Clients.All.SendAsync("SendActivity", pl); } catch (Exception ex) { this.Logger.LogError(ex); } } await this.DbContext.SaveChangesAsync(); sw.Stop(); return(new OperationResult <PlayActivityList> { Data = pl, IsSuccess = userTrack != null, OperationTime = sw.ElapsedMilliseconds }); } catch (Exception ex) { this.Logger.LogError(ex, $"CreatePlayActivity RoadieUser `{ roadieUser }` StreamInfo `{ streamInfo }`"); } return(new OperationResult <PlayActivityList>()); }
public void Parse_Guid(string input) { var parsed = SafeParser.ToGuid(input); Assert.NotNull(parsed); }
/// <summary> /// Updates (or Adds) Collection /// </summary> public async Task <OperationResult <bool> > UpdateCollection(User user, Collection model) { var isNew = model.Id == Guid.Empty; var now = DateTime.UtcNow; var sw = new Stopwatch(); sw.Start(); var errors = new List <Exception>(); data.Collection collection = new data.Collection(); if (!isNew) { collection = this.DbContext.Collections.FirstOrDefault(x => x.RoadieId == model.Id); if (collection == null) { return(new OperationResult <bool>(true, string.Format("Collection Not Found [{0}]", model.Id))); } } collection.IsLocked = model.IsLocked; collection.Name = model.Name; collection.SortName = model.SortName; collection.Edition = model.Edition; collection.ListInCSVFormat = model.ListInCSVFormat; collection.ListInCSV = model.ListInCSV; collection.Description = model.Description; collection.URLs = model.URLs; collection.Status = SafeParser.ToEnum <Statuses>(model.Status); collection.CollectionType = SafeParser.ToEnum <CollectionType>(model.CollectionType); collection.Tags = model.TagsList.ToDelimitedList(); collection.URLs = model.URLsList.ToDelimitedList(); collection.AlternateNames = model.AlternateNamesList.ToDelimitedList(); collection.CollectionCount = model.CollectionCount; var collectionImage = ImageHelper.ImageDataFromUrl(model.NewThumbnailData); if (collectionImage != null) { // Ensure is jpeg first collection.Thumbnail = ImageHelper.ConvertToJpegFormat(collectionImage); // Resize to store in database as thumbnail collection.Thumbnail = ImageHelper.ResizeImage(collection.Thumbnail, this.Configuration.MediumImageSize.Width, this.Configuration.MediumImageSize.Height); } if (model.Maintainer?.Value != null) { var maintainer = this.DbContext.Users.FirstOrDefault(x => x.RoadieId == SafeParser.ToGuid(model.Maintainer.Value)); if (maintainer != null) { collection.MaintainerId = maintainer.Id; } } collection.LastUpdated = now; if (isNew) { await this.DbContext.Collections.AddAsync(collection); } await this.DbContext.SaveChangesAsync(); this.CacheManager.ClearRegion(collection.CacheRegion); this.Logger.LogInformation($"UpdateArtist `{ collection }` By User `{ user }`"); return(new OperationResult <bool> { IsSuccess = !errors.Any(), Data = !errors.Any(), OperationTime = sw.ElapsedMilliseconds, Errors = errors }); }
public async Task <OperationResult <bool> > UpdateTrack(User user, Track model) { var didChangeTrack = false; var didChangeThumbnail = false; var sw = new Stopwatch(); sw.Start(); var errors = new List <Exception>(); var track = this.DbContext.Tracks .Include(x => x.ReleaseMedia) .Include(x => x.ReleaseMedia.Release) .Include(x => x.ReleaseMedia.Release.Artist) .FirstOrDefault(x => x.RoadieId == model.Id); if (track == null) { return(new OperationResult <bool>(true, string.Format("Track Not Found [{0}]", model.Id))); } try { var now = DateTime.UtcNow; track.IsLocked = model.IsLocked; track.Status = SafeParser.ToEnum <Statuses>(model.Status); track.Title = model.Title; track.AlternateNames = model.AlternateNamesList.ToDelimitedList(); track.Rating = model.Rating; track.AmgId = model.AmgId; track.LastFMId = model.LastFMId; track.MusicBrainzId = model.MusicBrainzId; track.SpotifyId = model.SpotifyId; track.Tags = model.TagsList.ToDelimitedList(); track.PartTitles = model.PartTitlesList == null || !model.PartTitlesList.Any() ? null : string.Join("\n", model.PartTitlesList); if (model.TrackArtistToken != null) { var artistId = SafeParser.ToGuid(model.TrackArtistToken.Value); if (artistId.HasValue) { var artist = this.GetArtist(artistId.Value); if (artist != null) { track.ArtistId = artist.Id; } } } else { track.ArtistId = null; } var trackImage = ImageHelper.ImageDataFromUrl(model.NewThumbnailData); if (trackImage != null) { // Ensure is jpeg first track.Thumbnail = ImageHelper.ConvertToJpegFormat(trackImage); // Save unaltered image to cover file var trackThumbnailName = track.PathToTrackThumbnail(this.Configuration, this.Configuration.LibraryFolder); File.WriteAllBytes(trackThumbnailName, track.Thumbnail); // Resize to store in database as thumbnail track.Thumbnail = ImageHelper.ResizeImage(track.Thumbnail, this.Configuration.MediumImageSize.Width, this.Configuration.MediumImageSize.Height); didChangeThumbnail = true; } track.LastUpdated = now; await this.DbContext.SaveChangesAsync(); this.CacheManager.ClearRegion(track.CacheRegion); this.Logger.LogInformation($"UpdateTrack `{ track }` By User `{ user }`: Edited Track [{ didChangeTrack }], Uploaded new image [{ didChangeThumbnail }]"); } catch (Exception ex) { this.Logger.LogError(ex); errors.Add(ex); } sw.Stop(); return(new OperationResult <bool> { IsSuccess = !errors.Any(), Data = !errors.Any(), OperationTime = sw.ElapsedMilliseconds, Errors = errors }); }
/// <summary> /// Updates (or Adds) Collection /// </summary> public async Task <OperationResult <bool> > UpdateCollection(User user, Collection model) { var isNew = model.Id == Guid.Empty; var now = DateTime.UtcNow; var sw = new Stopwatch(); sw.Start(); var errors = new List <Exception>(); var collection = new data.Collection(); if (!isNew) { collection = DbContext.Collections.FirstOrDefault(x => x.RoadieId == model.Id); if (collection == null) { return(new OperationResult <bool>(true, string.Format("Collection Not Found [{0}]", model.Id))); } // If collection is being renamed, see if collection already exists with new model supplied name var collectionName = collection.SortNameValue; var collectionModelName = model.SortNameValue; if (collectionName.ToAlphanumericName() != collectionModelName.ToAlphanumericName()) { var existingCollection = DbContext.Collections.FirstOrDefault(x => x.Name == model.Name || x.SortName == model.SortName); if (existingCollection != null) { return(new OperationResult <bool>($"Collection already exists `{ collection }` with name [{ collectionModelName }].")); } } } collection.IsLocked = model.IsLocked; var oldPathToImage = collection.PathToImage(Configuration); var didChangeName = collection.Name != model.Name && !isNew; collection.Name = model.Name; collection.SortName = model.SortName; collection.Edition = model.Edition; collection.ListInCSVFormat = model.ListInCSVFormat; collection.ListInCSV = model.ListInCSV; collection.Description = model.Description; collection.URLs = model.URLs; collection.Status = SafeParser.ToEnum <Statuses>(model.Status); collection.CollectionType = SafeParser.ToEnum <CollectionType>(model.CollectionType); collection.Tags = model.TagsList.ToDelimitedList(); collection.URLs = model.URLsList.ToDelimitedList(); collection.AlternateNames = model.AlternateNamesList.ToDelimitedList(); collection.CollectionCount = model.CollectionCount; if (model.Maintainer?.Value != null) { var maintainer = DbContext.Users.FirstOrDefault(x => x.RoadieId == SafeParser.ToGuid(model.Maintainer.Value)); if (maintainer != null) { collection.MaintainerId = maintainer.Id; } } if (isNew) { await DbContext.Collections.AddAsync(collection); await DbContext.SaveChangesAsync(); } if (didChangeName) { if (File.Exists(oldPathToImage)) { File.Move(oldPathToImage, collection.PathToImage(Configuration)); } } var collectionImage = ImageHelper.ImageDataFromUrl(model.NewThumbnailData); if (collectionImage != null) { // Save unaltered collection image File.WriteAllBytes(collection.PathToImage(Configuration, true), ImageHelper.ConvertToJpegFormat(collectionImage)); } collection.LastUpdated = now; await DbContext.SaveChangesAsync(); CacheManager.ClearRegion(collection.CacheRegion); Logger.LogInformation($"UpdateCollection `{collection}` By User `{user}`"); return(new OperationResult <bool> { IsSuccess = !errors.Any(), Data = !errors.Any(), OperationTime = sw.ElapsedMilliseconds, Errors = errors }); }