using Microsoft.IdentityModel.Tokens; namespace DocuMed.Api.Controllers; public class SectionController : ICarterModule { public virtual void AddRoutes(IEndpointRouteBuilder app) { var group = app.NewVersionedApi("Section").MapGroup($"api/section"); group.MapGet("", GetAllAsync) .WithDisplayName("GetAll") .HasApiVersion(1.0); group.MapGet("University/{universityId}", GetAllByUniversityAsync) .WithDisplayName("GetAllByUniversityId") .HasApiVersion(1.0); group.MapGet("{id}", GetAsync) .WithDisplayName("GetOne") .HasApiVersion(1.0); group.MapPost("", Post) .HasApiVersion(1.0); group.MapPut("", Put) .HasApiVersion(1.0); group.MapDelete("", Delete) .HasApiVersion(1.0); } // GET:Get All Entity public virtual async Task GetAllAsync([FromQuery] int page, IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService, CancellationToken cancellationToken) { return TypedResults.Ok(await repositoryWrapper.SetRepository
().TableNoTracking .Select(SectionMapper.ProjectToSDto).ToListAsync(cancellationToken)); } // GET:Get All Entity public virtual async Task GetAllByUniversityAsync(Guid universityId, IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService, CancellationToken cancellationToken) { return TypedResults.Ok(await repositoryWrapper.SetRepository
().TableNoTracking .Where(s => s.UniversityId == universityId) .Select(SectionMapper.ProjectToSDto).ToListAsync(cancellationToken)); } // GET:Get An Entity By Id public async Task GetAsync(Guid id, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken) => TypedResults.Ok(await repositoryWrapper.SetRepository
().GetByIdAsync(cancellationToken, id)); // POST:Add New Entity public virtual async Task Post([FromBody] SectionSDto dto, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken) { var ent = Section.Create(dto.Name,dto.Detail,dto.UniversityId); repositoryWrapper.SetRepository
().Add(ent); await repositoryWrapper.SaveChangesAsync(cancellationToken); return TypedResults.Ok(ent); } // PUT:Update Entity public virtual async Task Put([FromBody] SectionSDto dto, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken) { var ent = Section.Create(dto.Name,dto.Detail, dto.UniversityId); ent.Id = dto.Id; repositoryWrapper.SetRepository
().Update(ent); await repositoryWrapper.SaveChangesAsync(cancellationToken); return TypedResults.Ok(); } // DELETE:Delete Entity public virtual 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(); } }