[Route("api/[controller]/[action]")][ApiController]publicclassProductsController:ControllerBase{privatereadonlyIProductCommandRepository_command;privatereadonlyIProductQueryRepository_query;publicProductsController(IProductCommandRepositorycommand,IProductQueryRepositoryquery){_command=command;_query=query;}// Async Queries[HttpGet]publicasyncTask<IActionResult>GetProductsAsync(){varresult=await_query.ReadAllAsync(noTracking:true);returnOk(result);}[HttpGet]publicasyncTask<IActionResult>GetProductsWithCategoryAsync(){varresult=await_query.ReadAllAsync(noTracking:true,includeProperties: x =>x.Category);returnOk(result);}[HttpGet]publicasyncTask<IActionResult>GetProductsWithPaginationAsync([FromQuery]intpageNumber,[FromQuery]intpageSize){varresult=await_query.ReadAllAsync(noTracking:true,pagination: pagination =>{pagination.MaxPageSize=50;pagination.PageNumber=pageNumber;pagination.PageSize=pageSize;});returnOk(result);}[HttpGet("{id}")]publicasyncTask<IActionResult>GetProductAsync(intid){varresult=await_query.ReadSingleOrDefaultAsync(true, x =>x.Id==id);returnOk(result);}[HttpGet("{id}")]publicasyncTask<IActionResult>GetProductWithCategoryAsync(intid){varresult=await_query.ReadSingleOrDefaultAsync(true, x =>x.Id==id,includeProperties: x =>x.Category);returnOk(result);}// Sync Queries[HttpGet]publicIActionResultGetProducts(){varresult=_query.ReadAll(noTracking:true);returnOk(result);}[HttpGet]publicIActionResultGetProductsWithCategory(){varresult=_query.ReadAll(noTracking:true,includeProperties: x =>x.Category);returnOk(result);}[HttpGet]publicIActionResultGetProductsWithPagination([FromQuery]intpageNumber,[FromQuery]intpageSize){varresult=_query.ReadAll(noTracking:true,pagination: pagination =>{pagination.MaxPageSize=50;pagination.PageNumber=pageNumber;pagination.PageSize=pageSize;});returnOk(result);}[HttpGet("{id}")]publicIActionResultGetProduct(intid){varresult=_query.ReadSingleOrDefault(true, x =>x.Id==id);if(result==null)returnNotFound();returnOk(result);}[HttpGet("{id}")]publicIActionResultGetProductWithCategory(intid){varresult=_query.ReadSingleOrDefault(true, x =>x.Id==id,includeProperties: x =>x.Category);if(result==null)returnNotFound();returnOk(result);}// Async Commands[HttpPost]publicasyncTask<IActionResult>CreateAsync([FromBody]ProductDtocreateProduct){Productproduct=new(){DisplayName=createProduct.DisplayName,Price=createProduct.Price,CategoryId=createProduct.CategoryId,};varresult=await_command.CreateAsync(product);await_command.ExecuteAsync();returnCreated("",result);}[HttpPut("{id}")]publicasyncTask<IActionResult>UpdateAsync(intid,[FromBody]ProductDtoupdateProduct){varproduct=await_query.ReadFirstOrDefaultAsync(true, x =>x.Id==id);if(productisnull)returnNotFound();product.DisplayName=updateProduct.DisplayName;product.Price=updateProduct.Price;product.CategoryId=updateProduct.CategoryId;await_command.EditAsync(product);await_command.ExecuteAsync();returnNoContent();}[HttpDelete("{id}")]publicasyncTask<IActionResult>DeleteAsync(intid){IQueryable<Product>query=await_query.QueryAsync(true,filter: x =>x.Id==id);Productproduct=query.SingleOrDefault();if(product==null)returnNotFound();await_command.RemoveAsync(product);await_command.ExecuteAsync();returnNoContent();}// Sync Commands[HttpPost]publicIActionResultCreate([FromBody]ProductDtocreateProduct){Productproduct=new(){DisplayName=createProduct.DisplayName,Price=createProduct.Price,CategoryId=createProduct.CategoryId,};varresult=_command.Create(product);_command.Execute();returnCreated("",result);}[HttpPut("{id}")]publicIActionResultUpdate(intid,[FromBody]ProductDtoupdateProduct){varproduct=_query.ReadFirstOrDefault(true, x =>x.Id==id);if(productisnull)returnNotFound();product.DisplayName=updateProduct.DisplayName;product.Price=updateProduct.Price;product.CategoryId=updateProduct.CategoryId;_command.Edit(product);_command.Execute();returnNoContent();}[HttpDelete("{id}")]publicIActionResultDelete(intid){IQueryable<Product>query=_query.Query(true,filter: x =>x.Id==id);Productproduct=query.SingleOrDefault();if(product==null)returnNotFound();_command.Remove(product);_command.Execute();returnNoContent();}}