From e839124d375b7876ee4efcb5ee60c9f2bf373aac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=ABl=20Mugnier?= Date: Sat, 7 Nov 2020 22:46:46 +0100 Subject: [PATCH] =?UTF-8?q?New=20-=20Ajout=20d'une=20mutation=20GraphQL=20?= =?UTF-8?q?pour=20activer/d=C3=A9sactiver=20les=20deliverymode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SetDeliveryModeAvailabilityCommand.cs | 18 ++++++++++ .../SetDeliveryModesAvailabilityCommand.cs | 18 ++++++++++ .../Commands/DeliveryModeCommandsHandler.cs | 35 ++++++++++++++++++- .../DeliveryModeProfile.cs | 1 + .../SetDeliveryModesAvailabilityInput.cs | 11 ++++++ Sheaft.GraphQL.Services/SheaftMutation.cs | 7 ++++ .../SetDeliveryModesAvailabilityInputType.cs | 16 +++++++++ Sheaft.GraphQL.Types/SheaftMutationType.cs | 9 +++++ .../Extensions/SchemaBuilderExtensions.cs | 1 + 9 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 Sheaft.Application.Commands/DeliveryMode/SetDeliveryModeAvailabilityCommand.cs create mode 100644 Sheaft.Application.Commands/DeliveryMode/SetDeliveryModesAvailabilityCommand.cs create mode 100644 Sheaft.Application.Models/Inputs/SetDeliveryModesAvailabilityInput.cs create mode 100644 Sheaft.GraphQL.Types/Inputs/SetDeliveryModesAvailabilityInputType.cs diff --git a/Sheaft.Application.Commands/DeliveryMode/SetDeliveryModeAvailabilityCommand.cs b/Sheaft.Application.Commands/DeliveryMode/SetDeliveryModeAvailabilityCommand.cs new file mode 100644 index 000000000..548ba9f75 --- /dev/null +++ b/Sheaft.Application.Commands/DeliveryMode/SetDeliveryModeAvailabilityCommand.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using Sheaft.Core; +using Newtonsoft.Json; + +namespace Sheaft.Application.Commands +{ + public class SetDeliveryModeAvailabilityCommand : Command + { + [JsonConstructor] + public SetDeliveryModeAvailabilityCommand(RequestUser requestUser) : base(requestUser) + { + } + + public Guid Id { get; set; } + public bool Available { get; set; } + } +} diff --git a/Sheaft.Application.Commands/DeliveryMode/SetDeliveryModesAvailabilityCommand.cs b/Sheaft.Application.Commands/DeliveryMode/SetDeliveryModesAvailabilityCommand.cs new file mode 100644 index 000000000..94b20471a --- /dev/null +++ b/Sheaft.Application.Commands/DeliveryMode/SetDeliveryModesAvailabilityCommand.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using Sheaft.Core; +using Newtonsoft.Json; + +namespace Sheaft.Application.Commands +{ + public class SetDeliveryModesAvailabilityCommand : Command + { + [JsonConstructor] + public SetDeliveryModesAvailabilityCommand(RequestUser requestUser) : base(requestUser) + { + } + + public IEnumerable Ids { get; set; } + public bool Available { get; set; } + } +} diff --git a/Sheaft.Application.Handlers/Commands/DeliveryModeCommandsHandler.cs b/Sheaft.Application.Handlers/Commands/DeliveryModeCommandsHandler.cs index 560f7b5ae..e18d706f4 100644 --- a/Sheaft.Application.Handlers/Commands/DeliveryModeCommandsHandler.cs +++ b/Sheaft.Application.Handlers/Commands/DeliveryModeCommandsHandler.cs @@ -19,7 +19,9 @@ public class DeliveryModeCommandsHandler : ResultsHandler, IRequestHandler>, IRequestHandler>, IRequestHandler>, - IRequestHandler> + IRequestHandler>, + IRequestHandler>, + IRequestHandler> { public DeliveryModeCommandsHandler( ISheaftMediatr mediatr, @@ -132,5 +134,36 @@ public async Task> Handle(RestoreDeliveryModeCommand request, Cance return Ok(true); }); } + + public async Task> Handle(SetDeliveryModesAvailabilityCommand request, CancellationToken token) + { + return await ExecuteAsync(request, async () => + { + using (var transaction = await _context.BeginTransactionAsync(token)) + { + foreach (var id in request.Ids) + { + var result = await _mediatr.Process(new SetDeliveryModeAvailabilityCommand(request.RequestUser) { Id = id, Available = request.Available }, token); + if (!result.Success) + return Failed(result.Exception); + } + + await transaction.CommitAsync(token); + return Ok(true); + } + }); + } + + public async Task> Handle(SetDeliveryModeAvailabilityCommand request, CancellationToken token) + { + return await ExecuteAsync(request, async () => + { + var entity = await _context.GetByIdAsync(request.Id, token); + entity.SetAvailability(request.Available); + + await _context.SaveChangesAsync(token); + return Ok(true); + }); + } } } \ No newline at end of file diff --git a/Sheaft.Application.Mappers/DeliveryModeProfile.cs b/Sheaft.Application.Mappers/DeliveryModeProfile.cs index 49f490441..5a8d7d19a 100644 --- a/Sheaft.Application.Mappers/DeliveryModeProfile.cs +++ b/Sheaft.Application.Mappers/DeliveryModeProfile.cs @@ -26,6 +26,7 @@ public DeliveryModeProfile() CreateMap(); CreateMap(); CreateMap(); + CreateMap(); } } } diff --git a/Sheaft.Application.Models/Inputs/SetDeliveryModesAvailabilityInput.cs b/Sheaft.Application.Models/Inputs/SetDeliveryModesAvailabilityInput.cs new file mode 100644 index 000000000..a59977aba --- /dev/null +++ b/Sheaft.Application.Models/Inputs/SetDeliveryModesAvailabilityInput.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; + +namespace Sheaft.Application.Models +{ + public class SetDeliveryModesAvailabilityInput + { + public IEnumerable Ids { get; set; } + public bool Available { get; set; } + } +} \ No newline at end of file diff --git a/Sheaft.GraphQL.Services/SheaftMutation.cs b/Sheaft.GraphQL.Services/SheaftMutation.cs index 5cf10390f..9e08db839 100644 --- a/Sheaft.GraphQL.Services/SheaftMutation.cs +++ b/Sheaft.GraphQL.Services/SheaftMutation.cs @@ -418,6 +418,13 @@ public async Task DeleteQuickOrdersAsync(IdsWithReasonInput input) return await ExecuteCommandAsync(_mapper.Map(input, new DeleteQuickOrdersCommand(CurrentUser)), Token); } + public async Task> SetDeliveryModesAvailabilityAsync(SetDeliveryModesAvailabilityInput input, [Service] IDeliveryQueries deliveryModeQueries) + { + SetLogTransaction("GraphQL", nameof(SetProductsAvailabilityAsync)); + await ExecuteCommandAsync(_mapper.Map(input, new SetDeliveryModesAvailabilityCommand(CurrentUser)), Token); + return deliveryModeQueries.GetDeliveries(CurrentUser).Where(j => input.Ids.Contains(j.Id)); + } + public async Task> CreateDeliveryModeAsync(CreateDeliveryModeInput input, [Service] IDeliveryQueries deliveryQueries) { SetLogTransaction("GraphQL", nameof(CreateDeliveryModeAsync)); diff --git a/Sheaft.GraphQL.Types/Inputs/SetDeliveryModesAvailabilityInputType.cs b/Sheaft.GraphQL.Types/Inputs/SetDeliveryModesAvailabilityInputType.cs new file mode 100644 index 000000000..1ca9f3027 --- /dev/null +++ b/Sheaft.GraphQL.Types/Inputs/SetDeliveryModesAvailabilityInputType.cs @@ -0,0 +1,16 @@ +using HotChocolate.Types; +using Sheaft.Application.Models; + +namespace Sheaft.GraphQL.Types +{ + public class SetDeliveryModesAvailabilityInputType : SheaftInputType + { + protected override void Configure(IInputObjectTypeDescriptor descriptor) + { + descriptor.Field(c => c.Available); + + descriptor.Field(c => c.Ids) + .Type>>(); + } + } +} diff --git a/Sheaft.GraphQL.Types/SheaftMutationType.cs b/Sheaft.GraphQL.Types/SheaftMutationType.cs index 010a3212d..99f50cb89 100644 --- a/Sheaft.GraphQL.Types/SheaftMutationType.cs +++ b/Sheaft.GraphQL.Types/SheaftMutationType.cs @@ -236,6 +236,15 @@ protected override void Configure(IObjectTypeDescriptor descript .Authorize(Policies.PRODUCER) .Type>(); + descriptor.Field(c => c.SetDeliveryModesAvailabilityAsync(default, default)) + .Name("setDeliveryModesAvailability") + .Authorize(Policies.PRODUCER) + .Type>>() + .UsePaging() + .UseFiltering() + .UseSorting() + .UseSelection(); + //RETURNABLE descriptor.Field(c => c.CreateReturnableAsync(default, default)) .Name("createReturnable") diff --git a/Sheaft.Web.Api/Extensions/SchemaBuilderExtensions.cs b/Sheaft.Web.Api/Extensions/SchemaBuilderExtensions.cs index 733911695..94d0cc71e 100644 --- a/Sheaft.Web.Api/Extensions/SchemaBuilderExtensions.cs +++ b/Sheaft.Web.Api/Extensions/SchemaBuilderExtensions.cs @@ -114,6 +114,7 @@ public static ISchemaBuilder RegisterTypes(this ISchemaBuilder services) services.AddType(); services.AddType(); services.AddType(); + services.AddType(); services.AddType(); services.AddType(); services.AddType();