-
Notifications
You must be signed in to change notification settings - Fork 49
Степанов Дмитрий Лаб. 1 Группа 6511 #58
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
base: main
Are you sure you want to change the base?
Changes from all commits
4af3e53
8a78fe1
2365002
4d0a399
a728f2d
9fd7735
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,5 +6,5 @@ | |
| } | ||
| }, | ||
| "AllowedHosts": "*", | ||
| "BaseAddress": "" | ||
| } | ||
| "BaseAddress": "http://localhost:5171/patient" | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| @GeneratorService_HostAddress = http://localhost:5104 | ||
|
|
||
| GET {{GeneratorService_HostAddress}}/weatherforecast/ | ||
| Accept: application/json | ||
|
|
||
| ### |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| namespace PatientApp.Generator.Models; | ||
|
|
||
| public class Patient | ||
| { | ||
| public int Id { get; set; } | ||
| public string FullName { get; set; } | ||
| public DateOnly Birthday { get; set; } | ||
| public string Address { get; set; } | ||
|
|
||
| public double Height { get; set; } | ||
| public double Weight { get; set; } | ||
|
|
||
| public int BloodType { get; set; } | ||
|
|
||
| public bool Resus { get; set; } | ||
|
|
||
| public DateOnly LastVisit { get; set; } | ||
|
|
||
| public bool Vactination { get; set; } | ||
|
Comment on lines
+5
to
+19
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. Нужно добавить А также сделать свойства либо
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <Nullable>enable</Nullable> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Aspire.StackExchange.Redis.DistributedCaching" Version="13.1.2" /> | ||
| <PackageReference Include="Bogus" Version="35.6.5" /> | ||
| <PackageReference Include="Serilog" Version="4.3.2-dev-02419" /> | ||
| <PackageReference Include="Serilog.Sinks.Console" Version="6.1.1" /> | ||
| <PackageReference Include="StackExchange.Redis" Version="2.12.1" /> | ||
| <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\Patient\Patient.ServiceDefaults\PatientApp.ServiceDefaults.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| using PatientApp.Generator.Services; | ||
| using PatientApp.ServiceDefaults; | ||
| using Serilog; | ||
|
|
||
| var builder = WebApplication.CreateBuilder(args); | ||
|
|
||
| builder.AddServiceDefaults(); | ||
|
|
||
| builder.AddRedisDistributedCache("redis"); | ||
|
|
||
| builder.Services.AddSingleton<PatientGenerator>(); | ||
| builder.Services.AddScoped<PatientService>(); | ||
|
|
||
| builder.Services.AddCors(options => | ||
| { | ||
| options.AddDefaultPolicy(policy => | ||
| { | ||
| policy.AllowAnyOrigin() | ||
| .AllowAnyMethod() | ||
| .AllowAnyHeader(); | ||
| }); | ||
| }); | ||
|
Comment on lines
+14
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 так, чтобы делать запрос мог только клиент Для этого лучше задать явный список origins в |
||
|
|
||
| var app = builder.Build(); | ||
|
|
||
| app.UseCors(); | ||
| app.UseSerilogRequestLogging(); | ||
|
|
||
| app.MapDefaultEndpoints(); | ||
|
|
||
| app.MapGet("/patient", async ( | ||
| int id, | ||
| PatientService service, | ||
| ILogger<Program> logger, | ||
| CancellationToken cancellationToken) => | ||
| { | ||
| logger.LogInformation($"Received request for patient with ID: {id}"); | ||
|
|
||
| if (id <= 0) | ||
| { | ||
| logger.LogWarning($"Received invalid ID: {id}"); | ||
| return Results.BadRequest(new { error = "ID must be a positive number" }); | ||
| } | ||
|
|
||
| try | ||
| { | ||
| var application = await service.GetByIdAsync(id, cancellationToken); | ||
| return Results.Ok(application); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| logger.LogError(ex, $"Error while getting patient {id}"); | ||
| return Results.Problem("An error occurred while processing the request"); | ||
| } | ||
| }) | ||
| .WithName("GetPatient"); | ||
|
|
||
| app.Run(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| { | ||
| "$schema": "http://json.schemastore.org/launchsettings.json", | ||
| "profiles": { | ||
| "http": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": false, | ||
| "applicationUrl": "http://localhost:5171", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| }, | ||
| "https": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": false, | ||
| "applicationUrl": "https://localhost:7171;http://localhost:5171", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| using Bogus; | ||
| using Bogus.DataSets; | ||
| using static System.Math; | ||
| using PatientApp.Generator.Models; | ||
|
|
||
| namespace PatientApp.Generator.Services; | ||
|
|
||
| public class PatientGenerator(ILogger<PatientGenerator> logger) | ||
| { | ||
| private readonly Faker<Patient> _faker = new Faker<Patient>("ru") | ||
| .RuleFor(x => x.FullName, GeneratePatientFullName) | ||
| .RuleFor(x => x.Birthday, f => f.Date.PastDateOnly()) | ||
|
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. По дефолту первый параметр у этого метода равен |
||
| .RuleFor(x => x.Address, f => f.Address.FullAddress()) | ||
| .RuleFor(x => x.Weight, f => Round(f.Random.Double(5, 120), 2)) | ||
| .RuleFor(x => x.Height, f => Round(f.Random.Double(50, 200), 2)) | ||
|
Comment on lines
+14
to
+15
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. Что вероятно неправильно, потому что в 1 год быть 120кг 200см тяжело Но и 5кг и 50см тоже кстати тяжело, если там 1 месяц например |
||
| .RuleFor(x => x.BloodType, f => f.Random.Int(1, 4)) | ||
| .RuleFor(x => x.Resus, f => f.Random.Bool()) | ||
| .RuleFor(x => x.Vactination, f => f.Random.Bool()) | ||
| .RuleFor(x => x.LastVisit, (f, patient) => f.Date.BetweenDateOnly(patient.Birthday, DateOnly.FromDateTime(DateTime.UtcNow)) | ||
| ); | ||
|
|
||
| public Patient Generate(int id) | ||
| { | ||
| logger.LogInformation($"Generating Patient with ID: {id}"); | ||
|
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. Во всех операциях логирования убрать интерполяцию строк, нужно вот так: logger.LogInformation("Generating Patient with ID: {id}", id); |
||
| return _faker.UseSeed(id).RuleFor(x => x.Id, _ => id).Generate(); | ||
| } | ||
|
|
||
| private static string GeneratePatientFullName(Faker faker) | ||
| { | ||
| var gender = faker.Person.Gender; | ||
| var firstName = faker.Name.FirstName(gender); | ||
| var lastName = faker.Name.LastName(gender); | ||
| var patronymic = faker.Name.FirstName(Name.Gender.Male) + (gender == Name.Gender.Male ? "еевич" : "еевна"); | ||
|
|
||
| return string.Join(' ', firstName, lastName, patronymic); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| using Microsoft.Extensions.Caching.Distributed; | ||
| using System.Text.Json; | ||
| using PatientApp.Generator.Services; | ||
| using PatientApp.Generator.Models; | ||
|
|
||
| public class PatientService( | ||
| PatientGenerator generator, | ||
| IDistributedCache cache, | ||
| ILogger<PatientService> logger, | ||
| IConfiguration config | ||
| ) | ||
| { | ||
| private readonly TimeSpan _cacheExpiration = TimeSpan.FromMinutes(config.GetSection("CacheSetting").GetValue("CacheExpirationMinutes", 5)); | ||
| private const string CacheKeyPrefix = "patient:"; | ||
|
|
||
| public async Task<Patient> GetByIdAsync(int id, CancellationToken cancellationToken = default) | ||
| { | ||
| var cacheKey = $"{CacheKeyPrefix}{id}"; | ||
|
|
||
| logger.LogInformation($"Patient with Id: {id} was requested"); | ||
|
|
||
| var cachedData = await cache.GetStringAsync(cacheKey, cancellationToken); | ||
|
|
||
| if (!string.IsNullOrEmpty(cachedData)) | ||
| { | ||
| logger.LogInformation($"Patient with {id} was found in cache"); | ||
| var cachedPatient = JsonSerializer.Deserialize<Patient>(cachedData); | ||
| if (cachedPatient != null) return cachedPatient; | ||
| } | ||
|
|
||
| logger.LogInformation($"Patient with {id} was found in cache, start generating"); | ||
|
|
||
| var patient = generator.Generate(id); | ||
|
|
||
| var serializedData = JsonSerializer.Serialize(patient); | ||
| var cacheOptions = new DistributedCacheEntryOptions | ||
| { | ||
| AbsoluteExpirationRelativeToNow = _cacheExpiration | ||
| }; | ||
|
|
||
| await cache.SetStringAsync(cacheKey, serializedData, cacheOptions, cancellationToken); | ||
|
|
||
| logger.LogInformation($"Patint with Id: {id} was saved to cache with TTL {_cacheExpiration.TotalMinutes} minutes"); | ||
|
|
||
| return patient; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| }, | ||
| "AllowedHosts": "*" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| var builder = DistributedApplication.CreateBuilder(args); | ||
|
|
||
| var redis = builder.AddRedis("redis") | ||
| .WithRedisCommander(); | ||
|
|
||
| var generator = builder.AddProject<Projects.PatientApp_Generator>("generator") | ||
|
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. Генератору стоит подождать редис |
||
| .WithReference(redis) | ||
| .WithExternalHttpEndpoints(); | ||
|
|
||
| builder.AddProject<Projects.Client_Wasm>("client") | ||
| .WithReference(generator); | ||
|
Comment on lines
+10
to
+11
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. А клиенту подождать генератор |
||
|
|
||
| builder.Build().Run(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <Sdk Name="Aspire.AppHost.Sdk" Version="9.5.2" /> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <UserSecretsId>9850a275-b1ed-4763-b4e3-9c0fbc45d25f</UserSecretsId> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Aspire.Hosting" Version="9.5.2" /> | ||
| <PackageReference Include="Aspire.Hosting.AppHost" Version="9.5.2" /> | ||
| <PackageReference Include="Aspire.Hosting.Redis" Version="9.5.2" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\Client.Wasm\Client.Wasm.csproj" /> | ||
| <ProjectReference Include="..\..\GeneratorService\PatientApp.Generator.csproj" /> | ||
| <ProjectReference Include="..\Patient.ServiceDefaults\PatientApp.ServiceDefaults.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
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.
Этот файл не нужен