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}/hospital", GetHospitalsAsync) .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 private async Task GetHospitalsAsync(Guid id, IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService, CancellationToken cancellationToken) { return TypedResults.Ok(await repositoryWrapper.SetRepository().TableNoTracking .Where(s => s.UniversityId == id) .Select(HospitalMapper.ProjectToSDto).ToListAsync(cancellationToken)); } // GET:Get All Entity private async Task GetAllAsync([FromQuery] int page, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken) => TypedResults.Ok(await repositoryWrapper.SetRepository() .TableNoTracking .Select(UniversityMapper.ProjectToSDto).ToListAsync(cancellationToken)); // GET:Get An Entity By Id private async Task GetAsync(int id, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken) => TypedResults.Ok(await repositoryWrapper.SetRepository().GetByIdAsync(cancellationToken, id)); // POST:Add New Entity private async Task Post([FromBody] UniversitySDto dto, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken) { var ent = University.Create(dto.Name,dto.Address,dto.CityId); repositoryWrapper.SetRepository().Add(ent); await repositoryWrapper.SaveChangesAsync(cancellationToken); return TypedResults.Ok(ent); } // PUT:Update Entity private async Task Put([FromBody] UniversitySDto dto, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken) { var ent = University.Create(dto.Name,dto.Address,dto.CityId); ent.Id = dto.Id; repositoryWrapper.SetRepository().Update(ent); await repositoryWrapper.SaveChangesAsync(cancellationToken); return TypedResults.Ok(); } // DELETE:Delete Entity private async Task Delete(int id, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken) { var ent = await repositoryWrapper.SetRepository().GetByIdAsync(cancellationToken, id); repositoryWrapper.SetRepository().Delete(ent); await repositoryWrapper.SaveChangesAsync(cancellationToken); return TypedResults.Ok(); } }