First Commit 09/10

# Conflicts:
#	PyroFetes/PyroFetes.csproj
This commit is contained in:
2025-10-09 17:12:17 +02:00
parent 0e19ef1ab7
commit 4e64112a31
49 changed files with 908 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
using FastEndpoints;
using PyroFetes.DTO.Contact.Request;
using PyroFetes.DTO.Contact.Response;
namespace PyroFetes.Endpoints.Contact;
public class CreateContactEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint<CreateContactDto, GetContactDto>
{
public override void Configure()
{
Post("/api/contacts");
AllowAnonymous();
}
public override async Task HandleAsync(CreateContactDto req, CancellationToken ct)
{
Models.Contact contact = new()
{
LastName = req.LastName,
FirstName = req.FirstName,
PhoneNumber = req.PhoneNumber,
Email = req.Email,
Address = req.Address,
Role = req.Role,
};
pyroFetesDbContext.Add(contact);
await pyroFetesDbContext.SaveChangesAsync(ct);
GetContactDto response = new()
{
LastName = contact.LastName,
FirstName = contact.FirstName,
PhoneNumber = contact.PhoneNumber,
Email = contact.Email,
Address = contact.Address,
Role = contact.Role,
};
await Send.OkAsync(response, ct);
}
}

View File

@@ -0,0 +1,30 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Contact.Request;
using PyroFetes.DTO.Contact.Response;
namespace PyroFetes.Endpoints.Contact;
public class DeleteContactEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint <GetContactRequest, GetContactDto>
{
public override void Configure()
{
Delete ("/api/Contacts/{@Id}", x => new { x.Id });
AllowAnonymous();
}
public override async Task HandleAsync(GetContactRequest req, CancellationToken ct)
{
Models.Contact? databaseContact = await pyroFetesDbContext.Contacts.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);
if (databaseContact == null)
{
await Send.NotFoundAsync(ct);
return;
}
pyroFetesDbContext.Contacts.Remove(databaseContact);
await pyroFetesDbContext.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}

View File

@@ -0,0 +1,30 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Contact.Response;
namespace PyroFetes.Endpoints.Contact;
public class GetAllContactxuest(PyroFetesDbContext pyroFetesDbContext) : EndpointWithoutRequest<List<GetContactDto>>
{
public override void Configure()
{
Get ("/api/contacts");
AllowAnonymous();
}
public override async Task HandleAsync(CancellationToken ct)
{
List<GetContactDto> contacts = await pyroFetesDbContext.Contacts.Select(x => new GetContactDto()
{
Id = x.Id,
LastName = x.LastName,
FirstName = x.FirstName,
PhoneNumber = x.PhoneNumber,
Email = x.Email,
Address = x.Address,
Role = x.Role,
}).ToListAsync(ct);
await Send.OkAsync(contacts, ct);
}
}

View File

@@ -0,0 +1,39 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Contact.Request;
using PyroFetes.DTO.Contact.Response;
namespace PyroFetes.Endpoints.Contact;
public class GetContactEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint <GetContactRequest, GetContactDto>
{
public override void Configure()
{
Get ("/api/contacts/{@Id}", x => new { x.Id });
AllowAnonymous();
}
public override async Task HandleAsync(GetContactRequest database, CancellationToken ct)
{
Models.Contact? databaseContact = await pyroFetesDbContext.Contacts.SingleOrDefaultAsync(x => x.Id == database.Id, cancellationToken: ct);
if (databaseContact == null)
{
await Send.NotFoundAsync(ct);
return;
}
GetContactDto dto = new()
{
Id = databaseContact.Id,
LastName = databaseContact.LastName,
FirstName = databaseContact.FirstName,
PhoneNumber = databaseContact.PhoneNumber,
Email = databaseContact.Email,
Address = databaseContact.Address,
Role = databaseContact.Role,
};
await Send.OkAsync(dto, ct);
}
}

View File

@@ -0,0 +1,49 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Contact.Request;
using PyroFetes.DTO.Contact.Response;
namespace PyroFetes.Endpoints.Contact;
public class UpdateContactRequest(PyroFetesDbContext pyroFetesDbContext) : Endpoint <UpdateContactDto, GetContactDto>
{
public override void Configure()
{
Put ("/api/contacts/{@Id}", x => new { x.Id });
AllowAnonymous();
}
public override async Task HandleAsync(UpdateContactDto req, CancellationToken ct)
{
Models.Contact? databaseContact = await pyroFetesDbContext.Contacts.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);
if (databaseContact == null)
{
await Send.NotFoundAsync(ct);
return;
}
else
{
databaseContact.LastName = req.LastName;
databaseContact.FirstName = req.FirstName;
databaseContact.PhoneNumber = req.PhoneNumber;
databaseContact.Email = req.Email;
databaseContact.Address = req.Address;
databaseContact.Role = req.Role;
}
await pyroFetesDbContext.SaveChangesAsync(ct);
GetContactDto dto = new()
{
Id = databaseContact.Id,
LastName = req.LastName,
FirstName = req.FirstName,
PhoneNumber = req.PhoneNumber,
Email = req.Email,
Address = req.Address,
Role = req.Role,
};
await Send.OkAsync(dto, ct);
}
}