80 lines
1.8 KiB
C#
80 lines
1.8 KiB
C#
|
using Microsoft.OpenApi.Models;
|
||
|
|
||
|
var builder = WebApplication.CreateBuilder(args);
|
||
|
|
||
|
// Add Swagger services
|
||
|
builder.Services.AddEndpointsApiExplorer();
|
||
|
builder.Services.AddSwaggerGen(c =>
|
||
|
{
|
||
|
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
|
||
|
});
|
||
|
|
||
|
var app = builder.Build();
|
||
|
|
||
|
// Configure the HTTP request pipeline.
|
||
|
|
||
|
app.UseSwagger();
|
||
|
app.UseSwaggerUI(c =>
|
||
|
{
|
||
|
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
|
||
|
});
|
||
|
|
||
|
|
||
|
List<Medicine> medicines = new List<Medicine>();
|
||
|
|
||
|
app.MapGet("/medicines", () => medicines);
|
||
|
|
||
|
app.MapGet("/medicine/{id}", (Guid id) =>
|
||
|
{
|
||
|
var medicine = medicines.FirstOrDefault(m => m.Id == id);
|
||
|
return medicine is null ? Results.NotFound() : Results.Ok(medicine);
|
||
|
});
|
||
|
|
||
|
app.MapPost("/medicine", (Medicine medicine) =>
|
||
|
{
|
||
|
medicines.Add(medicine);
|
||
|
return Results.Created($"/medicine/{medicine.Id}", medicine);
|
||
|
});
|
||
|
|
||
|
app.MapPut("/medicine/{id}", (Guid id, Medicine inputMedicine) =>
|
||
|
{
|
||
|
var medicine = medicines.FirstOrDefault(m => m.Id == id);
|
||
|
|
||
|
if (medicine is null) return Results.NotFound();
|
||
|
|
||
|
medicine.Name = inputMedicine.Name;
|
||
|
medicine.Dose = inputMedicine.Dose;
|
||
|
medicine.ExpirationDate = inputMedicine.ExpirationDate;
|
||
|
|
||
|
return Results.NoContent();
|
||
|
});
|
||
|
|
||
|
app.MapDelete("/medicine/{id}", (Guid id) =>
|
||
|
{
|
||
|
var medicine = medicines.FirstOrDefault(m => m.Id == id);
|
||
|
|
||
|
if (medicine is null) return Results.NotFound();
|
||
|
|
||
|
medicines.Remove(medicine);
|
||
|
|
||
|
return Results.Ok(medicine);
|
||
|
});
|
||
|
|
||
|
app.MapGet("/Requests/", async () =>
|
||
|
{
|
||
|
var httpClient = new HttpClient();
|
||
|
var secondWorkerResponse = await httpClient.GetStringAsync("http://worker-1:8085/");
|
||
|
|
||
|
return secondWorkerResponse;
|
||
|
});
|
||
|
|
||
|
app.Run();
|
||
|
|
||
|
|
||
|
public class Medicine
|
||
|
{
|
||
|
public Guid Id { get; set; }
|
||
|
public string Name { get; set; }
|
||
|
public int Dose { get; set; }
|
||
|
public DateTime ExpirationDate { get; set; }
|
||
|
}
|