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

Port: Add propper 400 response to update and save actions in the resource controller #484

Merged
merged 2 commits into from
Nov 21, 2024
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 @@ -17,6 +17,7 @@
using Moryx.Runtime.Modules;
using Moryx.Runtime.Container;
using Moryx.Configuration;
using System.Runtime.Serialization;

namespace Moryx.AbstractionLayer.Resources.Endpoints
{
Expand Down Expand Up @@ -170,24 +171,32 @@ private ActionResult<ResourceModel> Construct(string type, MethodEntry method)
[HttpPost]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status409Conflict)]
[ProducesResponseType(StatusCodes.Status417ExpectationFailed)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[Authorize(Policy = ResourcePermissions.CanAdd)]
public ActionResult<ResourceModel> Save(ResourceModel model)
{
if (_resourceManagement.GetAllResources<IResource>(r => r.Id == model.Id).Count() > 0)
if (_resourceManagement.GetAllResources<IResource>(r => r.Id == model.Id).Any())
return Conflict($"The resource '{model.Id}' already exists.");
try
{
var id = _resourceManagement.Create(_resourceTypeTree[model.Type].ResourceType, r => {
var resourcesToSave = new HashSet<long>();
var resourceCache = new Dictionary<long, Resource>();
FromModel(model, resourcesToSave, resourceCache, r);
resourcesToSave.Skip(1).ForEach(id => _resourceManagement.Modify(id, r => true));
});

var id = _resourceManagement.Create(_resourceTypeTree[model.Type].ResourceType, r => {
var resourcesToSave = new HashSet<long>();
var resourceCache = new Dictionary<long, Resource>();
FromModel(model, resourcesToSave, resourceCache, r);
resourcesToSave.Skip(1).ForEach(id => _resourceManagement.Modify(id, r => true ));
});

return GetDetails(id);
return Ok(GetDetails(id));
}
catch (Exception e)
{
if (e is ArgumentException or SerializationException)
return BadRequest(e.Message);
throw;
}
}


/// <summary>
/// Convert ResourceModel back to resource and/or update its properties
/// </summary>
Expand Down Expand Up @@ -309,24 +318,35 @@ private void UpdateReferences(Resource instance, HashSet<long> resourcesToSave,

[HttpPut]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status417ExpectationFailed)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[Route("{id}")]
[Authorize(Policy = ResourcePermissions.CanEdit)]
public ActionResult<ResourceModel> Update(long id, ResourceModel model)
{
if (_resourceManagement.GetAllResources<IResource>(r=>r.Id == id) is null)
return NotFound(new MoryxExceptionResponse { Title = string.Format(Strings.ResourceNotFoundException_ById_Message, id) });

_resourceManagement.Modify(id, r => {
var resourcesToSave = new HashSet<long>();
var resourceCache = new Dictionary<long, Resource>();
FromModel(model, resourcesToSave, resourceCache, r);
resourcesToSave.ForEach(id => _resourceManagement.Modify(id, r => true));
return true;
});
try
{
_resourceManagement.Modify(id, r =>
{
var resourcesToSave = new HashSet<long>();
var resourceCache = new Dictionary<long, Resource>();
FromModel(model, resourcesToSave, resourceCache, r);
resourcesToSave.ForEach(id => _resourceManagement.Modify(id, r => true));
return true;
});
}
catch (Exception e)
{
if (e is ArgumentException or SerializationException)
return BadRequest(e.Message);
throw;
}

return GetDetails(id);
return Ok(GetDetails(id));
}

[HttpDelete]
Expand Down
14 changes: 13 additions & 1 deletion src/Moryx/Serialization/DefaultSerialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,19 @@ public virtual object ConvertValue(Type memberType, ICustomAttributeProvider att
return CollectionBuilder(memberType, currentValue, mappedEntry);
default:
var value = mappedEntry.Value.Current;
return value == null ? null : EntryConvert.ToObject(memberType, value, FormatProvider);
if (value is null)
return null;

try
{
return EntryConvert.ToObject(memberType, value, FormatProvider);
}
catch (Exception e)
{
if (e is FormatException or OverflowException)
throw new ArgumentException($"Invalid value {mappedEntry.Value.Current} for entry {mappedEntry.DisplayName ?? mappedEntry.Identifier}", e);
throw;
}
}
}
/// <see cref="ICustomSerialization"/>
Expand Down
Loading