Пример #1
0
        public async Task <ActionResultResponse <string> > Insert(string tenantId, string creatorId, string creatorFullName, string creatorAvata, VideoMeta videoMeta)
        {
            var videoId = Guid.NewGuid().ToString();
            // Insert new video.
            var video = new Video
            {
                Id               = videoId,
                AlbumId          = videoMeta.AlbumId,
                ConcurrencyStamp = videoId,
                Thumbnail        = videoMeta.Thumbnail,
                Url              = videoMeta.Url,
                Order            = videoMeta.Order,
                Type             = videoMeta.Type,
                VideoLinkId      = videoMeta.VideoLinkId,
                IsActive         = videoMeta.IsActive,
                TenantId         = tenantId,
                CreatorId        = creatorId,
                CreatorFullName  = creatorFullName
            };

            if (videoMeta.IsHomePage.HasValue)
            {
                video.IsHomePage           = videoMeta.IsHomePage.Value;
                video.LastUpdateIsHomePage = DateTime.Now;
            }

            var resultInsertVideo = await _videoRepository.Insert(video);

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

            #region insert video Translation.

            if (videoMeta.Translations.Count > 0)
            {
                var resultInsertTranslation = await InsertVideoTranslation();

                if (resultInsertTranslation.Code <= 0)
                {
                    return(resultInsertTranslation);
                }
            }
            #endregion

            return(new ActionResultResponse <string>(1,
                                                     _websiteResourceService.GetString("Add new video successful."),
                                                     string.Empty, videoId));


            #region Local functions

            async Task <ActionResultResponse <string> > InsertVideoTranslation()
            {
                var apiUrls = _configuration.GetApiUrl();

                if (apiUrls == null)
                {
                    return(new ActionResultResponse <string>(-5,
                                                             _sharedResourceService.GetString(
                                                                 "Missing some configuration. Please contact with administrator.")));
                }

                var videoTranslations = new List <VideoTranslation>();
                var videoTags         = new List <TagSubjectViewModel>();

                foreach (var videoTranslation in videoMeta.Translations)
                {
                    // Check name exists.
                    var isNameExists = await _videoTranslationRepository.CheckExists(videoId, tenantId,
                                                                                     videoTranslation.LanguageId, videoTranslation.Title);

                    if (isNameExists)
                    {
                        await RollbackInsertVideo();

                        return(new ActionResultResponse <string>(-2, _websiteResourceService.GetString(
                                                                     "Video name: \"{0}\" already exists.",
                                                                     videoTranslation.Title)));
                    }

                    var videoTranslationInsert = new VideoTranslation
                    {
                        TenantId    = tenantId,
                        VideoId     = videoId,
                        LanguageId  = videoTranslation.LanguageId.Trim(),
                        Title       = videoTranslation.Title.Trim(),
                        Description = videoTranslation.Description?.Trim(),
                        UnsignName  = videoTranslation.Title.StripVietnameseChars().ToUpper()
                    };

                    videoTranslations.Add(videoTranslationInsert);

                    var videoTagInsert = new TagSubjectViewModel
                    {
                        TenantId        = tenantId,
                        CreatorId       = creatorId,
                        CreatorFullName = creatorFullName,
                        CreatorAvata    = creatorAvata,
                        Type            = TagType.Video,
                        SubjectId       = videoId,
                        LanguageId      = videoTranslation.LanguageId.Trim(),
                        Tags            = videoTranslation.Tags
                    };
                    videoTags.Add(videoTagInsert);
                }
                var resultTag = await new HttpClientService()
                                .PostAsync <ActionResultResponse>($"{apiUrls.CoreApiUrl}/tags", videoTags);

                // Insert video translations.
                var resultInsertTranslation = await _videoTranslationRepository.Inserts(videoTranslations);

                if (resultInsertTranslation > 0)
                {
                    return(new ActionResultResponse <string>(1,
                                                             _websiteResourceService.GetString("Add new video translation successful."), string.Empty,
                                                             videoId));
                }

                await RollbackInsertVideoTranslation();
                await RollbackInsertVideo();

                return(new ActionResultResponse <string>(-3,
                                                         _websiteResourceService.GetString(
                                                             "Can not insert new video. Please contact with administrator.")));
            }

            async Task RollbackInsertVideo()
            {
                await _videoRepository.ForceDelete(videoId);
            }

            async Task RollbackInsertVideoTranslation()
            {
                await _videoTranslationRepository.ForceDelete(videoId);
            }

            #endregion
        }
