diff --git a/docs/en/tutorials/modular-crm/images/abp-studio-add-package-reference-4.png b/docs/en/tutorials/modular-crm/images/abp-studio-add-package-reference-4.png index fa3b5c4e23..1d092d0b57 100644 Binary files a/docs/en/tutorials/modular-crm/images/abp-studio-add-package-reference-4.png and b/docs/en/tutorials/modular-crm/images/abp-studio-add-package-reference-4.png differ diff --git a/docs/en/tutorials/modular-crm/images/abp-studio-add-package-reference-5.png b/docs/en/tutorials/modular-crm/images/abp-studio-add-package-reference-5.png index 4a8ef0ba58..b9c3347915 100644 Binary files a/docs/en/tutorials/modular-crm/images/abp-studio-add-package-reference-5.png and b/docs/en/tutorials/modular-crm/images/abp-studio-add-package-reference-5.png differ diff --git a/docs/en/tutorials/modular-crm/images/abp-studio-import-module-ordering.png b/docs/en/tutorials/modular-crm/images/abp-studio-import-module-ordering.png index e62c0dc315..9e08d15f1b 100644 Binary files a/docs/en/tutorials/modular-crm/images/abp-studio-import-module-ordering.png and b/docs/en/tutorials/modular-crm/images/abp-studio-import-module-ordering.png differ diff --git a/docs/en/tutorials/modular-crm/images/visual-studio-ordering-controller.png b/docs/en/tutorials/modular-crm/images/visual-studio-ordering-controller.png deleted file mode 100644 index 56a1dad5b1..0000000000 Binary files a/docs/en/tutorials/modular-crm/images/visual-studio-ordering-controller.png and /dev/null differ diff --git a/docs/en/tutorials/modular-crm/part-07.md b/docs/en/tutorials/modular-crm/part-07.md index 9dc18648bd..17eca0912b 100644 --- a/docs/en/tutorials/modular-crm/part-07.md +++ b/docs/en/tutorials/modular-crm/part-07.md @@ -58,81 +58,89 @@ namespace ModularCrm.Ordering.Contracts.Events The `IDistributedEventBus` service publishes events to the event bus. Until this point, the Ordering module has no functionality to create new orders. -In Part 3, we used ABP's Auto HTTP API Controller feature to expose HTTP APIs from application services automatically. In this section, we will create an ASP.NET Core API controller class to create a new order. In that way, you will also see that it is not different from creating a regular ASP.NET Core controller. +Open the `ModularCrm.Ordering` module's .NET solution, and edit the `OrderAppService` class in the `ModularCrm.Ordering` project. -Open the `ModularCrm.Ordering` module's .NET solution, create a `Controllers` folder in the `ModularCrm.Ordering` project and place a controller class named `OrdersController` in that new folder. The final folder structure should be like that: - -![visual-studio-ordering-controller](images/visual-studio-ordering-controller.png) - -Here is the full `OrdersController` class: +Here is the full `OrderAppService` class: ````csharp -using Microsoft.AspNetCore.Mvc; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; using ModularCrm.Ordering.Contracts.Enums; using ModularCrm.Ordering.Contracts.Events; +using ModularCrm.Ordering.Contracts.Services; using ModularCrm.Ordering.Entities; -using System; -using System.ComponentModel.DataAnnotations; -using System.Threading.Tasks; -using Volo.Abp.AspNetCore.Mvc; +using ModularCrm.Products.Integration; +using Volo.Abp.Application.Services; using Volo.Abp.Domain.Repositories; using Volo.Abp.EventBus.Distributed; -namespace ModularCrm.Ordering.Controllers +namespace ModularCrm.Ordering.Services; + +public class OrderAppService : ApplicationService, IOrderAppService { - [Route("api/orders")] - [ApiController] - public class OrdersController : AbpControllerBase + private readonly IRepository _orderRepository; + private readonly IProductIntegrationService _productIntegrationService; + private readonly IDistributedEventBus _distributedEventBus; + + public OrderAppService( + IRepository orderRepository, + IProductIntegrationService productIntegrationService, + IDistributedEventBus distributedEventBus) { - private readonly IRepository _orderRepository; - private readonly IDistributedEventBus _distributedEventBus; + _orderRepository = orderRepository; + _productIntegrationService = productIntegrationService; + _distributedEventBus = distributedEventBus; + ObjectMapperContext = typeof(OrderingWebModule); + } - public OrdersController( - IRepository orderRepository, - IDistributedEventBus distributedEventBus) - { - _orderRepository = orderRepository; - _distributedEventBus = distributedEventBus; - } + public async Task> GetListAsync() + { + var orders = await _orderRepository.GetListAsync(); + + // Prepare a list of products we need + var productIds = orders.Select(o => o.ProductId).Distinct().ToList(); + var products = (await _productIntegrationService + .GetProductsByIdsAsync(productIds)) + .ToDictionary(p => p.Id, p => p.Name); - [HttpPost] - public async Task CreateAsync(OrderCreationModel input) + var result = ObjectMapper.Map, List>(orders); + result = result.Select(a => { - // Create a new Order entity - var order = new Order - { - CustomerName = input.CustomerName, - ProductId = input.ProductId, - State = OrderState.Placed - }; - - // Save it to the database - await _orderRepository.InsertAsync(order); - - // Publish an event so other modules can be informed - await _distributedEventBus.PublishAsync( - new OrderPlacedEto - { - ProductId = order.ProductId, - CustomerName = order.CustomerName - }); - - return Created(); - } + a.ProductName = products[a.ProductId]; + return a; + }) + .ToList(); + + return result; + } - public class OrderCreationModel + public async Task CreateAsync(OrderCreationDto input) + { + // Create a new Order entity + var order = new Order { - public Guid ProductId { get; set; } + CustomerName = input.CustomerName, + ProductId = input.ProductId, + State = OrderState.Placed + }; - [Required] - [StringLength(120)] - public string CustomerName { get; set; } - } + // Save it to the database + await _orderRepository.InsertAsync(order); + + // Publish an event so other modules can be informed + await _distributedEventBus.PublishAsync( + new OrderPlacedEto + { + ProductId = order.ProductId, + CustomerName = order.CustomerName + }); } } ```` -The `OrdersController.CreateAsync` method creates a new `Order` entity, saves it to the database and finally publishes an `OrderPlacedEto` event. +The `OrderAppService.CreateAsync` method creates a new `Order` entity, saves it to the database and finally publishes an `OrderPlacedEto` event. ## Subscribing to an Event