Skip to content

Commit

Permalink
Add first draft version
Browse files Browse the repository at this point in the history
  • Loading branch information
vbilopav committed Apr 19, 2021
1 parent 79d03ca commit c6d44d8
Show file tree
Hide file tree
Showing 20 changed files with 325 additions and 37 deletions.
82 changes: 78 additions & 4 deletions CompaniesWebBlazor/Client/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -1,16 +1,90 @@
@page "/"

@inject HttpClient Http
@using System.Text.Json

<_Nav BrandContent="PgRoutiner Demo"></_Nav>

<main class="container vh-100">

<div>hai worldz</div>
<div class="row filter">
<div class="col form-group input-group">
<input type="text" class="form-control"
autocorrect="off" autocapitalize="off" spellcheck="false"
placeholder="Search by name..." />
<select class="form-select-lg">
<option selected>Select area...</option>
@foreach (var area in areas)
{
<option value="@area.Id">@area.Name</option>
}
</select>
</div>
</div>

</main>
<div class="row">
<div class="col">
<button type="button" class="btn btn-primary">
<i class="icon-doc-new" />
Add New
</button>
</div>
</div>

<div class="row pager">
<div class="col">
<button type="button" class="btn btn-sm btn-outline-primary">
First
</button>
<button type="button" class="btn btn-sm btn-outline-primary">
Last
</button>
</div>
</div>

<div class="row">

<table class="table table-hover">
<thead>
<tr>
<th scope="col"></th>
<th scope="col">Company</th>
<th scope="col">Website</th>
<th scope="col">Area</th>
<th scope="col">About</th>
</tr>
</thead>
<tbody>
@foreach (var company in result.Page ?? Enumerable.Empty<SearchResultPage>())
{
<tr>
<th scope="row">
<button type="button" class="btn btn-sm btn-outline-primary icon-edit"></button>
</th>
<td>@company.Name</td>
<td>@company.Website</td>
<td>@company.Area</td>
<td>@company.About</td>
</tr>
}
</tbody>
</table>

</div>

</main>
<_Footer></_Footer>

@code {
private IEnumerable<CompanyArea> areas = Enumerable.Empty<CompanyArea>();
private SearchResults result = new();

private const int pageSize = 10;
private int page = 1;
private SearchFilter filter = new();

protected override async Task OnInitializedAsync()
{
areas = await Http.GetFromJsonAsync<IEnumerable<CompanyArea>>("/companies/areas");
var f = Uri.EscapeDataString(JsonSerializer.Serialize(filter, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }));
result = await Http.GetFromJsonAsync<SearchResults>($"/companies/{f}/{page}/{pageSize}");
}
}
1 change: 1 addition & 0 deletions CompaniesWebBlazor/Client/Pages/Index.razor.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
main{padding-top:65px}main .filter input,main .filter select{margin-top:10px;margin-bottom:10px}main .filter input{max-width:50%}main .filter select{margin-left:10px !important}main .pager{margin-top:25px}
19 changes: 19 additions & 0 deletions CompaniesWebBlazor/Client/Pages/Index.razor.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
main {
padding-top: 65px;

.filter {
input, select {
margin-top: 10px;
margin-bottom: 10px;
}

input {
max-width: 50%;
}

select {
margin-left: 10px !important;
}
}

.pager {
margin-top: 25px;
}
}
2 changes: 1 addition & 1 deletion CompaniesWebBlazor/Client/Shared/_Nav.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<nav class="navbar fixed-top navbar-expand-lg navbar-dark bg-primary" id="navbar1">
<nav class="navbar fixed-top navbar-expand-lg navbar-dark bg-primary">
<div class="container">
<a class="navbar-brand me-1 mb-1 mt-0" href="@BrandLink">
@BrandContent
Expand Down
1 change: 1 addition & 0 deletions CompaniesWebBlazor/Client/_Imports.razor
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
@using Microsoft.JSInterop
@using CompaniesWebBlazor.Client
@using CompaniesWebBlazor.Client.Shared
@using CompaniesWebBlazor.Shared
46 changes: 46 additions & 0 deletions CompaniesWebBlazor/CompaniesDb/Extensions/CompanyAreaReadAll.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// <auto-generated />
using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using Norm;
using NpgsqlTypes;
using Npgsql;
using CompaniesWebBlazor.Shared;

