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

173 lines
4.7 KiB
C#
Raw Normal View History

2023-12-05 00:02:10 +04:00
using Microsoft.AspNetCore.Mvc;
using System.Text;
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("/Employees/", () =>
{
var result = Storage.Employees.Select(r => new worker_2.Models.Employee.GetList.EmployeeResult()
{
CompanyId = r.CompanyId,
FullName = r.FullName,
BirthDate = r.BirthDate,
EmployeeId = r.EmployeeId,
Position = r.Position,
}).ToArray();
return Results.Ok(result);
})
.WithName("List")
.WithOpenApi();
app.MapGet("/Employees/{employeeId}", (Guid employeeId) =>
{
var employee = Storage.Employees.FirstOrDefault(r => r.EmployeeId == employeeId);
if (employee is null)
{
return Results.NotFound($"Не найден работник {employeeId}");
}
return Results.Json(new worker_2.Models.Employee.Get.EmployeeResult()
{
CompanyId = employee.CompanyId,
FullName = employee.FullName,
BirthDate = employee.BirthDate,
EmployeeId = employee.EmployeeId,
Position = employee.Position,
});
})
.WithName("Get")
.WithOpenApi();
app.MapPost("/Employees/", ([FromBody] worker_2.Models.Employee.Create.EmployeeForm request) =>
{
if (Storage.Companies.FirstOrDefault(x => x.CompanyId == request.CompanyId) is null)
{
return Results.NotFound($"Не найдена компания {request.CompanyId}");
}
Guid employeeId = Guid.NewGuid();
Storage.Employees.Add(new EmployeeDal()
{
EmployeeId = employeeId,
BirthDate = request.BirthDate,
CompanyId = request.CompanyId,
FullName = request.FullName,
Position = request.Position,
});
return Results.Ok(employeeId);
})
.WithName("Create")
.WithOpenApi();
app.MapPatch("/Employees/{employeeId}", (Guid employeeId, [FromBody] worker_2.Models.Employee.Create.EmployeeForm request) =>
{
var employee = Storage.Employees.FirstOrDefault(r => r.EmployeeId == employeeId);
if (employee is null)
{
return Results.NotFound($"Не найден работник {employeeId}");
}
if(Storage.Companies.FirstOrDefault(x => x.CompanyId == request.CompanyId) is null)
{
return Results.NotFound($"Не найдена компания {request.CompanyId}");
}
if (!string.IsNullOrEmpty(request.FullName))
{
employee.FullName = request.FullName;
}
if (!string.IsNullOrEmpty(request.Position))
{
employee.Position = request.Position;
}
if (request.BirthDate.HasValue)
{
employee.BirthDate = request.BirthDate.Value;
}
return Results.Ok(employee);
})
.WithName("Update")
.WithOpenApi();
app.MapDelete("/Employees/{employeeId}", (Guid employeeId) =>
{
var employee = Storage.Employees.FirstOrDefault(r => r.EmployeeId == employeeId);
if (employee is null)
{
return Results.NotFound($"Не найден работник {employeeId}");
}
Storage.Employees.Remove(employee);
return Results.Ok(employeeId);
})
.WithName("Delete")
.WithOpenApi();
app.Run();
public class EmployeeDal
{
public Guid EmployeeId { get; set; }
public DateTime? BirthDate { get; set; }
public Guid CompanyId { get; set; }
public string FullName { get; set; }
public string Position { get; set; }
}
public class CompanyDal
{
public Guid CompanyId { get; set; }
public string Name { get; set; }
public string FieldOfActivity { get; set; }
public string Location { get; set; }
}
public class Storage
{
private static Random rand = new Random();
public static CompanyDal[] Companies { get; set; }
public static List<EmployeeDal> Employees { get; set; }
static Storage()
{
var client = new HttpClient();
string reqUrl = $"http://worker-1:8080/Companies/";
Companies = client.GetFromJsonAsync<CompanyDal[]>(reqUrl).Result;//для простоты блокируем
Employees = new List<EmployeeDal>()
{
new EmployeeDal() { EmployeeId = Guid.NewGuid(), BirthDate = DateTime.Today.AddDays(-100), CompanyId = Companies[rand.Next(Companies.Length)].CompanyId, FullName = "Миронов Евгений Олегович", Position = "Ведущий разработчик" },
new EmployeeDal() { EmployeeId = Guid.NewGuid(), BirthDate = DateTime.Today.AddDays(-150), CompanyId = Companies[rand.Next(Companies.Length)].CompanyId, FullName = "Сидоров Петр Иванович", Position = "Уборщик" }
};
}
}