Пример #1
0
        public async Task <ActionResultResponse> Handle(InsertPageCommand request, CancellationToken cancellationToken)
        {
            // Check Id exists.
            var isIdExists = await _pageRepository.CheckExists(request.Id);

            if (isIdExists)
            {
                return(new ActionResultResponse(-1, _resourceService.GetString("Page already exists.")));
            }

            var page = new Page(request.Id, request.Name, request.Description, request.Icon.Trim(), request.Order, -1, request.Url, request.IsActive, request.IsShowSidebar);

            if (request.ParentId.HasValue)
            {
                var parentInfo = await _pageRepository.GetInfo(request.ParentId.Value, true);

                if (parentInfo == null)
                {
                    return(new ActionResultResponse(-3, _resourceService.GetString("Parent page does not exists.")));
                }

                page.SetParent(parentInfo.Id);
            }

            if (request.TenantIds != null && request.TenantIds.Any())
            {
                page.SetTenantsPage(request.TenantIds);
            }

            page.SetPageType(request.ParentId.HasValue ? PageType.Tab : PageType.Sub);

            _pageRepository.Insert(page);
            var resultInsertPage = await _uow.SaveChangesAsync(cancellationToken);

            if (resultInsertPage <= 0)
            {
                return(new ActionResultResponse(resultInsertPage, _sharedResourceService.GetString("Something went wrong. Please contact with administrator.")));
            }

            return(new ActionResultResponse(resultInsertPage, _resourceService.GetString("Add new page successful.")));
        }
Пример #2
0
        public async Task <ActionResultResponse> Insert(PageMeta pageMeta)
        {
            // Check Id exists.
            var isIdExists = await _pageRepository.CheckExists(pageMeta.Id);

            if (isIdExists)
            {
                return(new ActionResultResponse(-1, _resourceService.GetString("Page already exists.")));
            }

            if (pageMeta.PageTranslations == null || !pageMeta.PageTranslations.Any())
            {
                return(new ActionResultResponse(-2, _resourceService.GetString("Please add at least one language.")));
            }

            var page = new Page
            {
                Id       = pageMeta.Id,
                BgColor  = pageMeta.BgColor?.Trim(),
                Icon     = pageMeta.Icon?.Trim(),
                IsActive = pageMeta.IsActive,
                ParentId = pageMeta.ParentId,
                Order    = pageMeta.Order,
                Url      = pageMeta.Url?.Trim(),
                IdPath   = "-1"
            };

            if (pageMeta.ParentId.HasValue)
            {
                var parentInfo = await _pageRepository.GetInfo(pageMeta.ParentId.Value);

                if (parentInfo == null)
                {
                    return(new ActionResultResponse(-3, _resourceService.GetString("Parent page does not exists.")));
                }

                page.ParentId = parentInfo.Id;
                page.IdPath   = $"{parentInfo.IdPath}.-1";
            }

            var resultInsertPage = await _pageRepository.Insert(page);

            if (resultInsertPage <= 0)
            {
                return(new ActionResultResponse(resultInsertPage, _sharedResourceService.GetString("Something went wrong. Please contact with administrator.")));
            }

            #region Update page idPath.

            page.IdPath = page.IdPath.Replace("-1", page.Id.ToString());
            await _pageRepository.Update(page);

            #endregion

            // Insert page translation.
            foreach (var pageTranslation in pageMeta.PageTranslations)
            {
                var pageTranslate = new PageTranslation
                {
                    PageId      = page.Id,
                    Description = pageTranslation.Description?.Trim(),
                    LanguageId  = pageTranslation.LanguageId?.Trim(),
                    Name        = pageTranslation.Name.Trim(),
                    UnsignName  = pageTranslation.Name.Trim().StripVietnameseChars().ToUpper()
                };
                var resultPageTranslation = await _pageTranslationService.Save(pageTranslate);

                if (resultPageTranslation.Code <= 0)
                {
                    // Roleback insert.
                    await RollbackInsert(page.Id);
                }
            }

            return(new ActionResultResponse(resultInsertPage, _resourceService.GetString("Add new page successful.")));
        }