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
1 change: 1 addition & 0 deletions Client.Wasm/Client.Wasm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<PackageReference Include="Blazorise.Icons.FontAwesome" Version="1.8.8" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.22" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.22" PrivateAssets="all" />
<PackageReference Include="Microsoft.Extensions.Http" Version="10.0.5" />
</ItemGroup>

</Project>
7 changes: 3 additions & 4 deletions Client.Wasm/Components/DataCard.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@inject IConfiguration Configuration
@inject HttpClient Client
@using Client.Wasm
@inject VehicleApiClient ApiClient

<CardDeck>
<Card>
Expand Down Expand Up @@ -67,8 +67,7 @@

private async Task RequestNewData()
{
var baseAddress = Configuration["BaseAddress"] ?? throw new KeyNotFoundException("Конфигурация клиента не содержит параметра BaseAddress");
Value = await Client.GetFromJsonAsync<JsonObject>($"{baseAddress}?id={Id}", new JsonSerializerOptions { });
Value = await ApiClient.GetVehicleAsync(Id);
StateHasChanged();
}
}
10 changes: 5 additions & 5 deletions Client.Wasm/Components/StudentCard.razor
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Card>
<Card>
<CardHeader>
<Heading Size="HeadingSize.Is5"><Icon Name="IconName.User" /> Лабораторная работа</Heading>
</CardHeader>
<CardBody>
<UnorderedList Unstyled>
<UnorderedListItem>Номер <Strong>№X "Название лабораторной"</Strong></UnorderedListItem>
<UnorderedListItem>Вариант <Strong>№Х "Название варианта"</Strong></UnorderedListItem>
<UnorderedListItem>Выполнена <Strong>Фамилией Именем 65ХХ</Strong> </UnorderedListItem>
<UnorderedListItem><Link To="https://puginarug.com/">Ссылка на форк</Link></UnorderedListItem>
<UnorderedListItem>Номер <Strong>№1 "Кэширование"</Strong></UnorderedListItem>
<UnorderedListItem>Вариант <Strong>№29 "Транспортное средство"</Strong></UnorderedListItem>
<UnorderedListItem>Выполнена <Strong>Нестеренко Андреем 6512</Strong> </UnorderedListItem>
<UnorderedListItem><Link To="https://github.com/MagGoldi/cloud-development">Ссылка на репозиторий</Link></UnorderedListItem>
</UnorderedList>
</CardBody>
</Card>
7 changes: 6 additions & 1 deletion Client.Wasm/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");

builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.Services.AddHttpClient<VehicleApiClient>(client =>
{
var baseAddress = builder.Configuration["BaseAddress"]
?? throw new InvalidOperationException("BaseAddress not found in configuration.");
client.BaseAddress = new Uri(baseAddress);
});
builder.Services.AddBlazorise(options => { options.Immediate = true; })
.AddBootstrapProviders()
.AddFontAwesomeIcons();
Expand Down
12 changes: 12 additions & 0 deletions Client.Wasm/VehicleApiClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Net.Http.Json;
using System.Text.Json.Nodes;

namespace Client.Wasm;