Пример #2
0
        public async Task <ActionResultResponse> Update(string tenantId, string lastUpdateUserId, string lastUpdateFullName, string lastUpdateAvata, string videoId, VideoMeta videoMeta)
        {
            var apiUrls = _configuration.GetApiUrl();

            if (apiUrls == null)
            {
                return(new ActionResultResponse <string>(-5,
                                                         _sharedResourceService.GetString(
                                                             "Missing some configuration. Please contact with administrator.")));
            }
            var videoTags = new List <TagSubjectViewModel>();

            var info = await _videoRepository.GetInfo(videoId);

            if (info == null)
            {
                return(new ActionResultResponse(-1, _websiteResourceService.GetString("Video does not exists.")));
            }

            if (info.TenantId != tenantId)
            {
                return(new ActionResultResponse(-2, _sharedResourceService.GetString(ErrorMessage.NotHavePermission)));
            }

            if (info.ConcurrencyStamp != videoMeta.ConcurrencyStamp)
            {
                return(new ActionResultResponse(-3, _websiteResourceService.GetString("The video already updated by other people. You can not update this video.")));
            }

            info.AlbumId            = videoMeta.AlbumId;
            info.Thumbnail          = videoMeta.Thumbnail;
            info.Url                = videoMeta.Url;
            info.VideoLinkId        = videoMeta.VideoLinkId;
            info.Type               = videoMeta.Type;
            info.Order              = videoMeta.Order;
            info.IsActive           = videoMeta.IsActive;
            info.ConcurrencyStamp   = Guid.NewGuid().ToString();
            info.LastUpdate         = DateTime.Now;
            info.LastUpdateUserId   = lastUpdateUserId;
            info.LastUpdateFullName = lastUpdateFullName;

            if (videoMeta.IsHomePage.HasValue)
            {
                info.IsHomePage           = videoMeta.IsHomePage.Value;
                info.LastUpdateIsHomePage = DateTime.Now;
            }
            //udpate translate
            foreach (var videoTranslation in videoMeta.Translations)
            {
                var isNameExists = await _videoTranslationRepository.CheckExists(info.Id, tenantId,
                                                                                 videoTranslation.LanguageId, videoTranslation.Title);

                if (isNameExists)
                {
                    return(new ActionResultResponse(-4, _websiteResourceService.GetString("Video name: \"{0}\" already exists.", videoTranslation.Title)));
                }

                var videoTranslationInfo =
                    await _videoTranslationRepository.GetInfo(info.Id, videoTranslation.LanguageId);

                if (videoTranslationInfo != null)
                {
                    videoTranslationInfo.Title       = videoTranslation.Title.Trim();
                    videoTranslationInfo.Description = videoTranslation.Description?.Trim();
                    videoTranslationInfo.UnsignName  = videoTranslation.Title.StripVietnameseChars().ToUpper();
                    await _videoTranslationRepository.Update(videoTranslationInfo);
                }
                else
                {
                    var videoTranslationInsert = new VideoTranslation
                    {
                        VideoId     = videoId,
                        LanguageId  = videoTranslation.LanguageId.Trim(),
                        Title       = videoTranslation.Title.Trim(),
                        Description = videoTranslation.Description?.Trim(),
                        UnsignName  = videoTranslation.Title.StripVietnameseChars().ToUpper()
                    };
                    await _videoTranslationRepository.Insert(videoTranslationInsert);
                }

                var videoTagInsert = new TagSubjectViewModel
                {
                    TenantId        = tenantId,
                    CreatorId       = lastUpdateUserId,
                    CreatorFullName = lastUpdateFullName,
                    CreatorAvata    = lastUpdateAvata,
                    Type            = TagType.Video,
                    SubjectId       = videoId,
                    LanguageId      = videoTranslation.LanguageId.Trim(),
                    Tags            = videoTranslation.Tags
                };
                videoTags.Add(videoTagInsert);
            }

            var resultTag = await new HttpClientService()
                            .PostAsync <ActionResultResponse>($"{apiUrls.CoreApiUrl}/tags", videoTags);

            return(new ActionResultResponse(1, _websiteResourceService.GetString("Update video successful.")));
        }