namespace CompaniesDb.Extensions
{
public static class CompanyAreaReadAll
{
public const string Name = "company_areas";

public const string Sql = @"
SELECT
""id"",
""name""
FROM
""company_areas""";

/// <summary>
/// Select table ""company_areas"" and return enumerator of instances of a "CompaniesDb.Extensions.CompanyArea" class.
/// </summary>
/// <returns>Single instance of a "CompaniesDb.Extensions.CompanyArea" class that is mapped to resulting record of table ""company_areas""</returns>
public static IEnumerable<CompanyArea> ReadCompanyAreaAll(this NpgsqlConnection connection)
{
return connection
.Prepared()
.Read<CompanyArea>(Sql);
}

/// <summary>
/// Asynchronously select table ""company_areas"" and return enumerator of instances of a "CompanyArea" class.
/// </summary>
/// <returns>IAsyncEnumerable of "CompaniesDb.Extensions.CompanyArea" instances.</returns>
public static IAsyncEnumerable<CompanyArea> ReadCompanyAreaAllAsync(this NpgsqlConnection connection)
{
return connection
.Prepared()
.ReadAsync<CompanyArea>(Sql);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace CompaniesDb.Extensions
{
public static class CompaniesCreateOnConflictDoUpdateReturning
public static class CompanyCreateOnConflictDoUpdateReturning
{
public const string Name = "companies";

Expand Down Expand Up @@ -61,7 +61,7 @@ DO UPDATE SET
/// <param name="model">Instance of a "CompaniesDb.Extensions.Company" model class.</param>
/// <param name="conflictedFields">Params list of field names that are tested for conflict. Default is list of primary keys.</param>
/// <returns>Single instance of a "CompaniesDb.Extensions.Company" class that is mapped to resulting record of table ""companies""</returns>
public static Company CreateOnConflictDoUpdateReturningCompanies(this NpgsqlConnection connection, Company model, params string[] conflictedFields)
public static Company CreateCompanyOnConflictDoUpdateReturning(this NpgsqlConnection connection, Company model, params string[] conflictedFields)
{
return connection
.Prepared()
Expand All @@ -84,7 +84,7 @@ public static Company CreateOnConflictDoUpdateReturningCompanies(this NpgsqlConn
/// <param name="model">Instance of a "CompaniesDb.Extensions.Company" model class.</param>
/// <param name="conflictedFields">Params list of field names that are tested for conflict. Default is list of primary keys.</param>
/// <returns>Single instance of a "CompaniesDb.Extensions.Company" class that is mapped to resulting record of table ""companies""</returns>
public static async ValueTask<Company> CreateOnConflictDoUpdateReturningCompaniesAsync(this NpgsqlConnection connection, Company model, params string[] conflictedFields)
public static async ValueTask<Company> CreateCompanyOnConflictDoUpdateReturningAsync(this NpgsqlConnection connection, Company model, params string[] conflictedFields)
{
return await connection
.Prepared()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace CompaniesDb.Extensions
{
public static class CompaniesDelete
public static class CompanyDelete
{
public const string Name = "companies";

Expand All @@ -23,7 +23,7 @@ DELETE FROM ""companies""
/// Delete record of table ""companies"" by matching values of key fields: id
/// </summary>
/// <param name="model">Instance of a "CompaniesDb.Extensions.Company" model class.</param>
public static void DeleteCompanies(this NpgsqlConnection connection, Company model)
public static void DeleteCompany(this NpgsqlConnection connection, Company model)
{
connection
.Prepared()
Expand All @@ -35,7 +35,7 @@ public static void DeleteCompanies(this NpgsqlConnection connection, Company mod
/// Delete record of table ""companies"" by matching values of key fields: id
/// </summary>
/// <param name="model">Instance of a "CompaniesDb.Extensions.Company" model class.</param>
public static async ValueTask DeleteCompaniesAsync(this NpgsqlConnection connection, Company model)
public static async ValueTask DeleteCompanyAsync(this NpgsqlConnection connection, Company model)
{
await connection
.Prepared()
Expand Down
2 changes: 1 addition & 1 deletion CompaniesWebBlazor/CompaniesDb/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Dictionary for database `companies_web_demo`

- Server: PostgreSQL `localhost:5434`, version `12.0`
- Local time stamp: `2021-04-19T14:05:33.3375168+02:00`
- Local time stamp: `2021-04-19T19:00:42.4508425+02:00`
- Schema: public

## Table of Contents
Expand Down
8 changes: 4 additions & 4 deletions CompaniesWebBlazor/CompaniesDb/Scripts/Data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ INSERT INTO public.company_areas OVERRIDING SYSTEM VALUE VALUES (1, 'IT');
INSERT INTO public.company_areas OVERRIDING SYSTEM VALUE VALUES (2, 'Healthcare');
INSERT INTO public.company_areas OVERRIDING SYSTEM VALUE VALUES (3, 'Finance');
INSERT INTO public.company_areas OVERRIDING SYSTEM VALUE VALUES (4, 'Trade');
INSERT INTO public.company_areas OVERRIDING SYSTEM VALUE VALUES (5, 'IT');
INSERT INTO public.company_areas OVERRIDING SYSTEM VALUE VALUES (6, 'Healthcare');
INSERT INTO public.company_areas OVERRIDING SYSTEM VALUE VALUES (7, 'Finance');
INSERT INTO public.company_areas OVERRIDING SYSTEM VALUE VALUES (8, 'Trade');
INSERT INTO public.company_areas OVERRIDING SYSTEM VALUE VALUES (9, 'Manufacturing');
INSERT INTO public.company_areas OVERRIDING SYSTEM VALUE VALUES (10, 'Transportation');
INSERT INTO public.company_areas OVERRIDING SYSTEM VALUE VALUES (5, 'AI');
INSERT INTO public.company_areas OVERRIDING SYSTEM VALUE VALUES (6, 'Services');
INSERT INTO public.company_areas OVERRIDING SYSTEM VALUE VALUES (7, 'Marketing');
INSERT INTO public.company_areas OVERRIDING SYSTEM VALUE VALUES (8, 'Production');
--
-- Name: company_areas_id_seq; Type: SEQUENCE SET; Schema: public; Owner: postgres
--
Expand Down
4 changes: 2 additions & 2 deletions CompaniesWebBlazor/CompaniesDb/appsettings.PgRoutiner.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"float4": "float",
"int4": "int"
},
"CustomModels": { "Companies": "Company" },
"CustomModels": { "Companies": "Company", "CompanyAreas": "CompanyArea" },
"ModelDir": "../Shared",
"ModelCustomNamespace": "CompaniesWebBlazor.Shared",
"EmptyModelDir": false,
Expand Down Expand Up @@ -227,7 +227,7 @@
"CrudCreateOnConflictDoUpdate": [],
"CrudCreateOnConflictDoUpdateReturning": [ "companies" ],
"CrudReadBy": [],
"CrudReadAll": [],
"CrudReadAll": [ "company_areas" ],
"CrudUpdate": [],
"CrudUpdateReturning": [],
"CrudDelete": [ "companies" ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Bogus" Version="33.0.2" />
<PackageReference Include="coverlet.collector" Version="3.0.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,35 @@

namespace CompaniesDbTests
{
public class CreateOnConflictDoUpdateReturningCompaniesUnitTests : PostgreSqlUnitTestFixture
public class CreateCompanyOnConflictDoUpdateReturningUnitTests : PostgreSqlUnitTestFixture
{
public CreateOnConflictDoUpdateReturningCompaniesUnitTests(PostgreSqlFixture fixture) : base(fixture) { }
public CreateCompanyOnConflictDoUpdateReturningUnitTests(PostgreSqlFixture fixture) : base(fixture) { }

[Fact]
public void CreateOnConflictDoUpdateReturningCompanies_Test1()
public void CreateCompanyOnConflictDoUpdateReturning_Test1()
{
// Arrange
var model = new Company { Name = "n", NameNormalized = "nn", About = "about", AreaId = 1, Website = "website" };

// Act
var result = Connection.CreateOnConflictDoUpdateReturningCompanies(model);
var result = Connection.CreateCompanyOnConflictDoUpdateReturning(model);

// Assert
model.Should().BeEquivalentTo(result,
model.Should().BeEquivalentTo(result,
o => o.Excluding(c => c.Id).Excluding(c => c.Modified)); // Id and Modified are generated fields
}


[Fact]
public void CreateOnConflictDoUpdateReturningCompanies_Test2_Upsert()
public void CreateCompanyOnConflictDoUpdateReturning_Test1_Upsert()
{
// Arrange
var model1 = new Company { Name = "n", NameNormalized = "nn", About = "about", AreaId = 1, Website = "website" };
var model2 = new Company { Name = "n2", NameNormalized = "nn", About = "about2", AreaId = 1, Website = "website2" };

// Act
var result1 = Connection.CreateOnConflictDoUpdateReturningCompanies(model1);
var result2 = Connection.CreateOnConflictDoUpdateReturningCompanies(model2, "name_normalized"); // name_normalized is unique index
var result1 = Connection.CreateCompanyOnConflictDoUpdateReturning(model1);
var result2 = Connection.CreateCompanyOnConflictDoUpdateReturning(model2, "name_normalized"); // name_normalized is unique index

// Assert
model2.Should().BeEquivalentTo(result2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@

namespace CompaniesDbTests
{
public class DeleteCompaniesUnitTests : PostgreSqlUnitTestFixture
public class DeleteCompanyUnitTests : PostgreSqlUnitTestFixture
{
public DeleteCompaniesUnitTests(PostgreSqlFixture fixture) : base(fixture) { }
public DeleteCompanyUnitTests(PostgreSqlFixture fixture) : base(fixture) { }

[Fact]
public void DeleteCompanies_Test1()
public void DeleteCompany_Test1()
{
// Arrange
var model = new Company { Name = "n", NameNormalized = "nn", About = "about", AreaId = 1, Website = "website" };
var result = Connection.CreateOnConflictDoUpdateReturningCompanies(model);
var result = Connection.CreateCompanyOnConflictDoUpdateReturning(model);

// Act
Connection.DeleteCompanies(result);
Connection.DeleteCompany(result);

// Assert
Assert.False(Connection.Read($"select * from {CompaniesDelete.Name} where id = {result.Id}").Any());
Assert.False(Connection.Read($"select * from {CompanyDelete.Name} where id = {result.Id}").Any());
}
}
}
42 changes: 42 additions & 0 deletions CompaniesWebBlazor/CompaniesDbTests/ReadCompanyAreaAllUnitTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// <auto-generated />
using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xunit;
using Norm;
using FluentAssertions;
using CompaniesDb.Extensions;
using CompaniesWebBlazor.Shared;

namespace CompaniesDbTests
{
public class ReadCompanyAreaAllUnitTests : PostgreSqlUnitTestFixture
{
public ReadCompanyAreaAllUnitTests(PostgreSqlFixture fixture) : base(fixture) { }

[Fact]
public void ReadCompanyAreaAll_Test1()
{
// Arrange

// Act
var result = Connection.ReadCompanyAreaAll().ToList();

// Assert
new List<CompanyArea>
{
new CompanyArea{ Id = 1, Name = "IT" },
new CompanyArea{ Id = 2, Name = "Healthcare" },
new CompanyArea{ Id = 3, Name = "Finance" },
new CompanyArea{ Id = 4, Name = "Trade" },
new CompanyArea{ Id = 5, Name = "AI" },
new CompanyArea{ Id = 6, Name = "Services" },
new CompanyArea{ Id = 7, Name = "Marketing" },
new CompanyArea{ Id = 8, Name = "Production" },
new CompanyArea{ Id = 9, Name = "Manufacturing" },
new CompanyArea{ Id = 10, Name = "Transportation" },
}.Should().BeEquivalentTo(result);
}
}
}
Loading

0 comments on commit c6d44d8

Please sign in to comment.