forked from Ed-Fi-Alliance-OSS/AdminAPI-1.x
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ADMINAPI-1100] - Finalize adminconsole/instances endpoints (#201)
* Add Update and Delete Instance feature * Add Update and Delete Instance feature * Fix GetInstanceById Fix EditInstance * Fix EditInstanceCommand * Change response code in PATCH verb --------- Co-authored-by: Juan Agudelo <jagudelo@wearegap.com>
- Loading branch information
1 parent
81c022f
commit 28d024e
Showing
24 changed files
with
1,022 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
Application/EdFi.Ods.AdminApi.AdminConsole/Features/Instances/DeleteInstance.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Licensed to the Ed-Fi Alliance under one or more agreements. | ||
// The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0. | ||
// See the LICENSE and NOTICES files in the project root for more information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using EdFi.Ods.AdminApi.AdminConsole.Infrastructure.Services.Instances.Commands; | ||
using EdFi.Ods.AdminApi.Common.Features; | ||
using EdFi.Ods.AdminApi.Common.Infrastructure; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Routing; | ||
using static EdFi.Ods.AdminApi.AdminConsole.Features.Instances.EditInstance; | ||
|
||
namespace EdFi.Ods.AdminApi.AdminConsole.Features.Instances; | ||
public class DeleteInstance : IFeature | ||
{ | ||
public void MapEndpoints(IEndpointRouteBuilder endpoints) | ||
{ | ||
AdminApiEndpointBuilder.MapDelete(endpoints, "/instances/{odsinstanceid}", Execute) | ||
.WithRouteOptions(b => b.WithResponseCode(200, FeatureCommonConstants.DeletedSuccessResponseDescription)) | ||
.BuildForVersions(AdminApiVersions.AdminConsole); | ||
} | ||
|
||
public async Task<IResult> Execute(IDeleteInstanceCommand deleteInstanceCommand, int odsInstanceId) | ||
{ | ||
await deleteInstanceCommand.Execute(odsInstanceId); | ||
return Results.Ok(); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
Application/EdFi.Ods.AdminApi.AdminConsole/Features/Instances/EditInstance.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Licensed to the Ed-Fi Alliance under one or more agreements. | ||
// The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0. | ||
// See the LICENSE and NOTICES files in the project root for more information. | ||
|
||
using EdFi.Ods.AdminApi.AdminConsole.Infrastructure.Services.Instances.Commands; | ||
using System.ComponentModel.DataAnnotations; | ||
using EdFi.Ods.AdminApi.Common.Features; | ||
using EdFi.Ods.AdminApi.Common.Infrastructure; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Routing; | ||
using FluentValidation; | ||
using static EdFi.Ods.AdminApi.AdminConsole.Features.Instances.AddInstance; | ||
using System.Dynamic; | ||
using System.Text.Json; | ||
|
||
namespace EdFi.Ods.AdminApi.AdminConsole.Features.Instances; | ||
|
||
public class EditInstance : IFeature | ||
{ | ||
public void MapEndpoints(IEndpointRouteBuilder endpoints) | ||
{ | ||
AdminApiEndpointBuilder.MapPatch(endpoints, "/instances/{odsinstanceid}", Execute) | ||
.WithRouteOptions(b => b.WithResponseCode(204)) | ||
.BuildForVersions(AdminApiVersions.AdminConsole); | ||
} | ||
|
||
public async Task<IResult> Execute(Validator validator, IEditInstanceCommand editInstanceCommand, EditInstanceRequest request, int odsInstanceId) | ||
{ | ||
await validator.GuardAsync(request); | ||
var instance = await editInstanceCommand.Execute(odsInstanceId, request); | ||
return Results.NoContent(); | ||
} | ||
|
||
public class EditInstanceRequest : IEditInstanceModel | ||
{ | ||
[Required] | ||
public ExpandoObject Document { get; set; } | ||
} | ||
|
||
public class Validator : AbstractValidator<EditInstanceRequest> | ||
{ | ||
public Validator() | ||
{ | ||
RuleFor(m => m.Document) | ||
.NotNull() | ||
.NotEmpty() | ||
.Must(BeValidDocument).WithMessage("Document must be a valid JSON."); | ||
} | ||
|
||
private bool BeValidDocument(ExpandoObject document) | ||
{ | ||
try | ||
{ | ||
var jDocument = JsonSerializer.Serialize(document); | ||
Newtonsoft.Json.Linq.JToken.Parse(jDocument); | ||
return true; | ||
} | ||
catch | ||
{ | ||
return false; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
Application/EdFi.Ods.AdminApi.AdminConsole/Helpers/ExpandoObjectConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Licensed to the Ed-Fi Alliance under one or more agreements. | ||
// The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0. | ||
// See the LICENSE and NOTICES files in the project root for more information. | ||
|
||
using System.Dynamic; | ||
using Newtonsoft.Json; | ||
|
||
namespace EdFi.Ods.AdminApi.AdminConsole.Helpers; | ||
|
||
public class ExpandoObjectConverter : JsonConverter | ||
{ | ||
public override bool CanConvert(Type objectType) | ||
{ | ||
return typeof(ExpandoObject).IsAssignableFrom(objectType); | ||
} | ||
|
||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
var dictionary = (IDictionary<string, object>)value; | ||
writer.WriteStartObject(); | ||
|
||
foreach (var kvp in dictionary) | ||
{ | ||
writer.WritePropertyName(kvp.Key); | ||
serializer.Serialize(writer, kvp.Value); | ||
} | ||
|
||
writer.WriteEndObject(); | ||
} | ||
|
||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
return serializer.Deserialize<ExpandoObject>(reader); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
Application/EdFi.Ods.AdminApi.AdminConsole/Helpers/ExpandoObjectHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Licensed to the Ed-Fi Alliance under one or more agreements. | ||
// The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0. | ||
// See the LICENSE and NOTICES files in the project root for more information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Dynamic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
using Newtonsoft.Json; | ||
|
||
namespace EdFi.Ods.AdminApi.AdminConsole.Helpers | ||
{ | ||
public static class ExpandoObjectHelper | ||
{ | ||
public static ExpandoObject NormalizeExpandoObject(ExpandoObject expando) | ||
{ | ||
var dictionary = (IDictionary<string, object>)expando; | ||
var normalized = new ExpandoObject() as IDictionary<string, object>; | ||
|
||
foreach (var kvp in dictionary) | ||
{ | ||
if (kvp.Value is JsonElement jsonElement) | ||
{ | ||
normalized[kvp.Key] = jsonElement.ValueKind switch | ||
{ | ||
JsonValueKind.String => jsonElement.GetString(), | ||
JsonValueKind.Number => jsonElement.GetDecimal(), // Cambia según el tipo esperado | ||
JsonValueKind.True => true, | ||
JsonValueKind.False => false, | ||
JsonValueKind.Null => null, | ||
JsonValueKind.Object => JsonConvert.DeserializeObject<ExpandoObject>(jsonElement.GetRawText()), | ||
_ => jsonElement.ToString() | ||
}; | ||
} | ||
else if (kvp.Value is ExpandoObject nestedExpando) | ||
{ | ||
normalized[kvp.Key] = NormalizeExpandoObject(nestedExpando); | ||
} | ||
else | ||
{ | ||
normalized[kvp.Key] = kvp.Value; | ||
} | ||
} | ||
|
||
return (ExpandoObject)normalized; | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.