Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions SW.Bitween.Api/Data/BitweenDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,35 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.OnDelete(DeleteBehavior.Restrict);
});

modelBuilder.Entity<BusGateway>(bg =>
{
bg.ToTable("BusGateways");
bg.HasKey(i => i.Id);
bg.Property(i => i.Id).ValueGeneratedOnAdd();
bg.Property(p => p.Name).IsRequired().HasMaxLength(200);
bg.HasOne<Document>().WithMany().HasForeignKey(p => p.DocumentId)
.OnDelete(DeleteBehavior.Restrict);
bg.HasMany(p => p.Routes).WithOne(p => p.BusGateway).HasForeignKey(p => p.BusGatewayId)
.OnDelete(DeleteBehavior.Restrict);
});

modelBuilder.Entity<BusGatewayRoute>(bgr =>
{
bgr.ToTable("BusGatewayRoutes");
bgr.HasKey(i => i.Id);
bgr.Property(i => i.Id).ValueGeneratedOnAdd();
bgr.HasOne(p => p.BusGateway).WithMany(p => p.Routes).HasForeignKey(p => p.BusGatewayId)
.OnDelete(DeleteBehavior.Restrict);
bgr.HasOne(p => p.Subscription).WithMany().HasForeignKey(p => p.SubscriptionId)
.OnDelete(DeleteBehavior.Restrict);
bgr.HasOne(p => p.Partner).WithMany().HasForeignKey(p => p.PartnerId)
.IsRequired(false).OnDelete(DeleteBehavior.Restrict);
bgr.Property(p => p.MatchExpression).HasConversion(
domainObject =>
domainObject == null ? null : MatchSpecValueConverter.SerializeMatchSpec(domainObject),
dbString => dbString == null ? null : MatchSpecValueConverter.DeserializeMatchSpec(dbString));
});

