-
Notifications
You must be signed in to change notification settings - Fork 2
Hamza/feature/bus gateway #188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
|
||
| private class Validate : AbstractValidator<BusGatewayCreate> | ||
| { | ||
| public Validate() | ||
| { | ||
| RuleFor(i => i.Name).NotEmpty().MaximumLength(200); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| }; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.