Skip to content

Commit

Permalink
All endpoints should use new JsonResult due to Umbraco breaking stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
abjerner committed Oct 1, 2021
1 parent 5c01526 commit 8a5427c
Showing 1 changed file with 31 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,19 @@ public RedirectsController(ILogger<RedirectsController> logger, IRedirectsServic
/// list once - even if it has been assigned multiple domains.
/// </summary>
[HttpGet]
public object GetRootNodes() {
public ActionResult GetRootNodes() {

RedirectRootNode[] rootNodes = _redirects.GetRootNodes();

return new {
return new JsonResult(new {
total = rootNodes.Length,
items = rootNodes.Select(x => new RedirectRootNodeModel(x))
};
});

}

[HttpPost]
public object AddRedirect([FromBody] JObject m) {
public ActionResult AddRedirect([FromBody] JObject m) {

AddRedirectOptions model = m.ToObject<AddRedirectOptions>();

Expand All @@ -81,9 +81,9 @@ public object AddRedirect([FromBody] JObject m) {

// Add the redirect
Redirect redirect = _redirects.AddRedirect(model);

// Return the redirect
return redirect;
// Map the result for the API
return new JsonResult(_backOffice.Map(redirect));

} catch (RedirectsException ex) {

Expand All @@ -97,7 +97,7 @@ public object AddRedirect([FromBody] JObject m) {
}

[HttpPost]
public object EditRedirect(Guid redirectId, [FromBody] EditRedirectOptions model) {
public ActionResult EditRedirect(Guid redirectId, [FromBody] EditRedirectOptions model) {

try {

Expand Down Expand Up @@ -125,8 +125,8 @@ public object EditRedirect(Guid redirectId, [FromBody] EditRedirectOptions model
// Save/update the redirect
_redirects.SaveRedirect(redirect);

// Return the redirect
return redirect;
// Map the result for the API
return new JsonResult(_backOffice.Map(redirect));

} catch (RedirectsException ex) {

Expand All @@ -153,7 +153,7 @@ public object EditRedirect(Guid redirectId, [FromBody] EditRedirectOptions model
/// <param name="forward">Indicates whether the query string should be forwarded. <c>false</c> by default.</param>
/// <returns>The updated redirect.</returns>
[HttpGet]
public object EditRedirect(Guid rootNodeKey, Guid redirectId, string url,
public ActionResult EditRedirect(Guid rootNodeKey, Guid redirectId, string url,
string linkMode, int linkId, Guid linkKey, string linkUrl,
bool permanent = true, bool forward = false) {

Expand Down Expand Up @@ -200,9 +200,9 @@ public object EditRedirect(Guid rootNodeKey, Guid redirectId, string url,

// Save/update the redirect
_redirects.SaveRedirect(redirect);

// Return the redirect
return redirect;
// Map the result for the API
return new JsonResult(_backOffice.Map(redirect));

} catch (RedirectsException ex) {

Expand All @@ -220,7 +220,7 @@ public object EditRedirect(Guid rootNodeKey, Guid redirectId, string url,
/// </summary>
/// <param name="redirectId">The ID of the redirect.</param>
[HttpGet]
public object DeleteRedirect(Guid redirectId) {
public ActionResult DeleteRedirect(Guid redirectId) {

try {

Expand All @@ -230,9 +230,9 @@ public object DeleteRedirect(Guid redirectId) {

// Delete the redirect
_redirects.DeleteRedirect(redirect);

// Return the redirect
return redirect;
// Map the result for the API
return new JsonResult(_backOffice.Map(redirect));

} catch (RedirectsException ex) {

Expand All @@ -255,7 +255,7 @@ public object DeleteRedirect(Guid redirectId) {
/// <param name="rootNodeKey">The root node key that the returned redirects should match. <c>null</c> means all redirects. <see cref="Guid.Empty"/> means all global redirects.</param>
/// <returns>A list of redirects.</returns>
[HttpGet]
public object GetRedirects(int page = 1, int limit = 20, string type = null, string text = null, Guid? rootNodeKey = null) {
public ActionResult GetRedirects(int page = 1, int limit = 20, string type = null, string text = null, Guid? rootNodeKey = null) {

try {

Expand Down Expand Up @@ -286,7 +286,7 @@ public object GetRedirects(int page = 1, int limit = 20, string type = null, str
}

[HttpGet]
public object GetRedirectsForNode(string type, Guid key) {
public ActionResult GetRedirectsForNode(string type, Guid key) {

try {

Expand Down Expand Up @@ -342,10 +342,10 @@ public object GetRedirectsForNode(string type, Guid key) {
Redirect[] redirects = _redirects.GetRedirectsByNodeKey(node.Type, key);

// Generate the response
return new {
return new JsonResult(new {
node,
redirects = _backOffice.Map(redirects)
};
});

} catch (RedirectsException ex) {

Expand All @@ -356,8 +356,15 @@ public object GetRedirectsForNode(string type, Guid key) {

}

private ObjectResult Error(RedirectsException ex) {
return StatusCode((int) ex.StatusCode, new RedirectsError(ex));
private ActionResult Error(RedirectsException ex) {

// Initialize a new error model based on the exception
RedirectsError body = new RedirectsError(ex);

return new JsonResult(body) {
StatusCode = (int) ex.StatusCode
};

}

}
Expand Down

0 comments on commit 8a5427c

Please sign in to comment.