-
Notifications
You must be signed in to change notification settings - Fork 50
Степанов Дмитрий Лаб. 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
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4af3e53
Объектная модель
negniy 8a78fe1
Изменена объектная модель (я слепая дура)
negniy 2365002
Написаны генератор и сервис генерации
negniy 4d0a399
Исправление клиента
negniy a728f2d
some fix
negniy 9fd7735
Изменение ридми
negniy 026d492
fixes
negniy 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
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
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
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 |
|---|---|---|
|
|
@@ -6,5 +6,5 @@ | |
| } | ||
| }, | ||
| "AllowedHosts": "*", | ||
| "BaseAddress": "" | ||
| } | ||
| "BaseAddress": "http://localhost:5171/patient" | ||
| } | ||
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
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,57 @@ | ||
| namespace PatientApp.Generator.Models; | ||
|
|
||
| /// <summary> | ||
| /// Представляет пациента в системе. | ||
| /// </summary> | ||
| public class Patient | ||
| { | ||
| /// <summary> | ||
| /// Уникальный идентификатор пациента. | ||
| /// </summary> | ||
| public int Id { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Полное имя пациента. | ||
| /// </summary> | ||
| public required string FullName { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Дата рождения пациента. | ||
| /// </summary> | ||
| public DateOnly Birthday { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Адрес проживания пациента. | ||
| /// </summary> | ||
| public string? Address { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Рост пациента. | ||
| /// </summary> | ||
| public double Height { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Вес пациента. | ||
| /// </summary> | ||
| public double Weight { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Группа крови пациента. | ||
| /// </summary> | ||
| public int BloodType { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Резус фактор пациента. | ||
| /// </summary> | ||
| public bool Resus { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Дата последнего визита. | ||
| /// </summary> | ||
| public DateOnly LastVisit { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Есть ли вакцинация. | ||
| /// </summary> | ||
| public bool Vactination { 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,23 @@ | ||
| <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="Serilog.Sinks.OpenTelemetry" Version="4.2.1-nblumhardt-02317" /> | ||
| <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> |
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,62 @@ | ||
| 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>(); | ||
|
|
||
| var allowedOrigins = builder.Configuration | ||
| .GetSection("Cors:AllowedOrigins") | ||
| .Get<string[]>(); | ||
|
|
||
| builder.Services.AddCors(options => | ||
| { | ||
| options.AddDefaultPolicy(policy => | ||
| { | ||
| policy.WithOrigins(allowedOrigins!) | ||
| .AllowAnyMethod() | ||
| .AllowAnyHeader(); | ||
| }); | ||
| }); | ||
|
|
||
| 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}", id); | ||
|
|
||
| if (id <= 0) | ||
| { | ||
| logger.LogWarning("Received invalid ID: {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}", id); | ||
| return Results.Problem("An error occurred while processing the request"); | ||
| } | ||
| }) | ||
| .WithName("GetPatient"); | ||
|
|
||
| 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,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" | ||
| } | ||
| } | ||
| } | ||
| } |
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,63 @@ | ||
| 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(100)) | ||
| .RuleFor(x => x.Address, f => f.Address.FullAddress()) | ||
| .RuleFor(x => x.Weight, GenerateWeight) | ||
| .RuleFor(x => x.Height, GenerateHeight) | ||
| .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}", 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); | ||
| } | ||
|
|
||
| private static double GenerateWeight(Faker faker, Patient patient) | ||
| { | ||
| var age = DateTime.UtcNow.Year - patient.Birthday.Year; | ||
|
|
||
| return age switch | ||
| { | ||
| < 3 => Round(faker.Random.Double(3, 15), 2), | ||
| < 12 => Round(faker.Random.Double(15, 50), 2), | ||
| < 18 => Round(faker.Random.Double(40, 80), 2), | ||
| _ => Round(faker.Random.Double(50, 120), 2) | ||
| }; | ||
| } | ||
|
|
||
| private static double GenerateHeight(Faker faker, Patient patient) | ||
| { | ||
| var age = DateTime.UtcNow.Year - patient.Birthday.Year; | ||
|
|
||
| return age switch | ||
| { | ||
| < 3 => Round(faker.Random.Double(40, 100), 2), | ||
| < 12 => Round(faker.Random.Double(100, 150), 2), | ||
| < 18 => Round(faker.Random.Double(140, 180), 2), | ||
| _ => Round(faker.Random.Double(150, 200), 2) | ||
| }; | ||
| } | ||
| } |
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,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", id); | ||
|
|
||
| var cachedData = await cache.GetStringAsync(cacheKey, cancellationToken); | ||
|
|
||
| if (!string.IsNullOrEmpty(cachedData)) | ||
| { | ||
| logger.LogInformation("Patient with {id} was found in cache", id); | ||
| var cachedPatient = JsonSerializer.Deserialize<Patient>(cachedData); | ||
| if (cachedPatient != null) return cachedPatient; | ||
| } | ||
|
|
||
| logger.LogInformation("Patient with {id} was found in cache, start generating", id); | ||
|
|
||
| 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 {TtlMinutes} minutes", id, _cacheExpiration.TotalMinutes); | ||
|
|
||
| return patient; | ||
| } | ||
| } | ||
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,8 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| } | ||
| } |
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.
Здесь не хватает пространства имен