Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

API - Customer - Minor improvements

KrzysztofPajak5928b1bb8cfe
diff --git a/src/API/Grand.Api/Controllers/OData/CustomerController.cs b/src/API/Grand.Api/Controllers/OData/CustomerController.cs
index 6613d992e..0d8eda90d 100644
--- a/src/API/Grand.Api/Controllers/OData/CustomerController.cs
+++ b/src/API/Grand.Api/Controllers/OData/CustomerController.cs
@@ -8,7 +8,6 @@ using Grand.Business.Customers.Utilities;
using Grand.Domain.Customers;
using MediatR;
using Microsoft.AspNetCore.Mvc;
-using Microsoft.AspNetCore.OData.Formatter;
using Swashbuckle.AspNetCore.Annotations;
using System.Linq;
using System.Threading.Tasks;
@@ -139,25 +138,24 @@ namespace Grand.Api.Controllers.OData
return Ok(address);
}
- //odata/Customer(email)/DeleteAddress
+ //odata/Customer/(email)/DeleteAddress
//body: { "addressId": "xxx" }
[SwaggerOperation(summary: "Invoke action DeleteAddress", OperationId = "DeleteAddress")]
[Route("({key})/[action]")]
[HttpPost]
- public async Task<IActionResult> DeleteAddress(string key, [FromBody] ODataActionParameters parameters)
+ public async Task<IActionResult> DeleteAddress(string key, [FromBody] DeleteAddressDto model)
{
if (!await _permissionService.Authorize(PermissionSystemName.Customers))
return Forbid();
- var addressId = parameters.FirstOrDefault(x => x.Key == "addressId").Value;
- if (addressId == null)
+ if (model == null || string.IsNullOrEmpty(model.AddressId))
return NotFound();
var customer = await _mediator.Send(new GetCustomerQuery() { Email = key });
if (customer == null)
return NotFound();
- var address = customer.Addresses.FirstOrDefault(x => x.Id == addressId.ToString());
+ var address = customer.Addresses.FirstOrDefault(x => x.Id == model.AddressId.ToString());
if (address == null)
return NotFound();
@@ -167,21 +165,23 @@ namespace Grand.Api.Controllers.OData
}
- //odata/Customer(email)/SetPassword
+ //odata/Customer/(email)/SetPassword
//body: { "password": "123456" }
[SwaggerOperation(summary: "Invoke action SetPassword", OperationId = "SetPassword")]
[Route("({key})/[action]")]
[HttpPost]
- public async Task<IActionResult> SetPassword(string key, [FromBody] ODataActionParameters parameters)
+ public async Task<IActionResult> SetPassword(string key, [FromBody] PasswordDto model)
{
if (!await _permissionService.Authorize(PermissionSystemName.Customers))
return Forbid();
- var password = parameters.FirstOrDefault(x => x.Key == "password").Value;
- if (password == null)
+ if (model == null || string.IsNullOrEmpty(model.Password))
return NotFound();
- var changePassRequest = new ChangePasswordRequest(key, false, _customerSettings.DefaultPasswordFormat, password.ToString());
+ if (!ModelState.IsValid)
+ return BadRequest(ModelState);
+
+ var changePassRequest = new ChangePasswordRequest(key, false, _customerSettings.DefaultPasswordFormat, model.Password);
var changePassResult = await _customerManagerService.ChangePassword(changePassRequest);
if (!changePassResult.Success)
{
diff --git a/src/API/Grand.Api/DTOs/Customers/DeleteAddressDto.cs b/src/API/Grand.Api/DTOs/Customers/DeleteAddressDto.cs
new file mode 100644
index 000000000..7890716f1
--- /dev/null
+++ b/src/API/Grand.Api/DTOs/Customers/DeleteAddressDto.cs
@@ -0,0 +1,8 @@
+namespace Grand.Api.DTOs.Customers
+{
+ public partial class DeleteAddressDto
+ {
+ public string AddressId { get; set; }
+
+ }
+}
diff --git a/src/API/Grand.Api/DTOs/Customers/PasswordDto.cs b/src/API/Grand.Api/DTOs/Customers/PasswordDto.cs
new file mode 100644
index 000000000..bd74a140a
--- /dev/null
+++ b/src/API/Grand.Api/DTOs/Customers/PasswordDto.cs
@@ -0,0 +1,8 @@
+namespace Grand.Api.DTOs.Customers
+{
+ public partial class PasswordDto
+ {
+ public string Password { get; set; }
+
+ }
+}
diff --git a/src/API/Grand.Api/Infrastructure/DependencyEdmModel.cs b/src/API/Grand.Api/Infrastructure/DependencyEdmModel.cs
index 68d81fb63..dccaadc88 100644
--- a/src/API/Grand.Api/Infrastructure/DependencyEdmModel.cs
+++ b/src/API/Grand.Api/Infrastructure/DependencyEdmModel.cs
@@ -267,6 +267,8 @@ namespace Grand.Api.Infrastructure
builder.EntitySet<CustomerDto>("Customer");
var customer = builder.EntityType<CustomerDto>();
builder.ComplexType<AddressDto>();
+ builder.ComplexType<DeleteAddressDto>();
+ builder.ComplexType<PasswordDto>();
ActionConfiguration addAddress = customer.Action("AddAddress");
addAddress.Parameter<string>(nameof(AddressDto.Id)).Required();
@@ -309,11 +311,11 @@ namespace Grand.Api.Infrastructure
updateAddress.Returns<AddressDto>();
ActionConfiguration deleteAddress = customer.Action("DeleteAddress");
- deleteAddress.Parameter<string>("addressId");
+ deleteAddress.Parameter<string>(nameof(DeleteAddressDto.AddressId));
deleteAddress.Returns<bool>();
ActionConfiguration changePassword = customer.Action("SetPassword");
- changePassword.Parameter<string>("password");
+ changePassword.Parameter<string>(nameof(PasswordDto.Password));
changePassword.Returns<bool>();
#endregion
diff --git a/src/API/Grand.Api/Validators/Customers/PasswordValidator.cs b/src/API/Grand.Api/Validators/Customers/PasswordValidator.cs
new file mode 100644
index 000000000..6eb1754dd
--- /dev/null
+++ b/src/API/Grand.Api/Validators/Customers/PasswordValidator.cs
@@ -0,0 +1,25 @@
+using FluentValidation;
+using Grand.Api.DTOs.Customers;
+using Grand.Business.Common.Interfaces.Localization;
+using Grand.Domain.Customers;
+using Grand.Infrastructure.Validators;
+using System.Collections.Generic;
+
+namespace Grand.Api.Validators.Customers
+{
+ public class PasswordValidator : BaseGrandValidator<PasswordDto>
+ {
+ public PasswordValidator(
+ IEnumerable<IValidatorConsumer<PasswordDto>> validators,
+ ITranslationService translationService,
+ CustomerSettings customerSettings)
+ : base(validators)
+ {
+ RuleFor(x => x.Password).NotEmpty().WithMessage(translationService.GetResource("Account.Fields.Password.Required"));
+
+ if (!string.IsNullOrEmpty(customerSettings.PasswordRegularExpression))
+ RuleFor(x => x.Password).Matches(customerSettings.PasswordRegularExpression).WithMessage(string.Format(translationService.GetResource("Account.Fields.Password.Validation")));
+
+ }
+ }
+}