Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Root.Apps.Web.Csweb.Controllers
- {
- using System;
- using System.Collections.Generic;
- using System.Net;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Logging;
- using Newtonsoft.Json.Linq;
- using Contentstack.Core;
- using Contentstack.Core.Models;
- using Microsoft.AspNetCore.Hosting; //TODO: which?
- using Microsoft.Extensions.Hosting;
- using Deliverystack.Core.Models.Repositories;
- public class ContentstackController : Controller
- {
- private readonly ILogger<ContentstackController> _logger;
- private IRepository _repository;
- private ContentstackClient _client;
- private IHostApplicationLifetime _applicationLifetime;
- public ContentstackController(
- ContentstackClient client,
- IRepository repository,
- ILogger<ContentstackController> logger,
- IHostApplicationLifetime applicationLifetime)
- {
- _client = client;
- _repository = repository;
- _logger = logger;
- _applicationLifetime = applicationLifetime;
- }
- public ContentResult ContentTypes()
- {
- string result = "{" + Environment.NewLine;
- foreach (JObject contentType in _client.GetContentTypes(new Dictionary<string, object>()).Result)
- {
- result += " \"" + contentType["uid"] + "\": \"" + contentType["title"] +
- "\", url: \"/Contentstack/ContentType?ctid=" + contentType["uid"] + "\"," + Environment.NewLine;
- }
- return Content(result + '}');
- }
- // public RedirectResult Reset()
- public ActionResult Reset()
- {
- // _applicationLifetime.StopApplication();
- // Program.Shutdown();
- // _applicationLifetime.StopApplication();
- _repository.Reset();
- // return Redirect("/");
- return Content("Reset() requested at " + DateTime.Now); // TODO: pause and redirect
- }
- public ActionResult Refresh()
- {
- _repository.Refresh();
- return Content("Refresh() requested at " + DateTime.Now); // TODO: pause and redirect
- }
- public ContentResult Stack()
- {
- return Content("{\"ContentstackOptions\": {\"ApiKey\": \""
- + _client.GetApplicationKey()
- + "\", \"AccessToken\": \""
- + _client.GetAccessToken()
- + ", \"Environment\": \""
- + _client.GetEnvironment()
- + "\"}}");
- }
- public ContentResult ContentType([FromQuery(Name = "ctid")] string ctid)
- {
- foreach (JObject contentType in _client.GetContentTypes(new Dictionary<string, object>()).Result)
- {
- if (String.Equals(contentType["uid"].ToString().ToLower(), ctid, StringComparison.InvariantCultureIgnoreCase))
- {
- // render the JSON representation of the Content Type identified by the ctid query string
- return Content(contentType.ToString());
- }
- }
- return ContentTypes();
- }
- public IActionResult Query([FromQuery(Name = "query")] string query)
- {
- using (var w = new WebClient())
- {
- string url = "https://graphql.contentstack.com/stacks/" +
- _client.GetApplicationKey() + "?access_token=" +
- _client.GetAccessToken() + "&environment=" +
- _client.GetEnvironment() + "&query=" + query;
- return Content(JObject.Parse(w.DownloadString(url)).ToString());
- }
- }
- public ContentResult Entries([FromQuery(Name = "ctid")] string ctid)
- {
- string result = "{" + Environment.NewLine;
- foreach (Entry element in _client.ContentType(ctid).Query().Find<Entry>().Result)
- {
- result += " { ctid: \"" + ctid + "\", uid: \"" + element.Uid + "\", title: \"" + element.Title + "\" }" + Environment.NewLine;
- }
- return Content(result + '}');
- }
- public IActionResult Entry([FromQuery(Name = "ctid")] string ctid, [FromQuery(Name = "uid")] string uid, [FromQuery(Name = "json")] bool json = false)
- {
- Entry entry = _client.ContentType(ctid).Entry(uid).Fetch<Entry>().Result;
- if (json)
- {
- return Content(entry.ToJson().ToString());
- }
- if (entry.Object.ContainsKey("view"))
- {
- return this.View(entry.Object["view"].ToString(), entry);
- }
- return this.View(entry);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement