110 lines
2.4 KiB
C#
110 lines
2.4 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
|
|
List<Profession> professions = new()
|
|
{
|
|
new Profession() { Id = Guid.NewGuid(), Name = "Software Developer" },
|
|
new Profession() { Id = Guid.NewGuid(), Name = "Plumber" },
|
|
new Profession() { Id = Guid.NewGuid(), Name = "Cook" }
|
|
};
|
|
|
|
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("/Professions/", () =>
|
|
{
|
|
return Results.Ok(professions.ToArray());
|
|
})
|
|
.WithName("List")
|
|
.WithOpenApi();
|
|
|
|
app.MapGet("/Professions/{professionId}", (Guid professionId) =>
|
|
{
|
|
var proffesion = professions.FirstOrDefault(p => p.Id == professionId);
|
|
|
|
if (proffesion is null)
|
|
{
|
|
return Results.NotFound($"Íå íàéäåíà ïðîôåññèÿ ñ èäåíòèôèêàòîðîì {professionId}");
|
|
}
|
|
|
|
return Results.Json(new Profession()
|
|
{
|
|
Id = proffesion.Id,
|
|
Name = proffesion.Name
|
|
});
|
|
})
|
|
.WithName("Get")
|
|
.WithOpenApi();
|
|
|
|
app.MapPost("/Professions/", (ProfessionRequest request) =>
|
|
{
|
|
Guid proffesionId = Guid.NewGuid();
|
|
professions.Add(new Profession()
|
|
{
|
|
Id = proffesionId,
|
|
Name = request.Name
|
|
});
|
|
})
|
|
.WithName("Create")
|
|
.WithOpenApi();
|
|
|
|
app.MapPatch("/Professions/{professionId}", (Guid professionId, ProfessionRequest request) =>
|
|
{
|
|
var profession = professions.FirstOrDefault(p => p.Id == professionId);
|
|
|
|
if (profession is null)
|
|
{
|
|
return Results.NotFound($"Íå íàéäåíà ïðîôåññèÿ ñ èäåíòèôèêàòîðîì {professionId}");
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(request.Name))
|
|
{
|
|
profession.Name = request.Name;
|
|
}
|
|
|
|
return Results.Ok(profession);
|
|
})
|
|
.WithName("Update")
|
|
.WithOpenApi();
|
|
|
|
app.MapDelete("/Professions/{professionId}", (Guid professionId) =>
|
|
{
|
|
var profession = professions.FirstOrDefault(p => p.Id == professionId);
|
|
|
|
if (profession is null)
|
|
{
|
|
return Results.NotFound($"Íå íàéäåíà ïðîôåññèÿ ñ èäåíòèôèêàòîðîì {professionId}");
|
|
}
|
|
|
|
professions.Remove(profession);
|
|
|
|
return Results.Ok(professionId);
|
|
})
|
|
.WithName("Delete")
|
|
.WithOpenApi();
|
|
|
|
app.Run();
|
|
|
|
|
|
public class Profession
|
|
{
|
|
public Guid Id { get; set; }
|
|
|
|
public string Name { get; set; }
|
|
}
|
|
|
|
public class ProfessionRequest
|
|
{
|
|
public string Name { get; set; }
|
|
} |