Skip to content

Commit

Permalink
docs: Add samples on how to use YeSql library
Browse files Browse the repository at this point in the history
  • Loading branch information
MrDave1999 committed Jan 4, 2024
1 parent 9db2a7f commit 01ce131
Show file tree
Hide file tree
Showing 27 changed files with 450 additions and 0 deletions.
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"];
}
7 changes: 7 additions & 0 deletions samples/Example.AspNetCore/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.AspNetCore/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.
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>
70 changes: 70 additions & 0 deletions samples/Example.NetFramework/Example.NetFramework.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{1A171DEE-BD4A-40AC-B926-9C2C50CA82D6}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>Example.NetFramework</RootNamespace>
<AssemblyName>Example.NetFramework</AssemblyName>
<TargetFrameworkVersion>v4.8.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\YeSql.Net.csproj">
<Project>{6bc0d80c-3a4d-4146-bc50-b7bcc25f56c2}</Project>
<Name>YeSql.Net</Name>
</ProjectReference>
<ProjectReference Include="..\Example.Reports\Example.Reports.csproj">
<Project>{17c13e48-008d-4992-94d2-d137fab869c8}</Project>
<Name>Example.Reports</Name>
</ProjectReference>
<ProjectReference Include="..\Example.SqlFiles\Example.SqlFiles.csproj">
<Project>{ce2252ee-6758-4ba2-8969-9252d8d60bc4}</Project>
<Name>Example.SqlFiles</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="CopySqlFilesToOutputDirectory" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
21 changes: 21 additions & 0 deletions samples/Example.NetFramework/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using YeSql.Net;

namespace Example.NetFramework
{
internal class Program
{
static void Main(string[] args)
{
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"]);
Console.ReadLine();
}
}
}
36 changes: 36 additions & 0 deletions samples/Example.NetFramework/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Example.NetFramework")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Example.NetFramework")]
[assembly: AssemblyCopyright("Copyright © 2024")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("1a171dee-bd4a-40ac-b926-9c2c50ca82d6")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Loading

0 comments on commit 01ce131

Please sign in to comment.