var builder = WebApplication.CreateBuilder(args); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseHttpsRedirection(); app.MapGet("/Skills/", () => { return Results.Ok(Storage.Skills.ToArray()); }) .WithName("List") .WithOpenApi(); app.MapGet("/Skills/{skillId}", (Guid skillId) => { var skill = Storage.Skills.FirstOrDefault(s => s.Id == skillId); if (skill == null) { return Results.NotFound($"Не найдена профессия с идентификатором {skillId}"); } return Results.Json(new Skill() { Id = skill.Id, Name = skill.Name, Level = skill.Level, ProfessionId = skill.ProfessionId }); }) .WithName("Get") .WithOpenApi(); app.MapPost("/Skills/", (SkillRequest request) => { if (Storage.Professions.FirstOrDefault(p => p.Id == request.ProfessionId) is null) { return Results.NotFound($"Не найдена профессия с идентификатором {request.ProfessionId}"); } var skillId = Guid.NewGuid(); Storage.Skills.Add(new Skill() { Id = skillId, Name = request.Name, Level = request.Level, ProfessionId = request.ProfessionId }); return Results.Ok(skillId); }) .WithName("Create") .WithOpenApi(); app.MapPatch("/Skills/{skillId}", (Guid skillId, SkillRequest request) => { var skill = Storage.Skills.FirstOrDefault(s => s.Id == skillId); if (skill == null) { Results.NotFound($"Не найдено умение с идентификатором {skillId}"); } if (!string.IsNullOrEmpty(request.Name)) { skill.Name = request.Name; } if (!string.IsNullOrEmpty(request.Name)) { skill.Level = request.Level; } if (!string.IsNullOrEmpty(request.Name)) { skill.ProfessionId = request.ProfessionId; } return Results.Ok(skill); }) .WithName("Update") .WithOpenApi(); app.MapDelete("/Skills/{skillId}", (Guid skillId) => { var skill = Storage.Skills.FirstOrDefault(s => s.Id == skillId); if (skill == null) { Results.NotFound($"Не найдено умение с идентификатором {skillId}"); } Storage.Skills.Remove(skill); return Results.Ok(skillId); }) .WithName("Delete") .WithOpenApi(); app.Run(); public class Profession { public Guid Id { get; set; } public string Name { get; set; } } public class Skill { public Guid Id { get; set; } public string Name { get; set; } public Levels Level { get; set; } public Guid ProfessionId { get; set; } } public class SkillRequest { public string Name { get; set; } public Levels Level { get; set; } public Guid ProfessionId { get; set; } } public enum Levels { None = 0, Basic = 1, Middle = 2, Expert = 3 } public class Storage { public static Profession[] Professions { get; set; } public static List Skills { get; set; } static Storage() { var client = new HttpClient(); string Url = "http://worker-2:80/Professions/"; Professions = client.GetFromJsonAsync(Url).Result; Skills = new List() { new Skill() { Id = Guid.NewGuid(), Name = "Knowledge of programming", Level = Levels.Expert, ProfessionId = Professions.FirstOrDefault(p => p.Name == "Software Developer").Id }, new Skill() { Id = Guid.NewGuid(), Name = "Stress resistance", Level = Levels.Expert, ProfessionId = Professions.FirstOrDefault(p => p.Name == "Software Developer").Id }, new Skill() { Id = Guid.NewGuid(), Name = "The ability to cook", Level = Levels.Middle, ProfessionId = Professions.FirstOrDefault(p => p.Name == "Cook").Id }, new Skill() { Id = Guid.NewGuid(), Name = "Knowledge of plumbing", Level = Levels.Middle, ProfessionId = Professions.FirstOrDefault(p => p.Name == "Plumber").Id } }; } }