Api-PWA/DocuMed.Api/Controllers/UniversityController.cs

81 lines
3.2 KiB
C#

using DocuMed.Domain.Entities.Hospitals;
namespace DocuMed.Api.Controllers;
public class UniversityController : ICarterModule
{
public virtual void AddRoutes(IEndpointRouteBuilder app)
{
var group = app.NewVersionedApi("University")
.MapGroup($"api/university");
group.MapGet("", GetAllAsync)
.WithDisplayName("Get All")
.HasApiVersion(1.0);
group.MapGet("{id}/section", GetAllByUniversityAsync)
.WithDisplayName("Get All Sections")
.HasApiVersion(1.0);
group.MapGet("{id}", GetAsync)
.WithDisplayName("Get One")
.HasApiVersion(1.0);
group.MapPost("", Post)
.HasApiVersion(1.0);
group.MapPut("", Put)
.HasApiVersion(1.0);
group.MapDelete("", Delete)
.HasApiVersion(1.0);
}
// GET:Get All Sections
public virtual async Task<IResult> GetAllByUniversityAsync(Guid id, IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService, CancellationToken cancellationToken)
{
return TypedResults.Ok(await repositoryWrapper.SetRepository<Section>().TableNoTracking
.Where(s => s.HospitalId == id)
.Select(SectionMapper.ProjectToSDto).ToListAsync(cancellationToken));
}
// GET:Get All Entity
public virtual async Task<IResult> GetAllAsync([FromQuery] int page, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken)
=> TypedResults.Ok(await repositoryWrapper.SetRepository<University>()
.TableNoTracking
.Select(UniversityMapper.ProjectToSDto).ToListAsync(cancellationToken));
// GET:Get An Entity By Id
public async Task<IResult> GetAsync(int id, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken)
=> TypedResults.Ok(await repositoryWrapper.SetRepository<University>().GetByIdAsync(cancellationToken, id));
// POST:Add New Entity
public virtual async Task<IResult> Post([FromBody] UniversitySDto dto, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken)
{
var ent = University.Create(dto.Name,dto.Address,dto.CityId);
repositoryWrapper.SetRepository<University>().Add(ent);
await repositoryWrapper.SaveChangesAsync(cancellationToken);
return TypedResults.Ok(ent);
}
// PUT:Update Entity
public virtual async Task<IResult> Put([FromBody] UniversitySDto dto, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken)
{
var ent = University.Create(dto.Name,dto.Address,dto.CityId);
ent.Id = dto.Id;
repositoryWrapper.SetRepository<University>().Update(ent);
await repositoryWrapper.SaveChangesAsync(cancellationToken);
return TypedResults.Ok();
}
// DELETE:Delete Entity
public virtual async Task<IResult> Delete(int id, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken)
{
var ent = await repositoryWrapper.SetRepository<University>().GetByIdAsync(cancellationToken, id);
repositoryWrapper.SetRepository<University>().Delete(ent);
await repositoryWrapper.SaveChangesAsync(cancellationToken);
return TypedResults.Ok();
}
}