forked from zangassis/contact-hub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
52 lines (40 loc) · 1.64 KB
/
Program.cs
File metadata and controls
52 lines (40 loc) · 1.64 KB
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
using Data;
using Models;
using Services;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<ContactDBContext>();
builder.Services.AddScoped<IContactRepository, ContactRepository>();
builder.Services.AddScoped<IContactService, ContactService>();
var app = builder.Build();
app.MapGet("v1/contacts", async (IContactService service) =>
{
var allContacts = await service.FindAllContactsAsync();
return allContacts.Any() ? Results.Ok(allContacts) : Results.NotFound();
}).Produces<Contact>();
app.MapGet("v1/contacts/{id}", async (IContactService service, Guid id) =>
{
var existingContact = await service.FindContactByIdAsync(id);
return existingContact is not null ? Results.Ok(existingContact) : Results.NotFound();
}).Produces<Contact>();
app.MapPost("v1/contacts", async (IContactService service, Contact contact) =>
{
var createdId = await service.CreateContactAsync(contact);
return Results.Created($"/v1/contacts/{createdId}", createdId);
}).Produces<Contact>();
app.MapPut("v1/contacts", async (IContactService service, Contact contact) =>
{
var existingContact = await service.FindContactByIdAsync(contact.Id);
if (existingContact is null)
return Results.NotFound();
await service.UpdateContactAsync(contact.Id, existingContact);
return Results.Ok("Contact updated successfully");
});
app.MapDelete("v1/contacts/{id}", async (IContactService service, Guid id) =>
{
var existingContact = await service.FindContactByIdAsync(id);
if (existingContact is null)
return Results.NotFound();
await service.DeleteContactAsync(id);
return Results.NoContent();
});
app.Run();