Skip to content

Commit

Permalink
Add GetAddressByIdAsync to IAddressApiService. Add address id validat…
Browse files Browse the repository at this point in the history
…ion to create order api.
  • Loading branch information
stepanbenes committed Apr 29, 2021
1 parent dc1317f commit 0c426f0
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 26 deletions.
11 changes: 8 additions & 3 deletions Nop.Plugin.Api/Controllers/OrdersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,19 @@ public async Task<IActionResult> CreateOrder(
{
return Error(HttpStatusCode.BadRequest, "billingAddress", "missing billing address");
}
if (orderDelta.Dto.BillingAddress.Id == 0)
{
return Error(HttpStatusCode.BadRequest, "billingAddress", "non-existing billing address");
}

if (orderDelta.Dto.ShippingAddress == null)
{
return Error(HttpStatusCode.BadRequest, "shippingAddress", "missing shipping address");
}
if (orderDelta.Dto.ShippingAddress.Id == 0)
{
return Error(HttpStatusCode.BadRequest, "shippingAddress", "non-existing shipping address");
}

if (!await CheckPermissions(orderDelta.Dto.CustomerId))
{
Expand Down Expand Up @@ -337,12 +345,9 @@ public async Task<IActionResult> CreateOrder(
var newOrder = await _factory.InitializeAsync();
orderDelta.Merge(newOrder);

// TODO: if address is is 0, insert addresses to repo first

customer.BillingAddressId = newOrder.BillingAddressId = orderDelta.Dto.BillingAddress.Id;
customer.ShippingAddressId = newOrder.ShippingAddressId = orderDelta.Dto.ShippingAddress.Id;


// If the customer has something in the cart it will be added too. Should we clear the cart first?
newOrder.CustomerId = customer.Id;

Expand Down
46 changes: 29 additions & 17 deletions Nop.Plugin.Api/Helpers/DTOHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
using System.Linq;
using System.Threading.Tasks;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Common;
using Nop.Core.Domain.Directory;
using Nop.Core.Domain.Localization;
using Nop.Core.Domain.Media;
using Nop.Core.Domain.Orders;
using Nop.Core.Domain.Stores;
using Nop.Core.Domain.Topics;
using Nop.Plugin.Api.DTO;
using Nop.Plugin.Api.DTO.Categories;
using Nop.Plugin.Api.DTO.Images;
using Nop.Plugin.Api.DTO.Languages;
Expand Down Expand Up @@ -298,6 +300,32 @@ public async Task<ManufacturerDto> PrepareManufacturerDtoAsync(Manufacturer manu
return manufacturerDto;
}

public void PrepareProductSpecificationAttributes(IEnumerable<ProductSpecificationAttribute> productSpecificationAttributes, ProductDto productDto)
{
if (productDto.ProductSpecificationAttributes == null)
{
productDto.ProductSpecificationAttributes = new List<ProductSpecificationAttributeDto>();
}

foreach (var productSpecificationAttribute in productSpecificationAttributes)
{
var productSpecificationAttributeDto = PrepareProductSpecificationAttributeDto(productSpecificationAttribute);

if (productSpecificationAttributeDto != null)
{
productDto.ProductSpecificationAttributes.Add(productSpecificationAttributeDto);
}
}
}

public AddressDto PrepareAddressDTO(Address address)
{
var addressDto = address.ToDto();
return addressDto;
}

#region Private methods

private async Task PrepareProductImagesAsync(IEnumerable<ProductPicture> productPictures, ProductDto productDto)
{
if (productDto.Images == null)
Expand Down Expand Up @@ -446,22 +474,6 @@ private ProductAttributeCombinationDto PrepareProductAttributeCombinationDto(Pro
return productAttributeCombination.ToDto();
}

public void PrepareProductSpecificationAttributes(IEnumerable<ProductSpecificationAttribute> productSpecificationAttributes, ProductDto productDto)
{
if (productDto.ProductSpecificationAttributes == null)
{
productDto.ProductSpecificationAttributes = new List<ProductSpecificationAttributeDto>();
}

foreach (var productSpecificationAttribute in productSpecificationAttributes)
{
var productSpecificationAttributeDto = PrepareProductSpecificationAttributeDto(productSpecificationAttribute);

if (productSpecificationAttributeDto != null)
{
productDto.ProductSpecificationAttributes.Add(productSpecificationAttributeDto);
}
}
}
#endregion
}
}
21 changes: 15 additions & 6 deletions Nop.Plugin.Api/Services/AddressApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ public class AddressApiService : IAddressApiService
{
private readonly IStaticCacheManager _cacheManager;
private readonly ICountryService _countryService;
private readonly IRepository<Address> _customerAddressRepository;
private readonly IRepository<Address> _addressRepository;
private readonly IRepository<CustomerAddressMapping> _customerAddressMappingRepository;

public AddressApiService(
IRepository<Address> customerAddressRepository,
IRepository<Address> addressRepository,
IRepository<CustomerAddressMapping> customerAddressMappingRepository,
IStaticCacheManager staticCacheManager,
ICountryService countryService)
{
_customerAddressRepository = customerAddressRepository;
_addressRepository = addressRepository;
_customerAddressMappingRepository = customerAddressMappingRepository;
_cacheManager = staticCacheManager;
_countryService = countryService;
Expand All @@ -44,7 +44,7 @@ public AddressApiService(
/// </returns>
public async Task<IList<AddressDto>> GetAddressesByCustomerIdAsync(int customerId)
{
var query = from address in _customerAddressRepository.Table
var query = from address in _addressRepository.Table
join cam in _customerAddressMappingRepository.Table on address.Id equals cam.AddressId
where cam.CustomerId == customerId
select address;
Expand All @@ -66,7 +66,7 @@ join cam in _customerAddressMappingRepository.Table on address.Id equals cam.Add
/// </returns>
public async Task<AddressDto> GetCustomerAddressAsync(int customerId, int addressId)
{
var query = from address in _customerAddressRepository.Table
var query = from address in _addressRepository.Table
join cam in _customerAddressMappingRepository.Table on address.Id equals cam.AddressId
where cam.CustomerId == customerId && address.Id == addressId
select address;
Expand All @@ -86,5 +86,14 @@ public async Task<IList<CountryDto>> GetAllCountriesAsync(bool mustAllowBilling
countries = countries.Where(c => c.AllowsShipping);
return countries.Select(c => c.ToDto()).ToList();
}
}

public async Task<AddressDto> GetAddressByIdAsync(int addressId)
{
var query = from address in _addressRepository.Table
where address.Id == addressId
select address;
var addressEntity = await query.FirstOrDefaultAsync();
return addressEntity?.ToDto();
}
}
}
1 change: 1 addition & 0 deletions Nop.Plugin.Api/Services/IAddressApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public interface IAddressApiService
Task<IList<AddressDto>> GetAddressesByCustomerIdAsync(int customerId);
Task<AddressDto> GetCustomerAddressAsync(int customerId, int addressId);
Task<IList<CountryDto>> GetAllCountriesAsync(bool mustAllowBilling = false, bool mustAllowShipping = false);
Task<AddressDto> GetAddressByIdAsync(int addressId);
}
}

0 comments on commit 0c426f0

Please sign in to comment.