Browse Source

Refactor OrderAppService class in ModularCrm.Ordering module

pull/20943/head
ahmetfarukulu 2 years ago
parent
commit
0f32eb3901
  1. BIN
      docs/en/tutorials/modular-crm/images/abp-studio-add-package-reference-4.png
  2. BIN
      docs/en/tutorials/modular-crm/images/abp-studio-add-package-reference-5.png
  3. BIN
      docs/en/tutorials/modular-crm/images/abp-studio-import-module-ordering.png
  4. BIN
      docs/en/tutorials/modular-crm/images/visual-studio-ordering-controller.png
  5. 116
      docs/en/tutorials/modular-crm/part-07.md

BIN
docs/en/tutorials/modular-crm/images/abp-studio-add-package-reference-4.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 33 KiB

BIN
docs/en/tutorials/modular-crm/images/abp-studio-add-package-reference-5.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 39 KiB

BIN
docs/en/tutorials/modular-crm/images/abp-studio-import-module-ordering.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 28 KiB

BIN
docs/en/tutorials/modular-crm/images/visual-studio-ordering-controller.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.3 KiB

116
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. 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: Here is the full `OrderAppService` class:
![visual-studio-ordering-controller](images/visual-studio-ordering-controller.png)
Here is the full `OrdersController` class:
````csharp ````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.Enums;
using ModularCrm.Ordering.Contracts.Events; using ModularCrm.Ordering.Contracts.Events;
using ModularCrm.Ordering.Contracts.Services;
using ModularCrm.Ordering.Entities; using ModularCrm.Ordering.Entities;
using System; using ModularCrm.Products.Integration;
using System.ComponentModel.DataAnnotations; using Volo.Abp.Application.Services;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Domain.Repositories; using Volo.Abp.Domain.Repositories;
using Volo.Abp.EventBus.Distributed; using Volo.Abp.EventBus.Distributed;
namespace ModularCrm.Ordering.Controllers namespace ModularCrm.Ordering.Services;
public class OrderAppService : ApplicationService, IOrderAppService
{ {
[Route("api/orders")] private readonly IRepository<Order> _orderRepository;
[ApiController] private readonly IProductIntegrationService _productIntegrationService;
public class OrdersController : AbpControllerBase private readonly IDistributedEventBus _distributedEventBus;
public OrderAppService(
IRepository<Order, Guid> orderRepository,
IProductIntegrationService productIntegrationService,
IDistributedEventBus distributedEventBus)
{ {
private readonly IRepository<Order, Guid> _orderRepository; _orderRepository = orderRepository;
private readonly IDistributedEventBus _distributedEventBus; _productIntegrationService = productIntegrationService;
_distributedEventBus = distributedEventBus;
ObjectMapperContext = typeof(OrderingWebModule);
}
public OrdersController( public async Task<List<OrderDto>> GetListAsync()
IRepository<Order, Guid> orderRepository, {
IDistributedEventBus distributedEventBus) var orders = await _orderRepository.GetListAsync();
{
_orderRepository = orderRepository; // Prepare a list of products we need
_distributedEventBus = distributedEventBus; var productIds = orders.Select(o => o.ProductId).Distinct().ToList();
} var products = (await _productIntegrationService
.GetProductsByIdsAsync(productIds))
.ToDictionary(p => p.Id, p => p.Name);
[HttpPost] var result = ObjectMapper.Map<List<Order>, List<OrderDto>>(orders);
public async Task<IActionResult> CreateAsync(OrderCreationModel input) result = result.Select(a =>
{ {
// Create a new Order entity a.ProductName = products[a.ProductId];
var order = new Order return a;
{ })
CustomerName = input.CustomerName, .ToList();
ProductId = input.ProductId,
State = OrderState.Placed return result;
}; }
// 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();
}
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] // Save it to the database
[StringLength(120)] await _orderRepository.InsertAsync(order);
public string CustomerName { get; set; }
} // 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 ## Subscribing to an Event

Loading…
Cancel
Save