public class VehicleApiClient(HttpClient httpClient)
{
public async Task<JsonObject?> GetVehicleAsync(int id)
{
return await httpClient.GetFromJsonAsync<JsonObject>($"api/vehicle/{id}");
}
}
2 changes: 1 addition & 1 deletion Client.Wasm/wwwroot/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
}
},
"AllowedHosts": "*",
"BaseAddress": ""
"BaseAddress": "http://localhost:5179/"
}
27 changes: 25 additions & 2 deletions CloudDevelopment.sln
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.36811.4
VisualStudioVersion = 17.13.35931.197
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client.Wasm", "Client.Wasm\Client.Wasm.csproj", "{AE7EEA74-2FE0-136F-D797-854FD87E022A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProjectApp.Api", "ProjectApp.Api\ProjectApp.Api.csproj", "{E7D4CA8B-53EA-9676-D96D-BE2F0CB11054}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProjectApp.Domain", "ProjectApp.Domain\ProjectApp.Domain.csproj", "{CC5A9873-4CC3-4B71-83AF-E4FD09F7B1AD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProjectApp.ServiceDefaults", "ProjectApp.ServiceDefaults\ProjectApp.ServiceDefaults.csproj", "{B2C3D4E5-F6A7-8901-BCDE-F12345678901}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProjectApp.AppHost", "ProjectApp.AppHost\ProjectApp.AppHost.csproj", "{2A5FB573-9376-4FEB-9289-A8387F435C13}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +22,22 @@ Global
{AE7EEA74-2FE0-136F-D797-854FD87E022A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AE7EEA74-2FE0-136F-D797-854FD87E022A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AE7EEA74-2FE0-136F-D797-854FD87E022A}.Release|Any CPU.Build.0 = Release|Any CPU
{E7D4CA8B-53EA-9676-D96D-BE2F0CB11054}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E7D4CA8B-53EA-9676-D96D-BE2F0CB11054}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E7D4CA8B-53EA-9676-D96D-BE2F0CB11054}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E7D4CA8B-53EA-9676-D96D-BE2F0CB11054}.Release|Any CPU.Build.0 = Release|Any CPU
{CC5A9873-4CC3-4B71-83AF-E4FD09F7B1AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CC5A9873-4CC3-4B71-83AF-E4FD09F7B1AD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CC5A9873-4CC3-4B71-83AF-E4FD09F7B1AD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CC5A9873-4CC3-4B71-83AF-E4FD09F7B1AD}.Release|Any CPU.Build.0 = Release|Any CPU
{B2C3D4E5-F6A7-8901-BCDE-F12345678901}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B2C3D4E5-F6A7-8901-BCDE-F12345678901}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B2C3D4E5-F6A7-8901-BCDE-F12345678901}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B2C3D4E5-F6A7-8901-BCDE-F12345678901}.Release|Any CPU.Build.0 = Release|Any CPU
{2A5FB573-9376-4FEB-9289-A8387F435C13}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2A5FB573-9376-4FEB-9289-A8387F435C13}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2A5FB573-9376-4FEB-9289-A8387F435C13}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2A5FB573-9376-4FEB-9289-A8387F435C13}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
29 changes: 29 additions & 0 deletions ProjectApp.Api/Controllers/VehicleController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using ProjectApp.Api.Services.VehicleGeneratorService;
using ProjectApp.Domain.Entities;
using Microsoft.AspNetCore.Mvc;

namespace ProjectApp.Api.Controllers;

/// <summary>
/// Контроллер для генерации и получения характеристик транспортных средств
/// </summary>

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Убрать пустую строку

[Route("api/[controller]")]
[ApiController]
public class VehicleController(IVehicleGeneratorService vehicleService, ILogger<VehicleController> logger) : ControllerBase
{
/// <summary>
/// Возвращает сгенерированное транспортное средство по его уникальному идентификатору
/// </summary>
/// <param name="id">Идентификатор машины</param>
/// <param name="cancellationToken">Токен отмены</param>
[HttpGet("{id}")]
public async Task<ActionResult<Vehicle>> GetById([FromRoute] int id, CancellationToken cancellationToken)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Добавить атрибут ProducesResponseType и можно типизировать id

{
logger.LogInformation("Request received for vehicle id {Id}", id);

var vehicle = await vehicleService.FetchByIdAsync(id, cancellationToken);

return Ok(vehicle);
}
}
66 changes: 66 additions & 0 deletions ProjectApp.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using ProjectApp.Api.Services.VehicleGeneratorService;
using ProjectApp.ServiceDefaults;

var builder = WebApplication.CreateBuilder(args);

builder.AddServiceDefaults();

builder.AddRedisDistributedCache("cache");

var allowedOrigins = builder.Configuration.GetSection("AllowedOrigins").Get<string[]>() ?? ["http://localhost:5127"];
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(policy =>
{
policy.WithOrigins(allowedOrigins)
.WithMethods("GET")
.WithHeaders("Content-Type");
});
});

