111 lines
3.5 KiB
C#
111 lines
3.5 KiB
C#
using Newtonsoft.Json;
|
|
|
|
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();
|
|
|
|
var jobs = new List<Job>();
|
|
var httpClient = new HttpClient();
|
|
app.MapGet("/getJobs/{clientId}", (string clientId) =>
|
|
{
|
|
var secondWorkerResponse = httpClient.GetStringAsync("http://worker-1:8080/getClient/" + clientId).Result;
|
|
Client client = JsonConvert.DeserializeObject<Client>(secondWorkerResponse);
|
|
if (secondWorkerResponse == null)
|
|
return Results.NotFound(new { message = "Клиент не найден" });
|
|
List<Job> clientJobs = jobs.Where(x => x.ClientId == clientId).ToList();
|
|
return Results.Json(clientJobs);
|
|
})
|
|
.WithName("GetJobs");
|
|
|
|
app.MapGet("getJob/{id}", (string id) =>
|
|
{
|
|
Job? job = jobs.FirstOrDefault(x => x.Id == id);
|
|
if (job == null)
|
|
return Results.NotFound(new { message = "Задача не найдена" });
|
|
return Results.Json(job);
|
|
})
|
|
.WithName("GetJob");
|
|
|
|
app.MapPost("createJob", (JobViewModel jobVM) =>
|
|
{
|
|
var secondWorkerResponse = httpClient.GetStringAsync("http://worker-1:8080/getClient/" + jobVM.ClientId).Result;
|
|
Client client = JsonConvert.DeserializeObject<Client>(secondWorkerResponse);
|
|
if (secondWorkerResponse == null)
|
|
return Results.NotFound(new { message = "Клиент не найден" });
|
|
Job job = new Job
|
|
{
|
|
Id = Guid.NewGuid().ToString(),
|
|
DeadLine = jobVM.DeadLine,
|
|
Description = jobVM.Description,
|
|
Client = client,
|
|
ClientId = jobVM.ClientId,
|
|
Title = jobVM.Title,
|
|
};
|
|
jobs.Add(job);
|
|
return Results.Json(job);
|
|
})
|
|
.WithName("CreateJob");
|
|
|
|
app.MapPut("updateJob/{id}", (string id, JobViewModel data) =>
|
|
{
|
|
Job? job = jobs.FirstOrDefault(x => x.Id == id);
|
|
if (job == null)
|
|
return Results.NotFound(new { message = "Задача не найдена" });
|
|
var secondWorkerResponse = httpClient.GetStringAsync("http://worker-1:8080/getClient/" + data.ClientId).Result;
|
|
Client client = JsonConvert.DeserializeObject<Client>(secondWorkerResponse);
|
|
if (secondWorkerResponse == null)
|
|
return Results.NotFound(new { message = "Клиент не найден" });
|
|
job.Title = data.Title;
|
|
job.DeadLine = data.DeadLine;
|
|
job.ClientId = data.ClientId;
|
|
job.Client = client;
|
|
job.Description = data.Description;
|
|
return Results.Json(job);
|
|
})
|
|
.WithName("UpdateJob");
|
|
|
|
app.MapDelete("deleteJob/{id}", (string id) =>
|
|
{
|
|
Job? job = jobs.FirstOrDefault(x => x.Id == id);
|
|
if (job == null)
|
|
return Results.NotFound(new { message = "Задача не найдена" });
|
|
jobs.Remove(job);
|
|
return Results.Ok();
|
|
})
|
|
.WithName("DeleteJob");
|
|
|
|
app.Run();
|
|
|
|
public class Client
|
|
{
|
|
public string Id { get; set; }
|
|
public string Name { get; set; }
|
|
public string Address { get; set; }
|
|
}
|
|
public class Job
|
|
{
|
|
public string Id { get; set; }
|
|
public string Title { get; set; }
|
|
public string Description { get; set; }
|
|
public DateTime DeadLine { get; set; }
|
|
public string ClientId { get; set; }
|
|
public Client Client { get; set; }
|
|
}
|
|
public class JobViewModel
|
|
{
|
|
public string Title { get; set; }
|
|
public string Description { get; set; }
|
|
public DateTime DeadLine { get; set; }
|
|
public string ClientId { get; set; }
|
|
} |