Skip to content

Commit

Permalink
New - Ajout d'une mutation GraphQL pour activer/désactiver les delive…
Browse files Browse the repository at this point in the history
…rymode
  • Loading branch information
noelmugnier committed Nov 7, 2020
1 parent f86a70a commit e839124
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using Sheaft.Core;
using Newtonsoft.Json;

namespace Sheaft.Application.Commands
{
public class SetDeliveryModeAvailabilityCommand : Command<bool>
{
[JsonConstructor]
public SetDeliveryModeAvailabilityCommand(RequestUser requestUser) : base(requestUser)
{
}

public Guid Id { get; set; }
public bool Available { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using Sheaft.Core;
using Newtonsoft.Json;

namespace Sheaft.Application.Commands
{
public class SetDeliveryModesAvailabilityCommand : Command<bool>
{
[JsonConstructor]
public SetDeliveryModesAvailabilityCommand(RequestUser requestUser) : base(requestUser)
{
}

public IEnumerable<Guid> Ids { get; set; }
public bool Available { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public class DeliveryModeCommandsHandler : ResultsHandler,
IRequestHandler<CreateDeliveryModeCommand, Result<Guid>>,
IRequestHandler<UpdateDeliveryModeCommand, Result<bool>>,
IRequestHandler<DeleteDeliveryModeCommand, Result<bool>>,
IRequestHandler<RestoreDeliveryModeCommand, Result<bool>>
IRequestHandler<RestoreDeliveryModeCommand, Result<bool>>,
IRequestHandler<SetDeliveryModesAvailabilityCommand, Result<bool>>,
IRequestHandler<SetDeliveryModeAvailabilityCommand, Result<bool>>
{
public DeliveryModeCommandsHandler(
ISheaftMediatr mediatr,
Expand Down Expand Up @@ -132,5 +134,36 @@ public async Task<Result<bool>> Handle(RestoreDeliveryModeCommand request, Cance
return Ok(true);
});
}

public async Task<Result<bool>> 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<bool>(result.Exception);
}
await transaction.CommitAsync(token);
return Ok(true);
}
});
}

public async Task<Result<bool>> Handle(SetDeliveryModeAvailabilityCommand request, CancellationToken token)
{
return await ExecuteAsync(request, async () =>
{
var entity = await _context.GetByIdAsync<DeliveryMode>(request.Id, token);
entity.SetAvailability(request.Available);
await _context.SaveChangesAsync(token);
return Ok(true);
});
}
}
}
1 change: 1 addition & 0 deletions Sheaft.Application.Mappers/DeliveryModeProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public DeliveryModeProfile()
CreateMap<CreateDeliveryModeInput, CreateDeliveryModeCommand>();
CreateMap<UpdateDeliveryModeInput, UpdateDeliveryModeCommand>();
CreateMap<IdInput, DeleteDeliveryModeCommand>();
CreateMap<SetDeliveryModesAvailabilityInput, SetDeliveryModesAvailabilityCommand>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;

namespace Sheaft.Application.Models
{
public class SetDeliveryModesAvailabilityInput
{
public IEnumerable<Guid> Ids { get; set; }
public bool Available { get; set; }
}
}
7 changes: 7 additions & 0 deletions Sheaft.GraphQL.Services/SheaftMutation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,13 @@ public async Task<bool> DeleteQuickOrdersAsync(IdsWithReasonInput input)
return await ExecuteCommandAsync<DeleteQuickOrdersCommand, bool>(_mapper.Map(input, new DeleteQuickOrdersCommand(CurrentUser)), Token);
}

public async Task<IQueryable<DeliveryModeDto>> SetDeliveryModesAvailabilityAsync(SetDeliveryModesAvailabilityInput input, [Service] IDeliveryQueries deliveryModeQueries)
{
SetLogTransaction("GraphQL", nameof(SetProductsAvailabilityAsync));
await ExecuteCommandAsync<SetDeliveryModesAvailabilityCommand, bool>(_mapper.Map(input, new SetDeliveryModesAvailabilityCommand(CurrentUser)), Token);
return deliveryModeQueries.GetDeliveries(CurrentUser).Where(j => input.Ids.Contains(j.Id));
}

public async Task<IQueryable<DeliveryModeDto>> CreateDeliveryModeAsync(CreateDeliveryModeInput input, [Service] IDeliveryQueries deliveryQueries)
{
SetLogTransaction("GraphQL", nameof(CreateDeliveryModeAsync));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using HotChocolate.Types;
using Sheaft.Application.Models;

namespace Sheaft.GraphQL.Types
{
public class SetDeliveryModesAvailabilityInputType : SheaftInputType<SetDeliveryModesAvailabilityInput>
{
protected override void Configure(IInputObjectTypeDescriptor<SetDeliveryModesAvailabilityInput> descriptor)
{
descriptor.Field(c => c.Available);

descriptor.Field(c => c.Ids)
.Type<NonNullType<ListType<IdType>>>();
}
}
}
9 changes: 9 additions & 0 deletions Sheaft.GraphQL.Types/SheaftMutationType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,15 @@ protected override void Configure(IObjectTypeDescriptor<SheaftMutation> descript
.Authorize(Policies.PRODUCER)
.Type<NonNullType<BooleanType>>();

descriptor.Field(c => c.SetDeliveryModesAvailabilityAsync(default, default))
.Name("setDeliveryModesAvailability")
.Authorize(Policies.PRODUCER)
.Type<NonNullType<ListType<DeliveryModeType>>>()
.UsePaging<DeliveryModeType>()
.UseFiltering<DeliveryModeFilterType>()
.UseSorting<DeliveryModeSortType>()
.UseSelection();

//RETURNABLE
descriptor.Field(c => c.CreateReturnableAsync(default, default))
.Name("createReturnable")
Expand Down
1 change: 1 addition & 0 deletions Sheaft.Web.Api/Extensions/SchemaBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public static ISchemaBuilder RegisterTypes(this ISchemaBuilder services)
services.AddType<SearchTermsInputType>();
services.AddType<SetProductsAvailabilityInputType>();
services.AddType<SetProductsSearchabilityInputType>();
services.AddType<SetDeliveryModesAvailabilityInputType>();
services.AddType<TimeSlotGroupInputType>();
services.AddType<UpdateStoreInputType>();
services.AddType<UpdateProducerInputType>();
Expand Down

0 comments on commit e839124

Please sign in to comment.