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

Migrate Learn to .NET #30

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ bin
debug
obj
launchSettings.json
*.user

# IdentityServer
tempkey.jwk
19 changes: 13 additions & 6 deletions services/learn/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
FROM python:3 as builder
ENV PYTHONUNBUFFERED=1
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS builder
WORKDIR /app
COPY requirements.txt /app/
RUN pip install -r requirements.txt
COPY . /app/
CMD ["python", "manage.py", "runserver"]

COPY Learn.csproj ./
RUN dotnet restore Learn.csproj

COPY . ./source/
WORKDIR /app/source
RUN dotnet publish -c release -o /release

FROM mcr.microsoft.com/dotnet/aspnet:5.0
WORKDIR /release
COPY --from=builder /release ./
ENTRYPOINT ["dotnet", "Learn.dll"]
8 changes: 0 additions & 8 deletions services/learn/Dockerfile.k8s-build

This file was deleted.

40 changes: 40 additions & 0 deletions services/learn/Learn.API/Controllers/Chats/ChatsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Learn.Application.Chats.CreateChat;
using Learn.Application.Chats.Dtos;
using Learn.Application.Chats.GetChats;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Learn.API.Controllers.Chats
{
[ApiController]
[Route("chats")]
public class ChatsController : ControllerBase
{
private readonly ISender _mediator;

public ChatsController(ISender mediator)
{
_mediator = mediator;
}

[HttpGet("my")]
[ProducesResponseType(typeof(IEnumerable<ChatDto>), StatusCodes.Status200OK)]
public async Task<IEnumerable<ChatDto>> Create()
{
var result = await _mediator.Send(new GetAuthenticatedUserChatsQuery());
return result;
}

[HttpPost]
[ProducesResponseType(typeof(ChatDto), StatusCodes.Status200OK)]
public async Task<ChatDto> Create([FromBody] CreateChatRequest request)
{
var result = await _mediator.Send(new CreateChatCommand(request.Name));
return result;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Learn.API.Controllers.Chats
{
public class CreateChatRequest
{
public string Name { get; set; }
}
}
30 changes: 30 additions & 0 deletions services/learn/Learn.API/Controllers/Users/UsersController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Learn.Application.Users.Dtos;
using Learn.Application.Users.GetUsers;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Learn.API.Users.Controllers
{
[ApiController]
[Route("users")]
public class UsersController : ControllerBase
{
private readonly ISender _mediator;

public UsersController(ISender mediator)
{
_mediator = mediator;
}

[HttpGet]
[ProducesResponseType(typeof(IEnumerable<UserDto>), StatusCodes.Status200OK)]
public async Task<IEnumerable<UserDto>> GetAll()
{
var result = await _mediator.Send(new GetUsersQuery());
return result;
}
}
}
12 changes: 12 additions & 0 deletions services/learn/Learn.API/Learn.API.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Learn.Infrastructure\Learn.Infrastructure.csproj" />
<ProjectReference Include="..\Learn.Application\Learn.Application.csproj" />
</ItemGroup>

</Project>
20 changes: 20 additions & 0 deletions services/learn/Learn.API/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

namespace Users
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
58 changes: 58 additions & 0 deletions services/learn/Learn.API/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Learn.Applciation;
using Learn.Infrastructure;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace Users
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile("appsettings.Development.json")
.AddEnvironmentVariables()
.Build();
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddInfrastructure(Configuration);

services.AddApplication();

services.AddControllers();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UsePathBase(new PathString("/api"));

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});

app.ConfigureInfrastructure();
}
}
}
13 changes: 13 additions & 0 deletions services/learn/Learn.API/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"UrlsOptions": {
"GatewayApiUrl": "http://localhost:8000"
},
"Urls": "http://0.0.0.0:8100"
}
10 changes: 10 additions & 0 deletions services/learn/Learn.API/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
30 changes: 30 additions & 0 deletions services/learn/Learn.Application/ApplicationDependencyInjection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using FluentValidation;
using Learn.Application.Configuration.Behaviours;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;

namespace Learn.Applciation
{
public static class ApplicationDependencyInjection
{
public static IServiceCollection AddApplication(this IServiceCollection services)
{
services.AddAutoMapper(Assembly.GetExecutingAssembly());
services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly());

services.AddMediatR(Assembly.GetExecutingAssembly());
services.AddPipelineBehaviour();

return services;
}

private static IServiceCollection AddPipelineBehaviour(this IServiceCollection services)
{
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(UnhandledExceptionBehaviour<,>));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehaviour<,>));

return services;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using Ardalis.Specification.EntityFrameworkCore;
using AutoMapper;
using Learn.Application.Chats.Dtos;
using Learn.Application.Users.Dtos;
using Learn.Application.Users.Specifications;
using Learn.BuildingBlocks.Application.ExecutionContext;
using Learn.Domain.Chats;
using Learn.Domain.Users;
using Learn.Infrastructure.EntityFramework;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Learn.Application.Chats.CreateChat
{
public class CreateChatCommand : IRequest<ChatDto>
{
public string Name { get; set; }

public CreateChatCommand(string name)
{
Name = name;
}
}

internal class CreateChatCommandHandler : IRequestHandler<CreateChatCommand, ChatDto>
{
private readonly LearnDbContext _dbContext;
private readonly IChatRepository _chatRepository;
private readonly IMapper _mapper;
private readonly IUserContext _userContext;

public CreateChatCommandHandler(LearnDbContext dbContext, IChatRepository chatRepository, IMapper mapper, IUserContext userContext)
{
_dbContext = dbContext;
_chatRepository = chatRepository;
_mapper = mapper;
_userContext = userContext;
}

public async Task<ChatDto> Handle(CreateChatCommand request, CancellationToken cancellationToken)
{
var createdBy = await _dbContext.Users
.WithSpecification(new UserAggregateSpecification())
.WithSpecification(new UserByEmailSpecification(_userContext.Email))
.FirstAsync();

var chat = Chat.CreateNew(request.Name, createdBy);

await _chatRepository.AddAsync(chat);

return _mapper.Map<ChatDto>(chat);
}
}
}
12 changes: 12 additions & 0 deletions services/learn/Learn.Application/Chats/Dtos/ChatDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;

namespace Learn.Application.Chats.Dtos
{
public class ChatDto
{
public Guid Id { get; set; }
public string Name { get; set; }
public List<ChatMemberDto> Members { get; set; }
}
}
9 changes: 9 additions & 0 deletions services/learn/Learn.Application/Chats/Dtos/ChatMemberDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Learn.Application.Chats.Dtos
{
public class ChatMemberDto
{
public string Name { get; set; }
}
}
Loading