namespace DocuMed.Api.Controllers; public class CityController : ICarterModule { public virtual void AddRoutes(IEndpointRouteBuilder app) { var group = app.NewVersionedApi("City").MapGroup($"api/city"); group.MapGet("university/{cityId}", GetAllCityUniversityAsync) .WithDisplayName("GetAllCityUniversity") .HasApiVersion(1.0); group.MapGet("", GetAllAsync) .WithDisplayName("GetAll") .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); } public virtual async Task GetAllCityUniversityAsync(Guid cityId, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken) => TypedResults.Ok(await repositoryWrapper.SetRepository().TableNoTracking .Where(c=>c.CityId==cityId) .Select(UniversityMapper.ProjectToSDto) .ToListAsync(cancellationToken)); // GET:Get All Entity public virtual async Task GetAllAsync([FromQuery] int page, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken) => TypedResults.Ok(await repositoryWrapper.SetRepository().TableNoTracking .Select(CityMapper.ProjectToSDto).ToListAsync(cancellationToken)); // GET:Get An Entity By Id public async Task GetAsync(int id, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken) => TypedResults.Ok(await repositoryWrapper.SetRepository().GetByIdAsync(cancellationToken, id)); // POST:Add New Entity public virtual async Task Post([FromBody] CitySDto dto, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken) { var ent = City.Create(dto.Name); repositoryWrapper.SetRepository().Add(ent); await repositoryWrapper.SaveChangesAsync(cancellationToken); return TypedResults.Ok(ent); } // PUT:Update Entity public virtual async Task Put([FromBody] CitySDto dto, IRepositoryWrapper repositoryWrapper, CancellationToken cancellationToken) { var ent = City.Create(dto.Name); 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(); } }