-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
91 lines (73 loc) · 2.17 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using Microsoft.EntityFrameworkCore;
using PetManager.Data;
using PetManager.Models;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddTransient<PetDbContext>();
builder.Services.AddSingleton<PetRepository>();
builder.Services.Configure<ConnectionString>(builder.Configuration.GetSection("ConnectionStrings"));
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapGet("/pets", (PetDbContext db) =>
{
var pets = db.Pets;
var owners = db.Owner;
foreach (var petItem in pets)
{
var owner = owners.SingleOrDefault(o => o.Id == petItem.OwnerId);
petItem.Owner = owner;
}
return Results.Ok(pets);
});
app.MapGet("/pets/viadapper", async (PetRepository db) =>
{
var pets = await db.GetAllPets();
foreach (var pet in pets)
pet.Owner = await db.GetOwner(pet.OwnerId);
return Results.Ok(pets);
});
app.MapGet("/pets/{id}", (Guid id, PetDbContext db) =>
{
var pets = db.Pets;
var pet = pets.SingleOrDefault(p => p.Id == id);
if (pet == null)
return Results.NotFound();
var owners = db.Owner;
var owner = owners.SingleOrDefault(o => o.Id == pet.OwnerId);
pet.Owner = owner;
return Results.Ok(pet);
});
app.MapPost("/pets", (PetDbContext db, Pet pet) =>
{
var pets = db.Pets;
db.Pets.Add(pet);
db.SaveChanges();
return Results.Created($"/pets/{pet.Id}", pet);
});
app.MapPut("/pets/{id}", (Guid id, Pet pet, PetDbContext db) =>
{
db.Entry(pet).State = EntityState.Modified;
db.Entry(pet.Owner).State = EntityState.Modified;
db.SaveChanges();
return Results.Ok(pet);
});
app.MapDelete("/pets/{id}", (Guid id, PetDbContext db) =>
{
var pets = db.Pets;
var petEntity = db.Pets.SingleOrDefault(p => p.Id == id);
if (petEntity == null)
return Results.NotFound();
var owners = db.Owner;
var owner = owners.SingleOrDefault(o => o.Id == petEntity.OwnerId);
owners.Remove(owner);
pets.Remove(petEntity);
db.SaveChanges();
return Results.NoContent();
});
app.UseHttpsRedirection();
app.Run();