Skip to content

Commit

Permalink
Update - Mise à jour de l'app de manage
Browse files Browse the repository at this point in the history
  • Loading branch information
noelmugnier committed Nov 27, 2020
1 parent 22ce888 commit 05a83dd
Show file tree
Hide file tree
Showing 81 changed files with 1,439 additions and 2,639 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ public async Task<Result<bool>> Handle(RefuseAgreementCommand request, Cancellat
return await ExecuteAsync(request, async () =>
{
var entity = await _context.GetByIdAsync<Agreement>(request.Id, token);
entity.RefuseAgreement(request.Reason);
entity.RefuseAgreement(request.Reason);
await _context.SaveChangesAsync(token);
_mediatr.Post(new AgreementRefusedEvent(request.RequestUser) { AgreementId = entity.Id });
Expand All @@ -169,9 +169,9 @@ public async Task<Result<bool>> Handle(ResetAgreementStatusToCommand request, Ca
{
return await ExecuteAsync(request, async () =>
{
var entity = await _context.Agreements.SingleOrDefaultAsync(a => a.Id == request.Id && a.RemovedOn.HasValue, token);
entity.Reset();
var entity = await _context.Agreements.SingleOrDefaultAsync(a => a.Id == request.Id, token);
entity.Reset();
await _context.SaveChangesAsync(token);
return Ok(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public async Task<Result<DeclarationStatus>> Handle(RefreshDeclarationStatusComm
legal.Declaration.SetResult(pspResult.Data.ResultCode, pspResult.Data.ResultMessage);
legal.Declaration.SetProcessedOn(pspResult.Data.ProcessedOn);
var success = await _context.SaveChangesAsync(token) > 0;
await _context.SaveChangesAsync(token);
switch (legal.Declaration.Status)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public async Task<Result<bool>> Handle(UpdateDeliveryModeCommand request, Cancel
producer.CanDirectSell = true;
else
{
producer.CanDirectSell = await _context.DeliveryModes.AnyAsync(c => c.Producer.Id == request.RequestUser.Id && (c.Kind == DeliveryKind.Collective || c.Kind == DeliveryKind.Farm || c.Kind == DeliveryKind.Market), token);
producer.CanDirectSell = await _context.DeliveryModes.AnyAsync(c => !c.RemovedOn.HasValue && c.Producer.Id == request.RequestUser.Id && (c.Kind == DeliveryKind.Collective || c.Kind == DeliveryKind.Farm || c.Kind == DeliveryKind.Market), token);
}
await _context.SaveChangesAsync(token);
Expand All @@ -113,10 +113,9 @@ public async Task<Result<bool>> Handle(DeleteDeliveryModeCommand request, Cancel
return BadRequest<bool>(MessageKind.DeliveryMode_CannotRemove_With_Active_Agreements, entity.Name, activeAgreements);
_context.Remove(entity);
entity.Producer.CanDirectSell = await _context.DeliveryModes.AnyAsync(c => c.Producer.Id == request.RequestUser.Id && (c.Kind == DeliveryKind.Collective || c.Kind == DeliveryKind.Farm || c.Kind == DeliveryKind.Market), token);
entity.Producer.CanDirectSell = await _context.DeliveryModes.AnyAsync(c => !c.RemovedOn.HasValue && c.Producer.Id == request.RequestUser.Id && (c.Kind == DeliveryKind.Collective || c.Kind == DeliveryKind.Farm || c.Kind == DeliveryKind.Market), token);
await _context.SaveChangesAsync(token);
return Ok(true);
});
}
Expand All @@ -126,9 +125,11 @@ public async Task<Result<bool>> Handle(RestoreDeliveryModeCommand request, Cance
return await ExecuteAsync(request, async () =>
{
var entity = await _context.DeliveryModes.SingleOrDefaultAsync(a => a.Id == request.Id && a.RemovedOn.HasValue, token);
_context.Restore(entity);
await _context.SaveChangesAsync(token);
entity.Producer.CanDirectSell = await _context.DeliveryModes.AnyAsync(c => !c.RemovedOn.HasValue && c.Producer.Id == request.RequestUser.Id && (c.Kind == DeliveryKind.Collective || c.Kind == DeliveryKind.Farm || c.Kind == DeliveryKind.Market), token);
await _context.SaveChangesAsync(token);
return Ok(true);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public async Task<Result<bool>> Handle(UpdateBankAccountCommand request, Cancell
request.Address.Country)
: null;
if (!string.IsNullOrWhiteSpace(bankAccount.Identifier))
{
var resetResult = await _pspService.UpdateBankIbanAsync(bankAccount, false, token);
Expand Down
163 changes: 87 additions & 76 deletions Sheaft.Application.Handlers/Commands/ProductCommandsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,109 +49,120 @@ public async Task<Result<Guid>> Handle(CreateProductCommand request, Cancellatio
{
return await ExecuteAsync(request, async () =>
{
if (!string.IsNullOrWhiteSpace(request.Reference))
{
var existingEntity = await _context.FindSingleAsync<Product>(p => p.Reference == request.Reference && p.Producer.Id == request.RequestUser.Id, token);
if (existingEntity != null)
return ValidationError<Guid>(MessageKind.CreateProduct_Reference_AlreadyExists, request.Reference);
}
else
using (var transaction = await _context.BeginTransactionAsync(token))
{
var resultIdentifier = await _mediatr.Process(new CreateProductIdentifierCommand(request.RequestUser) { ProducerId = request.RequestUser.Id }, token);
if (!resultIdentifier.Success)
return Failed<Guid>(resultIdentifier.Exception);
var reference = request.Reference;
if (!string.IsNullOrWhiteSpace(reference))
{
var existingEntity = await _context.FindSingleAsync<Product>(p => p.Reference == reference && p.Producer.Id == request.RequestUser.Id, token);
if (existingEntity != null)
return ValidationError<Guid>(MessageKind.CreateProduct_Reference_AlreadyExists, reference);
}
else
{
var resultIdentifier = await _mediatr.Process(new CreateProductIdentifierCommand(request.RequestUser) { ProducerId = request.RequestUser.Id }, token);
if (!resultIdentifier.Success)
return Failed<Guid>(resultIdentifier.Exception);
request.Reference = resultIdentifier.Data;
}
reference = resultIdentifier.Data;
}
var producer = await _context.GetByIdAsync<Producer>(request.RequestUser.Id, token);
var entity = new Product(Guid.NewGuid(), request.Reference, request.Name, request.WholeSalePricePerUnit, request.Conditioning, request.Unit, request.QuantityPerUnit, producer);
var producer = await _context.GetByIdAsync<Producer>(request.RequestUser.Id, token);
var entity = new Product(Guid.NewGuid(), reference, request.Name, request.WholeSalePricePerUnit, request.Conditioning, request.Unit, request.QuantityPerUnit, producer);
entity.SetVat(request.Vat);
entity.SetDescription(request.Description);
entity.SetAvailable(request.Available ?? true);
entity.SetStoreVisibility(request.VisibleToStores ?? false);
entity.SetConsumerVisibility(request.VisibleToConsumers ?? false);
entity.SetWeight(request.Weight);
entity.SetVat(request.Vat);
entity.SetDescription(request.Description);
entity.SetAvailable(request.Available ?? true);
entity.SetStoreVisibility(request.VisibleToStores ?? false);
entity.SetConsumerVisibility(request.VisibleToConsumers ?? false);
entity.SetWeight(request.Weight);
if (request.ReturnableId.HasValue)
{
var returnable = await _context.GetByIdAsync<Returnable>(request.ReturnableId.Value, token);
entity.SetReturnable(returnable);
}
if (request.ReturnableId.HasValue)
{
var returnable = await _context.GetByIdAsync<Returnable>(request.ReturnableId.Value, token);
entity.SetReturnable(returnable);
}
var tags = await _context.FindAsync<Tag>(t => request.Tags.Contains(t.Id), token);
entity.SetTags(tags);
var tags = await _context.FindAsync<Tag>(t => request.Tags.Contains(t.Id), token);
entity.SetTags(tags);
await _context.AddAsync(entity, token);
await _context.SaveChangesAsync(token);
await _context.AddAsync(entity, token);
await _context.SaveChangesAsync(token);
if (!request.SkipUpdateProducerTags)
_mediatr.Post(new UpdateProducerTagsCommand(request.RequestUser) { ProducerId = request.RequestUser.Id });
var imageResult = await _mediatr.Process(new UpdateProductPictureCommand(request.RequestUser) { ProductId = entity.Id, Picture = request.Picture }, token);
if (!imageResult.Success)
return Failed<Guid>(imageResult.Exception);
var imageResult = await _mediatr.Process(new UpdateProductPictureCommand(request.RequestUser) { ProductId = entity.Id, Picture = request.Picture }, token);
if (!imageResult.Success)
return Failed<Guid>(imageResult.Exception);
await transaction.CommitAsync(token);
return Created(entity.Id);
if (!request.SkipUpdateProducerTags)
_mediatr.Post(new UpdateProducerTagsCommand(request.RequestUser) { ProducerId = request.RequestUser.Id });
return Created(entity.Id);
}
});
}

public async Task<Result<bool>> Handle(UpdateProductCommand request, CancellationToken token)
{
return await ExecuteAsync(request, async () =>
{
var entity = await _context.GetByIdAsync<Product>(request.Id, token);
if (!string.IsNullOrWhiteSpace(request.Reference) && request.Reference != entity.Reference)
using (var transaction = await _context.BeginTransactionAsync(token))
{
var existingEntity = await _context.FindSingleAsync<Product>(p => p.Reference == request.Reference && p.Producer.Id == request.RequestUser.Id, token);
if (existingEntity != null)
return ValidationError<bool>(MessageKind.CreateProduct_Reference_AlreadyExists, request.Reference);
}
var entity = await _context.GetByIdAsync<Product>(request.Id, token);
if(string.IsNullOrWhiteSpace(request.Reference))
{
var resultIdentifier = await _mediatr.Process(new CreateProductIdentifierCommand(request.RequestUser) { ProducerId = request.RequestUser.Id }, token);
if (!resultIdentifier.Success)
return Failed<bool>(resultIdentifier.Exception);
var reference = request.Reference;
if (!string.IsNullOrWhiteSpace(reference) && reference != entity.Reference)
{
var existingEntity = await _context.FindSingleAsync<Product>(p => p.Reference == reference && p.Producer.Id == request.RequestUser.Id, token);
if (existingEntity != null)
return ValidationError<bool>(MessageKind.CreateProduct_Reference_AlreadyExists, reference);
}
request.Reference = resultIdentifier.Data;
}
if (string.IsNullOrWhiteSpace(reference))
{
var resultIdentifier = await _mediatr.Process(new CreateProductIdentifierCommand(request.RequestUser) { ProducerId = request.RequestUser.Id }, token);
if (!resultIdentifier.Success)
return Failed<bool>(resultIdentifier.Exception);
entity.SetVat(request.Vat);
entity.SetName(request.Name);
entity.SetDescription(request.Description);
entity.SetWholeSalePricePerUnit(request.WholeSalePricePerUnit);
entity.SetReference(request.Reference);
entity.SetWeight(request.Weight);
entity.SetAvailable(request.Available);
entity.SetStoreVisibility(request.VisibleToStores);
entity.SetConsumerVisibility(request.VisibleToConsumers);
entity.SetConditioning(request.Conditioning, request.QuantityPerUnit, request.Unit);
reference = resultIdentifier.Data;
}
if (request.ReturnableId.HasValue)
{
var returnable = await _context.GetByIdAsync<Returnable>(request.ReturnableId.Value, token);
entity.SetReturnable(returnable);
}
else
{
entity.SetReturnable(null);
}
entity.SetVat(request.Vat);
entity.SetName(request.Name);
entity.SetDescription(request.Description);
entity.SetWholeSalePricePerUnit(request.WholeSalePricePerUnit);
entity.SetReference(reference);
entity.SetWeight(request.Weight);
entity.SetAvailable(request.Available);
entity.SetStoreVisibility(request.VisibleToStores);
entity.SetConsumerVisibility(request.VisibleToConsumers);
entity.SetConditioning(request.Conditioning, request.QuantityPerUnit, request.Unit);
if (request.ReturnableId.HasValue)
{
var returnable = await _context.GetByIdAsync<Returnable>(request.ReturnableId.Value, token);
entity.SetReturnable(returnable);
}
else
{
entity.SetReturnable(null);
}
var tags = await _context.FindAsync<Tag>(t => request.Tags.Contains(t.Id), token);
entity.SetTags(tags);
var tags = await _context.FindAsync<Tag>(t => request.Tags.Contains(t.Id), token);
entity.SetTags(tags);
await _context.SaveChangesAsync(token);
await _context.SaveChangesAsync(token);
_mediatr.Post(new UpdateProducerTagsCommand(request.RequestUser) { ProducerId = request.RequestUser.Id });
var imageResult = await _mediatr.Process(new UpdateProductPictureCommand(request.RequestUser) { ProductId = entity.Id, Picture = request.Picture }, token);
if (!imageResult.Success)
return Failed<bool>(imageResult.Exception);
var imageResult = await _mediatr.Process(new UpdateProductPictureCommand(request.RequestUser) { ProductId = entity.Id, Picture = request.Picture }, token);
if (!imageResult.Success)
return Failed<bool>(imageResult.Exception);
await transaction.CommitAsync(token);
return Ok(true);
_mediatr.Post(new UpdateProducerTagsCommand(request.RequestUser) { ProducerId = request.RequestUser.Id });
return Ok(true);
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ public async Task<Result<bool>> Handle(SetDefaultQuickOrderCommand request, Canc
}
_context.UpdateRange(quickOrders);
await _context.SaveChangesAsync(token);
return Ok(await _context.SaveChangesAsync(token) > 0);
return Ok(true);
});
}

Expand All @@ -135,9 +136,11 @@ public async Task<Result<bool>> Handle(DeleteQuickOrderCommand request, Cancella
return await ExecuteAsync(request, async () =>
{
var entity = await _context.GetByIdAsync<QuickOrder>(request.Id, token);
_context.Remove(entity);
await _context.SaveChangesAsync(token);
return Ok(await _context.SaveChangesAsync(token) > 0);
return Ok(true);
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,24 @@ public async Task<Result<bool>> Handle(DeleteReturnableCommand request, Cancella
return await ExecuteAsync(request, async () =>
{
var entity = await _context.GetByIdAsync<Returnable>(request.Id, token);
_context.Remove(entity);
var results = await _context.SaveChangesAsync(token);
_context.Remove(entity);
await _context.SaveChangesAsync(token);
return Ok(results > 0);
return Ok(true);
});
}

public async Task<Result<bool>> Handle(RestoreReturnableCommand request, CancellationToken token)
{
return await ExecuteAsync(request, async () =>
{
var entity = await _context.Returnables.SingleOrDefaultAsync(a => a.Id == request.Id && a.RemovedOn.HasValue, token);
var entity = await _context.Returnables.SingleOrDefaultAsync(a => a.Id == request.Id && a.RemovedOn.HasValue, token);
_context.Restore(entity);
await _context.SaveChangesAsync(token);
return Ok(await _context.SaveChangesAsync(token) > 0);
return Ok(true);
});
}
}
Expand Down
8 changes: 6 additions & 2 deletions Sheaft.Application.Handlers/Commands/RewardCommandsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,11 @@ public async Task<Result<bool>> Handle(DeleteRewardCommand request, Cancellation
return await ExecuteAsync(request, async () =>
{
var entity = await _context.GetByIdAsync<Reward>(request.Id, token);
_context.Remove(entity);
await _context.SaveChangesAsync(token);
return Ok(await _context.SaveChangesAsync(token) > 0);
return Ok(true);
});
}

Expand All @@ -100,9 +102,11 @@ public async Task<Result<bool>> Handle(RestoreRewardCommand request, Cancellatio
return await ExecuteAsync(request, async () =>
{
var entity = await _context.Rewards.SingleOrDefaultAsync(r => r.Id == request.Id, token);
_context.Restore(entity);
await _context.SaveChangesAsync(token);
return Ok(await _context.SaveChangesAsync(token) > 0);
return Ok(true);
});
}
}
Expand Down
Loading

0 comments on commit 05a83dd

Please sign in to comment.