distributed-computing/tasks/mutriskov-ds/lab_3/worker-1/Program.cs

165 lines
3.9 KiB
C#
Raw Normal View History

2024-01-07 21:16:34 +04:00
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($"<22><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> {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($"<22><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> {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($"<22><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> {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($"<22><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> {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<Skill> Skills { get; set; }
static Storage()
{
var client = new HttpClient();
string Url = "http://worker-2:80/Professions/";
Professions = client.GetFromJsonAsync<Profession[]>(Url).Result;
Skills = new List<Skill>()
{
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 }
};
}
}