public async Task <RepositoryResponse <ApiCategoryViewModel> > Post([FromBody] ApiCategoryViewModel model) { if (model != null) { model.CreatedBy = User.Identity.Name; var result = await model.SaveModelAsync(true).ConfigureAwait(false); return(result); } return(new RepositoryResponse <ApiCategoryViewModel>()); }
public async Task <RepositoryResponse <ApiCategoryViewModel> > Post([FromBody] ApiCategoryViewModel model) { if (model != null) { model.CreatedBy = User.Claims.FirstOrDefault(c => c.Type == "Username")?.Value; var result = await model.SaveModelAsync(true).ConfigureAwait(false); return(result); } return(new RepositoryResponse <ApiCategoryViewModel>()); }
public async Task <ActionResult <JObject> > Details(string viewType, int?id) { switch (viewType) { case "be": if (id.HasValue) { var beResult = await ApiCategoryViewModel.Repository.GetSingleModelAsync(model => model.Id == id && model.Specificulture == _lang).ConfigureAwait(false); if (beResult.IsSucceed) { beResult.Data.DetailsUrl = SwCmsHelper.GetRouterUrl("Alias", new { beResult.Data.SeoName }, Request, Url); } return(Ok(JObject.FromObject(beResult))); } else { var model = new SiocCategory() { Specificulture = _lang, Status = GlobalConfigurationService.Instance.CmsConfigurations.DefaultStatus, PageSize = 20 , Priority = ApiCategoryViewModel.Repository.Max(a => a.Priority).Data + 1 }; RepositoryResponse <ApiCategoryViewModel> result = new RepositoryResponse <ApiCategoryViewModel>() { IsSucceed = true, Data = await ApiCategoryViewModel.InitViewAsync(model) }; return(JObject.FromObject(result)); } default: if (id.HasValue) { var beResult = await FECategoryViewModel.Repository.GetSingleModelAsync(model => model.Id == id && model.Specificulture == _lang).ConfigureAwait(false); if (beResult.IsSucceed) { beResult.Data.DetailsUrl = SwCmsHelper.GetRouterUrl("Alias", new { beResult.Data.SeoName }, Request, Url); } return(JObject.FromObject(beResult)); } else { var model = new SiocCategory(); RepositoryResponse <FECategoryViewModel> result = new RepositoryResponse <FECategoryViewModel>() { IsSucceed = true, Data = new FECategoryViewModel(model) { Specificulture = _lang, Status = SWStatus.Preview, PageSize = 20 } }; return(JObject.FromObject(result)); } } }
public async Task <RepositoryResponse <bool> > InitSWCms(InitCulture culture) { RepositoryResponse <bool> result = new RepositoryResponse <bool>(); SiocCmsContext context = null; SiocCmsAccountContext accountContext = null; IDbContextTransaction transaction = null; IDbContextTransaction accTransaction = null; bool isSucceed = true; try { if (!string.IsNullOrEmpty(CmsConfigurations.CmsConnectionString)) { context = new SiocCmsContext(); accountContext = new SiocCmsAccountContext(); await context.Database.MigrateAsync(); await accountContext.Database.MigrateAsync(); transaction = context.Database.BeginTransaction(); var countCulture = context.SiocCulture.Count(); var isInit = countCulture > 0; if (!isInit) { isSucceed = InitCultures(culture, context, transaction); isSucceed = isSucceed && InitPositions(context, transaction); isSucceed = isSucceed && InitThemes(context, transaction); isSucceed = isSucceed && InitConfigurations(culture, context, transaction); } else { isSucceed = true; } if (isSucceed && BECategoryViewModel.Repository.Count(context, transaction).Data == 0) { ApiCategoryViewModel cate = new ApiCategoryViewModel(new SiocCategory() { Title = "Home", Specificulture = culture.Specificulture, Template = "_Home", Type = (int)SWCmsConstants.CateType.Home, CreatedBy = "Admin" }, context, transaction); var createVNHome = await cate.SaveModelAsync(false, context, transaction).ConfigureAwait(false); ApiUrlAliasViewModel UrlAlias = new ApiUrlAliasViewModel(new SiocUrlAlias() { Specificulture = culture.Specificulture, Alias = cate.SeoName, SourceId = createVNHome.Data.Model.Id.ToString() }); var createAlias = await UrlAlias.SaveModelAsync(false, context, transaction).ConfigureAwait(false); isSucceed = createVNHome.IsSucceed && createAlias.IsSucceed; } if (isSucceed) { transaction.Commit(); } else { transaction.Rollback(); } } result.IsSucceed = isSucceed; return(result); } catch (Exception ex) // TODO: Add more specific exeption types instead of Exception only { transaction?.Rollback(); accTransaction?.Rollback(); result.IsSucceed = false; result.Exception = ex; return(result); } finally { context?.Dispose(); accountContext?.Dispose(); } }