diff --git a/src/Moryx.AbstractionLayer.Resources.Endpoints/ResourceManagementController.cs b/src/Moryx.AbstractionLayer.Resources.Endpoints/ResourceManagementController.cs index 0c74c7cd..26fa58a6 100644 --- a/src/Moryx.AbstractionLayer.Resources.Endpoints/ResourceManagementController.cs +++ b/src/Moryx.AbstractionLayer.Resources.Endpoints/ResourceManagementController.cs @@ -17,6 +17,7 @@ using Moryx.Runtime.Modules; using Moryx.Configuration; using System.Runtime.Serialization; +using System.ComponentModel.DataAnnotations; namespace Moryx.AbstractionLayer.Resources.Endpoints { @@ -32,8 +33,8 @@ public class ResourceModificationController : ControllerBase private readonly IResourceTypeTree _resourceTypeTree; private readonly ResourceSerialization _serialization; - public ResourceModificationController(IResourceManagement resourceManagement, - IResourceTypeTree resourceTypeTree, + public ResourceModificationController(IResourceManagement resourceManagement, + IResourceTypeTree resourceTypeTree, IModuleManager moduleManager, IServiceProvider serviceProvider) { @@ -121,7 +122,7 @@ public ActionResult InvokeMethod(long id, string method, Entry parameters return true; }); } - catch(MissingMethodException) + catch (MissingMethodException) { return BadRequest("Method could not be invoked. Please check spelling and access modifier (has to be `public` or `internal`)."); } @@ -142,30 +143,48 @@ public ActionResult InvokeMethod(long id, string method, Entry parameters public ActionResult ConstructWithParameters(string type, string method = null, [FromBody(EmptyBodyBehavior = EmptyBodyBehavior.Allow)] Entry arguments = null) { var trustedType = WebUtility.HtmlEncode(type); - - return method == null ? Construct(trustedType, null) - : Construct(trustedType, new MethodEntry { Name = method, Parameters = arguments }); + if (method is null) + return Construct(trustedType); + else + return Construct(trustedType, new MethodEntry { Name = method, Parameters = arguments }); } - private ActionResult Construct(string type, MethodEntry method) + private ActionResult Construct(string type) { - var resource = (Resource)Activator.CreateInstance(_resourceTypeTree[type].ResourceType); - if (resource is null) + Resource resource; + try + { + resource = (Resource)Activator.CreateInstance(_resourceTypeTree[type].ResourceType); + } + catch (Exception) + { return NotFound(new MoryxExceptionResponse { Title = Strings.RESOURCE_NOT_FOUND }); + } ValueProviderExecutor.Execute(resource, new ValueProviderExecutorSettings() .AddFilter(new DataMemberAttributeValueProviderFilter(false)) .AddDefaultValueProvider()); - if (method != null) - EntryConvert.InvokeMethod(resource, method, _serialization); - var model = new ResourceToModelConverter(_resourceTypeTree, _serialization).GetDetails(resource); model.Methods = Array.Empty(); // Reset methods because they can not be invoked on new objects - return model; } + private ActionResult Construct(string type, MethodEntry method) + { + try + { + var id = _resourceManagement.Create(_resourceTypeTree[type].ResourceType, r => EntryConvert.InvokeMethod(r, method, _serialization)); + return GetDetails(id); + } + catch (Exception e) + { + if (e is ArgumentException or SerializationException or ValidationException) + return BadRequest(e.Message); + throw; + } + } + [HttpPost] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status409Conflict)] @@ -185,11 +204,11 @@ public ActionResult Save(ResourceModel model) resourcesToSave.Skip(1).ForEach(id => _resourceManagement.Modify(id, r => true)); }); - return Ok(GetDetails(id)); + return GetDetails(id); } catch (Exception e) { - if (e is ArgumentException or SerializationException) + if (e is ArgumentException or SerializationException or ValidationException) return BadRequest(e.Message); throw; } @@ -323,7 +342,7 @@ private void UpdateReferences(Resource instance, HashSet resourcesToSave, [Authorize(Policy = ResourcePermissions.CanEdit)] public ActionResult Update(long id, ResourceModel model) { - if (_resourceManagement.GetAllResources(r=>r.Id == id) is null) + if (_resourceManagement.GetAllResources(r => r.Id == id) is null) return NotFound(new MoryxExceptionResponse { Title = string.Format(Strings.ResourceNotFoundException_ById_Message, id) }); try @@ -339,7 +358,7 @@ public ActionResult Update(long id, ResourceModel model) } catch (Exception e) { - if (e is ArgumentException or SerializationException) + if (e is ArgumentException or SerializationException or ValidationException) return BadRequest(e.Message); throw; }