namespace NetinaShop.Api.WebFramework.Bases; public class CrudEndpoint where TEntity : ApiEntity, new() { private readonly string _endpointName; public CrudEndpoint(string endpointName) { _endpointName = endpointName; } public virtual void AddRoutes(IEndpointRouteBuilder app) { var group = app.NewVersionedApi(_endpointName).MapGroup($"api/{_endpointName}"); group.MapGet("", GetAllAsync) .WithDisplayName("GetAll") .HasApiVersion(1.0); group.MapGet("{id}", GetAsync) .WithName("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(ISender sender , CancellationToken cancellationToken) { var res = sender.Send(Activator.CreateInstance(),cancellationToken); return TypedResults.Ok(res); } // GET:Get An Entity By Id public async Task GetAsync(Guid id, ISender sender, CancellationToken cancellationToken) => TypedResults.Ok(sender.Send(Activator.CreateInstance())); // POST:Create Entity public virtual async Task Post([FromBody] TCreateCommand ent , ISender mediator , CancellationToken cancellationToken) { return TypedResults.Ok(await mediator.Send(ent, cancellationToken)); } // PUT:Update Entity public virtual async Task Put([FromBody] TEntity ent , IRepositoryWrapper _repositoryWrapper, CancellationToken cancellationToken) { _repositoryWrapper.SetRepository().Update(ent); await _repositoryWrapper.SaveChangesAsync(cancellationToken); return TypedResults.Ok(); } // DELETE:Delete Entity public virtual async Task Delete(Guid id,IRepositoryWrapper _repositoryWrapper, CancellationToken cancellationToken) { var ent = await _repositoryWrapper.SetRepository().GetByIdAsync(cancellationToken, id); _repositoryWrapper.SetRepository().Delete(ent); await _repositoryWrapper.SaveChangesAsync(cancellationToken); return TypedResults.Ok(); } } [ApiController] //[AllowAnonymous] [ApiResultFilter] [Route("api/v{version:apiVersion}/[controller]")] // api/v1/[controller] public class BaseController : ControllerBase { //public UserRepository UserRepository { get; set; } => property injection public bool UserIsAutheticated => HttpContext.User.Identity.IsAuthenticated; } [Authorize(AuthenticationSchemes = "Bearer")] public class CrudController : BaseController where TDto : BaseDto, new() where TEntity : ApiEntity, new() { protected readonly IRepositoryWrapper _repositoryWrapper; public CrudController(IRepositoryWrapper repositoryWrapper) { _repositoryWrapper = repositoryWrapper; } // GET:Get All Entity [HttpGet] public virtual async Task GetAllAsync(CancellationToken cancellationToken) { var projectTo = typeof(TDto).BaseType?.GetProperty("ProjectToDto")?.GetValue(null, null); if (projectTo != null) { var exprss = projectTo as Expression>; var entites = await _repositoryWrapper .SetRepository() .TableNoTracking .Select(exprss) .ToListAsync(cancellationToken); return Ok(entites); } throw new BaseApiException("ProjectTo Not Found"); } // GET:Get An Entity By Id [HttpGet("{id}")] public virtual async Task GetAsync(string id, CancellationToken cancellationToken) { var ent = await _repositoryWrapper.SetRepository().GetByIdAsync(cancellationToken, id); var dto = ent.Adapt(); return Ok(dto); } // POST:Add New Entity [HttpPost] public virtual async Task PostOrginal([FromBody] TEntity ent, CancellationToken cancellationToken) { _repositoryWrapper.SetRepository().Add(ent); await _repositoryWrapper.SaveChangesAsync(cancellationToken); return Ok(ent); } // POST:Add New Entity By Dto [HttpPost("Dto")] public async Task PostDto([FromBody] TDto dto, CancellationToken cancellationToken) { _repositoryWrapper .SetRepository() .Add(dto.ToEntity()); await _repositoryWrapper.SaveChangesAsync(cancellationToken); return Ok(); } // PUT:Update Entity [HttpPut] public virtual async Task Put([FromBody] TEntity ent, CancellationToken cancellationToken) { _repositoryWrapper .SetRepository() .Update(ent); await _repositoryWrapper.SaveChangesAsync(cancellationToken); return Ok(); } // DELETE:Delete Entity [HttpDelete] [Route("{id:int}")] public virtual async Task Delete(int id, CancellationToken cancellationToken) { var ent = await _repositoryWrapper .SetRepository() .GetByIdAsync(cancellationToken, id); _repositoryWrapper.SetRepository().Delete(ent); await _repositoryWrapper.SaveChangesAsync(cancellationToken); return Ok(); } } [Authorize(AuthenticationSchemes = "Bearer")] public class CrudController : BaseController where TEntity : ApiEntity, new() { protected readonly IRepositoryWrapper _repositoryWrapper; public CrudController(IRepositoryWrapper repositoryWrapper) { _repositoryWrapper = repositoryWrapper; } // GET:Get All Entity [HttpGet] public virtual async Task GetAllAsync() { return Ok(await _repositoryWrapper.SetRepository().TableNoTracking.ToListAsync()); } // GET:Get An Entity By Id [HttpGet("{id}")] public async Task GetAsync(int id, CancellationToken cancellationToken) { var ent = await _repositoryWrapper.SetRepository().GetByIdAsync(cancellationToken, id); return Ok(ent); } // POST:Add New Entity [HttpPost] public virtual async Task Post([FromBody] TEntity ent, CancellationToken cancellationToken) { _repositoryWrapper.SetRepository().Add(ent); await _repositoryWrapper.SaveChangesAsync(cancellationToken); return Ok(ent); } // PUT:Update Entity [HttpPut] public virtual async Task Put([FromBody] TEntity ent, CancellationToken cancellationToken) { _repositoryWrapper.SetRepository().Update(ent); await _repositoryWrapper.SaveChangesAsync(cancellationToken); return Ok(); } // DELETE:Delete Entity [HttpDelete("{id}")] public virtual async Task Delete(int id, CancellationToken cancellationToken) { var ent = await _repositoryWrapper.SetRepository().GetByIdAsync(cancellationToken, id); _repositoryWrapper.SetRepository().Delete(ent); await _repositoryWrapper.SaveChangesAsync(cancellationToken); return Ok(); } }