-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add makeAddressDefault mutation
- Loading branch information
1 parent
021267b
commit f349644
Showing
4 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/VirtoCommerce.ProfileExperienceApiModule.Data/Commands/MakeAddressDefaultCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using GraphQL.Types; | ||
using VirtoCommerce.Xapi.Core.Infrastructure; | ||
|
||
namespace VirtoCommerce.ProfileExperienceApiModule.Data.Commands; | ||
|
||
public class MakeAddressDefaultCommand : ICommand<bool> | ||
{ | ||
public string UserId { get; set; } | ||
public string MemberId { get; set; } | ||
public string AddressId { get; set; } | ||
} | ||
|
||
public class MakeAddressDefaultCommandType : InputObjectGraphType<MakeAddressDefaultCommand> | ||
{ | ||
public MakeAddressDefaultCommandType() | ||
{ | ||
Field(x => x.AddressId); | ||
Field(x => x.MemberId); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...irtoCommerce.ProfileExperienceApiModule.Data/Commands/MakeAddressDefaultCommandBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System.Threading.Tasks; | ||
using GraphQL; | ||
using GraphQL.Types; | ||
using MediatR; | ||
using Microsoft.AspNetCore.Authorization; | ||
using VirtoCommerce.ProfileExperienceApiModule.Data.Services; | ||
using VirtoCommerce.Xapi.Core.BaseQueries; | ||
using VirtoCommerce.Xapi.Core.Extensions; | ||
|
||
namespace VirtoCommerce.ProfileExperienceApiModule.Data.Commands; | ||
|
||
public class MakeAddressDefaultCommandBuilder : CommandBuilder<MakeAddressDefaultCommand, bool, MakeAddressDefaultCommandType, BooleanGraphType> | ||
{ | ||
protected override string Name => "makeAddressDefault"; | ||
|
||
private readonly IProfileAuthorizationService _profileAuthorizationService; | ||
|
||
public MakeAddressDefaultCommandBuilder( | ||
IMediator mediator, | ||
IAuthorizationService authorizationService, | ||
IProfileAuthorizationService profileAuthorizationService) | ||
: base(mediator, authorizationService) | ||
{ | ||
_profileAuthorizationService = profileAuthorizationService; | ||
} | ||
|
||
protected override MakeAddressDefaultCommand GetRequest(IResolveFieldContext<object> context) | ||
{ | ||
var request = base.GetRequest(context); | ||
request.UserId = context.GetCurrentUserId(); | ||
return request; | ||
} | ||
|
||
protected override async Task BeforeMediatorSend(IResolveFieldContext<object> context, MakeAddressDefaultCommand request) | ||
{ | ||
await base.BeforeMediatorSend(context, request); | ||
await _profileAuthorizationService.CheckAuthAsync(context, request); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...irtoCommerce.ProfileExperienceApiModule.Data/Commands/MakeAddressDefaultCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using MediatR; | ||
using VirtoCommerce.ProfileExperienceApiModule.Data.Aggregates; | ||
|
||
namespace VirtoCommerce.ProfileExperienceApiModule.Data.Commands; | ||
|
||
public class MakeAddressDefaultCommandHandler : IRequestHandler<MakeAddressDefaultCommand, bool> | ||
{ | ||
private readonly IMemberAggregateRootRepository _memberAggregateRepository; | ||
|
||
public MakeAddressDefaultCommandHandler(IMemberAggregateRootRepository memberAggregateRepository) | ||
{ | ||
_memberAggregateRepository = memberAggregateRepository; | ||
} | ||
|
||
public async Task<bool> Handle(MakeAddressDefaultCommand request, CancellationToken cancellationToken) | ||
{ | ||
var memberAggregate = await _memberAggregateRepository.GetMemberAggregateRootByIdAsync<MemberAggregateRootBase>(request.MemberId); | ||
memberAggregate.MakeAddressDefault(request.AddressId); | ||
await _memberAggregateRepository.SaveAsync(memberAggregate); | ||
|
||
return true; | ||
} | ||
} |