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

Minimal api changes #1986

Merged
merged 7 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
3 changes: 1 addition & 2 deletions scripts/install-aspnet-codegenerator.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#!/bin/bash

VERSION=6.0.3
VERSION=7.0.0-dev
DEFAULT_NUPKG_PATH=~/.nuget/packages
SRC_DIR=$(pwd)
echo $SRC_DIR
NUPKG=artifacts/packages/Debug/Shipping/

#kill all dotnet procs
pkill -f dotnet
rm -rf artifacts
Expand Down
4 changes: 2 additions & 2 deletions src/Scaffolding/VS.Web.CG.EFCore/DbContextEditorServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ public EditSyntaxTreeResult AddModelToContext(ModelType dbContext, ModelType mod
var lastNode = dbContextNode.ChildNodes().Last();

var safeModelName = GetSafeModelName(modelType.Name, dbContext.TypeSymbol);
var nullablilitySign = nullableEnabled ? "? " : " ";
var nullabilityClause = nullableEnabled ? " = default!;" : "";
// Todo : Need pluralization for property name below.
// It is not always safe to just use DbSet<modelType.Name> as there can be multiple class names in different namespaces.
var dbSetProperty = "public DbSet<" + modelType.FullName + ">" + nullablilitySign + safeModelName + " { get; set; }" + Environment.NewLine;
var dbSetProperty = "public DbSet<" + modelType.FullName + "> " + safeModelName + " { get; set; }" + nullabilityClause + Environment.NewLine;
var propertyDeclarationWrapper = CSharpSyntaxTree.ParseText(dbSetProperty);

var newNode = rootNode.InsertNodesAfter(lastNode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ internal async Task AddEndpointsMethod(string membersBlockText, string endpoints
usings.Add(templateModel.DbContextNamespace);
}

if (templateModel.OpenAPI)
{
usings.Add("Microsoft.AspNetCore.Http.HttpResults");
deepchoudhery marked this conversation as resolved.
Show resolved Hide resolved
usings.Add("Microsoft.AspNetCore.OpenApi");
}
deepchoudhery marked this conversation as resolved.
Show resolved Hide resolved

var endpointsCodeFile = new CodeFile { Usings = usings.ToArray()};
var docBuilder = new DocumentBuilder(docEditor, endpointsCodeFile, ConsoleLogger);
var newRoot = docBuilder.AddUsings(new CodeChangeOptions());
Expand Down
6 changes: 6 additions & 0 deletions src/Scaffolding/VS.Web.CG.Mvc/Minimal Api/MinimalApiModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ public HashSet<string> RequiredNamespaces
requiredNamespaces.Add(DbContextNamespace);
}

if (OpenAPI)
{
requiredNamespaces.Add("Microsoft.AspNetCore.Http.HttpResults");
requiredNamespaces.Add("Microsoft.AspNetCore.OpenApi");
}

// Finally we remove the ControllerNamespace as it's not required.
requiredNamespaces.Remove(EndpointsNamespace);
return new HashSet<string>(requiredNamespaces);
Expand Down
234 changes: 121 additions & 113 deletions src/Scaffolding/VS.Web.CG.Mvc/Templates/MinimalApi/MinimalApi.cshtml
Original file line number Diff line number Diff line change
@@ -1,113 +1,121 @@
@inherits Microsoft.VisualStudio.Web.CodeGeneration.Templating.RazorTemplateBase
@{
string modelName = Model.ModelType.Name;
string modelConstructor = $"{modelName}()";
string modelArray = $"{modelName}[]";
string routePrefix = "/api/" + modelName;
string endPointsClassName = Model.EndpointsName;
string methodName = $"Map{@modelName}Endpoints";
string pluralModel = $"{@modelName}s";
string getAllModels = $"GetAll{@pluralModel}";
string getModelById = $"Get{@modelName}ById";
string deleteModel = $"Delete{@modelName}";
string createModel = $"Create{@modelName}";
string updateModel = $"Update{@modelName}";
}
@{
foreach (var namespaceName in Model.RequiredNamespaces)
{
@:using @namespaceName;
}
}
namespace @Model.EndpointsNamespace;

public static class @endPointsClassName
{
public static void @Model.MethodName (this IEndpointRouteBuilder routes)
{
routes.MapGet("@routePrefix", () =>
{
return new [] { new @modelConstructor };
})
@{
if(Model.OpenAPI)
{
@:.WithTags(nameof(@modelName))
@:.WithName("@getAllModels")
@:.Produces<@modelArray>(StatusCodes.Status200OK);
}
else
{
@:.WithName("@getAllModels");
}
}

routes.MapGet("@routePrefix/{id}", (int id) =>
{
//return new @modelName { ID = id };
})
@{
if(Model.OpenAPI)
{
@:.WithTags(nameof(@modelName))
@:.WithName("@getModelById")
@:.Produces<@modelName>(StatusCodes.Status200OK);
}
else
{
@:.WithName("@getModelById");
}
}

routes.MapPut("@routePrefix/{id}", (int id, @modelName input) =>
{
return Results.NoContent();
})
@{
if(Model.OpenAPI)
{
@:.WithTags(nameof(@modelName))
@:.WithName("@updateModel")
@:.Produces(StatusCodes.Status204NoContent);
}
else
{
@:.WithName("@updateModel");
}
}

routes.MapPost("@routePrefix/", (@modelName model) =>
{
//return Results.Created($"/@pluralModel/{model.ID}", model);
})
@{
if(Model.OpenAPI)
{
@:.WithTags(nameof(@modelName))
@:.WithName("@createModel")
@:.Produces<@modelName>(StatusCodes.Status201Created);
}
else
{
@:.WithName("@createModel");
}
}

routes.MapDelete("@routePrefix/{id}", (int id) =>
{
//return Results.Ok(new @modelName { ID = id });
})
@{
if(Model.OpenAPI)
{
@:.WithTags(nameof(@modelName))
@:.WithName("@deleteModel")
@:.Produces<@modelName>(StatusCodes.Status200OK);
}
else
{
@:.WithName("@deleteModel");
}
}
}
}
@inherits Microsoft.VisualStudio.Web.CodeGeneration.Templating.RazorTemplateBase
@{
string modelName = Model.ModelType.Name;
string modelConstructor = $"{modelName}()";
string modelArray = $"{modelName}[]";
string routePrefix = "/api/" + modelName;
string endPointsClassName = Model.EndpointsName;
string methodName = $"Map{@modelName}Endpoints";
string pluralModel = $"{@modelName}s";
string getAllModels = $"GetAll{@pluralModel}";
string getModelById = $"Get{@modelName}ById";
string deleteModel = $"Delete{@modelName}";
string createModel = $"Create{@modelName}";
string updateModel = $"Update{@modelName}";
string resultsExtension = (Model.OpenAPI ? "TypedResults" : "Results") + ".NoContent()";
}
@{
foreach (var namespaceName in Model.RequiredNamespaces)
{
@:using @namespaceName;
}
}
namespace @Model.EndpointsNamespace;

public static class @endPointsClassName
{
public static void @Model.MethodName (this IEndpointRouteBuilder routes)
{
var group = routes.MapGroup("@routePrefix");
@{
if(Model.OpenAPI)
{
@:group.WithOpenApi();
}
}
group.MapGet("/", () =>
{
return new [] { new @modelConstructor };
})
@{
if(Model.OpenAPI)
{
@:.WithTags(nameof(@modelName))
deepchoudhery marked this conversation as resolved.
Show resolved Hide resolved
@:.WithName("@getAllModels")
@:.Produces<@modelArray>(StatusCodes.Status200OK);
}
else
{
@:.WithName("@getAllModels");
}
}

group.MapGet("/{id}", (int id) =>
{
//return new @modelName { ID = id };
})
@{
if(Model.OpenAPI)
{
@:.WithTags(nameof(@modelName))
@:.WithName("@getModelById")
@:.Produces<@modelName>(StatusCodes.Status200OK);
}
else
{
@:.WithName("@getModelById");
}
}

group.MapPut("/{id}", (int id, @modelName input) =>
{
return @resultsExtension;
})
@{
if(Model.OpenAPI)
{
@:.WithTags(nameof(@modelName))
@:.WithName("@updateModel")
@:.Produces(StatusCodes.Status204NoContent);
}
else
{
@:.WithName("@updateModel");
}
}

group.MapPost("/", (@modelName model) =>
{
//return Results.Created($"/@pluralModel/{model.ID}", model);
deepchoudhery marked this conversation as resolved.
Show resolved Hide resolved
})
@{
if(Model.OpenAPI)
{
@:.WithTags(nameof(@modelName))
@:.WithName("@createModel")
@:.Produces<@modelName>(StatusCodes.Status201Created);
}
else
{
@:.WithName("@createModel");
}
}

group.MapDelete("/{id}", (int id) =>
{
//return Results.Ok(new @modelName { ID = id });
})
@{
if(Model.OpenAPI)
{
@:.WithTags(nameof(@modelName))
@:.WithName("@deleteModel")
@:.Produces<@modelName>(StatusCodes.Status200OK);
}
else
{
@:.WithName("@deleteModel");
}
}
}
}
Loading