Пример #3
0
        public async Task <ActionResultResponse <string> > Insert(string tenantId, string creatorId,
                                                                  string creatorFullName, string creatorAvatar, NewsMeta newsMeta, bool isSend)
        {
            var newsId = Guid.NewGuid().ToString();
            var news   = new News
            {
                Id = newsId,
                ConcurrencyStamp = newsId,
                IsActive         = newsMeta.IsActive,
                FeatureImage     = newsMeta.FeatureImage,
                BannerImage      = newsMeta.BannerImage,
                AltImage         = newsMeta.AltImage,
                Source           = newsMeta.Source,
                Status           = isSend ? ApproverStatus.Pending : ApproverStatus.Draft,
                TenantId         = tenantId,
                CreatorId        = creatorId,
                CreatorFullName  = creatorFullName,
                CreatorAvatar    = creatorAvatar
            };

            if (news.Status == ApproverStatus.Pending)
            {
                news.SentTime = DateTime.Now;
            }

            if (newsMeta.IsHot.HasValue)
            {
                news.IsHot           = newsMeta.IsHot.Value;
                news.LastUpdateIsHot = DateTime.Now;
            }

            if (newsMeta.IsHomePage.HasValue)
            {
                news.IsHomePage           = newsMeta.IsHomePage.Value;
                news.LastUpdateIsHomePage = DateTime.Now;
            }
            var result = await _newsRepository.Insert(news);

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

            #region insert news Translation.

            if (newsMeta.NewsTranslations.Count > 0)
            {
                var resultInsertTranslation = await InsertNewsTranslation();

                if (resultInsertTranslation.Code <= 0)
                {
                    return(resultInsertTranslation);
                }
            }

            #endregion

            #region insert Categories News.

            if (newsMeta.CategoriesNews.Count > 0)
            {
                var resultInsertCategoriesNews = await InsertCategoriesNews();

                if (resultInsertCategoriesNews.Code <= 0)
                {
                    return(resultInsertCategoriesNews);
                }
            }

            #endregion

            return(new ActionResultResponse <string>(1,
                                                     _websiteResourceService.GetString("Add new news successful."),
                                                     string.Empty, newsId));

            #region Local functions

            async Task <ActionResultResponse <string> > InsertNewsTranslation()
            {
                var apiUrls = _configuration.GetApiUrl();

                if (apiUrls == null)
                {
                    return(new ActionResultResponse <string>(-5,
                                                             _sharedResourceService.GetString(
                                                                 "Missing some configuration. Please contact with administrator.")));
                }
                var newsTranslations = new List <NewsTranslation>();
                var newsTags         = new List <TagSubjectViewModel>();

                foreach (var newsTranslation in newsMeta.NewsTranslations)
                {
                    // Check name exists.
                    var isNameExists = await _newsTranslationRepository.CheckExists(tenantId, newsId,
                                                                                    newsTranslation.LanguageId, newsTranslation.Title);

                    if (isNameExists)
                    {
                        await RollbackInsertNews();

                        return(new ActionResultResponse <string>(-1,
                                                                 _websiteResourceService.GetString("News title: \"{0}\" already exists.",
                                                                                                   newsTranslation.Title)));
                    }

                    var newsTranslationInsert = new NewsTranslation
                    {
                        NewsId          = newsId,
                        TenantId        = tenantId,
                        LanguageId      = newsTranslation.LanguageId.Trim(),
                        Title           = newsTranslation.Title.Trim(),
                        MetaTitle       = newsTranslation.MetaTitle?.Trim(),
                        Description     = newsTranslation.Description?.Trim(),
                        MetaDescription = newsTranslation.MetaDescription?.Trim(),
                        MetaKeyword     = newsTranslation.MetaKeyword?.Trim(),
                        Content         = newsTranslation.Content?.Trim(),
                        SeoLink         = newsTranslation.SeoLink?.Trim(),
                        UnsignName      = newsTranslation.Title.StripVietnameseChars().ToUpper()
                    };

                    if (!string.IsNullOrEmpty(newsTranslation.SeoLink))
                    {
                        newsTranslationInsert.SeoLink = newsTranslation.SeoLink.ToUrlString();
                    }
                    else
                    {
                        newsTranslationInsert.SeoLink = newsTranslation.Title.ToUrlString();
                    }

                    // Check Seolink exists.
                    var isSeolinkExists = await _newsTranslationRepository.CheckSeoLinkExists(tenantId, newsId,
                                                                                              newsTranslation.LanguageId, newsTranslationInsert.SeoLink);

                    if (isSeolinkExists)
                    {
                        await RollbackInsertNews();

                        return(new ActionResultResponse <string>(-2,
                                                                 _websiteResourceService.GetString("Seo link: \"{0}\" already exists.",
                                                                                                   newsTranslationInsert.SeoLink)));
                    }

                    newsTranslations.Add(newsTranslationInsert);

                    var newsTagInsert = new TagSubjectViewModel
                    {
                        TenantId        = tenantId,
                        CreatorId       = creatorId,
                        CreatorFullName = creatorFullName,
                        CreatorAvata    = creatorAvatar,
                        Type            = TagType.News,
                        SubjectId       = newsId,
                        LanguageId      = newsTranslation.LanguageId.Trim(),
                        Tags            = newsTranslation.Tags
                    };
                    newsTags.Add(newsTagInsert);
                }

                var resultTag = await new HttpClientService()
                                .PostAsync <ActionResultResponse>($"{apiUrls.CoreApiUrl}/tags", newsTags);

                var resultTranslation = await _newsTranslationRepository.Inserts(newsTranslations);

                if (resultTranslation > 0)
                {
                    return(new ActionResultResponse <string>(resultTranslation,
                                                             _websiteResourceService.GetString("Add new news translation successful.")));
                }

                await RollbackInsertNewsTranslation();
                await RollbackInsertNews();

                return(new ActionResultResponse <string>(-3,
                                                         _websiteResourceService.GetString(
                                                             "Can not insert news translation. Please contact with administrator.")));
            }

            async Task <ActionResultResponse <string> > InsertCategoriesNews()
            {
                var categoriesNews = new List <CategoriesNews>();

                foreach (var categories in newsMeta.CategoriesNews)
                {
                    var iscategoriesNewExists =
                        await _categoriesNewsRepository.CheckExistsByNewsIdAndCategoryId(newsId,
                                                                                         categories);

                    if (iscategoriesNewExists)
                    {
                        continue;
                    }
                    var categoriesInsert = new CategoriesNews
                    {
                        NewsId     = newsId,
                        CategoryId = categories
                    };

                    var iscategoriesExists = await _categoryRepository.CheckExistsById(tenantId, categories);

                    if (iscategoriesExists)
                    {
                        categoriesNews.Add(categoriesInsert);
                    }
                }
                var resultCategoriesNews = await _categoriesNewsRepository.Inserts(categoriesNews);

                if (resultCategoriesNews > 0)
                {
                    return(new ActionResultResponse <string>(resultCategoriesNews,
                                                             _websiteResourceService.GetString("Add new categories news successful.")));
                }

                await RollbackInsertCategoriesNews();
                await RollbackInsertNewsTranslation();
                await RollbackInsertNews();

                return(new ActionResultResponse <string>(-3,
                                                         _websiteResourceService.GetString(
                                                             "Can not insert categories news. Please contact with administrator.")));
            }

            async Task RollbackInsertNews()
            {
                await _newsRepository.ForceDelete(newsId);
            }

            async Task RollbackInsertNewsTranslation()
            {
                await _newsTranslationRepository.Delete(newsId);
            }

            async Task RollbackInsertCategoriesNews()
            {
                await _categoriesNewsRepository.DeleteByNewsId(newsId);
            }

            #endregion Local functions
        }
