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

[Blazor] Added one to many, many to many and enum support for blazor front #478

Merged
merged 18 commits into from
Dec 6, 2020
Merged
Show file tree
Hide file tree
Changes from 13 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
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@

<ItemGroup>
<ProjectReference Include="..\<%= namespace %>.Client.Shared\<%= namespace %>.Client.Shared.csproj" />
<ProjectReference Include="..\..\<%= namespace %>.Crosscutting\<%= namespace %>.Crosscutting.csproj" />
</ItemGroup>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using <%= namespace %>.Client.Models;
using Microsoft.AspNetCore.Components.Authorization;
Expand All @@ -26,6 +28,7 @@ namespace <%= namespace %>.Client.Services.EntityServices
{
private const string AuthorizationHeader = "Authorization";
private readonly AuthenticationStateProvider _authenticationStateProvider;
private readonly JsonSerializerOptions _options;

protected readonly HttpClient _httpClient;

Expand All @@ -44,26 +47,35 @@ namespace <%= namespace %>.Client.Services.EntityServices
_httpClient.DefaultRequestHeaders.Add(AuthorizationHeader, $"Bearer {JwtToken.IdToken}");
}
BaseUrl = baseUrl;
_options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters =
{
new JsonStringEnumConverter(JsonNamingPolicy.CamelCase),
}
};
}

public virtual async Task<IList<T>> GetAll()
{
return await _httpClient.GetFromJsonAsync<IList<T>>(BaseUrl);
return await _httpClient.GetFromJsonAsync<IList<T>>(BaseUrl, _options);
}

public virtual async Task<T> Get(string id)
{
return await _httpClient.GetFromJsonAsync<T>($"{BaseUrl}/{id}");
return await _httpClient.GetFromJsonAsync<T>($"{BaseUrl}/{id}", _options);
}

public virtual async Task Add(T model)
{
await _httpClient.PostAsJsonAsync(BaseUrl,model);
await _httpClient.PostAsJsonAsync(BaseUrl, model, _options);
}

public virtual async Task Update(T model)
{
await _httpClient.PutAsJsonAsync(BaseUrl, model);
await _httpClient.PutAsJsonAsync(BaseUrl, model, _options);
}

