Endopoint presque fini
This commit is contained in:
34
PF3/Endpoints/SoundCategory/CreateSoundCategoryEndpoint.cs
Normal file
34
PF3/Endpoints/SoundCategory/CreateSoundCategoryEndpoint.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using FastEndpoints;
|
||||
using PF3.DTO.SoundCategory.Request;
|
||||
using PF3.DTO.SoundCategory.Response;
|
||||
using PF3.Models;
|
||||
|
||||
namespace PF3.Endpoints.SoundCategory;
|
||||
|
||||
public class CreateSoundCategoryEndpoint(PF3DbContext pf3DbContext) : Endpoint<CreateSoundCategoryDto, ReadSoundCategoryDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Post("/api/soundcategorys");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CreateSoundCategoryDto req, CancellationToken ct)
|
||||
{
|
||||
var soundCategory = new Models.SoundCategory
|
||||
{
|
||||
Name = req.Name,
|
||||
};
|
||||
|
||||
pf3DbContext.SoundsCategorys.Add(soundCategory);
|
||||
await pf3DbContext.SaveChangesAsync(ct);
|
||||
|
||||
var result = new ReadSoundCategoryDto
|
||||
{
|
||||
Id = soundCategory.Id,
|
||||
Name = soundCategory.Name
|
||||
};
|
||||
|
||||
await Send.OkAsync(result, ct);
|
||||
}
|
||||
}
|
||||
29
PF3/Endpoints/SoundCategory/DeleteSoundCategoryEndpoint.cs
Normal file
29
PF3/Endpoints/SoundCategory/DeleteSoundCategoryEndpoint.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PF3.DTO.SoundCategory.Request;
|
||||
|
||||
namespace PF3.Endpoints.SoundCategory;
|
||||
|
||||
public class DeleteSoundCategoryEndpoint(PF3DbContext pf3DbContext) : Endpoint<IdSoundCategoryDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("/api/soundcategorys/{Id}");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(IdSoundCategoryDto req, CancellationToken ct)
|
||||
{
|
||||
var soundCategory = await pf3DbContext.SoundsCategorys.FirstOrDefaultAsync(st => st.Id == req.Id, ct);
|
||||
if (soundCategory is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
pf3DbContext.SoundsCategorys.Remove(soundCategory);
|
||||
await pf3DbContext.SaveChangesAsync(ct);
|
||||
|
||||
await Send.OkAsync(ct);
|
||||
}
|
||||
}
|
||||
27
PF3/Endpoints/SoundCategory/GetAllSoundCategoryEndpoint.cs
Normal file
27
PF3/Endpoints/SoundCategory/GetAllSoundCategoryEndpoint.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PF3.DTO.SoundCategory.Response;
|
||||
|
||||
namespace PF3.Endpoints.SoundCategory;
|
||||
|
||||
public class GetAllSoundCategorysEndpoint(PF3DbContext pf3DbContext) : EndpointWithoutRequest<List<ReadSoundCategoryDto>>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/api/soundcategorys");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CancellationToken ct)
|
||||
{
|
||||
var soundCategorys = await pf3DbContext.SoundsCategorys.ToListAsync(ct);
|
||||
|
||||
var result = soundCategorys.Select(sC => new ReadSoundCategoryDto
|
||||
{
|
||||
Id = sC.Id,
|
||||
Name = sC.Name
|
||||
}).ToList();
|
||||
|
||||
await Send.OkAsync(result, ct);
|
||||
}
|
||||
}
|
||||
35
PF3/Endpoints/SoundCategory/GetSoundCategoryEndpoint.cs
Normal file
35
PF3/Endpoints/SoundCategory/GetSoundCategoryEndpoint.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PF3.DTO.SoundCategory.Request;
|
||||
using PF3.DTO.SoundCategory.Response;
|
||||
|
||||
namespace PF3.Endpoints.SoundCategory;
|
||||
|
||||
public class GetSoundCategoryEndpoint(PF3DbContext pf3DbContext) : Endpoint<IdSoundCategoryDto, ReadSoundCategoryDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/api/soundcategorys/{Id}");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(IdSoundCategoryDto req, CancellationToken ct)
|
||||
{
|
||||
var soundCategory = await pf3DbContext.SoundsCategorys
|
||||
.Where(sc => sc.Id == req.Id)
|
||||
.Select(sc => new ReadSoundCategoryDto
|
||||
{
|
||||
Id = sc.Id,
|
||||
Name = sc.Name,
|
||||
})
|
||||
.FirstOrDefaultAsync(ct);
|
||||
|
||||
if (soundCategory is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
await Send.OkAsync(soundCategory, ct);
|
||||
}
|
||||
}
|
||||
37
PF3/Endpoints/SoundCategory/UpdateSoundCategoryEndpoint.cs
Normal file
37
PF3/Endpoints/SoundCategory/UpdateSoundCategoryEndpoint.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PF3.DTO.SoundCategory.Request;
|
||||
using PF3.DTO.SoundCategory.Response;
|
||||
|
||||
namespace PF3.Endpoints.SoundCategory;
|
||||
|
||||
public class UpdateSoundCategoryEndpoint(PF3DbContext pf3DbContext) : Endpoint<UpdateSoundCategoryDto, ReadSoundCategoryDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Patch("/api/soundcategorys/{Id}/name");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UpdateSoundCategoryDto req, CancellationToken ct)
|
||||
{
|
||||
var soundCategory = await pf3DbContext.SoundsCategorys.FirstOrDefaultAsync(st => st.Id == req.Id, ct);
|
||||
if (soundCategory is null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
soundCategory.Name = req.Name;
|
||||
|
||||
await pf3DbContext.SaveChangesAsync(ct);
|
||||
|
||||
var result = new ReadSoundCategoryDto
|
||||
{
|
||||
Id = soundCategory.Id,
|
||||
Name = soundCategory.Name
|
||||
};
|
||||
|
||||
await Send.OkAsync(result, ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user