Skip to content
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
32 changes: 32 additions & 0 deletions .github/workflows/build_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This workflow will build a .NET project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net

name: Build and test

on:
push:
branches: [ "avdeev/tests" ]
pull_request:
branches: [ "avdeev/tests" ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- name: Checkout avdeev/tests branch
uses: actions/checkout@v4
with:
ref: 'avdeev/tests'

- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
- name: Restore dependencies
run: dotnet restore Homeworks/UnitTests/src
- name: Build
run: dotnet build Homeworks/UnitTests/src --no-restore
- name: Test
run: dotnet test Homeworks/UnitTests/src --no-build --verbosity normal
3 changes: 3 additions & 0 deletions Homeworks/RabbitMQ/compose.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cd D:\OTUS\ASP.NET\Homeworks\RabbitMQ
docker-compose -f compose.yml up
PAUSE
94 changes: 69 additions & 25 deletions Homeworks/RabbitMQ/compose.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
services:
#Administration Db
promocode-factory-administration-db:
image: "postgres:latest"
container_name: 'promocode-factory-administration-db'
restart: always
ports:
- 5433:5432
environment:
- POSTGRES_PASSWORD=docker

#Administration Api
promocode-factory-administration-api:
build: src/Pcf.Administration/
container_name: 'promocode-factory-administration-api'

rabbit:
image: rabbitmq:3-management
restart: always
ports:
- "8091:8080"
hostname: rabbitmqhost
environment:
- "ConnectionStrings:PromocodeFactoryAdministrationDb=Host=promocode-factory-administration-db;Database=promocode_factory_administration_db;Username=postgres;Password=docker"
depends_on:
- promocode-factory-administration-db

RABBITMQ_DEFAULT_USER: myuser
RABBITMQ_DEFAULT_PASS: mypass
volumes:
- rabbitmq_data:/var/lib/rabbitmq
ports:
- "5672:5672"
- "15672:15672"
healthcheck:
test: [ "CMD", "rabbitmqctl", "status" ]
interval: 10s
timeout: 5s
retries: 5

#ReceivingFromPartner Db
promocode-factory-receiving-from-partner-db:
Expand All @@ -41,10 +37,16 @@ services:
- "8092:8080"
environment:
- "ConnectionStrings:PromocodeFactoryReceivingFromPartnerDb=Host=promocode-factory-receiving-from-partner-db;Database=promocode_factory_receiving_from_partner_db;Username=postgres;Password=docker"
- "IntegrationSettings:GivingToCustomerApiUrl=http://promocode-factory-giving-to-customer-api"
- "IntegrationSettings:AdministrationApiUrl=http://promocode-factory-administration-api"
- "IntegrationSettings:GivingToCustomerApiUrl=http://promocode-factory-giving-to-customer-api:8080"
- "IntegrationSettings:AdministrationApiUrl=http://promocode-factory-administration-api:8080"
- "RmqSettings:Host=rabbit"
- "RmqSettings:Login=myuser"
- "RmqSettings:Password=mypass"
depends_on:
- promocode-factory-receiving-from-partner-db
promocode-factory-receiving-from-partner-db:
condition: service_started
rabbit:
condition: service_healthy

#GivingToCustomer Db
promocode-factory-giving-to-customer-db:
Expand All @@ -64,6 +66,48 @@ services:
ports:
- "8093:8080"
environment:
- "ConnectionStrings:PromocodeFactoryGivingToCustomerDb=Host=promocode-factory-giving-to-customer-db;Database=promocode_factory_giving_to_customer_db;Username=postgres;Password=docker"
- "ConnectionStrings:PromocodeFactoryGivingToCustomerDb=Host=promocode-factory-giving-to-customer-db;Database=promocode_factory_giving_to_customer_db;Username=postgres;Password=docker"
- "RmqSettings:Host=rabbit"
- "RmqSettings:Login=myuser"
- "RmqSettings:Password=mypass"
depends_on:
- promocode-factory-giving-to-customer-db
promocode-factory-giving-to-customer-db:
condition: service_started
rabbit:
condition: service_healthy

#Administration Db
promocode-factory-administration-db:
image: "postgres:latest"
container_name: 'promocode-factory-administration-db'
restart: always
ports:
- 5433:5432
environment:
- POSTGRES_PASSWORD=docker

#Administration Api
promocode-factory-administration-api:
build: src/Pcf.Administration/
container_name: 'promocode-factory-administration-api'
restart: always
ports:
- "8091:8080"
environment:
- "ConnectionStrings:PromocodeFactoryAdministrationDb=Host=promocode-factory-administration-db;Database=promocode_factory_administration_db;Username=postgres;Password=docker"
- "RmqSettings:Host=rabbit"
- "RmqSettings:Login=myuser"
- "RmqSettings:Password=mypass"
depends_on:
promocode-factory-administration-db:
condition: service_started
rabbit:
condition: service_healthy

volumes:
rabbitmq_data:

#networks:
# default:
# name: myLocalNetwork # создана извне: docker network create --driver=bridge myLocalNetwork
# external: true
1 change: 1 addition & 0 deletions Homeworks/RabbitMQ/src/Pcf.Administration/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ WORKDIR /app
# Copy the solution file and project files, then restore dependencies
COPY *.sln .
COPY */*.csproj ./
COPY nuget.config .
RUN for file in $(ls *.csproj); do mkdir -p ${file%.*} && mv $file ${file%.*}; done
RUN dotnet restore

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ public async Task<ActionResult<EmployeeResponse>> GetEmployeeByIdAsync(Guid id)
/// <param name="id">Id сотрудника, например <example>451533d5-d8d5-4a11-9c7b-eb9f14e1a32f</example></param>
/// <returns></returns>
[HttpPost("{id:guid}/appliedPromocodes")]

public async Task<IActionResult> UpdateAppliedPromocodesAsync(Guid id)
{
var employee = await _employeeRepository.GetByIdAsync(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@

<ItemGroup>
<PackageReference Include="EFCore.NamingConventions" Version="8.0.3" />
<PackageReference Include="Infrastructure.RabbitMq" Version="1.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="8.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.7" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4" />
<PackageReference Include="NSwag.AspNetCore" Version="14.1.0" />
</ItemGroup>

<ItemGroup>
<Folder Include="Services\" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

namespace Pcf.Administration.WebHost.Services.Employees;

public class AdministrationDto
{
public Guid PartnerId { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Infrastructure.RabbitMq;
using Pcf.Administration.Core.Abstractions.Repositories;
using Pcf.Administration.Core.Domain.Administration;
using RabbitMQ.Client.Events;

namespace Pcf.Administration.WebHost.Services.Employees;

public class EmployeesService : RabbitMqConsumer, IEmployeesService
{
private readonly IRepository<Employee> _employeeRepository;

public EmployeesService(IRepository<Employee> employeeRepository)
{
_employeeRepository = employeeRepository;
}

protected override async Task OnConsumerOnReceivedAsync(object sender, BasicDeliverEventArgs e)
{
var body = e.Body;
var message = JsonSerializer.Deserialize<AdministrationDto>(Encoding.UTF8.GetString(body.ToArray()));

var employee = await _employeeRepository.GetByIdAsync(message!.PartnerId);

if (employee == null)
return;

employee.AppliedPromocodesCount++;

await _employeeRepository.UpdateAsync(employee);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Infrastructure.RabbitMq;

namespace Pcf.Administration.WebHost.Services.Employees;

public interface IEmployeesService : IRabbitMqConsumer
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Infrastructure.RabbitMq;

namespace Pcf.Administration.WebHost.Settings
{
public class ApplicationSettings
{
public RmqSettings RmqSettings { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
using Pcf.Administration.DataAccess.Data;
using Pcf.Administration.Core.Abstractions.Repositories;
using System;
using System.Threading.Tasks;
using Infrastructure.RabbitMq;
using Pcf.Administration.WebHost.Services.Employees;
using Pcf.Administration.WebHost.Settings;
using RabbitMQ.Client;

namespace Pcf.Administration.WebHost
{
Expand All @@ -28,7 +33,7 @@ public void ConfigureServices(IServiceCollection services)
{
services.AddControllers().AddMvcOptions(x =>
x.SuppressAsyncSuffixInActionNames = false);
services.AddScoped(typeof(IRepository<>), typeof(EfRepository<>));
services.AddSingleton(typeof(IRepository<>), typeof(EfRepository<>));
services.AddScoped<IDbInitializer, EfDbInitializer>();
services.AddDbContext<DataContext>(x =>
{
Expand All @@ -38,6 +43,9 @@ public void ConfigureServices(IServiceCollection services)
x.UseLazyLoadingProxies();
});

ConfigureRabbitMq(services).Wait();
services.AddSingleton<IEmployeesService, EmployeesService>();

AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);

services.AddOpenApiDocument(options =>
Expand All @@ -48,7 +56,22 @@ public void ConfigureServices(IServiceCollection services)
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IDbInitializer dbInitializer)
private async Task ConfigureRabbitMq(IServiceCollection services)
{
var rmqSettings = Configuration.Get<ApplicationSettings>().RmqSettings;
var connection = await RabbitMqConfiguration.GetRabbitConnection(rmqSettings);

var channel = await connection.CreateChannelAsync();

services.AddSingleton<IChannel>(_ => channel);
}

public void Configure(
IApplicationBuilder app,
IWebHostEnvironment env,
IDbInitializer dbInitializer,
IEmployeesService employeesService,
IChannel channel)
{
if (env.IsDevelopment())
{
Expand All @@ -73,7 +96,13 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IDbIniti
{
endpoints.MapControllers();
});


employeesService.Register(
channel,
Configuration["AdministrationExchangeName"]!,
Configuration["AdministrationQueue"]!,
Configuration["AdministrationRoutingKey"]!);

dbInitializer.InitializeDb();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,15 @@
"ConnectionStrings":{
"PromocodeFactoryAdministrationDb":"Host=localhost;Database=promocode_factory_administration_db;Username=postgres;Password=docker;Port=5433"
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"RmqSettings": {
"Host": "localhost",
"Port": 5672,
"VHost": "/",
"Login": "guest",
"Password": "guest"
},
"AdministrationExchangeName": "exchange.administration",
"AdministrationRoutingKey": "routingKey.administration",
"AdministrationQueue": "queue.administration"
}
12 changes: 12 additions & 0 deletions Homeworks/RabbitMQ/src/Pcf.Administration/nuget.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="github" value="https://nuget.pkg.github.com/SYAvdeev/index.json" />
</packageSources>
<packageSourceCredentials>
<github>
<add key="Username" value="SYAvdeev" />
<add key="ClearTextPassword" value="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" />
</github>
</packageSourceCredentials>
</configuration>
1 change: 1 addition & 0 deletions Homeworks/RabbitMQ/src/Pcf.GivingToCustomer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ WORKDIR /app

# Copy the solution file and project files, then restore dependencies
COPY *.sln .
COPY nuget.config .
COPY */*.csproj ./
RUN for file in $(ls *.csproj); do mkdir -p ${file%.*} && mv $file ${file%.*}; done
RUN dotnet restore
Expand Down
Loading