public async Task <ApiResponse> CountPage() { ApiResponse res = new ApiResponse(); try { var authorizeState = new SapoAuthorizationState() { ShopName = _shopUrl, AccessToken = _accessToken }; var client = new SapoAPIClient(authorizeState); //var objs = client.Get("/admin/pages/count.json"); var objs = client.Get("/admin/themes.json").ToString(); if (objs.Count() >= 1) { res.Data = objs; res.Code = HttpStatusCode.OK; } } catch (Exception ex) { res.Message = "Kết nối đến Sapo thất bại, vui lòng thử lại"; } return(res); }
public async Task <ApiResponse> CreateOrUpdatePage(string html, string title, string url, string id, string referPageId = null) { ApiResponse res = new ApiResponse(); try { //_log.Warn(_shopUrl); //_log.Warn(_accessToken); var authorizeState = new SapoAuthorizationState() { ShopName = _shopUrl, AccessToken = _accessToken }; var client = new SapoAPIClient(authorizeState); //var objs = client.Get("/admin/pages/count.json"); //var ss = client.Get("/admin/themes.json").ToString(); var ss = client.Get("/admin/themes.json").ToString(); var themes = JsonConvert.DeserializeObject <Themes>(ss); var theme = themes.themes.Where(x => x.role == "main").FirstOrDefault(); if (theme == null) { return(res); } //_log.Warn(ss); //_log.Warn(theme); string name = $"punnel-{id}"; string asset_key = $"templates/page.{name}.bwt"; var asset = new AssetPut() { key = asset_key, value = @"{% layout none %} " + html }; var assets = new ObjAssetWrap(); assets.asset = asset; var objc = client.Put($"/admin/themes/{theme.id}/assets.json", assets); //var kkk = client.Get($"/admin/themes/{theme.id}/assets.json"); //_log.WarnFormat("theme {0}", kkk); //page Page page = null; long?referId = null; if (string.IsNullOrEmpty(referPageId) == false) { referId = long.Parse(referPageId); } if (referId != null) { var sp = client.Get($"/admin/pages/{referId}.json"); if (sp != null) { page = JsonConvert.DeserializeObject <ObjWrap>(sp.ToString()).page; } } ObjPageWrap objPage = new ObjPageWrap(); if (page == null) { objPage.page = new PagePost() { title = title, content = $"Landing page {title}. Click nút [Xem trên web] để xem trang", published = true, published_on = DateTime.UtcNow, template_layout = $"page.{name}", alias = url }; var obj = client.Post("/admin/pages.json", objPage); page = JsonConvert.DeserializeObject <ObjWrap>(obj.ToString()).page; } else { objPage.page = new PagePost() { id = page.id, title = title, content = $"Landing page {title}. Click nút [Xem trên web] để xem trang", published = true, published_on = DateTime.UtcNow, template_layout = $"page.{name}", alias = url }; var obj = client.Put($"/admin/pages/{page.id}.json", objPage); page = JsonConvert.DeserializeObject <ObjWrap>(obj.ToString()).page; } res.Data = page.id; res.Code = HttpStatusCode.OK; } catch (Exception ex) { _log.Error(ex); res.Message = "Thao tác này chưa thực hiện được, vui lòng thử lại"; } return(res); }