public virtual async Task Delete(string id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ using <%= namespace %>.Crosscutting.Enums;
<%_ } _%>
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace <%= namespace %>.Client.Models
{
Expand All @@ -51,7 +52,7 @@ namespace <%= namespace %>.Client.Models
[Required]
<%_ } _%>
<%_ if (fields[idx].fieldIsEnum) { _%>
public <%= fields[idx].fieldType %>? <%= fieldNamePascalized %> { get; set; }
public <%= fields[idx].fieldType %> <%= fieldNamePascalized %> { get; set; }
<%_ } else { _%>
public <%= fieldType %> <%= fieldNamePascalized %> { get; set; }
<%_ } _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ _%>
<div class="table-responsive" id="entities">
<table class="table table-striped" aria-describedby="page-heading">
<thead>
<tr>
<tr>
<th scope="col"><span>ID</span></th>
<%_ fields.forEach(field => { _%>
<th scope="col"><span><%= field.fieldNameHumanized %></span></th>
<%_ }); _%>
<%_ relationships.forEach(relation => {
if(relation.relationshipType === 'one-to-one') { _%>
if(relation.relationshipType === 'one-to-one' || relation.relationshipType === 'many-to-one' || ((relation.relationshipType === 'many-to-many' || relation.relationshipType === 'one-to-many') && relation.ownerSide === true)) { _%>
<th scope="col"><span><%= relation.relationshipNameHumanized %></span></th>
<%_ }
}); _%>
Expand All @@ -80,14 +80,23 @@ _%>
<td>@<%= lowerCasedEntityClass %>.<%= field.fieldNamePascalized %></td>
<%_ }); _%>
<%_ relationships.forEach(relation => {
if(relation.relationshipType === 'one-to-one') { _%>
if(relation.relationshipType === 'one-to-one' || relation.relationshipType === 'many-to-one') { _%>
<td>
@if (<%= lowerCasedEntityClass %>.Region != null)
@if (<%= lowerCasedEntityClass %>.<%= relation.relationshipFieldNamePascalized %> != null)
{
<div>
<a href="/<%= relation.otherEntityNameLowerCased %>/@<%= lowerCasedEntityClass %>.<%= relation.otherEntityNamePascalized %>.Id/view">@<%= lowerCasedEntityClass %>.<%= relation.otherEntityNamePascalized %>.Id</a>
<a href="/<%= relation.otherEntityNameLowerCased %>/@<%= lowerCasedEntityClass %>.<%= relation.relationshipFieldNamePascalized %>.Id/view">@<%= lowerCasedEntityClass %>.<%= relation.relationshipFieldNamePascalized %>.Id</a>
</div>
}
</td>
<%_ } else if ((relation.relationshipType === 'many-to-many' || relation.relationshipType === 'one-to-many') && relation.ownerSide === true ) { _%>
<td>
@foreach (var <%= relation.relationshipFieldNameLowerCased %> in <%= lowerCasedEntityClass %>.<%= relation.relationshipFieldNamePascalizedPlural %>){
<span>
<a href="/<%= relation.otherEntityNameLowerCased %>/@<%= relation.relationshipFieldNameLowerCased %>.Id/view">@<%= relation.relationshipFieldNameLowerCased %>.Id</a>
@if (<%= relation.relationshipFieldNameLowerCased %> != <%= lowerCasedEntityClass %>.<%= relation.relationshipFieldNamePascalizedPlural %>.Last()) { <span>, </span>}
</span>
}
</td>
<%_ }
}); _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,29 @@ _%>
<span>@<%= entityClassName %>.<%= field.fieldNamePascalized %></span>
</dd>
<%_ }); _%>
<%_ relationships.forEach(relation => {
if(relation.relationshipType === 'one-to-one') { _%>
<dt><span><%= relation.otherEntityNamePascalized %></span></dt>
<%_ relationships.forEach(relation => {
if(relation.relationshipType === 'one-to-one' || relation.relationshipType === 'many-to-one' ) { _%>
<dt><span><%= relation.relationshipFieldNamePascalized %></span></dt>
<dd>
@if (<%= entityClassName %>.<%= relation.otherEntityNamePascalized %> != null)
@if (<%= entityClassName %>.<%= relation.relationshipFieldNamePascalized %> != null)
{
<div>
<a href="/<%= relation.otherEntityNameLowerCased %>/@<%= entityClassName %>.Id/view">@<%= entityClassName %>.<%= relation.otherEntityNamePascalized %>.Id</a>
<a href="/<%= relation.otherEntityNameLowerCased %>/@<%= entityClassName %>.Id/view">@<%= entityClassName %>.<%= relation.relationshipFieldNamePascalized %>.Id</a>
</div>
}
</dd>
<%_ }
}); _%>
<%_ } else if ((relation.relationshipType === 'many-to-many' || relation.relationshipType === 'one-to-many') && relation.ownerSide === true ) { _%>
<dt><span><%= relation.relationshipFieldNamePascalized %></span></dt>
<dd>
@foreach (var <%= relation.otherEntityNameLowerCased %> in <%= entityClassName %>.<%= relation.relationshipFieldNamePascalizedPlural %>){
<span>
<a href="/<%= relation.otherEntityNameLowerCased %>/@<%= relation.otherEntityNameLowerCased %>.Id/view">@<%= relation.otherEntityNameLowerCased %>.Id</a>
@if (<%= relation.otherEntityNameLowerCased %> != <%= entityClassName %>.<%= relation.relationshipFieldNamePascalizedPlural %>.Last()) { <span>, </span>}
</span>
}
</dd>
<%_
}}); _%>
</dl>

