/// <summary> /// Сохраняет релиз /// </summary> /// <param name="music">Музыкальный релиз</param> /// <returns>Id сущности музыка</returns> private int SaveRelease(DbMusic music) { //проверяем существует ли релиз var musicEntity = _unitOfWork.MusicRepository.Queryable(). Include(a => a.Genres). Include(m => m.Musicians). FirstOrDefault(mr => mr.Title.Equals(music.Title) && mr.Artist.Equals(music.Artist)); if (musicEntity != null) { //обновляем релиз musicEntity.Genres = music.Genres; musicEntity.Musicians = music.Musicians; musicEntity.ReleaseDate = music.ReleaseDate; musicEntity.Duration = music.Duration; musicEntity.Type = music.Type; musicEntity.PosterImagePath = music.PosterImagePath; _unitOfWork.MusicRepository.Update(musicEntity); _unitOfWork.SaveChanges(); return(musicEntity.Id); } //создаем новый релиз _unitOfWork.MusicRepository.Insert(music); _unitOfWork.SaveChanges(); return(music.Id); }
/// <summary> /// Мапирует релиз сайта album-info в музыкальный релиз /// </summary> /// <param name="release">Релиз сайта album-info</param> /// <returns>Музыкальный релиз</returns> private DbMusic MapAlbumInfoReleaseToMusic(AlbumInfoRelease release) { var music = new DbMusic(); var releaseName = PcreRegex.Match(release.Name, Helper.ReleaseNamePattern).Value; var releaseType = PcreRegex.Matches(release.Name, Helper.ReleaseTypePattern) .Select(m => m.Value).LastOrDefault(); var releaseArtist = release.Name.Substring(0, release.Name.IndexOf(" - ", StringComparison.Ordinal)); var releaseGenres = PcreRegex.Matches(release.Genre, Helper.ReleaseGenrePattern).Select(m => m.Value); var dateFormat = release.Date.Length > 4 ? Helper.LongDateFormat : Helper.ShortDateFormat; var provider = CultureInfo.CreateSpecificCulture(Const.CultureInfoText); var releaseDate = DateTime.ParseExact(release.Date, dateFormat, provider); SaveMusician(music, releaseArtist); SaveGenre(music, releaseGenres); music.Title = string.IsNullOrEmpty(releaseName) ? Helper.UnknownName : releaseName; music.ReleaseDate = releaseDate; music.Type = releaseType; music.Artist = string.IsNullOrEmpty(releaseArtist) ? Helper.UnknownArtist : releaseArtist; music.PosterImagePath = release.CoverPath; music.PosterImageUrl = release.ImageFullUrl; return(music); }
private async Task NotifyMusicAsync(DbMusic music) { try { var release = Mapper.Map <MusicNotification>(music); string template = _unitOfWork.EmailTemplateRepository .GetById(Convert.ToInt32(Resources.Music)).Body; int quota = release.Receivers.Count / _transport.Count(); if (quota < 1) { quota = 1; } var tasks = new List <Task>(); var messages = new List <MimeMessage>(); int smtpCount = 0; for (int i = 0; i < release.Receivers.Count; i++) { var body = Engine.Razor.RunCompile( template, Resources.Music, null, new { Music = release, Count = i }); var message = CreateMessage(release.Receivers.ElementAt(i), body); messages.Add(message); if ((i + 1) % quota == 0) { tasks.Add(SendEmailAsync(_transport.ElementAt(smtpCount), messages)); smtpCount++; messages.Clear(); } } await Task.WhenAll(tasks).ConfigureAwait(false); } catch (Exception exception) { _logger.Error(exception.Message); } }
/// <summary> /// Сохранение исполнителя релиза /// </summary> /// <param name="music">Музыкальный релиз</param> /// <param name="releaseArtist">Исполнитель релиза</param> private void SaveMusician(DbMusic music, string releaseArtist) { //проверяем существует ли исполнитель var musicianEntity = _unitOfWork.MusicianRepository.Queryable(). FirstOrDefault(m => m.FullName.Equals(releaseArtist)); if (musicianEntity != null) { music.Musicians.Add(musicianEntity); } else { var newMusician = new DbMusician { FullName = releaseArtist }; _unitOfWork.MusicianRepository.Insert(newMusician); _unitOfWork.SaveChanges(); music.Musicians.Add(newMusician); } }
/// <summary> /// Сохранение жанров релиза /// </summary> /// <param name="music">Музыкальный релиз</param> /// <param name="releaseGenres">Список жанров релиза</param> private void SaveGenre(DbMusic music, IEnumerable <string> releaseGenres) { foreach (var releaseGenre in releaseGenres) { //проверяем существует ли жанр var genreEntity = _unitOfWork.MusicGenreRepository.Queryable().FirstOrDefault(g => g.Name.Equals(releaseGenre)); if (genreEntity != null) { music.Genres.Add(genreEntity); } else { var newMusicGenres = new DbMusicGenre { Name = releaseGenre }; _unitOfWork.MusicGenreRepository.Insert(newMusicGenres); _unitOfWork.SaveChanges(); music.Genres.Add(newMusicGenres); } } }