Пример #4
0
        public async Task <ActionResultResponse> Update(string tenantId, string lastUpdateUserId,
                                                        string lastUpdateFullName, string lastUpdateAvatar, string newsId,
                                                        NewsMeta newsMeta, bool isSend)
        {
            var apiUrls = _configuration.GetApiUrl();

            if (apiUrls == null)
            {
                return(new ActionResultResponse <string>(-5,
                                                         _sharedResourceService.GetString(
                                                             "Missing some configuration. Please contact with administrator.")));
            }
            var newsTags = new List <TagSubjectViewModel>();

            var info = await _newsRepository.GetInfo(newsId);

            if (info == null)
            {
                return(new ActionResultResponse(-1, _websiteResourceService.GetString("News does not exists.")));
            }

            if (info.TenantId != tenantId)
            {
                return(new ActionResultResponse(-2,
                                                _websiteResourceService.GetString(ErrorMessage.NotHavePermission)));
            }

            if (info.ConcurrencyStamp != newsMeta.ConcurrencyStamp)
            {
                return(new ActionResultResponse(-3,
                                                _websiteResourceService.GetString(
                                                    "The news already updated by other people. you are not allowed to edit the news information.")));
            }

            info.Status             = isSend ? ApproverStatus.Pending : ApproverStatus.Draft;
            info.IsActive           = newsMeta.IsActive;
            info.FeatureImage       = newsMeta.FeatureImage;
            info.BannerImage        = newsMeta.BannerImage;
            info.AltImage           = newsMeta.AltImage;
            info.Source             = newsMeta.Source;
            info.ConcurrencyStamp   = Guid.NewGuid().ToString();
            info.LastUpdate         = DateTime.Now;
            info.LastUpdateUserId   = lastUpdateUserId;
            info.LastUpdateFullName = lastUpdateFullName;
            info.LastUpdateAvatar   = lastUpdateAvatar;
            info.Source             = newsMeta.Source;

            if (newsMeta.IsHot.HasValue)
            {
                info.IsHot           = newsMeta.IsHot.Value;
                info.LastUpdateIsHot = DateTime.Now;
            }

            if (newsMeta.IsHomePage.HasValue)
            {
                info.IsHomePage           = newsMeta.IsHomePage.Value;
                info.LastUpdateIsHomePage = DateTime.Now;
            }

            if (info.Status == ApproverStatus.Approved)
            {
                info.Status = isSend ? ApproverStatus.Pending : ApproverStatus.Draft;
            }

            await _newsRepository.Update(info);

            #region news translation.
            if (newsMeta.NewsTranslations.Count > 0)
            {
                var resultUpdateTranslation = await UpdateNewTranslation();

                if (resultUpdateTranslation.Code <= 0)
                {
                    return(resultUpdateTranslation);
                }
            }
            #endregion

            #region CategoriesNews.
            if (newsMeta.CategoriesNews.Count > 0)
            {
                var resultUpdateCategoriesNews = await UpdateCategoriesNews();

                if (resultUpdateCategoriesNews.Code <= 0)
                {
                    return(resultUpdateCategoriesNews);
                }
            }
            #endregion

            return(new ActionResultResponse(1, _websiteResourceService.GetString("Update news successful.")));

            async Task <ActionResultResponse> UpdateNewTranslation()
            {
                foreach (var newsTranslation in newsMeta.NewsTranslations)
                {
                    var isNameExists = await _newsTranslationRepository.CheckExists(tenantId, info.Id,
                                                                                    newsTranslation.LanguageId, newsTranslation.Title);

                    if (isNameExists)
                    {
                        return(new ActionResultResponse(-4, _websiteResourceService.GetString("News name: \"{0}\" already exists.")));
                    }

                    var newsTranslationInfo =
                        await _newsTranslationRepository.GetInfo(info.Id, newsTranslation.LanguageId);

                    if (newsTranslationInfo != null)
                    {
                        newsTranslationInfo.Title           = newsTranslation.Title.Trim();
                        newsTranslationInfo.MetaTitle       = newsTranslation.MetaTitle?.Trim();
                        newsTranslationInfo.Description     = newsTranslation.Description?.Trim();
                        newsTranslationInfo.MetaDescription = newsTranslation.MetaDescription?.Trim();
                        newsTranslationInfo.MetaKeyword     = newsTranslation.MetaKeyword?.Trim();
                        newsTranslationInfo.UnsignName      = newsTranslation.Title?.StripVietnameseChars().ToUpper();
                        newsTranslationInfo.Content         = newsTranslation.Content?.Trim();

                        if (!string.IsNullOrEmpty(newsTranslation.SeoLink))
                        {
                            newsTranslationInfo.SeoLink = newsTranslation.SeoLink.ToPlainText();
                        }
                        else
                        {
                            newsTranslationInfo.SeoLink = newsTranslation.Title.ToPlainText();
                        }

                        // Check Seolink exists.
                        var isSeolinkExists = await _newsTranslationRepository.CheckSeoLinkExists(tenantId, newsId,
                                                                                                  newsTranslation.LanguageId, newsTranslationInfo.SeoLink);

                        if (isSeolinkExists)
                        {
                            return(new ActionResultResponse(-5,
                                                            _websiteResourceService.GetString("Seo link: \"{0}\" already exists.",
                                                                                              newsTranslationInfo.SeoLink)));
                        }

                        await _newsTranslationRepository.Update(newsTranslationInfo);
                    }
                    else
                    {
                        var newsTranslationInsert = new NewsTranslation
                        {
                            NewsId          = newsId,
                            TenantId        = tenantId,
                            LanguageId      = newsTranslation.LanguageId.Trim(),
                            Title           = newsTranslation.Title.Trim(),
                            MetaTitle       = newsTranslation.MetaTitle?.Trim(),
                            Description     = newsTranslation.Description?.Trim(),
                            MetaDescription = newsTranslation.MetaDescription?.Trim(),
                            MetaKeyword     = newsTranslation.MetaKeyword?.Trim(),
                            Content         = newsTranslation.Content?.Trim(),
                            UnsignName      = newsTranslation.Title?.StripVietnameseChars().ToUpper()
                        };

                        if (!string.IsNullOrEmpty(newsTranslation.SeoLink))
                        {
                            newsTranslationInsert.SeoLink = newsTranslation.SeoLink.ToUrlString();
                        }
                        else
                        {
                            newsTranslationInsert.SeoLink = newsTranslation.Title.ToUrlString();
                        }
                        // Check Seolink exists.
                        var isSeolinkExists = await _newsTranslationRepository.CheckSeoLinkExists(tenantId, newsId,
                                                                                                  newsTranslation.LanguageId, newsTranslationInsert.SeoLink);

                        if (isSeolinkExists)
                        {
                            return(new ActionResultResponse(-6,
                                                            _websiteResourceService.GetString("Seo link: \"{0}\" already exists.",
                                                                                              newsTranslationInsert.SeoLink)));
                        }
                        await _newsTranslationRepository.Insert(newsTranslationInsert);
                    }
                    var videoTagInsert = new TagSubjectViewModel
                    {
                        TenantId        = tenantId,
                        CreatorId       = lastUpdateUserId,
                        CreatorFullName = lastUpdateFullName,
                        CreatorAvata    = lastUpdateAvatar,
                        Type            = TagType.News,
                        SubjectId       = newsId,
                        LanguageId      = newsTranslation.LanguageId.Trim(),
                        Tags            = newsTranslation.Tags
                    };
                    newsTags.Add(videoTagInsert);
                }

                var resultTag = await new HttpClientService()
                                .PostAsync <ActionResultResponse>($"{apiUrls.CoreApiUrl}/tags", newsTags);

                return(new ActionResultResponse(1,
                                                _websiteResourceService.GetString("Update news translation successful.")));
            }

            async Task <ActionResultResponse> UpdateCategoriesNews()
            {
                var resultDelete = await _categoriesNewsRepository.DeleteByNewsId(newsId);

                var categoriesNews = new List <CategoriesNews>();

                foreach (var categories in newsMeta.CategoriesNews)
                {
                    var categoriesInsert = new CategoriesNews
                    {
                        NewsId     = newsId,
                        CategoryId = categories
                    };

                    var iscategoriesExists = await _categoryRepository.CheckExistsById(tenantId, categories);

                    if (iscategoriesExists)
                    {
                        categoriesNews.Add(categoriesInsert);
                    }
                }
                var resultcategories = await _categoriesNewsRepository.Inserts(categoriesNews);

                if (resultcategories > 0)
                {
                    return(new ActionResultResponse(resultcategories,
                                                    _websiteResourceService.GetString("Update categories news successful.")));
                }

                return(new ActionResultResponse(-7,
                                                _websiteResourceService.GetString("Can not update categories news. Please contact with administrator.")));
            }
        }
