Skip to content

Commit

Permalink
New - Ajout d'une property HasProducts aux producteurs pour affiner l…
Browse files Browse the repository at this point in the history
…a map landing (#10)

* New - Ajout d'une property HasProducts aux producteurs pour affiner la map du site landing

* Fix - Correction SQL
  • Loading branch information
noelmugnier authored Feb 11, 2021
1 parent 562c5dc commit ad2a7f0
Show file tree
Hide file tree
Showing 15 changed files with 3,777 additions and 220 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Sheaft.Application.Common.Interfaces.Services;
using Sheaft.Application.Common.Models;
using Sheaft.Application.Common.Models.Inputs;
using Sheaft.Application.Producer.Commands;
using Sheaft.Domain;
using Sheaft.Domain.Enum;

Expand Down Expand Up @@ -76,13 +77,10 @@ public async Task<Result<Guid>> Handle(CreateDeliveryModeCommand request, Cancel
entity.SetAutoCompleteRelatedPurchaseOrders(request.AutoCompleteRelatedPurchaseOrder);
entity.SetMaxPurchaseOrdersPerTimeSlot(request.MaxPurchaseOrdersPerTimeSlot);

if (request.Kind == DeliveryKind.Collective || request.Kind == DeliveryKind.Farm ||
request.Kind == DeliveryKind.Market)
producer.CanDirectSell = true;

await _context.AddAsync(entity, token);
await _context.SaveChangesAsync(token);


_mediatr.Post(new UpdateProducerAvailabilityCommand(request.RequestUser) {ProducerId = entity.Producer.Id});
return Success(entity.Id);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Sheaft.Application.Common.Interfaces;
using Sheaft.Application.Common.Interfaces.Services;
using Sheaft.Application.Common.Models;
using Sheaft.Application.Producer.Commands;
using Sheaft.Domain;
using Sheaft.Domain.Enum;

Expand Down Expand Up @@ -45,13 +46,10 @@ public async Task<Result> Handle(DeleteDeliveryModeCommand request, Cancellation
return Failure(MessageKind.DeliveryMode_CannotRemove_With_Active_Agreements, entity.Name,
activeAgreements);

_context.Remove(entity);
entity.Producer.CanDirectSell = await _context.DeliveryModes.AnyAsync(
c => !c.RemovedOn.HasValue && c.Producer.Id == entity.Producer.Id &&
(c.Kind == DeliveryKind.Collective || c.Kind == DeliveryKind.Farm ||
c.Kind == DeliveryKind.Market), token);

_context.Remove(entity);
await _context.SaveChangesAsync(token);

_mediatr.Post(new UpdateProducerAvailabilityCommand(request.RequestUser) {ProducerId = entity.Producer.Id});
return Success();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Sheaft.Application.Common.Interfaces;
using Sheaft.Application.Common.Interfaces.Services;
using Sheaft.Application.Common.Models;
using Sheaft.Application.Producer.Commands;
using Sheaft.Domain;
using Sheaft.Domain.Enum;

Expand Down Expand Up @@ -42,13 +43,10 @@ public async Task<Result> Handle(RestoreDeliveryModeCommand request, Cancellatio
await _context.DeliveryModes.SingleOrDefaultAsync(a => a.Id == request.DeliveryModeId && a.RemovedOn.HasValue,
token);

_context.Restore(entity);
entity.Producer.CanDirectSell = await _context.DeliveryModes.AnyAsync(
c => !c.RemovedOn.HasValue && c.Producer.Id == entity.Producer.Id &&
(c.Kind == DeliveryKind.Collective || c.Kind == DeliveryKind.Farm ||
c.Kind == DeliveryKind.Market), token);

_context.Restore(entity);
await _context.SaveChangesAsync(token);

_mediatr.Post(new UpdateProducerAvailabilityCommand(request.RequestUser) {ProducerId = entity.Producer.Id});
return Success();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Sheaft.Application.Common.Interfaces.Services;
using Sheaft.Application.Common.Models;
using Sheaft.Application.Common.Models.Inputs;
using Sheaft.Application.Producer.Commands;
using Sheaft.Domain;
using Sheaft.Domain.Enum;

Expand Down Expand Up @@ -77,18 +78,9 @@ public async Task<Result> Handle(UpdateDeliveryModeCommand request, Cancellation
entity.SetOpeningHours(openingHours);
}

if (request.Kind == DeliveryKind.Collective || request.Kind == DeliveryKind.Farm ||
request.Kind == DeliveryKind.Market)
entity.Producer.CanDirectSell = true;
else
{
entity.Producer.CanDirectSell = await _context.DeliveryModes.AnyAsync(
c => !c.RemovedOn.HasValue && c.Producer.Id == entity.Producer.Id &&
(c.Kind == DeliveryKind.Collective || c.Kind == DeliveryKind.Farm ||
c.Kind == DeliveryKind.Market), token);
}

await _context.SaveChangesAsync(token);

_mediatr.Post(new UpdateProducerAvailabilityCommand(request.RequestUser) {ProducerId = entity.Producer.Id});
return Success();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public async Task<Result<IEnumerable<Guid>>> Handle(CreateBusinessOrderCommand r

var invalidProductIds = products
.Where(p => p.RemovedOn.HasValue || !p.Available || !p.VisibleToStores ||
p.Producer.RemovedOn.HasValue || !p.Producer.CanDirectSell)
p.Producer.RemovedOn.HasValue)
.Select(p => p.Id.ToString("N"));
if (invalidProductIds.Any())
return Failure<IEnumerable<Guid>>(MessageKind.Order_CannotCreate_Some_Products_Invalid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,19 @@ public async Task<Result> Handle(GenerateProducersFileCommand request, Cancellat

internal class ProducerListItem
{
internal ProducerListItem(Domain.User user)
internal ProducerListItem(Domain.Producer user)
{
Address = new AddressItem(user.Address);
Id = user.Id.ToString("N");
Name = user.Name;
Picture = user.Picture;
HasProducts = user.HasProducts;
}

public string Id { get; set; }
public string Name { get; set; }
public string Picture { get; set; }
public bool HasProducts { get; set; }
public AddressItem Address { get; set; }
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Sheaft.Application.Common;
using Sheaft.Application.Common.Handlers;
using Sheaft.Application.Common.Interfaces;
using Sheaft.Application.Common.Interfaces.Services;
using Sheaft.Application.Common.Models;
using Sheaft.Application.Common.Options;
using Sheaft.Application.Legal.Commands;
using Sheaft.Application.Wallet.Commands;
using Sheaft.Domain;
using Sheaft.Domain.Enum;

namespace Sheaft.Application.Producer.Commands
{
public class UpdateProducerAvailabilityCommand : Command
{
[JsonConstructor]
public UpdateProducerAvailabilityCommand(RequestUser requestUser) : base(requestUser)
{
}

public Guid ProducerId { get; set; }
}

public class UpdateProducerAvailabilityCommandHandler : CommandsHandler,
IRequestHandler<UpdateProducerAvailabilityCommand, Result>
{
public UpdateProducerAvailabilityCommandHandler(
IAppDbContext context,
ISheaftMediatr mediatr,
ILogger<UpdateProducerAvailabilityCommandHandler> logger)
: base(mediatr, context, logger)
{
}

public async Task<Result> Handle(UpdateProducerAvailabilityCommand request, CancellationToken token)
{
var producer = await _context.FindByIdAsync<Domain.Producer>(request.ProducerId, token);
producer.CanDirectSell = await _context.DeliveryModes.AnyAsync(
c => !c.RemovedOn.HasValue && c.Producer.Id == producer.Id &&
(c.Kind == DeliveryKind.Collective || c.Kind == DeliveryKind.Farm ||
c.Kind == DeliveryKind.Market), token);

await _context.SaveChangesAsync(token);
return Success();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Sheaft.Application.Common;
using Sheaft.Application.Common.Handlers;
using Sheaft.Application.Common.Interfaces;
using Sheaft.Application.Common.Interfaces.Services;
using Sheaft.Application.Common.Models;
using Sheaft.Application.Common.Options;
using Sheaft.Application.Legal.Commands;
using Sheaft.Application.Wallet.Commands;
using Sheaft.Domain;
using Sheaft.Domain.Enum;

namespace Sheaft.Application.Producer.Commands
{
public class UpdateProducerProductsCommand : Command
{
[JsonConstructor]
public UpdateProducerProductsCommand(RequestUser requestUser) : base(requestUser)
{
}

public Guid ProducerId { get; set; }
}

public class UpdateProducerProductsCommandHandler : CommandsHandler,
IRequestHandler<UpdateProducerProductsCommand, Result>
{
public UpdateProducerProductsCommandHandler(
IAppDbContext context,
ISheaftMediatr mediatr,
ILogger<UpdateProducerProductsCommandHandler> logger)
: base(mediatr, context, logger)
{
}

public async Task<Result> Handle(UpdateProducerProductsCommand request, CancellationToken token)
{
var producer = await _context.FindByIdAsync<Domain.Producer>(request.ProducerId, token);
producer.HasProducts = await _context.Products.AnyAsync(p => p.Producer.Id == producer.Id, token);

await _context.SaveChangesAsync(token);
return Success();
}
}
}
3 changes: 3 additions & 0 deletions Sheaft.Application/Product/Commands/CreateProductCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ public async Task<Result<Guid>> Handle(CreateProductCommand request, Cancellatio
_mediatr.Post(new UpdateProducerTagsCommand(request.RequestUser)
{ProducerId = request.ProducerId});

_mediatr.Post(new UpdateProducerProductsCommand(request.RequestUser)
{ProducerId = request.ProducerId});

return Success(entity.Id);
}
}
Expand Down
5 changes: 4 additions & 1 deletion Sheaft.Application/Product/Commands/DeleteProductCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Sheaft.Application.Common.Interfaces;
using Sheaft.Application.Common.Interfaces.Services;
using Sheaft.Application.Common.Models;
using Sheaft.Application.Producer.Commands;
using Sheaft.Domain;

namespace Sheaft.Application.Product.Commands
Expand Down Expand Up @@ -37,9 +38,11 @@ public DeleteProductCommandHandler(
public async Task<Result> Handle(DeleteProductCommand request, CancellationToken token)
{
var entity = await _context.GetByIdAsync<Domain.Product>(request.ProductId, token);

_context.Remove(entity);

await _context.SaveChangesAsync(token);

_mediatr.Post(new UpdateProducerProductsCommand(request.RequestUser) {ProducerId = entity.Producer.Id});
return Success();
}
}
Expand Down
5 changes: 4 additions & 1 deletion Sheaft.Application/Product/Commands/RestoreProductCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Sheaft.Application.Common.Interfaces;
using Sheaft.Application.Common.Interfaces.Services;
using Sheaft.Application.Common.Models;
using Sheaft.Application.Producer.Commands;
using Sheaft.Domain;

namespace Sheaft.Application.Product.Commands
Expand Down Expand Up @@ -39,9 +40,11 @@ public async Task<Result> Handle(RestoreProductCommand request, CancellationToke
{
var entity =
await _context.Products.SingleOrDefaultAsync(a => a.Id == request.ProductId && a.RemovedOn.HasValue, token);

_context.Restore(entity);

await _context.SaveChangesAsync(token);

_mediatr.Post(new UpdateProducerProductsCommand(request.RequestUser) {ProducerId = entity.Producer.Id});
return Success();
}
}
Expand Down
1 change: 1 addition & 0 deletions Sheaft.Domain/Producer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public Producer(Guid id, string name, string firstname, string lastname, string

public virtual IReadOnlyCollection<ProducerTag> Tags => _tags?.AsReadOnly();

public bool HasProducts { get; set; }
public bool CanDirectSell { get; set; }
public bool NotSubjectToVat { get; private set; }

Expand Down
Loading

0 comments on commit ad2a7f0

Please sign in to comment.