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

docs: Add samples on how to use YeSql library #83

Merged
merged 6 commits into from
Jan 4, 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
4 changes: 3 additions & 1 deletion .github/workflows/dotnetcore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ jobs:
with:
dotnet-version: ${{ matrix.dotnet-version }}
- name: Test
run: dotnet test ./tests/YeSql.Net.Tests.csproj
run: |
dotnet test ./tests/YeSql.Net.Tests.csproj
dotnet test ./samples/Example.AspNetCore.Tests/Example.AspNetCore.Tests.csproj
5 changes: 5 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="CopySqlFilesToOutputDirectory" Version="0.1.0-alpha" />
<PackageVersion Include="CopySqlFilesToPublishDirectory" Version="0.1.0-alpha" />
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="7.0.10" />
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageVersion Include="IsExternalInit" Version="1.0.3" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.3" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageVersion Include="FluentAssertions" Version="6.10.0" />
<PackageVersion Include="NUnit" Version="3.13.3" />
Expand Down
29 changes: 19 additions & 10 deletions YeSql.Net.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YeSql.Net", "src\YeSql.Net.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YeSql.Net.Tests", "tests\YeSql.Net.Tests.csproj", "{A057DB65-24DF-4D36-9C35-78F96B54F95D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ParserSample", "samples\ParserSample\ParserSample.csproj", "{CCC6D826-5D29-428A-AAE6-FD3E1D61BB8D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.ConsoleApp", "samples\Example.ConsoleApp\Example.ConsoleApp.csproj", "{52A8D12E-EC36-441C-939D-DEE30822C09B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LoaderSample", "samples\LoaderSample\LoaderSample.csproj", "{B62F5C67-64F7-4BFC-A0AE-DE04B6A64D5E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.Reports", "samples\Example.Reports\Example.Reports.csproj", "{8A45DB52-0C49-4DA8-AFA4-0B1853EB4CC4}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Example.SqlFiles", "samples\Example.SqlFiles\Example.SqlFiles.csproj", "{1A5327E6-1897-4300-B08E-472C13A517B6}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{A2554DDC-4D44-4D81-82C3-6491F99C3159}"
ProjectSection(SolutionItems) = preProject
Expand All @@ -33,16 +35,23 @@ Global
{A057DB65-24DF-4D36-9C35-78F96B54F95D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A057DB65-24DF-4D36-9C35-78F96B54F95D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A057DB65-24DF-4D36-9C35-78F96B54F95D}.Release|Any CPU.Build.0 = Release|Any CPU
{CCC6D826-5D29-428A-AAE6-FD3E1D61BB8D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CCC6D826-5D29-428A-AAE6-FD3E1D61BB8D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CCC6D826-5D29-428A-AAE6-FD3E1D61BB8D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CCC6D826-5D29-428A-AAE6-FD3E1D61BB8D}.Release|Any CPU.Build.0 = Release|Any CPU
{B62F5C67-64F7-4BFC-A0AE-DE04B6A64D5E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B62F5C67-64F7-4BFC-A0AE-DE04B6A64D5E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B62F5C67-64F7-4BFC-A0AE-DE04B6A64D5E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B62F5C67-64F7-4BFC-A0AE-DE04B6A64D5E}.Release|Any CPU.Build.0 = Release|Any CPU
{52A8D12E-EC36-441C-939D-DEE30822C09B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{52A8D12E-EC36-441C-939D-DEE30822C09B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{52A8D12E-EC36-441C-939D-DEE30822C09B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{52A8D12E-EC36-441C-939D-DEE30822C09B}.Release|Any CPU.Build.0 = Release|Any CPU
{8A45DB52-0C49-4DA8-AFA4-0B1853EB4CC4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8A45DB52-0C49-4DA8-AFA4-0B1853EB4CC4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8A45DB52-0C49-4DA8-AFA4-0B1853EB4CC4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8A45DB52-0C49-4DA8-AFA4-0B1853EB4CC4}.Release|Any CPU.Build.0 = Release|Any CPU
{1A5327E6-1897-4300-B08E-472C13A517B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1A5327E6-1897-4300-B08E-472C13A517B6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1A5327E6-1897-4300-B08E-472C13A517B6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1A5327E6-1897-4300-B08E-472C13A517B6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2C9084BD-1A26-4A59-A1C9-D7085585B2CF}
EndGlobalSection
EndGlobal
26 changes: 26 additions & 0 deletions samples/Example.AspNetCore.Tests/Example.AspNetCore.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" />
<PackageReference Include="FluentAssertions" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="NUnit" />
<PackageReference Include="NUnit3TestAdapter" />
<PackageReference Include="NUnit.Analyzers" />
<PackageReference Include="coverlet.msbuild">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Example.AspNetCore\Example.AspNetCore.csproj" />
</ItemGroup>

</Project>
4 changes: 4 additions & 0 deletions samples/Example.AspNetCore.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
global using NUnit.Framework;
global using FluentAssertions;
global using Microsoft.AspNetCore.Mvc.Testing;
global using System.Net;
31 changes: 31 additions & 0 deletions samples/Example.AspNetCore.Tests/SqlTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace Example.AspNetCore.Tests;

public class SqlTests
{
[TestCase("/api/Sql/GetUsersSql", "SELECT* FROM [user];")]
[TestCase("/api/Sql/GetRolesSql", "SELECT* FROM [role];")]
[TestCase("/api/Sql/GetProductsSql", "SELECT* FROM [product];")]
[TestCase("/api/Sql/GetCustomersSql", "SELECT* FROM [customer];")]
[TestCase("/api/Sql/GetOrdersSql", "SELECT* FROM [order];")]
[TestCase("/api/Sql/GetPermissionsSql", "SELECT* FROM [permission];")]
[TestCase("/api/Sql/GetThirdPartiesSql", "SELECT* FROM [third_party];")]
public async Task Get_WhenSqlCodeIsRetrieved_ShouldReturnsHttpStatusCodeOk(
string requestUri,
string expectedSql)
{
// Arrange
using var factory = new WebApplicationFactory<Program>();
var client = factory.CreateClient();

// Act
var httpResponse = await client.GetAsync(requestUri);
var result = (await httpResponse
.Content
.ReadAsStringAsync())
.TrimEnd(Environment.NewLine.ToCharArray());

// Asserts
httpResponse.StatusCode.Should().Be(HttpStatusCode.OK);
result.Should().Be(expectedSql);
}
}
20 changes: 20 additions & 0 deletions samples/Example.AspNetCore/Example.AspNetCore.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>

</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\YeSql.Net.csproj" />
<ProjectReference Include="..\Example.Reports\Example.Reports.csproj" />
<ProjectReference Include="..\Example.SqlFiles\Example.SqlFiles.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="CopySqlFilesToOutputDirectory" />
<PackageReference Include="CopySqlFilesToPublishDirectory" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" />
<PackageReference Include="Swashbuckle.AspNetCore" />
</ItemGroup>

</Project>
32 changes: 32 additions & 0 deletions samples/Example.AspNetCore/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using YeSql.Net;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

var sqlStatements = new YeSqlLoader().Load();
builder.Services.AddSingleton(sqlStatements);
builder.Services.AddControllers();

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

public partial class Program { }
41 changes: 41 additions & 0 deletions samples/Example.AspNetCore/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:8839",
"sslPort": 44331
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5194",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7068;http://localhost:5194",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
40 changes: 40 additions & 0 deletions samples/Example.AspNetCore/SqlController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Microsoft.AspNetCore.Mvc;
using YeSql.Net;

namespace Example.AspNetCore;

[ApiController]
[Route("api/[controller]")]
public class SqlController : ControllerBase
{
private readonly IYeSqlCollection _sqlCollection;
public SqlController(IYeSqlCollection sqlCollection) => _sqlCollection = sqlCollection;

[HttpGet("GetUsersSql")]
public string GetUsersSql()
=> _sqlCollection["GetUsers"];

[HttpGet("GetRolesSql")]
public string GetRolesSql()
=> _sqlCollection["GetRoles"];

[HttpGet("GetProductsSql")]
public string GetProductsSql()
=> _sqlCollection["GetProducts"];

[HttpGet("GetCustomersSql")]
public string GetCustomersSql()
=> _sqlCollection["GetCustomers"];

[HttpGet("GetOrdersSql")]
public string GetOrdersSql()
=> _sqlCollection["GetOrders"];

[HttpGet("GetPermissionsSql")]
public string GetPermissionsSql()
=> _sqlCollection["GetPermissions"];

[HttpGet("GetThirdPartiesSql")]
public string GetThirdPartiesSql()
=> _sqlCollection["GetThirdParties"];
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- name: GetProducts
-- Gets a list of products.
SELECT* FROM product;
SELECT* FROM [product];
-- End.
17 changes: 17 additions & 0 deletions samples/Example.ConsoleApp/Example.ConsoleApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\YeSql.Net.csproj" />
<ProjectReference Include="..\Example.SqlFiles\Example.SqlFiles.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="CopySqlFilesToOutputDirectory" />
<PackageReference Include="CopySqlFilesToPublishDirectory" />
</ItemGroup>

</Project>
10 changes: 10 additions & 0 deletions samples/Example.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using YeSql.Net;

var sqlStatements = new YeSqlLoader().Load();
Console.Write(sqlStatements["GetUsers"]);
Console.Write(sqlStatements["GetRoles"]);
Console.Write(sqlStatements["GetProducts"]);
Console.Write(sqlStatements["GetCustomers"]);
Console.Write(sqlStatements["GetOrders"]);
Console.Write(sqlStatements["GetPermissions"]);
Console.Write(sqlStatements["GetThirdParties"]);
7 changes: 7 additions & 0 deletions samples/Example.ConsoleApp/sample.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- name: GetUsers
-- Gets user records.
SELECT* FROM [user];

-- name: GetRoles
-- Gets role records.
SELECT* FROM [role];
4 changes: 4 additions & 0 deletions samples/Example.ConsoleApp/sql/sample.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- name: GetProducts
-- Gets a list of products.
SELECT* FROM [product];
-- End.
6 changes: 6 additions & 0 deletions samples/Example.NetFramework/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8.1" />
</startup>
</configuration>
Loading