Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace StatusCode() when its possible #12344

Merged
merged 2 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public async Task<IActionResult> Update([FromForm] DashboardPartViewModel[] part
{
if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageAdminDashboard))
{
return StatusCode(401);
return Unauthorized();
}

var contentItemIds = parts.Select(i => i.ContentItemId).ToArray();
Expand All @@ -161,15 +161,15 @@ public async Task<IActionResult> Update([FromForm] DashboardPartViewModel[] part

if (latestItems == null)
{
return StatusCode(404);
return NotFound();
}

foreach (var contentItem in latestItems)
{
var dashboardPart = contentItem.As<DashboardPart>();
if (dashboardPart == null)
{
return StatusCode(403);
return Forbid();
}

var partViewModel = parts.Where(m => m.ContentItemId == contentItem.ContentItemId).FirstOrDefault();
Expand Down Expand Up @@ -200,7 +200,7 @@ public async Task<IActionResult> Update([FromForm] DashboardPartViewModel[] part

if (Request.Headers != null && Request.Headers["X-Requested-With"] == "XMLHttpRequest")
{
return StatusCode(200);
return Ok();
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public async Task<IActionResult> Delete(string id, string treeNodeId)

if (adminMenu.RemoveMenuItem(treeNode) == false)
{
return new StatusCodeResult(500);
return this.InternalServerError();
}

await _adminMenuService.SaveAsync(adminMenu);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public async Task<IActionResult> Render()
}
}

return StatusCode(500, new { errors = errors });
return this.InternalServerError(new { errors });
}

var previewAspect = await _contentManager.PopulateAspectAsync(contentItem, new PreviewAspect());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public async Task<IActionResult> Delete(string contentItemId)

if (contentItem == null)
{
return StatusCode(204);
return NoContent();
}

if (!await _authorizationService.AuthorizeAsync(User, CommonPermissions.DeleteContent, contentItem))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,22 +288,22 @@ public async Task<IActionResult> UpdatePosition(string contentItemId, double pos
{
if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageLayers))
{
return StatusCode(401);
return Unauthorized();
}

// Load the latest version first if any
var contentItem = await _contentManager.GetAsync(contentItemId, VersionOptions.Latest);

if (contentItem == null)
{
return StatusCode(404);
return NotFound();
}

var layerMetadata = contentItem.As<LayerMetadata>();

if (layerMetadata == null)
{
return StatusCode(403);
return Forbid();
}

layerMetadata.Position = position;
Expand All @@ -324,7 +324,7 @@ public async Task<IActionResult> UpdatePosition(string contentItemId, double pos

if (layerMetadata == null)
{
return StatusCode(403);
return Forbid();
}

layerMetadata.Position = position;
Expand All @@ -341,7 +341,7 @@ public async Task<IActionResult> UpdatePosition(string contentItemId, double pos

if (Request.Headers != null && Request.Headers["X-Requested-With"] == "XMLHttpRequest")
{
return StatusCode(200);
return Ok();
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,12 @@ public async Task<IActionResult> MoveMedia(string oldPath, string newPath)

if (!_allowedFileExtensions.Contains(Path.GetExtension(newPath), StringComparer.OrdinalIgnoreCase))
{
return StatusCode(StatusCodes.Status400BadRequest, S["This file extension is not allowed: {0}", Path.GetExtension(newPath)]);
return BadRequest(S["This file extension is not allowed: {0}", Path.GetExtension(newPath)]);
}

if (await _mediaFileStore.GetFileInfoAsync(newPath) != null)
{
return StatusCode(StatusCodes.Status400BadRequest, S["Cannot move media because a file already exists with the same name"]);
return BadRequest(S["Cannot move media because a file already exists with the same name"]);
}

await _mediaFileStore.MoveFileAsync(oldPath, newPath);
Expand Down Expand Up @@ -385,7 +385,7 @@ public async Task<ActionResult<IFileStoreEntry>> CreateFolder(

if (_invalidFolderNameCharacters.Any(invalidChar => name.Contains(invalidChar)))
{
return StatusCode(StatusCodes.Status400BadRequest, S["Cannot create folder because the folder name contains invalid characters"]);
return BadRequest(S["Cannot create folder because the folder name contains invalid characters"]);
}

var newPath = _mediaFileStore.Combine(path, name);
Expand All @@ -399,13 +399,13 @@ public async Task<ActionResult<IFileStoreEntry>> CreateFolder(
var mediaFolder = await _mediaFileStore.GetDirectoryInfoAsync(newPath);
if (mediaFolder != null)
{
return StatusCode(StatusCodes.Status400BadRequest, S["Cannot create folder because a folder already exists with the same name"]);
return BadRequest(S["Cannot create folder because a folder already exists with the same name"]);
}

var existingFile = await _mediaFileStore.GetFileInfoAsync(newPath);
if (existingFile != null)
{
return StatusCode(StatusCodes.Status400BadRequest, S["Cannot create folder because a file already exists with the same name"]);
return BadRequest(S["Cannot create folder because a file already exists with the same name"]);
}

await _mediaFileStore.TryCreateDirectoryAsync(newPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public async Task<ActionResult> Index(string token)

if (!await ShouldProceedWithTokenAsync(token))
{
return StatusCode(404);
return NotFound();
}

var model = new SetupViewModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public async Task<IActionResult> Create(CreateApiViewModel model)

var token = CreateSetupToken(settings);

return StatusCode(201, GetEncodedUrl(settings, token));
return Created(GetEncodedUrl(settings, token), null);
}
else
{
Expand Down Expand Up @@ -165,7 +165,7 @@ public async Task<ActionResult> Setup(SetupApiViewModel model)

if (shellSettings.State == TenantState.Running)
{
return StatusCode(201);
return Created(GetEncodedUrl(shellSettings, null), null);
}

if (shellSettings.State != TenantState.Uninitialized)
Expand Down Expand Up @@ -280,7 +280,7 @@ public async Task<ActionResult> Setup(SetupApiViewModel model)
ModelState.AddModelError(error.Key, error.Value);
}

return StatusCode(500, ModelState);
return this.InternalServerError(ModelState);
}

return Ok(executionId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Net;

namespace Microsoft.AspNetCore.Mvc
{
Expand Down Expand Up @@ -27,6 +28,14 @@ public static ActionResult ChallengeOrForbid(this Controller controller)
public static ActionResult ChallengeOrForbid(this Controller controller, params string[] authenticationSchemes)
=> controller.User?.Identity?.IsAuthenticated ?? false ? (ActionResult)controller.Forbid(authenticationSchemes) : controller.Challenge(authenticationSchemes);

/// <summary>
/// Creates <see cref="ObjectResult"/> that produces a <see cref="HttpStatusCode.InternalServerError"/> response.
/// </summary>
/// <param name="controller">The <see cref="Controller"/>.</param>
/// <param name="value">An optinal value to set on <see cref="ObjectResult"/>.</param>
public static ActionResult InternalServerError(this Controller controller, object value = null)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jtkech why this not accessible from within OC.XmlRpc module?!!

=> controller.StatusCode((int)HttpStatusCode.InternalServerError, value);

/// <summary>
/// Creates a <see cref="LocalRedirectResult"/> object that redirects to the specified local localUrl
/// </summary>
Expand Down