Пример #5
0
        public async Task <ActionResultResponse <string> > Insert(string tenantId, string creatorId, string creatorFullName, string creatorAvatar, EventMeta eventMeta)
        {
            if (eventMeta.StartDate.HasValue && eventMeta.EndDate.HasValue)
            {
                if (DateTime.Compare(eventMeta.StartDate.Value, eventMeta.EndDate.Value) > 0)
                {
                    return(new ActionResultResponse <string>(-1, _websiteEventResourceService.GetString("Start time greater than end time.")));
                }
            }

            var eventId = Guid.NewGuid().ToString();
            var events  = new Domain.Models.Event
            {
                Id = eventId,
                ConcurrencyStamp            = eventId,
                IsActive                    = eventMeta.IsActive,
                StartDate                   = eventMeta.StartDate,
                EndDate                     = eventMeta.EndDate,
                LimitedUsers                = eventMeta.LimitedUsers,
                IsAllowRegisterWhenOverload = eventMeta.IsAllowRegisterWhenOverload,
                LimitedAccompanyPersons     = eventMeta.LimitedAccompanyPersons,
                StaffOnly                   = eventMeta.StaffOnly,
                IsAllowRegister             = eventMeta.IsAllowRegister,
                IsAllowAccompanyPerson      = eventMeta.IsAllowAccompanyPerson,
                Thumbnail                   = eventMeta.Thumbnail,
                Status          = ApproverStatus.Draft,
                TenantId        = tenantId,
                CreatorId       = creatorId,
                CreatorFullName = creatorFullName,
                CreatorAvatar   = creatorAvatar
            };

            var result = await _eventRepository.Insert(events);

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


            #region insert Translation.
            if (eventMeta.EventTranslations.Count > 0)
            {
                var resultInsertTranslation = await InsertEventTranslation();

                if (resultInsertTranslation.Code <= 0)
                {
                    return(resultInsertTranslation);
                }
            }
            #endregion

            await SendNotificationToApprover(tenantId, creatorId, creatorFullName, creatorAvatar, eventId);

            return(new ActionResultResponse <string>(1, _websiteEventResourceService.GetString("Add new event successful."),
                                                     string.Empty, eventId));

            #region Local functions
            async Task <ActionResultResponse <string> > InsertEventTranslation()
            {
                var apiUrls = _configuration.GetApiUrl();

                if (apiUrls == null)
                {
                    return(new ActionResultResponse <string>(-5,
                                                             _sharedResourceService.GetString(
                                                                 "Missing some configuration. Please contact with administrator.")));
                }

                var eventTags         = new List <TagSubjectViewModel>();
                var eventTranslations = new List <EventTranslation>();

                foreach (var eventTranslation in eventMeta.EventTranslations)
                {
                    // Check name exists.
                    //var isNameExists = await _eventTranslationRepository.CheckExists( eventId,
                    //    eventTranslation.LanguageId, eventTranslation.Name);
                    //if (isNameExists)
                    //{
                    //    await RollbackInsertEvent();
                    //    return new ActionResultResponse<string>(-1,
                    //        _websiteEventResourceService.GetString("Event name: \"{0}\" already exists.",
                    //            eventTranslation.Name));
                    //}

                    var eventTranslationInsert = new EventTranslation
                    {
                        EventId         = eventId,
                        LanguageId      = eventTranslation.LanguageId.Trim(),
                        Name            = eventTranslation.Name.Trim(),
                        MetaTitle       = eventTranslation.MetaTitle?.Trim(),
                        Description     = eventTranslation.Description?.Trim(),
                        MetaDescription = eventTranslation.MetaDescription?.Trim(),
                        Content         = eventTranslation.Content?.Trim(),
                        UnsignName      = eventTranslation.Name?.StripVietnameseChars().ToUpper(),
                        Address         = eventTranslation?.Address?.Trim()
                    };

                    eventTranslationInsert.SeoLink = !string.IsNullOrEmpty(eventTranslation.SeoLink)
                        ? eventTranslation.SeoLink.ToUrlString() : eventTranslation.Name.ToUrlString();

                    // Check Seolink exists.
                    var isSeolinkExists = await _eventTranslationRepository.CheckSeoLinkExists(eventId,
                                                                                               eventTranslation.LanguageId, eventTranslationInsert.SeoLink);

                    if (isSeolinkExists)
                    {
                        await RollbackInsertEvent();

                        return(new ActionResultResponse <string>(-2,
                                                                 _websiteEventResourceService.GetString("Seo link: \"{0}\" already exists.",
                                                                                                        eventTranslationInsert.SeoLink)));
                    }

                    eventTranslations.Add(eventTranslationInsert);

                    var eventTagInsert = new TagSubjectViewModel
                    {
                        TenantId        = tenantId,
                        CreatorId       = creatorId,
                        CreatorFullName = creatorFullName,
                        CreatorAvata    = creatorAvatar,
                        Type            = TagType.Event,
                        SubjectId       = eventId,
                        LanguageId      = eventTranslation.LanguageId.Trim(),
                        Tags            = eventTranslation.Tags
                    };
                    eventTags.Add(eventTagInsert);
                }

                var resultTag = await new HttpClientService()
                                .PostAsync <ActionResultResponse>($"{apiUrls.CoreApiUrl}/tags", eventTags);

                var resultTranslation = await _eventTranslationRepository.Inserts(eventTranslations);

                if (resultTranslation > 0)
                {
                    return(new ActionResultResponse <string>(resultTranslation,
                                                             _websiteEventResourceService.GetString("Add new event translation successful.")));
                }

                await RollbackInsertEventTranslation();
                await RollbackInsertEvent();

                return(new ActionResultResponse <string>(-3,
                                                         _websiteEventResourceService.GetString("Can not insert event translation. Please contact with administrator.")));
            }

            async Task RollbackInsertEvent()
            {
                await _eventRepository.ForceDelete(eventId);
            }

            async Task RollbackInsertEventTranslation()
            {
                await _eventTranslationRepository.Delete(eventId);
            }

            #endregion Local functions
        }
