Skip to content

Commit

Permalink
[ADMINAPI-1065] Add tenants endpoints (Ed-Fi-Alliance-OSS#179)
Browse files Browse the repository at this point in the history
* Add Db Contexts

* Add correct injection of context

* Add GenericRepository

* Add tenantId

* Fix program.cs

* Fix db provider issue

* Tenants Endpoints

* Add Db Contexts

* Add correct injection of context

* Add GenericRepository

* Add tenantId

* Tenants Endpoints

* Resolve conflicts

* get tenant changes

* clean up and pgsql migration

* change tenant endpoint name to tenants

* change addTenant result creation url and delete tenant model

---------

Co-authored-by: Danny Fernandez A <dfernandeza@growthaccelerationpartners.com>
  • Loading branch information
ricardogamboagap and dfernandez-gap authored Nov 7, 2024
1 parent f2a3d5f commit b80950b
Show file tree
Hide file tree
Showing 16 changed files with 683 additions and 286 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// 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.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EdFi.Ods.AdminApi.AdminConsole.Infrastructure.Services.Tenants.Commands;
using FluentValidation;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Swashbuckle.AspNetCore.Annotations;

namespace EdFi.Ods.AdminApi.AdminConsole.Features.Tenants;
internal class AddTenant : IFeature
{
public void MapEndpoints(IEndpointRouteBuilder endpoints)
{
AdminApiAdminConsoleEndpointBuilder.MapPost(endpoints, "/tenants", Execute)
.WithRouteOptions(b => b.WithResponseCode(201))
.BuildForVersions();
}

public async Task<IResult> Execute(Validator validator, IAddTenantCommand addTenantCommand, AddTenantRequest request)
{
await validator.GuardAsync(request);
var addedTenant = await addTenantCommand.Execute(request);
return Results.Created($"/tenants/{addedTenant.TenantId}", null);
}

public class AddTenantRequest : IAddTenantModel
{
[Required]
public int DocId { get; set; }
[Required]
public int InstanceId { get; set; }
[Required]
public int EdOrgId { get; set; }
[Required]
public int TenantId { get; set; }
[Required]
public string Document { get; set; }
}
internal class Validator : AbstractValidator<AddTenantRequest>
{
public Validator()
{
RuleFor(m => m.InstanceId)
.NotNull();

RuleFor(m => m.EdOrgId)
.NotNull();

RuleFor(m => m.TenantId)
.NotNull();

RuleFor(m => m.Document)
.NotNull()
.Must(BeValidDocument).WithMessage("Document must be a valid JSON.");
}

private bool BeValidDocument(string document)
{
try
{
Newtonsoft.Json.Linq.JToken.Parse(document);
return true;
}
catch (Newtonsoft.Json.JsonReaderException)
{
return false;
}
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// See the LICENSE and NOTICES files in the project root for more information.

using System.Dynamic;
using EdFi.Ods.AdminApi.AdminConsole.Infrastructure.Services.Tenants.Queries;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Newtonsoft.Json;
Expand All @@ -17,27 +18,23 @@ public void MapEndpoints(IEndpointRouteBuilder endpoints)
AdminApiAdminConsoleEndpointBuilder.MapGet(endpoints, "/tenants", GetTenants)
.BuildForVersions();

AdminApiAdminConsoleEndpointBuilder.MapGet(endpoints, "/tenant", GetTenant)
AdminApiAdminConsoleEndpointBuilder.MapGet(endpoints, "/tenants/{tenantId}", GetTenant)
.BuildForVersions();
}

internal Task<IResult> GetTenants()
internal async Task<IResult> GetTenants(IGetTenantQuery getTenantQuery)
{
using (StreamReader r = new StreamReader("Mockdata/data-tenants.json"))
{
string json = r.ReadToEnd();
List<ExpandoObject> result = JsonConvert.DeserializeObject<List<ExpandoObject>>(json);
return Task.FromResult(Results.Ok(result));
}
var tenants = await getTenantQuery.GetAll();
dynamic result = new ExpandoObject();
result.Documents = tenants.Select(x => x.Document);
return Results.Ok(result.Documents);
}

internal Task<IResult> GetTenant(int id)
internal async Task<IResult> GetTenant(IGetTenantQuery getTenantQuery, int tenantId)
{
using (StreamReader r = new StreamReader("Mockdata/data-tenant.json"))
{
string json = r.ReadToEnd();
ExpandoObject result = JsonConvert.DeserializeObject<ExpandoObject>(json);
return Task.FromResult(Results.Ok(result));
}
var tenant = await getTenantQuery.Get(tenantId);
dynamic result = new ExpandoObject();
result.Document = tenant.Document;
return Results.Ok(result.Document);
}
}

This file was deleted.

Loading

0 comments on commit b80950b

Please # to comment.