Initial commit
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
using AutoMapper;
|
||||
using FastEndpoints;
|
||||
using MetaCourse.Api.Data;
|
||||
using MetaCourse.Api.DTOs.Courses;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace MetaCourse.Api.Endpoints.Courses;
|
||||
|
||||
public class GetCoursesRequest
|
||||
{
|
||||
public string? Search { get; set; }
|
||||
}
|
||||
|
||||
public class GetCoursesEndpoint(AppDbContext db, AutoMapper.IMapper mapper) : Endpoint<GetCoursesRequest, List<GetCourseDto>>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("api/courses");
|
||||
AllowAnonymous();
|
||||
Summary(s =>
|
||||
{
|
||||
s.Summary = "Liste les cours publiés";
|
||||
s.Description = "Retourne tous les cours avec le statut Publié, avec recherche optionnelle.";
|
||||
});
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(GetCoursesRequest req, CancellationToken ct)
|
||||
{
|
||||
var query = db.Courses
|
||||
.AsNoTracking()
|
||||
.Include(c => c.Creator)
|
||||
.Include(c => c.Topics)
|
||||
.Where(c => c.Status == Entities.CourseStatus.Published);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(req.Search))
|
||||
{
|
||||
var search = req.Search.ToLower();
|
||||
query = query.Where(c =>
|
||||
c.Title.ToLower().Contains(search) ||
|
||||
c.Description.ToLower().Contains(search) ||
|
||||
c.Creator.Name.ToLower().Contains(search));
|
||||
}
|
||||
|
||||
var courses = await query.OrderByDescending(c => c.CreatedAt).ToListAsync(ct);
|
||||
await SendOkAsync(mapper.Map<List<GetCourseDto>>(courses), ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user