modelBuilder.Entity<GlobalAdapterValuesSet>(gav =>
{
gav.ToTable("GlobalAdapterValuesSets");
Expand Down
16 changes: 16 additions & 0 deletions SW.Bitween.Api/Domain/Gateway/BusGateway.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using SW.PrimitiveTypes;

namespace SW.Bitween.Domain.Gateway;

public class BusGateway : BaseEntity, IAudited
{
public string Name { get; set; }
public int DocumentId { get; set; }
public ICollection<BusGatewayRoute> Routes { get; set; }
public DateTime CreatedOn { get; set; }
public string CreatedBy { get; set; }
public DateTime? ModifiedOn { get; set; }
public string ModifiedBy { get; set; }
}
25 changes: 25 additions & 0 deletions SW.Bitween.Api/Domain/Gateway/BusGatewayRoute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using SW.Bitween.Model;
using SW.PrimitiveTypes;

namespace SW.Bitween.Domain.Gateway;

public class BusGatewayRoute : BaseEntity, IAudited
{
public BusGateway BusGateway { get; set; }
public int BusGatewayId { get; set; }
public Subscription Subscription { get; set; }
public int SubscriptionId { get; set; }

// Optional: supplies partner values (__partner__ / {{partner.KEY}}) to the assigned subscription.
public Partner Partner { get; set; }
public int? PartnerId { get; set; }

// Filter over the bound document's promoted properties. Matching runs the assigned subscription.
public IPropertyMatchSpecification MatchExpression { get; set; }

public DateTime CreatedOn { get; set; }
public string CreatedBy { get; set; }
public DateTime? ModifiedOn { get; set; }
public string ModifiedBy { get; set; }
}
2 changes: 1 addition & 1 deletion SW.Bitween.Api/Domain/Subscription/Subscription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Subscription(string name, int documentId, SubscriptionType type) : this(n
type, null)
{
Inactive = true;
if (type != SubscriptionType.GatewayApiCall)
if (type != SubscriptionType.GatewayApiCall && type != SubscriptionType.BusGateway)
throw new ArgumentException();
}
private Subscription(string name, int documentId, SubscriptionType type, int? partnerId = null,
Expand Down
2 changes: 2 additions & 0 deletions SW.Bitween.Api/Domain/Xchange/Xchange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public Xchange(int documentId, IWorkGroup workGroup, XchangeFile file, string[]
SubscriptionType.Internal => new InternalXchangeCreatedEvent(),
SubscriptionType.ApiCall => new ApiXchangeCreatedEvent(),
SubscriptionType.GatewayApiCall => new ApiXchangeCreatedEvent(),
// Bus-gateway xchanges are bus-triggered async processing, like Internal.
SubscriptionType.BusGateway => new InternalXchangeCreatedEvent(),
SubscriptionType.Receiving => new ReceivingXchangeCreatedEvent(),
SubscriptionType.Aggregation => new AggregateXchangeCreatedEvent(),
_ => throw new ArgumentOutOfRangeException(nameof(subscriptionType), subscriptionType, null)
Expand Down
2 changes: 2 additions & 0 deletions SW.Bitween.Api/Interfaces/IInfolinkCache.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System.Threading.Tasks;
using SW.Bitween.Domain;
using SW.Bitween.Domain.Gateway;

namespace SW.Bitween;

public interface IInfolinkCache
{
public Task<Subscription[]> ListSubscriptionsByDocumentAsync(int documentId);
public Task<BusGatewayRoute[]> ListBusGatewayRoutesByDocumentAsync(int documentId);
public Task<Notifier[]> ListNotifiersAsync();

public Task<Subscription> SubscriptionByIdAsync(int subscriptionId);
Expand Down
78 changes: 78 additions & 0 deletions SW.Bitween.Api/Resources/BusGateways/AddRoute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using Microsoft.EntityFrameworkCore;
using SW.Bitween.Domain;
using SW.Bitween.Domain.Accounts;
using SW.Bitween.Domain.Gateway;
using SW.Bitween.Model;
using SW.PrimitiveTypes;
using System.Threading.Tasks;

namespace SW.Bitween.Resources.BusGateways
{
[HandlerName(nameof(AddRoute))]
public class AddRoute : ICommandHandler<int, BusGatewayRouteCreate, object>
{
private readonly BitweenDbContext _dbContext;
private readonly RequestContext _requestContext;
private readonly IInfolinkCache _cache;

public AddRoute(BitweenDbContext dbContext, RequestContext requestContext, IInfolinkCache cache)
{
_dbContext = dbContext;
_requestContext = requestContext;
_cache = cache;
}

public async Task<object> Handle(int gatewayId, BusGatewayRouteCreate model)
{
_requestContext.EnsureAccess(AccountRole.Admin, AccountRole.Member);

var gateway = await _dbContext.Set<BusGateway>()
.FirstOrDefaultAsync(bg => bg.Id == gatewayId);

if (gateway == null)
throw new SWNotFoundException($"BusGateway with Id {gatewayId} not found");

await ValidateSubscription(_dbContext, model.SubscriptionId, gateway.DocumentId);
await ValidatePartner(_dbContext, model.PartnerId);

var route = new BusGatewayRoute
{
BusGatewayId = gatewayId,
SubscriptionId = model.SubscriptionId,
PartnerId = model.PartnerId,
MatchExpression = model.MatchExpression
};

_dbContext.Add(route);
await _dbContext.SaveChangesAsync();
await _cache.BroadcastRevoke();
return route.Id;
}

internal static async Task ValidateSubscription(BitweenDbContext dbContext, int subscriptionId,
int gatewayDocumentId)
{
var subscription = await dbContext.Set<Subscription>()
.FirstOrDefaultAsync(s => s.Id == subscriptionId);

if (subscription == null)
throw new SWNotFoundException($"Subscription with Id {subscriptionId} not found");

if (subscription.Type != SubscriptionType.BusGateway)
throw new SWException($"Subscription must be of type BusGateway. Current type: {subscription.Type}");

if (subscription.DocumentId != gatewayDocumentId)
throw new SWException("Subscription must be bound to the same document as the bus gateway");
}

internal static async Task ValidatePartner(BitweenDbContext dbContext, int? partnerId)
{
if (!partnerId.HasValue)
return;

var partnerExists = await dbContext.Set<Partner>().AnyAsync(p => p.Id == partnerId.Value);
if (!partnerExists)
throw new SWNotFoundException($"Partner with Id {partnerId.Value} not found");
}
}
}
53 changes: 53 additions & 0 deletions SW.Bitween.Api/Resources/BusGateways/Create.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using FluentValidation;
using Microsoft.EntityFrameworkCore;
using SW.Bitween.Domain;
using SW.Bitween.Domain.Accounts;
using SW.Bitween.Domain.Gateway;
using SW.Bitween.Model;
using SW.PrimitiveTypes;
using System.Threading.Tasks;

namespace SW.Bitween.Resources.BusGateways
{
public class Create : ICommandHandler<BusGatewayCreate, object>
{
private readonly BitweenDbContext _dbContext;
private readonly RequestContext _requestContext;
private readonly IInfolinkCache _cache;

public Create(BitweenDbContext dbContext, RequestContext requestContext, IInfolinkCache cache)
{
_dbContext = dbContext;
_requestContext = requestContext;
_cache = cache;
}

public async Task<object> Handle(BusGatewayCreate model)
{
_requestContext.EnsureAccess(AccountRole.Admin, AccountRole.Member);

var documentExists = await _dbContext.Set<Document>().AnyAsync(d => d.Id == model.DocumentId);
if (!documentExists)
throw new SWNotFoundException($"Document with Id {model.DocumentId} not found");

var entity = new BusGateway
{
Name = model.Name,
DocumentId = model.DocumentId
};

_dbContext.Add(entity);
await _dbContext.SaveChangesAsync();
await _cache.BroadcastRevoke();
return entity.Id;
}
Comment thread
hamzahalq marked this conversation as resolved.

private class Validate : AbstractValidator<BusGatewayCreate>
{
public Validate()
{
RuleFor(i => i.Name).NotEmpty().MaximumLength(200);
}
}
}
}
43 changes: 43 additions & 0 deletions SW.Bitween.Api/Resources/BusGateways/Delete.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Microsoft.EntityFrameworkCore;
using SW.Bitween.Domain.Accounts;
using SW.Bitween.Domain.Gateway;
using SW.PrimitiveTypes;
using System.Threading.Tasks;

namespace SW.Bitween.Resources.BusGateways
{
public class Delete : IDeleteHandler<int, object>
{
private readonly BitweenDbContext _dbContext;
private readonly RequestContext _requestContext;
private readonly IInfolinkCache _cache;

public Delete(BitweenDbContext dbContext, RequestContext requestContext, IInfolinkCache cache)
{
_dbContext = dbContext;
_requestContext = requestContext;
_cache = cache;
}

public async Task<object> Handle(int key)
{
_requestContext.EnsureAccess(AccountRole.Admin, AccountRole.Member);

var gateway = await _dbContext.Set<BusGateway>()
.Include(bg => bg.Routes)
.FirstOrDefaultAsync(bg => bg.Id == key);

if (gateway == null)
throw new SWNotFoundException($"BusGateway with Id {key} not found");

// Routes are FK-restricted to the gateway; remove them explicitly before the gateway.
if (gateway.Routes != null && gateway.Routes.Count > 0)
_dbContext.RemoveRange(gateway.Routes);

_dbContext.Remove(gateway);
await _dbContext.SaveChangesAsync();
await _cache.BroadcastRevoke();
return null;
}
}
}
57 changes: 57 additions & 0 deletions SW.Bitween.Api/Resources/BusGateways/Get.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Microsoft.EntityFrameworkCore;
using SW.Bitween.Domain;
using SW.Bitween.Domain.Gateway;
using SW.Bitween.Model;
using SW.PrimitiveTypes;
using System.Linq;
using System.Threading.Tasks;

namespace SW.Bitween.Resources.BusGateways
{
public class Get : IGetHandler<int, object>
{
private readonly BitweenDbContext _dbContext;

public Get(BitweenDbContext dbContext)
{
_dbContext = dbContext;
}

public async Task<object> Handle(int key)
{
var gateway = await _dbContext.Set<BusGateway>()
.AsNoTracking()
.Include(bg => bg.Routes)
.ThenInclude(r => r.Subscription)
.Include(bg => bg.Routes)
.ThenInclude(r => r.Partner)
.FirstOrDefaultAsync(bg => bg.Id == key);

if (gateway == null)
throw new SWNotFoundException($"BusGateway with id '{key}' was not found");

var documentName = await _dbContext.Set<Document>()
.Where(d => d.Id == gateway.DocumentId)
.Select(d => d.Name)
.FirstOrDefaultAsync();

return new BusGatewayRow
{
Id = gateway.Id,
Name = gateway.Name,
DocumentId = gateway.DocumentId,
DocumentName = documentName,
RoutesCount = gateway.Routes.Count,
Routes = gateway.Routes.Select(r => new BusGatewayRouteDto
{
Id = r.Id,
SubscriptionId = r.SubscriptionId,
SubscriptionName = r.Subscription != null ? r.Subscription.Name : null,
PartnerId = r.PartnerId,
PartnerName = r.Partner != null ? r.Partner.Name : null,
MatchExpression = r.MatchExpression
}).ToList()
};
}
}
}
40 changes: 40 additions & 0 deletions SW.Bitween.Api/Resources/BusGateways/RemoveRoute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Microsoft.EntityFrameworkCore;
using SW.Bitween.Domain.Accounts;
using SW.Bitween.Domain.Gateway;
using SW.Bitween.Model;
using SW.PrimitiveTypes;
using System.Threading.Tasks;

namespace SW.Bitween.Resources.BusGateways
{
[HandlerName(nameof(RemoveRoute))]
public class RemoveRoute : ICommandHandler<int, RemoveRouteRequest, object>
{
private readonly BitweenDbContext _dbContext;
private readonly RequestContext _requestContext;
private readonly IInfolinkCache _cache;

public RemoveRoute(BitweenDbContext dbContext, RequestContext requestContext, IInfolinkCache cache)
{
_dbContext = dbContext;
_requestContext = requestContext;
_cache = cache;
}

public async Task<object> Handle(int gatewayId, RemoveRouteRequest request)
{
_requestContext.EnsureAccess(AccountRole.Admin, AccountRole.Member);

var route = await _dbContext.Set<BusGatewayRoute>()
.FirstOrDefaultAsync(r => r.Id == request.RouteId && r.BusGatewayId == gatewayId);

if (route == null)
throw new SWNotFoundException($"Route with Id {request.RouteId} not found in gateway {gatewayId}");

_dbContext.Remove(route);
await _dbContext.SaveChangesAsync();
await _cache.BroadcastRevoke();
return null;
}
}
}
Loading