Skip to content

Commit

Permalink
add grpc api
Browse files Browse the repository at this point in the history
  • Loading branch information
nkz-soft committed Oct 15, 2022
1 parent 41b0657 commit 85085a9
Show file tree
Hide file tree
Showing 93 changed files with 1,105 additions and 410 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ jobs:
run: dotnet restore
- name: Build
run: dotnet build -c Release --no-restore
- name: Test
- name: Application Test
run: dotnet test ./tests/NKZSoft.Template.Application.Tests --configuration Release --no-build --verbosity normal --collect:"XPlat Code Coverage" --results-directory ./coverage
# - name: REST Test
# run: dotnet test ./tests/NKZSoft.Template.Presentation.REST.Tests --configuration Release --no-build --verbosity normal --collect:"XPlat Code Coverage" --results-directory ./coverage
- name: Code Coverage Report
uses: irongut/[email protected]
with:
Expand Down
21 changes: 21 additions & 0 deletions TG.MicroserviceTemplate.sln → NKZSoft.MicroserviceTemplate.sln
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docker", "docker", "{275335
deployment\docker\docker-compose-infrastructure.yaml = deployment\docker\docker-compose-infrastructure.yaml
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NKZSoft.Template.Presentation.GRPC", "src\NKZSoft.Template.Presentation.GRPC\NKZSoft.Template.Presentation.GRPC.csproj", "{FA32F3BD-A075-426D-9B69-81AF72021801}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NKZSoft.Template.Presentation.Starter", "src\NKZSoft.Template.Presentation.Starter\NKZSoft.Template.Presentation.Starter.csproj", "{6693D52E-B1D4-45B8-8330-631E6023FBB5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NKZSoft.Template.Presentation.GRPC.Tests", "tests\NKZSoft.Template.Presentation.GRPC.Tests\NKZSoft.Template.Presentation.GRPC.Tests.csproj", "{D62D0A2D-B69C-40AE-AC5C-94E9A6D5F5E8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -95,6 +101,18 @@ Global
{9A0A4214-A590-4411-ADEE-28B47D3DEB16}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9A0A4214-A590-4411-ADEE-28B47D3DEB16}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9A0A4214-A590-4411-ADEE-28B47D3DEB16}.Release|Any CPU.Build.0 = Release|Any CPU
{FA32F3BD-A075-426D-9B69-81AF72021801}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FA32F3BD-A075-426D-9B69-81AF72021801}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FA32F3BD-A075-426D-9B69-81AF72021801}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FA32F3BD-A075-426D-9B69-81AF72021801}.Release|Any CPU.Build.0 = Release|Any CPU
{6693D52E-B1D4-45B8-8330-631E6023FBB5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6693D52E-B1D4-45B8-8330-631E6023FBB5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6693D52E-B1D4-45B8-8330-631E6023FBB5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6693D52E-B1D4-45B8-8330-631E6023FBB5}.Release|Any CPU.Build.0 = Release|Any CPU
{D62D0A2D-B69C-40AE-AC5C-94E9A6D5F5E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D62D0A2D-B69C-40AE-AC5C-94E9A6D5F5E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D62D0A2D-B69C-40AE-AC5C-94E9A6D5F5E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D62D0A2D-B69C-40AE-AC5C-94E9A6D5F5E8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -116,6 +134,9 @@ Global
{910736CD-FD22-4447-B99E-A65AFC25DDF1} = {664D406C-2F83-48F0-BFC3-408D5CB53C65}
{9A0A4214-A590-4411-ADEE-28B47D3DEB16} = {664D406C-2F83-48F0-BFC3-408D5CB53C65}
{27533508-5FAD-4228-ADB9-10DD5CFA00F0} = {6DA7A25E-9209-433F-AEE3-74F91D6C6DA1}
{FA32F3BD-A075-426D-9B69-81AF72021801} = {19AA42FA-3860-4D08-B5B0-9D26D751D5B8}
{6693D52E-B1D4-45B8-8330-631E6023FBB5} = {19AA42FA-3860-4D08-B5B0-9D26D751D5B8}
{D62D0A2D-B69C-40AE-AC5C-94E9A6D5F5E8} = {664D406C-2F83-48F0-BFC3-408D5CB53C65}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3CB609D9-5D54-4C11-A371-DAAC8B74E430}
Expand Down
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
# microservice-template
Template for microservice based on DDD and Clean Architecture with .NET

