distributed-computing/tasks/mironov-eo/lab_3/worker-1/Program.cs

131 lines
3.3 KiB
C#
Raw Normal View History

2023-12-05 00:02:10 +04:00
using Microsoft.AspNetCore.Mvc;
List<CompanyDal> companies = new()
{
new CompanyDal() { CompanyId = Guid.NewGuid(), Name = "Microsoft", FieldOfActivity = "Информационные технологии", Location = "США"},
new CompanyDal() { CompanyId = Guid.NewGuid(), Name = "Додо пицца", FieldOfActivity = "Питание", Location = "Россия"},
new CompanyDal() { CompanyId = Guid.NewGuid(), Name = "Renault", FieldOfActivity = "Автомобилестроение", Location = "Франция"},
};
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("/Companies/", () =>
{
var result = companies.Select(r => new worker_1.Models.Company.GetList.CompanyResult()
{
CompanyId = r.CompanyId,
Name = r.Name,
FieldOfActivity = r.FieldOfActivity,
Location = r.Location,
}).ToArray();
return Results.Ok(result);
})
.WithName("List")
.WithOpenApi();
app.MapGet("/Companies/{companyId}", (Guid companyId) =>
{
var company = companies.FirstOrDefault(r => r.CompanyId == companyId);
if (company is null)
{
return Results.NotFound($"Не найдена компания {companyId}");
}
return Results.Json(new worker_1.Models.Company.Get.CompanyResult()
{
CompanyId = company.CompanyId,
Name = company.Name,
FieldOfActivity = company.FieldOfActivity,
Location = company.Location,
});
})
.WithName("Get")
.WithOpenApi();
app.MapPost("/Companies/", ([FromBody] worker_1.Models.Company.Create.CompanyForm request) =>
{
Guid companyId = Guid.NewGuid();
companies.Add(new CompanyDal()
{
CompanyId = companyId,
FieldOfActivity = request.FieldOfActivity,
Location = request.Location,
Name = request.Name,
});
return Results.Ok(companyId);
})
.WithName("Create")
.WithOpenApi();
app.MapPatch("/Companies/{companyId}", (Guid companyId, [FromBody] worker_1.Models.Company.Create.CompanyForm request) =>
{
var company = companies.FirstOrDefault(r => r.CompanyId == companyId);
if (company is null)
{
return Results.NotFound($"Не найдена компания {companyId}");
}
if (!string.IsNullOrEmpty(request.Name))
{
company.Name = request.Name;
}
if (!string.IsNullOrEmpty(request.Location))
{
company.Location = request.Location;
}
if (!string.IsNullOrEmpty(request.FieldOfActivity))
{
company.FieldOfActivity = request.FieldOfActivity;
}
return Results.Ok(company);
})
.WithName("Update")
.WithOpenApi();
app.MapDelete("/Companies/{companyId}", (Guid companyId) =>
{
var company = companies.FirstOrDefault(r => r.CompanyId == companyId);
if (company is null)
{
return Results.NotFound($"Не найдена компания {companyId}");
}
companies.Remove(company);
return Results.Ok(companyId);
})
.WithName("Delete")
.WithOpenApi();
app.Run();
public class CompanyDal
{
public Guid CompanyId { get; set; }
public string Name { get; set; }
public string FieldOfActivity { get; set; }
public string Location { get; set; }
}