Пример #1
0
        public async Task <ActionResultResponse <string> > Insert(string tenantId, string creatorId, string creatorFullName, string creatorAvatar,
                                                                  AlbumMeta albumMeta)
        {
            var albumId = Guid.NewGuid().ToString();

            // Insert new album.
            var resultInsertAlbum = await _albumRepository.Insert(new Album
            {
                Id = albumId,
                ConcurrencyStamp = albumId,
                IsActive         = albumMeta.IsActive,
                TenantId         = tenantId,
                CreatorId        = creatorId,
                CreatorFullName  = creatorFullName,
                Type             = albumMeta.Type,
                Thumbnail        = albumMeta.Thumbnail,
                IsPublic         = albumMeta.IsPublic,
            });

            if (resultInsertAlbum <= 0)
            {
                return(new ActionResultResponse <string>(resultInsertAlbum,
                                                         _sharedResourceService.GetString(ErrorMessage.SomethingWentWrong)));
            }

            // Insert album translation.
            var resultInsertTranslation = await InsertTranslations();

            if (resultInsertTranslation.Code < 0)
            {
                await RollbackInsertAlbum();
            }
            if (albumMeta.Type == AlbumType.Photo)
            {
                await InsertPhotos(tenantId, albumId, creatorId, creatorFullName,
                                   albumMeta.Photos);
            }
            else
            {
                await InsertVideos(tenantId, albumId, creatorId, creatorFullName, creatorAvatar, albumMeta.Videos);
            }

            return(new ActionResultResponse <string>(1,
                                                     _sharedResourceService.GetString("Insert Album success"), "", albumId));

            #region Local functions
            async Task RollbackInsertAlbum()
            {
                await _albumRepository.ForceDelete(albumId);
            }

            async Task RollbackInsertAlbumTranslation()
            {
                await _albumTranslationRepository.ForceDelete(albumId);
            }

            async Task <ActionResultResponse <string> > InsertTranslations()
            {
                var albumTranslations = new List <AlbumTranslation>();

                foreach (var albumTranslation in albumMeta.Translations)
                {
                    // Check title exists.
                    var isTitleExists = await _albumTranslationRepository.CheckExists(albumId, tenantId,
                                                                                      albumTranslation.LanguageId, albumTranslation.Title);

                    if (isTitleExists)
                    {
                        await RollbackInsertAlbum();
                        await RollbackInsertAlbumTranslation();

                        return(new ActionResultResponse <string>(-1, _websiteResourceService.GetString("Album title already exists.")));
                    }
                    var seoLink = !string.IsNullOrEmpty(albumTranslation.SeoLink)
                        ? albumTranslation.SeoLink.Trim()
                        : albumTranslation.Title.Trim().ToUrlString();

                    // Check seolink exists.
                    var isSeoLinkExists = await _albumTranslationRepository.CheckExistsBySeoLink(albumId, tenantId,
                                                                                                 albumTranslation.LanguageId,
                                                                                                 seoLink);

                    if (isSeoLinkExists)
                    {
                        await RollbackInsertAlbum();
                        await RollbackInsertAlbumTranslation();

                        return(new ActionResultResponse <string>(-2, _websiteResourceService.GetString("SeoLink already exists. Please choose another.")));
                    }

                    albumTranslations.Add(new AlbumTranslation
                    {
                        TenantId        = tenantId,
                        AlbumId         = albumId,
                        LanguageId      = albumTranslation.LanguageId.Trim(),
                        Title           = albumTranslation.Title.Trim(),
                        Description     = albumTranslation.Description?.Trim(),
                        UnsignName      = albumTranslation.Title.StripVietnameseChars().ToUpper(),
                        MetaTitle       = albumTranslation.MetaTitle?.Trim(),
                        MetaDescription = albumTranslation.MetaDescription?.Trim(),
                        SeoLink         = seoLink,
                    });
                }

                var result = await _albumTranslationRepository.Inserts(albumTranslations);

                if (result <= 0)
                {
                    await RollbackInsertAlbum();
                    await RollbackInsertAlbumTranslation();

                    return(new ActionResultResponse <string>(-2, _sharedResourceService.GetString(ErrorMessage.SomethingWentWrong)));
                }

                return(new ActionResultResponse <string>(1, _websiteResourceService.GetString("Add new album translation succesful.")));
            }

            #endregion
        }
Пример #2
0
        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
        }