builder.Services.AddSingleton<VehicleFaker>();
builder.Services.AddScoped<VehicleGeneratorService>();
builder.Services.AddScoped<IVehicleGeneratorService>(sp =>
ActivatorUtilities.CreateInstance<CachedVehicleGeneratorService>(sp,
sp.GetRequiredService<VehicleGeneratorService>()));

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new Microsoft.OpenApi.OpenApiInfo
{
Title = "Vehicle Generator API"
});

var xmlFilename = $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFilename);
if (File.Exists(xmlPath))
{
options.IncludeXmlComments(xmlPath);
}

var domainXmlPath = Path.Combine(AppContext.BaseDirectory, "ProjectApp.Domain.xml");
if (File.Exists(domainXmlPath))
{
options.IncludeXmlComments(domainXmlPath);
}
});

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

if (!app.Environment.IsDevelopment())
{
app.UseHttpsRedirection();
}
app.UseCors();
app.MapControllers();
app.MapDefaultEndpoints();

app.Run();
23 changes: 23 additions & 0 deletions ProjectApp.Api/ProjectApp.Api.csproj
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>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aspire.StackExchange.Redis.DistributedCaching" Version="9.5.2" />
<PackageReference Include="Bogus" Version="35.6.5" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.24" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.1.4" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ProjectApp.Domain\ProjectApp.Domain.csproj" />
<ProjectReference Include="..\ProjectApp.ServiceDefaults\ProjectApp.ServiceDefaults.csproj" />
</ItemGroup>

</Project>
41 changes: 41 additions & 0 deletions ProjectApp.Api/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:46825",
"sslPort": 44333
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5179",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7170;http://localhost:5179",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using ProjectApp.Domain.Entities;

namespace ProjectApp.Api.Services.VehicleGeneratorService;

/// <summary>
/// Интерфейс сервиса получения характеристик машины
/// </summary>
public interface IVehicleGeneratorService
{
/// <summary>
/// Запрашивает машину по ID
/// </summary>
public Task<Vehicle> FetchByIdAsync(int id, CancellationToken cancellationToken = default);
}
41 changes: 41 additions & 0 deletions ProjectApp.Api/Services/VehicleGeneratorService/VehicleFaker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Bogus;
using ProjectApp.Domain.Entities;

namespace ProjectApp.Api.Services.VehicleGeneratorService;

public class VehicleFaker
{
private readonly Faker<Vehicle> _faker;

public VehicleFaker()
{
var fuelTypes = new[] { "Бензин", "Дизель", "Электро", "Гибрид", "Газ" };
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вынести это и ниже по коду справочники в private static readonly поля


_faker = new Faker<Vehicle>("ru")
.RuleFor(v => v.Id, f => f.IndexFaker + 1)
.RuleFor(v => v.Brand, f => f.PickRandom(
"Toyota", "BMW", "Mercedes", "Volkswagen", "Hyundai",
"Ford", "Kia", "Audi", "Nissan", "Renault"))
.RuleFor(v => v.Model, (f, v) => f.PickRandom(
"Comfort", "Sport", "Elite", "Plus", "Pro",
"Active", "Max", "Base", "Premium", "Line"))
.RuleFor(v => v.RegistrationNumber, f =>
{
var letters = "АВЕКМНОРСТУХ";
var letter1 = letters[f.Random.Int(0, letters.Length - 1)];
var digits = f.Random.Int(100, 999);
var letter2 = letters[f.Random.Int(0, letters.Length - 1)];
var letter3 = letters[f.Random.Int(0, letters.Length - 1)];
var region = f.Random.Int(10, 199);
return $"{letter1}{digits}{letter2}{letter3} {region}";
})
.RuleFor(v => v.OwnerName, f => f.Name.FullName())
.RuleFor(v => v.Year, f => f.Random.Int(1984, 2026))
.RuleFor(v => v.EngineVolume, f => Math.Round(f.Random.Decimal(0.8m, 6.0m), 1))
.RuleFor(v => v.Mileage, f => f.Random.Int(0, 500000))
.RuleFor(v => v.FuelType, f => f.PickRandom(fuelTypes))
.RuleFor(v => v.Price, f => Math.Round(f.Random.Decimal(100000m, 10000000m), 0));
}

public Vehicle Generate() => _faker.Generate();
}
Loading