public async Task <bool> DeleteSeriesWatched(SeriesWatched seriesWatched) { using (var trans = _context.Database.BeginTransaction(_capPublisher, autoCommit: false)) { try { _context.SeriesWatched.Remove(seriesWatched); await _context.SaveChangesAsync(); var seriesWatchedEvent = _mapper.Map <SeriesWatchedDeletedEvent>(seriesWatched); await _capPublisher.SendEvent(EventInfo.SeriesWatchedDeleted, seriesWatchedEvent); await trans.CommitAsync(); return(true); } catch (Exception e) { await trans.RollbackAsync(); _logger.LogError(e, $"Unexpected error while deleting series watched" + $" SeriesId:{seriesWatched.SeriesId} UserId:{seriesWatched.ViewerId}"); return(false); } } }
public async Task <bool> CreateSeriesWatched(Series series, string viewerId) { using (var trans = _context.Database.BeginTransaction(_capPublisher, autoCommit: false)) { try { var seriesWatched = new SeriesWatched { SeriesId = series.SeriesId, ViewerId = viewerId }; _context.SeriesWatched.Add(seriesWatched); await _context.SaveChangesAsync(); var seriesWatchedEvent = _mapper.Map <SeriesWatchedEvent>(seriesWatched); await _capPublisher.SendEvent(EventInfo.SeriesWatchedCreated, seriesWatchedEvent); await trans.CommitAsync(); return(true); } catch (Exception e) { await trans.RollbackAsync(); _logger.LogError(e, $"Unexpected error while creating series watched" + $" SeriesId:{series.SeriesId} UserId:{viewerId}"); return(false); } } }
public async Task <bool> CreateEpisodeWatched(Episode episode, string viewerId) { using (var trans = _context.Database.BeginTransaction(_capPublisher, autoCommit: false)) { try { if (!await IsSeriesWatchedByViewer(episode.SeriesId, viewerId)) { var seriesWatched = new SeriesWatched { SeriesId = episode.SeriesId, ViewerId = viewerId }; _context.SeriesWatched.Add(seriesWatched); await _context.SaveChangesAsync(); var seriesWatchedEvent = _mapper.Map <SeriesWatchedEvent>(seriesWatched); await _capPublisher.SendEvent(EventInfo.SeriesWatchedCreated, seriesWatchedEvent); } var episodeWatched = new EpisodeWatched { EpisodeId = episode.EpisodeId, ViewerId = viewerId }; _context.EpisodeWatched.Add(episodeWatched); var episodeWatchedEvent = _mapper.Map <EpisodeWatchedEvent>(episodeWatched); await _context.SaveChangesAsync(); await _capPublisher.SendEvent(EventInfo.EpisodeWatchedCreated, episodeWatchedEvent); await trans.CommitAsync(); return(true); } catch (Exception e) { await trans.RollbackAsync(); _logger.LogError(e, $"Unexpected error while creating episode watched" + $" EpisodeId:{episode.EpisodeId} UserId:{viewerId}"); return(false); } } }