-
Notifications
You must be signed in to change notification settings - Fork 24
Карпачева Полина Лаб. 1 Группа 6512 #19
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
Open
PolinaKrp
wants to merge
9
commits into
itsecd:main
Choose a base branch
from
PolinaKrp:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+674
−57
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9ad2ccd
лабораторная 1
PolinaKrp c4c60f4
Update setup_pr.yml
PolinaKrp e68541a
Update setup_pr.yml
PolinaKrp 84d9ec8
Update launchSettings.json
PolinaKrp 793e167
Delete .DS_Store
PolinaKrp 4c2c530
Delete AspireApp.EmptyService directory
PolinaKrp 7aa21f3
Delete AspireApp.Web directory
PolinaKrp 834e450
правки
PolinaKrp a04c423
Delete .DS_Store
PolinaKrp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\AspireApp.ServiceDefaults\AspireApp.ServiceDefaults.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Aspire.StackExchange.Redis.DistributedCaching" Version="8.2.2" /> | ||
| <PackageReference Include="Bogus" Version="35.6.5" /> | ||
| <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.15" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace AspireApp.ApiService.Entities; | ||
|
|
||
| /// <summary> | ||
| /// Товар на складе | ||
| /// </summary> | ||
| public class Warehouse | ||
| { | ||
| /// <summary> | ||
| /// Идентификатор | ||
| /// </summary> | ||
| [JsonPropertyName("id")] | ||
| public int Id { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Наименование товара | ||
| /// </summary> | ||
| [JsonPropertyName("name")] | ||
| public string? Name { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Категория товара | ||
| /// </summary> | ||
| [JsonPropertyName("category")] | ||
| public string? Category { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Количество на складе | ||
| /// </summary> | ||
| [JsonPropertyName("stockQuantity")] | ||
| public int StockQuantity { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Цена за единицу товара | ||
| /// </summary> | ||
| [JsonPropertyName("price")] | ||
| public decimal Price { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Вес единицы товара | ||
| /// </summary> | ||
| [JsonPropertyName("weight")] | ||
| public double Weight { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Габариты единицы товара | ||
| /// </summary> | ||
| [JsonPropertyName("dimensions")] | ||
| public string? Dimensions { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Хрупкий ли товар | ||
| /// </summary> | ||
| [JsonPropertyName("isFragile")] | ||
| public bool IsFragile { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Дата последней поставки | ||
| /// </summary> | ||
| [JsonPropertyName("lastDeliveryDate")] | ||
| public DateOnly LastDeliveryDate { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Дата следующей планируемой поставки | ||
| /// </summary> | ||
| [JsonPropertyName("nextDeliveryDate")] | ||
| public DateOnly NextDeliveryDate { get; set; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| using AspireApp.ApiService.Entities; | ||
|
|
||
| namespace AspireApp.ApiService.Generator; | ||
|
|
||
| /// <summary> | ||
| /// Интерфейс для работы с кэшем товаров | ||
| /// </summary> | ||
| public interface IWarehouseCache | ||
| { | ||
| /// <summary> | ||
| /// Получить товар из кэша по идентификатору | ||
| /// </summary> | ||
| Task<Warehouse?> GetAsync(int id); | ||
|
|
||
| /// <summary> | ||
| /// Сохранить товар в кэш | ||
| /// </summary> | ||
| Task SetAsync(Warehouse warehouse); | ||
| } |
11 changes: 11 additions & 0 deletions
11
AspireApp.ApiService/Generator/IWarehouseGeneratorService.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| using AspireApp.ApiService.Entities; | ||
|
|
||
| namespace AspireApp.ApiService.Generator; | ||
|
|
||
| /// <summary> | ||
| /// Сервис обработки товаров на складе | ||
| /// </summary> | ||
| public interface IWarehouseGeneratorService | ||
| { | ||
| Task<Warehouse> ProcessWarehouse(int id); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| using Microsoft.Extensions.Caching.Distributed; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.Logging; | ||
| using System.Text.Json; | ||
| using AspireApp.ApiService.Entities; | ||
|
|
||
| namespace AspireApp.ApiService.Generator; | ||
|
|
||
| /// <summary> | ||
| /// Кэширование товаров | ||
| /// </summary> | ||
| public class WarehouseCache( | ||
| IDistributedCache cache, | ||
| ILogger<WarehouseCache> logger, | ||
| IConfiguration configuration) : IWarehouseCache | ||
| { | ||
| private readonly TimeSpan _defaultExpiration = int.TryParse(configuration["CacheExpiration"], out var seconds) | ||
| ? TimeSpan.FromSeconds(seconds) | ||
| : TimeSpan.FromSeconds(3600); | ||
|
|
||
| public async Task<Warehouse?> GetAsync(int id) | ||
| { | ||
| var key = $"warehouse_{id}"; | ||
| var cached = await cache.GetStringAsync(key); | ||
| if (cached == null) | ||
| return null; | ||
|
|
||
| try | ||
| { | ||
| return JsonSerializer.Deserialize<Warehouse>(cached); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| logger.LogError(ex, "Ошибка десериализации товара {Id} из кэша", id); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| public async Task SetAsync(Warehouse warehouse) | ||
| { | ||
| var key = $"warehouse_{warehouse.Id}"; | ||
| var options = new DistributedCacheEntryOptions | ||
| { | ||
| AbsoluteExpirationRelativeToNow = _defaultExpiration | ||
| }; | ||
| var serialized = JsonSerializer.Serialize(warehouse); | ||
| await cache.SetStringAsync(key, serialized, options); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| using Bogus; | ||
| using AspireApp.ApiService.Entities; | ||
|
|
||
| namespace AspireApp.ApiService.Generator; | ||
|
|
||
| /// <summary> | ||
| /// Генератор случайных товаров с использованием Bogus | ||
| /// </summary> | ||
| public class WarehouseGenerator | ||
| { | ||
| private readonly Faker<Warehouse> _faker; | ||
|
|
||
| public WarehouseGenerator() | ||
| { | ||
| _faker = new Faker<Warehouse>() | ||
| .RuleFor(w => w.Id, f => f.IndexFaker + 1) // будет перезаписан позже | ||
| .RuleFor(w => w.Name, f => f.Commerce.ProductName()) | ||
| .RuleFor(w => w.Category, f => f.Commerce.Categories(1)[0]) | ||
| .RuleFor(w => w.StockQuantity, f => f.Random.Int(0, 1000)) | ||
| .RuleFor(w => w.Price, f => decimal.Parse(f.Commerce.Price())) | ||
| .RuleFor(w => w.Weight, f => f.Random.Double(0.1, 50.0)) | ||
| .RuleFor(w => w.Dimensions, f => $"{f.Random.Int(10, 100)}x{f.Random.Int(10, 100)}x{f.Random.Int(10, 100)}") | ||
| .RuleFor(w => w.IsFragile, f => f.Random.Bool(0.3f)) | ||
| .RuleFor(w => w.LastDeliveryDate, f => DateOnly.FromDateTime(f.Date.Past(30))) | ||
| .RuleFor(w => w.NextDeliveryDate, f => DateOnly.FromDateTime(f.Date.Future(30))); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Генерирует один случайный товар (Id будет перезаписан вызывающим кодом) | ||
| /// </summary> | ||
| public Warehouse Generate() | ||
| { | ||
| return _faker.Generate(); | ||
| } | ||
| } |
53 changes: 53 additions & 0 deletions
53
AspireApp.ApiService/Generator/WarehouseGeneratorService.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.Logging; | ||
| using AspireApp.ApiService.Entities; | ||
|
|
||
| namespace AspireApp.ApiService.Generator; | ||
|
|
||
| /// <summary> | ||
| /// Служба для запуска юзкейса по обработке товаров на складе | ||
| /// </summary> | ||
| public class WarehouseGeneratorService( | ||
| IWarehouseCache warehouseCache, | ||
| ILogger<WarehouseGeneratorService> logger, | ||
| WarehouseGenerator generator) : IWarehouseGeneratorService | ||
| { | ||
| public async Task<Warehouse> ProcessWarehouse(int id) | ||
| { | ||
| logger.LogInformation("Обработка товара с Id = {Id} начата", id); | ||
|
|
||
| // Получаем товар из кэша | ||
| Warehouse? warehouse = null; | ||
| try | ||
| { | ||
| warehouse = await warehouseCache.GetAsync(id); | ||
| if (warehouse != null) | ||
| { | ||
| logger.LogInformation("Товар {Id} получен из кэша", id); | ||
| return warehouse; | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| logger.LogWarning(ex, "Не удалось получить товар {Id} из кэша (ошибка игнорируется)", id); | ||
| } | ||
|
|
||
| // Если в кэше нет или ошибка — генерируем новый товар | ||
| logger.LogInformation("Товар {Id} в кэше не найден или кэш недоступен, запуск генерации", id); | ||
| warehouse = generator.Generate(); | ||
| warehouse.Id = id; | ||
|
|
||
| // Попытка сохранить в кэш | ||
| try | ||
| { | ||
| logger.LogInformation("Сохранение товара {Id} в кэш", id); | ||
| await warehouseCache.SetAsync(warehouse); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| logger.LogWarning(ex, "Не удалось сохранить товар {Id} в кэш (ошибка игнорируется)", id); | ||
| } | ||
|
|
||
| return warehouse; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| using AspireApp.ApiService.Generator; | ||
| using Microsoft.AspNetCore.Builder; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Hosting; | ||
|
|
||
| var builder = WebApplication.CreateBuilder(args); | ||
|
|
||
| builder.AddServiceDefaults(); | ||
|
|
||
| builder.AddRedisDistributedCache("RedisCache"); | ||
|
|
||
| builder.Services.AddScoped<IWarehouseCache, WarehouseCache>(); | ||
| builder.Services.AddSingleton<WarehouseGenerator>(); | ||
| builder.Services.AddScoped<IWarehouseGeneratorService, WarehouseGeneratorService>(); | ||
|
|
||
| builder.Services.AddCors(options => | ||
| { | ||
| options.AddDefaultPolicy(policy => | ||
| { | ||
| policy.AllowAnyOrigin() | ||
| .AllowAnyMethod() | ||
| .AllowAnyHeader(); | ||
|
Comment on lines
+20
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Настроить Cors |
||
| }); | ||
| }); | ||
|
|
||
| var app = builder.Build(); | ||
|
|
||
| app.MapDefaultEndpoints(); | ||
|
|
||
| app.MapGet("/warehouse", async (IWarehouseGeneratorService service, int id) => | ||
| { | ||
| var warehouse = await service.ProcessWarehouse(id); | ||
| return Results.Ok(warehouse); | ||
| }); | ||
|
|
||
| app.UseCors(); | ||
|
|
||
| app.Run(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| { | ||
| "profiles": { | ||
| "http": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": false, | ||
| "applicationUrl": "http://localhost:5000", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| {"BaseAddress": "http://localhost:53677", | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| }, | ||
| "AllowedHosts": "*" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| using Aspire.Hosting; | ||
|
|
||
| var builder = DistributedApplication.CreateBuilder(args); | ||
|
|
||
| var cache = builder.AddRedis("RedisCache"); | ||
|
|
||
| var service = builder.AddProject("service-api", "../AspireApp.ApiService/AspireApp.ApiService.csproj") | ||
| .WithReference(cache); | ||
|
|
||
| builder.AddProject("client-wasm", "../Client.Wasm/Client.Wasm.csproj") | ||
| .WithReference(service); | ||
|
|
||
| builder.Build().Run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <IsAspireHost>true</IsAspireHost> | ||
| <UserSecretsId>59f7c075-68cd-4f31-89d0-9f0df5a69013</UserSecretsId> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\AspireApp.ApiService\AspireApp.ApiService.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Aspire.Hosting.AppHost" Version="8.2.2" /> | ||
| <PackageReference Include="Aspire.Hosting.Redis" Version="8.2.2" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Папки Entities и Generator вынести из Properties