<button @onclick="Back" class="btn btn-info">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ using System.Threading.Tasks;
using <%= namespace %>.Client.Models;
using <%= namespace %>.Client.Pages.Utils;
using <%= namespace %>.Client.Services.EntityServices.<%= entityClassName %>;
<%_ relationships.forEach(relation => {
if(relation.relationshipType === 'one-to-one') { _%>
<%_ relationships.forEach(relation => { _%>
using <%= namespace %>.Client.Services.EntityServices.<%= relation.otherEntityNamePascalized %>;
<%_ }
}); _%>
<%_ }); _%>
using Microsoft.AspNetCore.Components;

namespace <%= namespace %>.Client.Pages.Entities.<%= entityClassName %>
Expand All @@ -42,43 +40,58 @@ namespace <%= namespace %>.Client.Pages.Entities.<%= entityClassName %>
[Inject]
private I<%= entityClassName %>Service <%= entityClassName %>Service { get; set; }

<%_ relationships.forEach(relation => {
if(relation.relationshipType === 'one-to-one') { _%>
[Inject]
private I<%= relation.otherEntityNamePascalized %>Service <%= relation.otherEntityNamePascalized %>Service { get; set; }
<%_ }
}); _%>
private INavigationService NavigationService { get; set; }

<%_ relationships.forEach(relation => {
if (relation.otherEntityNamePascalized !== entityClassName ) { _%>
[Inject]
private INavigationService NavigationService { get; set; }
private I<%= relation.otherEntityNamePascalized %>Service <%= relation.otherEntityNamePascalized %>Service { get; set; }

<%_ }}); _%>
<%_ relationships.forEach(relation => { _%>
private IEnumerable<<%= asModel(relation.otherEntityNamePascalized) %>> <%= relation.relationshipFieldNamePascalizedPlural %> { get; set; } = new List<<%= asModel(relation.otherEntityNamePascalized) %>>();

<%_ }); _%>

public <%= asModel(entityClassName) %> <%= asModel(entityClassName) %> { get; set; } = new <%= asModel(entityClassName) %>();

