Skip to content

Commit

Permalink
Update (#2)
Browse files Browse the repository at this point in the history
* Bump ini from 1.3.5 to 1.3.8 (jhipster#492)

Bumps [ini](https://github.com/isaacs/ini) from 1.3.5 to 1.3.8.
- [Release notes](https://github.com/isaacs/ini/releases)
- [Commits](npm/ini@v1.3.5...v1.3.8)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Change dto json/dto structure (jhipster#485)

* Change dto structure for use relation id rather than complete json structure

* [Blazor] Added dto support on Blazor (jhipster#493)

Added dto support on Blazor

* Added release-it package  (jhipster#494)

* Added release it package

* Fix npm spawn command on blazor client (jhipster#496)

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: nicolas63 <nicolas_raymond@orange.fr>
  • Loading branch information
3 people authored Dec 17, 2020
1 parent 229af83 commit 99bbf12
Show file tree
Hide file tree
Showing 29 changed files with 2,145 additions and 169 deletions.
26 changes: 26 additions & 0 deletions .release-it.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"github": {
"release": true
},
"plugins": {
"@j-ulrich/release-it-regex-bumper": {
"out": [
{
"file": "./.sonarcloud.properties",
"search": "sonar.projectVersion=\\d+\\.\\d+\\.\\d+",
"replace": "sonar.projectVersion={{version}}"
},
{
"file": "./docs/conf.py",
"search": "release = '\\d+\\.\\d+\\.\\d+'",
"replace": "release = '{{version}}'"
},
{
"file": "./test-integration/samples/**/.yo-rc.json",
"search": "\"version\": \"\\d+\\.\\d+\\.\\d+\"",
"replace": "\"version\": \"{{version}}\""
}
]
}
}
}
11 changes: 11 additions & 0 deletions generators/client/files-blazor.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ const CLIENT_TEST_DIR = constants.CLIENT_TEST_DIR;
* For any other config an object { file:.., method:.., template:.. } can be used
*/
const files = {
blazorAutoMapperProfiles: [
{
path: CLIENT_SRC_DIR,
templates: [
{
file: 'Project.Client/AutoMapper/AutoMapperProfile.cs',
renameTo: generator => `${generator.mainClientDir}/AutoMapper/AutoMapperProfile.cs`,
},
],
},
],
blazorAppModels: [
{
path: CLIENT_SRC_DIR,
Expand Down
11 changes: 11 additions & 0 deletions generators/client/needle-api/needle-client-blazor.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,15 @@ module.exports = class extends needleBase {

this.addBlockContentToFile(rewriteFileModel, errorMessage);
}

addDtoMapping(entityName) {
const errorMessage = `${chalk.yellow('Reference to ') + entityName} ${chalk.yellow('not added to AutoMapper.\n')}`;
const autoMapperProfilePath = `src/${this.mainClientDir}/AutoMapper/AutoMapperProfile.cs`;
const mappingEntry =
// prettier-ignore
this.generator.stripMargin(`|CreateMap<${entityName}Model, ${entityName}Dto>().ReverseMap();`);
const rewriteFileModel = this.generateFileModel(autoMapperProfilePath, 'jhipster-needle-add-dto-model-mapping', mappingEntry);

this.addBlockContentToFile(rewriteFileModel, errorMessage);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<%#
Copyright 2013-2020 the original author or authors from the JHipster project.

This file is part of the JHipster project, see https://www.jhipster.tech/
for more information.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-%>
using AutoMapper;
using System.Linq;
using <%= namespace %>.Client.Models;
using <%= namespace %>.Dto;


namespace <%= namespace %>.Client.AutoMapper {

public class AutoMapperProfile : Profile
{
public AutoMapperProfile()
{
CreateMap<UserDto, UserModel>()
.ForMember(userModel => userModel.Authorities,
opt => opt.MapFrom(userDto => userDto.Roles))
.ReverseMap()
.ForPath(userDto => userDto.Roles,
opt => opt.MapFrom(userModel => userModel.Authorities));

CreateMap<ManagedUserDto, UserSaveModel>().ReverseMap();
CreateMap<LoginDto, LoginModel>().ReverseMap();

// jhipster-needle-add-dto-model-mapping - JHipster will add dto to model and model to dto mapping
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
</dl>

<button type="submit" @onclick="@Back" class="btn btn-info">
<fa-icon icon="arrow-left"></fa-icon>&nbsp;<span>Back</span>
<Icon Name='"fa-arrow-left"' />&nbsp;<span>Back</span>
</button>
</div>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
@foreach (var user in UserModels)
{
<tr>
<td><a href="/user-management/@user.Login/view">@user.Id</a></td>
<td><a href="/admin/user-management/@user.Login/view">@user.Id</a></td>
<td>@user.Login</td>
<td>@user.Email</td>
<td>
@if (user.Activated)
{
@if (AuthenticationService.CurrentUser.Login == user.Login)
@if (AuthenticationService?.CurrentUser?.Login == user.Login)
{
<button class="btn btn-success btn-sm" disabled @onclick=@(() => ActiveUser(user, false))>
Activated
Expand Down Expand Up @@ -91,7 +91,7 @@
<span class="d-none d-md-inline">Edit</span>
</a>

@if (AuthenticationService.CurrentUser.Login == user.Login)
@if (AuthenticationService?.CurrentUser?.Login == user.Login)
{
<button type="button" @onclick=@(() => DeleteUser(user.Login)) disabled
class="btn btn-danger btn-sm">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using System;
using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;
using AutoMapper;
using Blazored.Modal;
using Blazored.SessionStorage;
using Blazorise;
using Blazorise.Bootstrap;
using Blazorise.Icons.FontAwesome;
using <%= namespace %>.Client.Pages.Utils;
using <%= namespace %>.Client.Services;
using <%= namespace %>.Client.Services.AccountServices;
using <%= namespace %>.Client.Services.EntityServices;
// jhipster-needle-add-using-for-services - JHipster will add using services
using <%= namespace %>.Client.Services.EntityServices.User;
Expand Down Expand Up @@ -37,10 +40,13 @@ namespace <%= namespace %>.Client
builder.Services.AddSingleton<ISessionStorageService, SessionStorageService>().AddSingleton<ISyncSessionStorageService, SessionStorageService>();
builder.Services.AddBlazoredModal();

builder.Services.AddAutoMapper(Assembly.GetExecutingAssembly());

builder.Services.AddSingleton<AuthenticationStateProvider, AuthenticationService>();
builder.Services.AddSingleton<INavigationService, NavigationService>();

builder.Services.AddSingleton<IUserService, UserService>();
builder.Services.AddSingleton<IRegisterService, RegisterService>();

// jhipster-needle-add-services-in-di - JHipster will add services in DI

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="10.1.1" />
<PackageReference Include="Blazored.Modal" Version="5.1.0" />
<PackageReference Include="Blazored.SessionStorage" Version="1.0.12" />
<PackageReference Include="Blazorise.Bootstrap" Version="0.9.1.2" />
Expand Down Expand Up @@ -83,6 +84,7 @@


<ItemGroup>
<ProjectReference Include="..\..\<%= namespace %>.Dto\<%= namespace %>.Dto.csproj" />
<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 @@ -18,6 +18,8 @@ using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;
using AutoMapper;
using <%= namespace %>.Dto;
using <%= namespace %>.Client.Models;

namespace <%= namespace %>.Client.Services.AccountServices
Expand All @@ -27,16 +29,19 @@ namespace <%= namespace %>.Client.Services.AccountServices
private const string RegisterUrl = "/api/register";

private readonly HttpClient _httpClient;
private readonly IMapper _mapper;

public RegisterService(HttpClient httpClient)
public RegisterService(HttpClient httpClient, IMapper mapper)
{
_httpClient = httpClient;
_mapper = mapper;
_httpClient.BaseAddress = new Uri(Configuration.BaseUri);
}

public async Task<HttpResponseMessage> Save(UserSaveModel registerModel)
{
return await _httpClient.PostAsJsonAsync(RegisterUrl, registerModel);
var registerDto = _mapper.Map<ManagedUserDto>(registerModel);
return await _httpClient.PostAsJsonAsync(RegisterUrl, registerDto);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ using System.Net.Http;
using System.Net.Http.Json;
using System.Security.Claims;
using System.Threading.Tasks;
using AutoMapper;
using Blazored.SessionStorage;
using <%= namespace %>.Client.Models;
using <%= namespace %>.Dto;
using Microsoft.AspNetCore.Components.Authorization;

namespace <%= namespace %>.Client.Services
Expand All @@ -34,15 +36,17 @@ namespace <%= namespace %>.Client.Services

private readonly HttpClient _httpClient;
private readonly ISyncSessionStorageService _sessionStorage;
private readonly IMapper _mapper;

public bool IsAuthenticated { get; set; }
public UserModel CurrentUser { get; set; }
public JwtToken JwtToken { get; set; }

public AuthenticationService(HttpClient httpClient, ISyncSessionStorageService sessionStorage)
public AuthenticationService(HttpClient httpClient, ISyncSessionStorageService sessionStorage, IMapper mapper)
{
_httpClient = httpClient;
_sessionStorage = sessionStorage;
_mapper = mapper;
_httpClient.BaseAddress = new Uri(Configuration.BaseUri);
var token = _sessionStorage.GetItem<string>(JhiAuthenticationtoken);
if (!string.IsNullOrEmpty(token))
Expand All @@ -54,7 +58,8 @@ namespace <%= namespace %>.Client.Services

public async Task<bool> SignIn(LoginModel loginModel)
{
var result = await _httpClient.PostAsJsonAsync(AuthenticatationUrl, loginModel);
var loginDto = _mapper.Map<LoginDto>(loginModel);
var result = await _httpClient.PostAsJsonAsync(AuthenticatationUrl, loginDto);
if (result.IsSuccessStatusCode)
{
JwtToken = await result.Content.ReadFromJsonAsync<JwtToken>();
Expand All @@ -81,7 +86,8 @@ namespace <%= namespace %>.Client.Services
_httpClient.DefaultRequestHeaders.Add(AuthorizationHeader, $"Bearer {jwtToken.IdToken}");
try
{
CurrentUser = await _httpClient.GetFromJsonAsync<UserModel>(AccountUrl);
var userDto = await _httpClient.GetFromJsonAsync<UserDto>(AccountUrl);
CurrentUser = _mapper.Map<UserModel>(userDto);
}
catch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,26 @@ using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using AutoMapper;
using <%= namespace %>.Client.Models;
using Microsoft.AspNetCore.Components.Authorization;

namespace <%= namespace %>.Client.Services.EntityServices
{
public class AbstractEntityService<T> where T : class
public class AbstractEntityService<TModel, TDto> where TModel : class
where TDto : class
{
private const string AuthorizationHeader = "Authorization";
private readonly AuthenticationStateProvider _authenticationStateProvider;
private readonly JsonSerializerOptions _options;

protected readonly HttpClient _httpClient;
protected readonly IMapper _mapper;

protected JwtToken JwtToken { get; set; }
protected string BaseUrl { get; }

public AbstractEntityService(HttpClient httpClient, AuthenticationStateProvider authenticationStateProvider, string baseUrl)
public AbstractEntityService(HttpClient httpClient, AuthenticationStateProvider authenticationStateProvider, IMapper mapper, string baseUrl)
{
_httpClient = httpClient;
_authenticationStateProvider = authenticationStateProvider;
Expand All @@ -47,6 +50,7 @@ namespace <%= namespace %>.Client.Services.EntityServices
_httpClient.DefaultRequestHeaders.Add(AuthorizationHeader, $"Bearer {JwtToken.IdToken}");
}
BaseUrl = baseUrl;
_mapper = mapper;
_options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
Expand All @@ -58,24 +62,28 @@ namespace <%= namespace %>.Client.Services.EntityServices
};
}

public virtual async Task<IList<T>> GetAll()
public virtual async Task<IList<TModel>> GetAll()
{
return await _httpClient.GetFromJsonAsync<IList<T>>(BaseUrl, _options);
var resultsDto = await _httpClient.GetFromJsonAsync<IList<TDto>>(BaseUrl, _options);
return _mapper.Map<IList<TModel>>(resultsDto);
}

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

public virtual async Task Add(T model)
public virtual async Task Add(TModel model)
{
await _httpClient.PostAsJsonAsync(BaseUrl, model, _options);
var dto = _mapper.Map<TDto>(model);
await _httpClient.PostAsJsonAsync(BaseUrl, dto, _options);
}

public virtual async Task Update(T model)
public virtual async Task Update(TModel model)
{
await _httpClient.PutAsJsonAsync(BaseUrl, model, _options);
var dto = _mapper.Map<TDto>(model);
await _httpClient.PutAsJsonAsync(BaseUrl, dto, _options);
}

public virtual async Task Delete(string id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using <%= namespace %>.Client.Models;
using <%= namespace %>.Dto;

namespace <%= namespace %>.Client.Services.EntityServices.User
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;
using AutoMapper;
using <%= namespace %>.Client.Models;
using <%= namespace %>.Dto;
using Microsoft.AspNetCore.Components.Authorization;

namespace <%= namespace %>.Client.Services.EntityServices.User
{
public class UserService : AbstractEntityService<UserModel>,IUserService
public class UserService : AbstractEntityService<UserModel, UserDto>,IUserService
{
public UserService(HttpClient httpClient, AuthenticationStateProvider authenticationStateProvider) : base(httpClient, authenticationStateProvider, "/api/users")
public UserService(HttpClient httpClient, AuthenticationStateProvider authenticationStateProvider, IMapper mapper) : base(httpClient, authenticationStateProvider, mapper, "/api/users")
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ using System.Security.Claims;
using System.Threading.Tasks;
using AutoFixture;
using Blazored.Modal.Services;
using Blazorise;
using Blazorise.Bootstrap;
using Blazorise.Icons.FontAwesome;
using Bunit;
using Bunit.Rendering;
using FluentAssertions;
Expand All @@ -43,6 +46,12 @@ namespace <%= namespace %>.Client.Test.Pages.Admin.UserManagement
{
_userService = new Mock<IUserService>();
_navidationService = new Mock<INavigationService>();
Services.AddBlazorise(options =>
{
options.ChangeTextOnKeyPress = true;
})
.AddBootstrapProviders()
.AddFontAwesomeIcons();
Services.AddSingleton<IUserService>(_userService.Object);
Services.AddSingleton<INavigationService>(_navidationService.Object);
}
Expand Down
1 change: 1 addition & 0 deletions generators/entity-client/files-blazor.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,5 @@ function writeFiles() {
blazorNeedle.addEntityToMenu(this.entityClass);
blazorNeedle.addServiceInDI(this.entityClass);
blazorNeedle.addUsingForService(this.namespace, this.entityClass);
blazorNeedle.addDtoMapping(this.entityClass);
}
Loading

0 comments on commit 99bbf12

Please sign in to comment.