public async Task <int> Insert(AlbumTranslation albumTranslation) { _imageGroupTranslationRepository.Create(albumTranslation); return(await Context.SaveChangesAsync()); }
public async Task <int> Update(AlbumTranslation albumTranslation) { return(await Context.SaveChangesAsync()); }
public async Task <ActionResultResponse <string> > Update(string tenantId, string lastUpdateUserId, string lastUpdateFullName, string lastUpdateAvatar, string photoId, AlbumMeta albumMeta) { var info = await _albumRepository.GetInfo(photoId); if (info == null) { return(new ActionResultResponse <string>(-1, _websiteResourceService.GetString("Album does not exists."))); } if (info.TenantId != tenantId) { return(new ActionResultResponse <string>(-2, _sharedResourceService.GetString(ErrorMessage.NotHavePermission))); } if (info.ConcurrencyStamp != albumMeta.ConcurrencyStamp) { return(new ActionResultResponse <string>(-3, _websiteResourceService.GetString("The album already updated by another person. You can not update this album."))); } // Update album info. await UpdateAlbum(); // Update translate. await UpdateAlbumTranslation(); // Update photos. if (info.Type == AlbumType.Photo) { await UpdatePhotos(); } else { await UpdateVideos(); } return(new ActionResultResponse <string>(1, _websiteResourceService.GetString("Update album successful."), "", info.ConcurrencyStamp)); #region Local functions async Task UpdateAlbum() { info.IsActive = albumMeta.IsActive; info.ConcurrencyStamp = Guid.NewGuid().ToString(); info.LastUpdate = DateTime.Now; info.LastUpdateUserId = lastUpdateUserId; info.LastUpdateFullName = lastUpdateFullName; info.Thumbnail = albumMeta.Thumbnail; info.IsPublic = albumMeta.IsPublic; await _albumRepository.Update(info); } async Task <ActionResultResponse> UpdateAlbumTranslation() { foreach (var albumTranslation in albumMeta.Translations) { var isNameExists = await _albumTranslationRepository.CheckExists(info.Id, tenantId, albumTranslation.LanguageId, albumTranslation.Title); if (isNameExists) { return(new ActionResultResponse(-4, _websiteResourceService .GetString("Album: \"{0}\" already exists.", albumTranslation.Title))); } var seoLink = !string.IsNullOrEmpty(albumTranslation.SeoLink) ? albumTranslation.SeoLink.Trim() : albumTranslation.Title.Trim().ToUrlString(); // Check seolink exists. var isSeoLinkExists = await _albumTranslationRepository.CheckExistsBySeoLink(info.Id, info.TenantId, albumTranslation.LanguageId, seoLink); if (isSeoLinkExists) { return(new ActionResultResponse(-5, _websiteResourceService.GetString("SeoLink already exists. Please choose another."))); } var albumTranslationInfo = await _albumTranslationRepository.GetInfo(info.Id, albumTranslation.LanguageId); if (albumTranslationInfo != null) { albumTranslationInfo.Title = albumTranslation.Title.Trim(); albumTranslationInfo.Description = albumTranslation.Description?.Trim(); albumTranslationInfo.UnsignName = albumTranslation.Title.StripVietnameseChars().ToUpper(); albumTranslationInfo.MetaTitle = albumTranslation.MetaTitle?.Trim(); albumTranslationInfo.MetaDescription = albumTranslation.MetaDescription?.Trim(); albumTranslationInfo.SeoLink = seoLink; await _albumTranslationRepository.Update(albumTranslationInfo); } else { albumTranslationInfo = new AlbumTranslation { AlbumId = photoId, LanguageId = albumTranslation.LanguageId.Trim(), Title = albumTranslation.Title.Trim(), Description = albumTranslation.Description?.Trim(), UnsignName = albumTranslation.Title.StripVietnameseChars().ToUpper(), TenantId = tenantId, SeoLink = seoLink, MetaTitle = albumTranslation.MetaTitle?.Trim(), MetaDescription = albumTranslation.MetaDescription?.Trim() }; await _albumTranslationRepository.Insert(albumTranslationInfo); } } return(new ActionResultResponse(1, _websiteResourceService.GetString("Update album successful."))); } async Task UpdatePhotos() { var listExistings = await _photoRepository.GetsByAlbumId(info.TenantId, info.Id); if (listExistings == null || !listExistings.Any()) { await InsertPhotos(info.TenantId, info.Id, lastUpdateUserId, lastUpdateFullName, albumMeta.Photos); } else { // Get list edit photos. var listUpdateId = listExistings.Select(x => x.Id).Intersect(albumMeta.Photos.Select(x => x.Id)).ToList(); if (listUpdateId.Any()) { var listUpdate = listExistings.Where(x => listUpdateId.Contains(x.Id)); foreach (var photo in listUpdate) { var photoMeta = albumMeta.Photos.FirstOrDefault(x => x.Id == photo.Id); if (photoMeta != null) { photo.Title = photoMeta.Title; photo.Description = photoMeta.Description; photo.LastUpdateUserId = lastUpdateUserId; photo.LastUpdateFullName = lastUpdateFullName; photo.LastUpdate = DateTime.Now; photo.ConcurrencyStamp = Guid.NewGuid().ToString(); } } } // Get list delete photos. var listDeleteId = listExistings.Select(x => x.Id).Except(albumMeta.Photos.Select(x => x.Id)) .ToList(); if (listDeleteId.Any()) { await _photoRepository.Delete(listDeleteId); } // Get list new photos. var listNewId = albumMeta.Photos.Select(x => x.Id).Except(listExistings.Select(x => x.Id)).ToList(); if (listNewId.Any()) { var listNew = albumMeta.Photos.Where(x => listNewId.Contains(x.Id)).ToList(); if (listNew.Any()) { await InsertPhotos(info.TenantId, info.Id, lastUpdateUserId, lastUpdateFullName, listNew); } } } } async Task <ActionResultResponse> UpdateVideos() { var listVideoIds = await _videoRepository.GetListVideoIdByAlbumId(info.Id); if (listVideoIds == null || !listVideoIds.Any()) { return(await InsertVideos(tenantId, info.Id, lastUpdateUserId, lastUpdateFullName, lastUpdateAvatar, albumMeta.Videos)); } else { // Update video info. var listUpdateIds = listVideoIds.Intersect(albumMeta.Videos.Select(x => x.Id)).ToList(); if (listUpdateIds.Any()) { foreach (var videoMeta in albumMeta.Videos.Where(x => listUpdateIds.Contains(x.Id)).ToList()) { await _videoService.Update(tenantId, lastUpdateUserId, lastUpdateFullName, lastUpdateAvatar, videoMeta.Id, videoMeta); } } // Delete videos. var listDeletedIds = listVideoIds.Except(albumMeta.Videos.Select(x => x.Id)).ToList(); if (listDeletedIds.Any()) { await _videoRepository.DeleteByIds(listDeletedIds); } // Add new videos. var listNewsVideoIds = albumMeta.Videos.Select(x => x.Id).Except(listVideoIds).ToList(); if (listNewsVideoIds.Any()) { var listNewVideo = albumMeta.Videos.Where(x => listNewsVideoIds.Contains(x.Id)).ToList(); var result = await InsertVideos(info.TenantId, info.Id, lastUpdateUserId, lastUpdateFullName, lastUpdateAvatar, listNewVideo); } return(new ActionResultResponse(1, _websiteResourceService.GetString("Update video album successful."))); } } #endregion }