<%_ relationships.forEach(relation => {
if(relation.relationshipType === 'one-to-one') { _%>
public IEnumerable<long> <%= relation.relationshipFieldNamePascalizedPlural %> { get; set; } = new List<long>();
<%_ relationships.forEach(relation => { _%>
<%_ if (relation.relationshipType === 'one-to-one' || relation.relationshipType === 'many-to-one') { _%>
public IEnumerable<long> <%= relation.otherEntityNamePascalized %>Ids { get; set; } = new List<long>();

public long <%= relation.otherEntityNamePascalized %>Id { get; set; }

<%_ } else if ((relation.relationshipType === 'many-to-many' || relation.relationshipType === 'one-to-many') && relation.ownerSide === true ) { _%>
public IReadOnlyList<long> <%= relation.otherEntityNamePascalized %>Ids { get; set; } = new List<long>();

public IReadOnlyList<long> Selected<%= relation.relationshipFieldNamePascalizedPlural %> { get; set; }

public long <%= relation.otherEntityNamePascalized %>Id { get; set; }
<%_ }
}); _%>

protected override async Task OnInitializedAsync()
{
<%_ relationships.forEach(relation => {
if(relation.relationshipType === 'one-to-one') { _%>
<%= relation.relationshipFieldNamePascalizedPlural %> = (await <%= relation.otherEntityNamePascalized %>Service.GetAll()).Select(<%= relation.otherEntityNameLowerCased %> => <%= relation.otherEntityNameLowerCased %>.Id);
<%_ }
}); _%>
if (relation.relationshipType === 'one-to-one' || relation.relationshipType === 'many-to-one') { _%>
<%= relation.relationshipFieldNamePascalizedPlural %> = await <%= relation.otherEntityNamePascalized %>Service.GetAll();
<%= relation.otherEntityNamePascalized %>Ids = <%= relation.relationshipFieldNamePascalizedPlural %>.Select(<%= relation.otherEntityNameLowerCased %> => <%= relation.otherEntityNameLowerCased %>.Id).ToList();
<%_ } else if ((relation.relationshipType === 'many-to-many' || relation.relationshipType === 'one-to-many') && relation.ownerSide === true ) { _%>
<%= relation.relationshipFieldNamePascalizedPlural %> = await <%= relation.otherEntityNamePascalized %>Service.GetAll();
<%= relation.otherEntityNamePascalized %>Ids = <%= relation.relationshipFieldNamePascalizedPlural %>.Select(<%= relation.otherEntityNameLowerCased %> => <%= relation.otherEntityNameLowerCased %>.Id).ToList();
<%_ }}); _%>
if (Id != 0)
{
<%= asModel(entityClassName) %> = await <%= entityClassName %>Service.Get(Id.ToString());
<%_ relationships.forEach(relation => {
if(relation.relationshipType === 'one-to-one') { _%>
<%= relation.otherEntityNamePascalized %>Id = <%= asModel(entityClassName) %>.<%= relation.otherEntityNamePascalized %>?.Id ?? 0;
if (relation.relationshipType === 'one-to-one' || relation.relationshipType === 'many-to-one') { _%>
<%= relation.otherEntityNamePascalized %>Id = <%= asModel(entityClassName) %>.<%= relation.relationshipFieldNamePascalized %>?.Id ?? 0;
<%_ } else if ((relation.relationshipType === 'many-to-many' || relation.relationshipType === 'one-to-many') && relation.ownerSide === true ) { _%>
Selected<%= relation.relationshipFieldNamePascalizedPlural %> = new List<long>(<%= asModel(entityClassName) %>.<%= relation.relationshipFieldNamePascalizedPlural %>.Select(<%= relation.otherEntityNameLowerCased %> => <%= relation.otherEntityNameLowerCased %>.Id));
<%_ }
}); _%>
}
}

private void Back()
{
NavigationService.Previous();
Expand All @@ -87,10 +100,18 @@ namespace <%= namespace %>.Client.Pages.Entities.<%= entityClassName %>
private async Task Save()
{
<%_ relationships.forEach(relation => {
if(relation.relationshipType === 'one-to-one') { _%>
<%= asModel(entityClassName) %>.<%= relation.otherEntityNamePascalized %> = <%= relation.otherEntityNamePascalized %>Id != 0 ? new <%= asModel(relation.otherEntityNamePascalized) %> { Id = <%= relation.otherEntityNamePascalized %>Id } : null;
<%_ }
}); _%>
if (relation.relationshipType === 'one-to-one' || relation.relationshipType === 'many-to-one') { _%>
<%= asModel(entityClassName) %>.<%= relation.relationshipFieldNamePascalized %> = <%= relation.otherEntityNamePascalized %>Id != 0 ? <%= relation.relationshipFieldNamePascalizedPlural %>.SingleOrDefault(<%= relation.otherEntityNameLowerCased %> => <%= relation.otherEntityNameLowerCased %>.Id.Equals(<%= relation.otherEntityNamePascalized %>Id)) : null;
<%_ } else if ((relation.relationshipType === 'many-to-many' || relation.relationshipType === 'one-to-many') && relation.ownerSide === true ) { _%>
if (Selected<%= relation.relationshipFieldNamePascalizedPlural %> != null)
{
<%= asModel(entityClassName) %>.<%= relation.relationshipFieldNamePascalizedPlural %> = <%= relation.relationshipFieldNamePascalizedPlural %>?.Where(<%= relation.otherEntityNameLowerCased %> => Selected<%= relation.relationshipFieldNamePascalizedPlural %>.Contains(<%= relation.otherEntityNameLowerCased %>.Id)).ToList();
}
else
{
<%= asModel(entityClassName) %>.<%= relation.relationshipFieldNamePascalizedPlural %> = null;
}
<%_ }}); _%>
if (Id != 0)
{
await <%= entityClassName %>Service.Update(<%= asModel(entityClassName) %>);
Expand Down
Loading