-
Notifications
You must be signed in to change notification settings - Fork 39
Белякова Вероника Лаб. 1 Группа 6511 #39
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
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": "https://localhost:7491/api/employee" | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\Employee.ServiceDefaults\Employee.ServiceDefaults.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Aspire.StackExchange.Redis.DistributedCaching" Version="13.1.2" /> | ||
| <PackageReference Include="Bogus" Version="35.6.5" /> | ||
| <PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="10.1.5" /> | ||
| <PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="10.1.5" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| @ApiService_HostAddress = http://localhost:5547 | ||
|
|
||
| GET {{ApiService_HostAddress}}/weatherforecast/ | ||
| Accept: application/json | ||
|
|
||
| ### |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| namespace Employee.ApiService.Models; | ||
|
|
||
| /// <summary> | ||
| /// Класс сотрудник компании | ||
| /// </summary> | ||
| public class EmployeeModel | ||
| { | ||
|
|
||
| /// <summary> | ||
| /// Идентификатор сотрудника в системе | ||
| /// </summary> | ||
| public required int Id { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// ФИО | ||
| /// </summary> | ||
| public required string Name { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Должность | ||
| /// </summary> | ||
| public required string Position { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Отдел | ||
| /// </summary> | ||
| public required string Department { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Дата приема | ||
| /// </summary> | ||
| public required DateOnly DateAdmission { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Оклад | ||
| /// </summary> | ||
| public required decimal Salary { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Электронная почта | ||
| /// </summary> | ||
| public required string Email { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Номер телефона | ||
| /// </summary> | ||
| public required string Phone { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Индикатор увольнения | ||
| /// </summary> | ||
| public bool DismissalIndicator { get; set; } = false; | ||
|
|
||
| /// <summary> | ||
| /// Дата увольнения | ||
| /// </summary> | ||
| public DateOnly? DateDismissal { get; set; } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| using Employee.ApiService.Services; | ||
| using Employee.ServiceDefaults; | ||
|
|
||
| var builder = WebApplication.CreateBuilder(args); | ||
|
|
||
| builder.AddServiceDefaults(); | ||
| builder.AddRedisDistributedCache("redis"); | ||
|
|
||
| builder.Services.AddEndpointsApiExplorer(); | ||
| builder.Services.AddSwaggerGen(); | ||
|
|
||
| builder.Services.AddCors(options => | ||
| { | ||
| options.AddPolicy("wasm", policy => | ||
| { | ||
| policy.AllowAnyOrigin() | ||
| .AllowAnyMethod() | ||
| .AllowAnyHeader(); | ||
| }); | ||
| }); | ||
|
|
||
| builder.Services.AddSingleton<EmployeeGenerator>(); | ||
| builder.Services.AddScoped<EmployeeService>(); | ||
|
|
||
| var app = builder.Build(); | ||
|
|
||
| if (app.Environment.IsDevelopment()) | ||
| { | ||
| app.UseSwagger(); | ||
| app.UseSwaggerUI(); | ||
| } | ||
|
Comment on lines
+27
to
+31
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. Если добавлять |
||
|
|
||
| app.MapDefaultEndpoints(); | ||
| app.UseHttpsRedirection(); | ||
| app.UseCors("wasm"); | ||
|
|
||
| app.MapGet("/api/employee", async (int id, EmployeeService service) => | ||
| { | ||
| var employee = await service.GetEmployeeAsync(id); | ||
| return Results.Ok(employee); | ||
| }); | ||
|
|
||
| app.Run(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| { | ||
| "$schema": "https://json.schemastore.org/launchsettings.json", | ||
| "profiles": { | ||
| "http": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": false, | ||
| "applicationUrl": "http://localhost:5547", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| }, | ||
| "https": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": false, | ||
| "applicationUrl": "https://localhost:7491;http://localhost:5547", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| using Bogus; | ||
| using Employee.ApiService.Models; | ||
|
|
||
| namespace Employee.ApiService.Services; | ||
|
|
||
| /// <summary> | ||
| /// Генератор тестовых сотрудников | ||
| /// </summary> | ||
| public class EmployeeGenerator | ||
| { | ||
| /// <summary> | ||
| /// Справочник профессий | ||
| /// </summary> | ||
| private static readonly string[] _professions = | ||
| [ | ||
| "Developer", | ||
| "Manager", | ||
| "Analyst", | ||
| "QA", | ||
| "DevOps", | ||
| "Designer" | ||
| ]; | ||
|
|
||
| /// <summary> | ||
| /// Справочник суффиксов должностей и коэффициентов зарплаты | ||
| /// </summary> | ||
| private static readonly Dictionary<string, decimal> _positionLevels = new() | ||
| { | ||
| { "Junior", 0.7m }, | ||
| { "Middle", 1.0m }, | ||
| { "Senior", 1.5m }, | ||
| { "Lead", 2.0m } | ||
| }; | ||
|
|
||
| /// <summary> | ||
| /// Константа базовой зарплаты | ||
| /// </summary> | ||
| private const decimal BaseSalary = 100000m; | ||
|
|
||
| /// <summary> | ||
| /// Генерация должности | ||
| /// </summary> | ||
| private static string GeneratePosition(Faker f) | ||
| { | ||
| var level = f.PickRandom(_positionLevels.Keys.ToArray()); | ||
| var profession = f.PickRandom(_professions); | ||
|
|
||
| return $"{level} {profession}"; | ||
| } | ||
|
Comment on lines
+43
to
+49
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. Этот метод возможно тоже не стоит выделять и написать все в Выделять стоит только большие участки кода |
||
|
|
||
| /// <summary> | ||
| /// Генерация даты приема | ||
| /// </summary> | ||
| private static DateOnly GenerateAdmissionDate(Faker f) | ||
| { | ||
| return DateOnly.FromDateTime(f.Date.Past(10)); | ||
| } | ||
|
Comment on lines
+54
to
+57
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. Как будто нет смысла выделять это в метод, можно в лямбде А еще есть |
||
|
|
||
| /// <summary> | ||
| /// Генерация зарплаты с учетом коэффициента уровня | ||
| /// </summary> | ||
| private static decimal GenerateSalary(Faker f, string position) | ||
| { | ||
| var level = _positionLevels.Keys.FirstOrDefault(position.Contains); | ||
|
|
||
| decimal coefficient = 1; | ||
|
|
||
| if (level != null) | ||
| { | ||
| coefficient = _positionLevels[level]; | ||
| } | ||
|
|
||
| var randomFactor = f.Random.Decimal(0.9m, 1.1m); | ||
|
|
||
| var salary = BaseSalary * coefficient * randomFactor; | ||
|
|
||
| return Math.Round(salary, 2); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Генерация даты увольнения | ||
| /// </summary> | ||
| private static DateOnly? GenerateDismissalDate(Faker f, EmployeeModel employee) | ||
| { | ||
| if (!employee.DismissalIndicator) | ||
| return null; | ||
|
|
||
| var start = employee.DateAdmission.ToDateTime(TimeOnly.MinValue); | ||
|
|
||
| var dismissal = f.Date.Between(start, DateTime.Now); | ||
|
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. Есть |
||
|
|
||
| return DateOnly.FromDateTime(dismissal); | ||
| } | ||
| /// <summary> | ||
| /// Генерация ФИО | ||
| /// </summary> | ||
| private static string GenerateFullName(Faker f) | ||
| { | ||
| var gender = f.PickRandom<Bogus.DataSets.Name.Gender>(); | ||
|
|
||
| var firstName = f.Name.FirstName(gender); | ||
| var lastName = f.Name.LastName(gender); | ||
|
|
||
| // имя отца | ||
| var fatherName = f.Name.FirstName(Bogus.DataSets.Name.Gender.Male); | ||
|
|
||
| var patronymic = gender == Bogus.DataSets.Name.Gender.Male | ||
| ? fatherName + "ович" | ||
| : fatherName + "овна"; | ||
|
|
||
| return $"{lastName} {firstName} {patronymic}"; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Преднастроенный генератор | ||
| /// </summary> | ||
| private static readonly Faker<EmployeeModel> _faker = new Faker<EmployeeModel>("ru") | ||
| .RuleFor(e => e.Name, f => GenerateFullName(f)) | ||
|
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(e => e.Name, GenerateFullName)Ниже аналогично |
||
| .RuleFor(e => e.Position, f => GeneratePosition(f)) | ||
| .RuleFor(e => e.Department, f => f.Commerce.Department()) | ||
| .RuleFor(e => e.DateAdmission, f => GenerateAdmissionDate(f)) | ||
| .RuleFor(e => e.Salary, (f, e) => GenerateSalary(f, e.Position)) | ||
| .RuleFor(e => e.Email, (f, e) => f.Internet.Email()) | ||
| .RuleFor(e => e.Phone, f => f.Phone.PhoneNumber("+7(###)###-##-##")) | ||
| .RuleFor(e => e.DismissalIndicator, f => f.Random.Bool(0.2f)) | ||
| .RuleFor(e => e.DateDismissal, (f, e) => GenerateDismissalDate(f, e)); | ||
|
|
||
| /// <summary> | ||
| /// Генерация сотрудника | ||
| /// </summary> | ||
| public EmployeeModel Generate(int id) | ||
| { | ||
| var employee = _faker.Generate(); | ||
| employee.Id = id; | ||
|
|
||
| return employee; | ||
| } | ||
|
|
||
| } | ||
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.
Этот файл можно удалить