Пример #6
0
        public async Task <ActionResultResponse <string> > Update(string tenantId, string lastUpdateUserId, string lastUpdateFullName, string lastUpdateAvatar,
                                                                  string eventId, EventMeta eventMeta)
        {
            var apiUrls = _configuration.GetApiUrl();

            if (apiUrls == null)
            {
                return(new ActionResultResponse <string>(-5,
                                                         _sharedResourceService.GetString(
                                                             "Missing some configuration. Please contact with administrator.")));
            }
            var eventTags = new List <TagSubjectViewModel>();

            if (eventMeta.StartDate.HasValue && eventMeta.EndDate.HasValue)
            {
                if (DateTime.Compare(eventMeta.StartDate.Value, eventMeta.EndDate.Value) > 0)
                {
                    return(new ActionResultResponse <string>(-1, _websiteEventResourceService.GetString("Start time greater than end time.")));
                }
            }

            var info = await _eventRepository.GetInfo(eventId);

            if (info == null)
            {
                return(new ActionResultResponse <string>(-1, _websiteEventResourceService.GetString("Event does not exists.")));
            }

            if (info.TenantId != tenantId)
            {
                return(new ActionResultResponse <string>(-2,
                                                         _websiteEventResourceService.GetString(ErrorMessage.NotHavePermission)));
            }

            if (info.ConcurrencyStamp != eventMeta.ConcurrencyStamp)
            {
                return(new ActionResultResponse <string>(-3,
                                                         _websiteEventResourceService.GetString(
                                                             "The event already updated by other people. you are not allowed to edit the event information.")));
            }

            info.IsActive  = eventMeta.IsActive;
            info.Thumbnail = eventMeta.Thumbnail;

            info.StartDate    = eventMeta.StartDate;
            info.EndDate      = eventMeta.EndDate;
            info.LimitedUsers = eventMeta.LimitedUsers;
            info.IsAllowRegisterWhenOverload = eventMeta.IsAllowRegisterWhenOverload;
            info.LimitedAccompanyPersons     = eventMeta.LimitedAccompanyPersons;
            info.StaffOnly              = eventMeta.StaffOnly;
            info.IsAllowRegister        = eventMeta.IsAllowRegister;
            info.IsAllowAccompanyPerson = eventMeta.IsAllowAccompanyPerson;

            info.ConcurrencyStamp   = Guid.NewGuid().ToString();
            info.LastUpdate         = DateTime.Now;
            info.LastUpdateUserId   = lastUpdateUserId;
            info.LastUpdateFullName = lastUpdateFullName;
            info.LastUpdateAvatar   = lastUpdateAvatar;

            await _eventRepository.Update(info);

            #region translation.
            if (eventMeta.EventTranslations.Count > 0)
            {
                var resultUpdateTranslation = await UpdateEventTranslation();

                if (resultUpdateTranslation.Code <= 0)
                {
                    return(resultUpdateTranslation);
                }
            }
            #endregion

            return(new ActionResultResponse <string>(1, _websiteEventResourceService.GetString("Update event successful."),
                                                     string.Empty, info.ConcurrencyStamp));

            async Task <ActionResultResponse <string> > UpdateEventTranslation()
            {
                foreach (var eventTranslation in eventMeta.EventTranslations)
                {
                    //var isNameExists = await _eventTranslationRepository.CheckExists( info.Id,
                    //    eventTranslation.LanguageId, eventTranslation.Name);
                    //if (isNameExists)
                    //    return new ActionResultResponse(-4, _websiteEventResourceService.GetString("Event name: \"{0}\" already exists."));

                    var eventTranslationInfo =
                        await _eventTranslationRepository.GetInfo(info.Id, eventTranslation.LanguageId);

                    if (eventTranslationInfo != null)
                    {
                        eventTranslationInfo.Name            = eventTranslation.Name.Trim();
                        eventTranslationInfo.MetaTitle       = eventTranslation.MetaTitle?.Trim();
                        eventTranslationInfo.Description     = eventTranslation.Description?.Trim();
                        eventTranslationInfo.MetaDescription = eventTranslation.MetaDescription?.Trim();
                        eventTranslationInfo.Content         = eventTranslation.Content?.Trim();
                        eventTranslationInfo.UnsignName      = eventTranslation.Name?.StripVietnameseChars().ToUpper();
                        eventTranslationInfo.Address         = eventTranslation.Address?.Trim();
                        if (!string.IsNullOrEmpty(eventTranslation.SeoLink))
                        {
                            eventTranslationInfo.SeoLink = eventTranslation.SeoLink.ToUrlString();
                        }
                        else
                        {
                            eventTranslationInfo.SeoLink = eventTranslation.Name.ToUrlString();
                        }

                        // Check Seolink exists.
                        var isSeolinkExists = await _eventTranslationRepository.CheckSeoLinkExists(eventId,
                                                                                                   eventTranslation.LanguageId, eventTranslationInfo.SeoLink);

                        if (isSeolinkExists)
                        {
                            return(new ActionResultResponse <string>(-5,
                                                                     _websiteEventResourceService.GetString("Seo link: \"{0}\" already exists.",
                                                                                                            eventTranslationInfo.SeoLink)));
                        }

                        await _eventTranslationRepository.Update(eventTranslationInfo);
                    }
                    else
                    {
                        var eventTranslationInsert = new EventTranslation
                        {
                            EventId         = eventId,
                            LanguageId      = eventTranslation.LanguageId.Trim(),
                            Name            = eventTranslation.Name.Trim(),
                            MetaTitle       = eventTranslation.MetaTitle?.Trim(),
                            Description     = eventTranslation.Description?.Trim(),
                            MetaDescription = eventTranslation.MetaDescription?.Trim(),
                            Content         = eventTranslation.Content?.Trim(),
                            Address         = eventTranslation.Address?.Trim(),
                            UnsignName      = eventTranslation.Name.StripVietnameseChars().ToUpper()
                        };

                        if (!string.IsNullOrEmpty(eventTranslation.SeoLink))
                        {
                            eventTranslationInsert.SeoLink = eventTranslation.SeoLink.ToUrlString();
                        }
                        else
                        {
                            eventTranslationInsert.SeoLink = eventTranslation.Name.ToUrlString();
                        }

                        // Check Seolink exists.
                        var isSeolinkExists = await _eventTranslationRepository.CheckSeoLinkExists(eventId,
                                                                                                   eventTranslation.LanguageId, eventTranslationInsert.SeoLink);

                        if (isSeolinkExists)
                        {
                            return(new ActionResultResponse <string>(-6,
                                                                     _websiteEventResourceService.GetString("Seo link: \"{0}\" already exists.",
                                                                                                            eventTranslationInsert.SeoLink)));
                        }

                        await _eventTranslationRepository.Insert(eventTranslationInsert);
                    }

                    var eventTagInsert = new TagSubjectViewModel
                    {
                        TenantId        = tenantId,
                        CreatorId       = lastUpdateUserId,
                        CreatorFullName = lastUpdateFullName,
                        CreatorAvata    = lastUpdateAvatar,
                        Type            = TagType.Event,
                        SubjectId       = eventId,
                        LanguageId      = eventTranslation.LanguageId.Trim(),
                        Tags            = eventTranslation.Tags
                    };
                    eventTags.Add(eventTagInsert);
                }

                var resultTag = await new HttpClientService()
                                .PostAsync <ActionResultResponse>($"{apiUrls.CoreApiUrl}/tags", eventTags);

                return(new ActionResultResponse <string>(1,
                                                         _websiteEventResourceService.GetString("Update event translation successful."), string.Empty, info.ConcurrencyStamp));
            }
        }