distributed-computing/labs/lab_3/example_1.cs

77 lines
1.9 KiB
C#
Raw Normal View History

2023-10-25 23:53:03 +04:00
List<TaskEntity> tasks = new()
{
new TaskEntity() { Uuid= Guid.NewGuid(), Subject = "Сдать третью лабу", IsCompleted = false },
};
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
// Массив сущностей.
app.MapGet("/", () =>
{
return tasks.Select(t => new TaskEntityDto()
{
Uuid = t.Uuid,
Subject = t.Subject,
IsCompleted = t.IsCompleted,
});
});
// Подробности сущности.
app.MapGet("/{uuid}", (Guid uuid) =>
{
var task = tasks.FirstOrDefault(t => t.Uuid == uuid);
if (task == null)
return Results.NotFound();
return Results.Json(new TaskEntityDto()
{
Uuid = task.Uuid,
Subject = task.Subject,
IsCompleted = task.IsCompleted,
});
});
// Создание сущности.
app.MapPost("/", () => { throw new NotImplementedException(); });
// Изменение сущности.
app.MapPut("/{uuid}", () => { throw new NotImplementedException(); });
// Удаление сущности.
app.MapPost("/", () => { throw new NotImplementedException(); });
app.Run();
/// <summary>
/// Сама сущность.
/// </summary>
public class TaskEntity
{
public Guid Uuid { get; set; }
public string Subject { get; set; } = string.Empty;
public bool IsCompleted { get; set; }
}
/// <summary>
/// DTO-класс для описания сущности в API.
/// </summary>
public class TaskEntityDto : TaskEntity { }
/// <summary>
/// Запрос на создание сущности.
/// </summary>
public class TaskCreateRequest
{
public string Subject { get; set; } = string.Empty;
public bool IsCompleted { get; set; }
}
/// <summary>
/// Запрос на изменение сущности.
/// </summary>
public class TaskEditRequest
{
public string Subject { get; set; } = string.Empty;
public bool IsCompleted { get; set; }
}