Technologies used
The main aim of this project is to provide a means for building microservices with the latest technology and architecture.

## ⭐ Give a star

If you're using this repository for your learning, samples or your project, please give a star. Thanks :+1:

## Table of Contents

- [The aim of the project](#the-aim-of-this-project)
- [Plan](#plan)
- [Technologies - Libraries](technologies-used)

## The aim of the project

## Plan

## Technologies used

[NET Core 6](https://dotnet.microsoft.com/en-us/download/dotnet/6.0)

Expand All @@ -13,4 +29,4 @@ Technologies used

[FluentValidation](https://github.com/FluentValidation/FluentValidation)

[FluentAssertions](https://github.com/fluentassertions/fluentassertions)
[FluentAssertions](https://github.com/fluentassertions/fluentassertions)
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
namespace NKZSoft.Template.Application.Common.Filters;

using System.Diagnostics.CodeAnalysis;

public static class FilterDefinitionExtension
{
public static bool HasValue<T>(this FilterFieldDefinition<T>? testValue)
public static bool HasValue<T>([NotNullWhen(true)] this FilterFieldDefinition<T>? testValue)
{
return testValue switch
{
Expand All @@ -11,4 +13,4 @@ public static bool HasValue<T>(this FilterFieldDefinition<T>? testValue)
var nullableValue => nullableValue.Value != null
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace NKZSoft.Template.Application.Common.Interfaces;

public interface IDbInitializer
{
Task SeedAsync(IApplicationDbContext context, CancellationToken cancellationToken = default);
}
22 changes: 19 additions & 3 deletions src/NKZSoft.Template.Application/Common/Paging/PageContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public PageContext(
ListGroup = listGroup ?? Enumerable.Empty<GroupDescriptor>();
}

public int PageIndex { get; }
public int PageIndex { get; set; }

public int PageSize { get; }
public int PageSize { get; set; }

public T Filter { get; }
public T Filter { get; set; }

public IEnumerable<SortDescriptor> ListSort { get; init; }

Expand All @@ -32,4 +32,20 @@ public bool IsValid()
return PageIndex > 0 && PageSize > 0 &&
Filter != null && ListSort != null;
}

public static PageContext<T> operator++ (PageContext<T> obj) => Increment(obj);

public static PageContext<T> operator-- (PageContext<T> obj) => Decrement(obj);

public static PageContext<T> Increment(PageContext<T> obj)
{
obj.PageIndex++;
return obj;
}

public static PageContext<T> Decrement(PageContext<T> obj)
{
obj.PageIndex--;
return obj;
}
}
17 changes: 8 additions & 9 deletions src/NKZSoft.Template.Application/DependencyInjection.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using System.Reflection;
namespace NKZSoft.Template.Application;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using NKZSoft.Template.Application.Common.Behaviours;
using NKZSoft.Template.Application.Mapper;

namespace NKZSoft.Template.Application;

using Common.Behaviours;
using Mapper;

Expand All @@ -15,8 +11,11 @@ public static IServiceCollection AddApplication(this IServiceCollection services
{
services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly());
services.AddMediatR(Assembly.GetExecutingAssembly());

services.TryAddSingleton(MappingConfig.Configure());

var typeAdapterConfig = TypeAdapterConfig.GlobalSettings;
typeAdapterConfig.Scan(Assembly.GetExecutingAssembly());

services.TryAddSingleton<IMapper>(new MapsterMapper.Mapper(typeAdapterConfig));
services.TryAddSingleton<IMapper, ServiceMapper>();

services.AddTransient(typeof(IPipelineBehavior<,>), typeof(UnhandledExceptionBehaviour<,>));
Expand All @@ -25,4 +24,4 @@ public static IServiceCollection AddApplication(this IServiceCollection services

return services;
}
}
}
6 changes: 5 additions & 1 deletion src/NKZSoft.Template.Application/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
global using System.Collections.Generic;
global using System.Reflection;

global using Microsoft.Extensions.Logging;
global using Microsoft.EntityFrameworkCore;
global using FluentValidation;
Expand All @@ -8,9 +10,11 @@
global using MapsterMapper;
global using System.Linq.Expressions;
global using Ardalis.Specification;
global using Ardalis.Specification.EntityFrameworkCore;

global using NKZSoft.Template.Common;
global using NKZSoft.Template.Application.Common.Exceptions;
global using NKZSoft.Template.Application.Common.Filters;
global using NKZSoft.Template.Application.Common.Paging;
global using NKZSoft.Template.Application.Common.Handlers;
global using NKZSoft.Template.Application.Common.Interfaces;
global using NKZSoft.Template.Application.Common.Interfaces;
12 changes: 3 additions & 9 deletions src/NKZSoft.Template.Application/Mapper/MappingConfig.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
using NKZSoft.Template.Application.Models;
using NKZSoft.Template.Domain.AggregatesModel.ToDoAggregates.Entities;
using NKZSoft.Template.Domain.AggregatesModel.ToDoAggregates.Entities;

namespace NKZSoft.Template.Application.Mapper;

using Models;

public class MappingConfig
public class MappingConfig : IRegister
{
public static TypeAdapterConfig Configure()
public void Register(TypeAdapterConfig config)
{
var config = new TypeAdapterConfig();

config.NewConfig<ToDoItem, ToDoItemDto>();

config.NewConfig<ToDoList, ToDoListDto>();

return config;
}
}
6 changes: 2 additions & 4 deletions src/NKZSoft.Template.Application/Models/ToDoItemDto.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using NKZSoft.Template.Application.Common.Models;

namespace NKZSoft.Template.Application.Models;

using Common.Models;

public sealed record ToDoItemDto(int Id, string Title, string? Note, string CreatedBy, DateTime Created, string ModifiedBy, DateTime? Modified, DateTime? Deleted)
: BaseDto(CreatedBy, Created, ModifiedBy, Modified, Deleted);
public sealed record ToDoItemDto(Guid Id, string Title, string? Note, string CreatedBy, DateTime Created, string ModifiedBy, DateTime? Modified, DateTime? Deleted)
: BaseDto(CreatedBy, Created, ModifiedBy, Modified, Deleted);
3 changes: 0 additions & 3 deletions src/NKZSoft.Template.Application/Models/ToDoListDto.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using NKZSoft.Template.Application.Common.Models;
using NKZSoft.Template.Domain.AggregatesModel.ToDoAggregates.Entities;

namespace NKZSoft.Template.Application.Models;

using Common.Models;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ namespace NKZSoft.Template.Application.TodoItems.Commands.Create;

using Common.Interfaces;

public sealed class CreateTodoItemCommandHandler : IRequestHandler<CreateToВoItemCommand, IResult<int>>
public sealed class CreateTodoItemCommandHandler : IRequestHandler<CreateToВoItemCommand, IResult<Guid>>
{
private readonly IApplicationDbContext _context;

public CreateTodoItemCommandHandler(IApplicationDbContext context) => _context = context;

public async Task<IResult<int>> Handle(CreateToВoItemCommand request, CancellationToken cancellationToken)
public async Task<IResult<Guid>> Handle(CreateToВoItemCommand request, CancellationToken cancellationToken)
{
var entity = new ToDoItem(request.Title);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace NKZSoft.Template.Application.TodoItems.Commands.Create;

public sealed record CreateToВoItemCommand(string Title, int? ListId) : IRequest<IResult<int>>;
public sealed record CreateToВoItemCommand(string Title, int? ListId) : IRequest<IResult<Guid>>;
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace NKZSoft.Template.Application.TodoItems.Commands.Delete;

public sealed record DeleteTodoItemCommand(int Id) : IRequest;
public sealed record DeleteTodoItemCommand(Guid Id) : IRequest;
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace NKZSoft.Template.Application.TodoItems.Commands.Update;

public sealed record UpdateTodoItemCommand(int Id, string Title, string Description) : IRequest;
public sealed record UpdateTodoItemCommand(Guid Id, string Title, string Description) : IRequest;
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public partial record ToDoItemFilter
{
public FilterFieldDefinition<int>? Id { get; set; }
public FilterFieldDefinition<Guid>? Id { get; set; }

public FilterFieldDefinition<string>? Title { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public sealed class ToDoItemFilterBuilder

public ToDoItemFilterBuilder() => _filter = new ToDoItemFilter();

public ToDoItemFilterBuilder Id(int id)
public ToDoItemFilterBuilder Id(Guid id)
{
_filter.Id = new FilterFieldDefinition<int>() { Value = id };
_filter.Id = new FilterFieldDefinition<Guid>() { Value = id };
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ namespace NKZSoft.Template.Application.TodoItems.Queries.Get;

using Application.Models;

public sealed record GetTodoItemQuery(int Id) : IRequest<Result<ToDoItemDto>>;
public sealed record GetTodoItemQuery(Guid Id) : IRequest<Result<ToDoItemDto>>;
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
using NKZSoft.Template.Application.Models;
using NKZSoft.Template.Domain.AggregatesModel.ToDoAggregates.Entities;

namespace NKZSoft.Template.Application.TodoItems.Queries.Get;
namespace NKZSoft.Template.Application.TodoItems.Queries.Get;

using Domain.AggregatesModel.ToDoAggregates.Entities;
using Application.Models;
using Common.Exceptions;
using Common.Handlers;
using Common.Interfaces;

public sealed class GetTodoItemQueryHandler : HandlerQueryBase<GetTodoItemQuery, Result<ToDoItemDto>>
{
public GetTodoItemQueryHandler(IApplicationDbContext applicationDbContext,
public GetTodoItemQueryHandler(IApplicationDbContext applicationDbContext,
ICurrentUserService currentUserService,
IMapper mapper)
: base(applicationDbContext, mapper, currentUserService)
{
}

public override async Task<Result<ToDoItemDto>> Handle(GetTodoItemQuery request, CancellationToken cancellationToken)
{
var entity = await ContextDb.Set<ToDoItem>()
.AsNoTracking()
.Where(e => e.Id == request.Id)
.SingleOrDefaultAsync(cancellationToken);
.SingleOrDefaultAsync(cancellationToken)
.ConfigureAwait(false);

entity.ThrowIfNull(new NotFoundException());

return Result.Ok(await entity.BuildAdapter(Mapper.Config)
.AdaptToTypeAsync<ToDoItemDto>());
var dtoItem = await entity
.BuildAdapter(Mapper.Config)
.AdaptToTypeAsync<ToDoItemDto>()
.ConfigureAwait(false);

return Result.Ok(dtoItem);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ namespace NKZSoft.Template.Application.TodoItems.Queries.GetPage;

public sealed class GetPageTodoItemsQuery : PagingQuery<Result<CollectionViewModel<ToDoItemDto>>, ToDoItemFilter>
{
public GetPageTodoItemsQuery(IPageContext<ToDoItemFilter> pageContext) : base(pageContext)
public GetPageTodoItemsQuery(PageContext<ToDoItemFilter> pageContext) : base(pageContext)
{
}

public static GetPageTodoItemsQuery Create(PageContext<ToDoItemFilter> pageContext) => new(pageContext);
}
}
Loading

0 comments on commit 85085a9

Please sign in to comment.