Moved using statements to globalusing file in webhooks.api

This commit is contained in:
Sumit Ghosh 2021-10-12 19:28:22 +05:30
parent ac9874e44a
commit 2cdb56fb69
27 changed files with 704 additions and 881 deletions

View File

@ -1,15 +1,11 @@
using Microsoft.AspNetCore.Mvc;
namespace Webhooks.API.Controllers;
namespace Webhooks.API.Controllers
public class HomeController : Controller
{
public class HomeController : Controller
{
// GET: /<controller>/
public IActionResult Index()
{
return new RedirectResult("~/swagger");
}
}
}

View File

@ -1,12 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Webhooks.API.Model;
namespace Webhooks.API.Controllers;
namespace Webhooks.API.Controllers
public class WebhookSubscriptionRequest : IValidatableObject
{
public class WebhookSubscriptionRequest : IValidatableObject
{
public string Url { get; set; }
public string Token { get; set; }
public string Event { get; set; }
@ -31,5 +26,4 @@ namespace Webhooks.API.Controllers
}
}
}
}

View File

@ -1,21 +1,9 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Webhooks.API.Infrastructure;
using Webhooks.API.Model;
using Webhooks.API.Services;
namespace Webhooks.API.Controllers;
namespace Webhooks.API.Controllers
[Route("api/v1/[controller]")]
[ApiController]
public class WebhooksController : ControllerBase
{
[Route("api/v1/[controller]")]
[ApiController]
public class WebhooksController : ControllerBase
{
private readonly WebhooksContext _dbContext;
private readonly IIdentityService _identityService;
private readonly IGrantUrlTesterService _grantUrlTester;
@ -109,7 +97,4 @@ namespace Webhooks.API.Controllers
return NotFound($"Subscriptions {id} not found");
}
}
}

View File

@ -1,8 +1,5 @@
using System;
namespace Webhooks.API.Exceptions;
namespace Webhooks.API.Exceptions
public class WebhooksDomainException : Exception
{
public class WebhooksDomainException : Exception
{
}
}

View File

@ -1,13 +1,9 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Webhooks.API.Infrastructure.ActionResult;
namespace Webhooks.API.Infrastructure.ActionResult
class InternalServerErrorObjectResult : ObjectResult
{
class InternalServerErrorObjectResult : ObjectResult
{
public InternalServerErrorObjectResult(object error) : base(error)
{
StatusCode = StatusCodes.Status500InternalServerError;
}
}
}

View File

@ -1,13 +1,7 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Collections.Generic;
using System.Linq;
namespace Webhooks.API.Infrastructure;
namespace Webhooks.API.Infrastructure
public class AuthorizeCheckOperationFilter : IOperationFilter
{
public class AuthorizeCheckOperationFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
// Check for authorize attribute
@ -32,5 +26,4 @@ namespace Webhooks.API.Infrastructure
}
};
}
}
}

View File

@ -1,17 +1,7 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System.Net;
using Webhooks.API.Exceptions;
using Webhooks.API.Infrastructure.ActionResult;
namespace Webhooks.API.Infrastructure;
namespace Webhooks.API.Infrastructure
public class HttpGlobalExceptionFilter : IExceptionFilter
{
public class HttpGlobalExceptionFilter : IExceptionFilter
{
private readonly IWebHostEnvironment env;
private readonly ILogger<HttpGlobalExceptionFilter> logger;
@ -65,5 +55,4 @@ namespace Webhooks.API.Infrastructure
public object DeveloperMeesage { get; set; }
}
}
}

View File

@ -1,20 +1,16 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Webhooks.API.Model;
namespace Webhooks.API.Infrastructure;
namespace Webhooks.API.Infrastructure
public class WebhooksContext : DbContext
{
public class WebhooksContext : DbContext
{
public WebhooksContext(DbContextOptions<WebhooksContext> options) : base(options)
{
}
public DbSet<WebhookSubscription> Subscriptions { get; set; }
}
}
public class WebhooksContextDesignFactory : IDesignTimeDbContextFactory<WebhooksContext>
{
public class WebhooksContextDesignFactory : IDesignTimeDbContextFactory<WebhooksContext>
{
public WebhooksContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<WebhooksContext>()
@ -22,5 +18,4 @@ namespace Webhooks.API.Infrastructure
return new WebhooksContext(optionsBuilder.Options);
}
}
}

View File

@ -1,10 +1,7 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
using System.Collections.Generic;
namespace Webhooks.API.IntegrationEvents;
namespace Webhooks.API.IntegrationEvents
public record OrderStatusChangedToPaidIntegrationEvent : IntegrationEvent
{
public record OrderStatusChangedToPaidIntegrationEvent : IntegrationEvent
{
public int OrderId { get; }
public IEnumerable<OrderStockItem> OrderStockItems { get; }
@ -14,10 +11,10 @@ namespace Webhooks.API.IntegrationEvents
OrderId = orderId;
OrderStockItems = orderStockItems;
}
}
}
public record OrderStockItem
{
public record OrderStockItem
{
public int ProductId { get; }
public int Units { get; }
@ -26,5 +23,4 @@ namespace Webhooks.API.IntegrationEvents
ProductId = productId;
Units = units;
}
}
}

View File

@ -1,14 +1,7 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using Microsoft.Extensions.Logging;
using System.Linq;
using System.Threading.Tasks;
using Webhooks.API.Model;
using Webhooks.API.Services;
namespace Webhooks.API.IntegrationEvents;
namespace Webhooks.API.IntegrationEvents
public class OrderStatusChangedToPaidIntegrationEventHandler : IIntegrationEventHandler<OrderStatusChangedToPaidIntegrationEvent>
{
public class OrderStatusChangedToPaidIntegrationEventHandler : IIntegrationEventHandler<OrderStatusChangedToPaidIntegrationEvent>
{
private readonly IWebhooksRetriever _retriever;
private readonly IWebhooksSender _sender;
private readonly ILogger _logger;
@ -26,5 +19,4 @@ namespace Webhooks.API.IntegrationEvents
var whook = new WebhookData(WebhookType.OrderPaid, @event);
await _sender.SendAll(subscriptions, whook);
}
}
}

View File

@ -1,9 +1,7 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
namespace Webhooks.API.IntegrationEvents;
namespace Webhooks.API.IntegrationEvents
public record OrderStatusChangedToShippedIntegrationEvent : IntegrationEvent
{
public record OrderStatusChangedToShippedIntegrationEvent : IntegrationEvent
{
public int OrderId { get; private init; }
public string OrderStatus { get; private init; }
public string BuyerName { get; private init; }
@ -14,5 +12,4 @@ namespace Webhooks.API.IntegrationEvents
OrderStatus = orderStatus;
BuyerName = buyerName;
}
}
}

View File

@ -1,14 +1,7 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using Microsoft.Extensions.Logging;
using System.Linq;
using System.Threading.Tasks;
using Webhooks.API.Model;
using Webhooks.API.Services;
namespace Webhooks.API.IntegrationEvents;
namespace Webhooks.API.IntegrationEvents
public class OrderStatusChangedToShippedIntegrationEventHandler : IIntegrationEventHandler<OrderStatusChangedToShippedIntegrationEvent>
{
public class OrderStatusChangedToShippedIntegrationEventHandler : IIntegrationEventHandler<OrderStatusChangedToShippedIntegrationEvent>
{
private readonly IWebhooksRetriever _retriever;
private readonly IWebhooksSender _sender;
private readonly ILogger _logger;
@ -26,5 +19,4 @@ namespace Webhooks.API.IntegrationEvents
var whook = new WebhookData(WebhookType.OrderShipped, @event);
await _sender.SendAll(subscriptions, whook);
}
}
}

View File

@ -1,9 +1,7 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
namespace Webhooks.API.IntegrationEvents;
namespace Webhooks.API.IntegrationEvents
public record ProductPriceChangedIntegrationEvent : IntegrationEvent
{
public record ProductPriceChangedIntegrationEvent : IntegrationEvent
{
public int ProductId { get; private init; }
public decimal NewPrice { get; private init; }
@ -16,5 +14,4 @@ namespace Webhooks.API.IntegrationEvents
NewPrice = newPrice;
OldPrice = oldPrice;
}
}
}

View File

@ -1,13 +1,9 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using System.Threading.Tasks;
namespace Webhooks.API.IntegrationEvents;
namespace Webhooks.API.IntegrationEvents
public class ProductPriceChangedIntegrationEventHandler : IIntegrationEventHandler<ProductPriceChangedIntegrationEvent>
{
public class ProductPriceChangedIntegrationEventHandler : IIntegrationEventHandler<ProductPriceChangedIntegrationEvent>
{
public async Task Handle(ProductPriceChangedIntegrationEvent @event)
{
int i = 0;
}
}
}

View File

@ -1,10 +1,7 @@
using System;
using System.Text.Json;
namespace Webhooks.API.Model;
namespace Webhooks.API.Model
public class WebhookData
{
public class WebhookData
{
public DateTime When { get; }
public string Payload { get; }
@ -17,5 +14,4 @@ namespace Webhooks.API.Model
Type = hookType.ToString();
Payload = JsonSerializer.Serialize(data);
}
}
}

View File

@ -1,9 +1,7 @@
using System;
namespace Webhooks.API.Model;
namespace Webhooks.API.Model
public class WebhookSubscription
{
public class WebhookSubscription
{
public int Id { get; set; }
public WebhookType Type { get; set; }
@ -11,5 +9,4 @@ namespace Webhooks.API.Model
public string DestUrl { get; set; }
public string Token { get; set; }
public string UserId { get; set; }
}
}

View File

@ -1,9 +1,8 @@
namespace Webhooks.API.Model
namespace Webhooks.API.Model;
public enum WebhookType
{
public enum WebhookType
{
CatalogItemPriceChange = 1,
OrderShipped = 2,
OrderPaid = 3
}
}

View File

@ -1,11 +1,4 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Webhooks.API;
using Webhooks.API.Infrastructure;
CreateWebHostBuilder(args).Build()
CreateWebHostBuilder(args).Build()
.MigrateDbContext<WebhooksContext>((_, __) => { })
.Run();

View File

@ -1,13 +1,7 @@
using Microsoft.Extensions.Logging;
using System;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
namespace Webhooks.API.Services;
namespace Webhooks.API.Services
class GrantUrlTesterService : IGrantUrlTesterService
{
class GrantUrlTesterService : IGrantUrlTesterService
{
private readonly IHttpClientFactory _clientFactory;
private readonly ILogger _logger;
public GrantUrlTesterService(IHttpClientFactory factory, ILogger<IGrantUrlTesterService> logger)
@ -53,5 +47,4 @@ namespace Webhooks.API.Services
firstUrl.Port == secondUrl.Port &&
firstUrl.Host == firstUrl.Host;
}
}
}

View File

@ -1,9 +1,6 @@
using System.Threading.Tasks;
namespace Webhooks.API.Services;
namespace Webhooks.API.Services
public interface IGrantUrlTesterService
{
public interface IGrantUrlTesterService
{
Task<bool> TestGrantUrl(string urlHook, string url, string token);
}
}

View File

@ -1,7 +1,6 @@
namespace Webhooks.API.Services
namespace Webhooks.API.Services;
public interface IIdentityService
{
public interface IIdentityService
{
string GetUserIdentity();
}
}

View File

@ -1,12 +1,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Webhooks.API.Model;
namespace Webhooks.API.Services;
namespace Webhooks.API.Services
public interface IWebhooksRetriever
{
public interface IWebhooksRetriever
{
Task<IEnumerable<WebhookSubscription>> GetSubscriptionsOfType(WebhookType type);
}
}

View File

@ -1,11 +1,6 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Webhooks.API.Model;
namespace Webhooks.API.Services;
namespace Webhooks.API.Services
public interface IWebhooksSender
{
public interface IWebhooksSender
{
Task SendAll(IEnumerable<WebhookSubscription> receivers, WebhookData data);
}
}

View File

@ -1,11 +1,7 @@

using Microsoft.AspNetCore.Http;
using System;
namespace Webhooks.API.Services;
namespace Webhooks.API.Services
public class IdentityService : IIdentityService
{
public class IdentityService : IIdentityService
{
private IHttpContextAccessor _context;
public IdentityService(IHttpContextAccessor context)
@ -17,5 +13,4 @@ namespace Webhooks.API.Services
{
return _context.HttpContext.User.FindFirst("sub").Value;
}
}
}

View File

@ -1,14 +1,7 @@
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Webhooks.API.Infrastructure;
using Webhooks.API.Model;
namespace Webhooks.API.Services;
namespace Webhooks.API.Services
public class WebhooksRetriever : IWebhooksRetriever
{
public class WebhooksRetriever : IWebhooksRetriever
{
private readonly WebhooksContext _db;
public WebhooksRetriever(WebhooksContext db)
{
@ -19,5 +12,4 @@ namespace Webhooks.API.Services
var data = await _db.Subscriptions.Where(s => s.Type == type).ToListAsync();
return data;
}
}
}

View File

@ -1,17 +1,7 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Webhooks.API.Model;
namespace Webhooks.API.Services;
namespace Webhooks.API.Services
public class WebhooksSender : IWebhooksSender
{
public class WebhooksSender : IWebhooksSender
{
private readonly IHttpClientFactory _httpClientFactory;
private readonly ILogger _logger;
public WebhooksSender(IHttpClientFactory httpClientFactory, ILogger<WebhooksSender> logger)
@ -45,5 +35,4 @@ namespace Webhooks.API.Services
return client.SendAsync(request);
}
}
}

View File

@ -1,38 +1,7 @@
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Devspaces.Support;
using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.ServiceBus;
using Microsoft.EntityFrameworkCore;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus;
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.IdentityModel.Tokens.Jwt;
using System.Reflection;
using System.Threading;
using Webhooks.API.Infrastructure;
using Webhooks.API.IntegrationEvents;
using Webhooks.API.Services;
namespace Webhooks.API;
namespace Webhooks.API
public class Startup
{
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
@ -122,10 +91,10 @@ namespace Webhooks.API
eventBus.Subscribe<OrderStatusChangedToShippedIntegrationEvent, OrderStatusChangedToShippedIntegrationEventHandler>();
eventBus.Subscribe<OrderStatusChangedToPaidIntegrationEvent, OrderStatusChangedToPaidIntegrationEventHandler>();
}
}
}
static class CustomExtensionMethods
{
static class CustomExtensionMethods
{
public static IServiceCollection AddAppInsight(this IServiceCollection services, IConfiguration configuration)
{
services.AddApplicationInsightsTelemetry(configuration);
@ -346,5 +315,4 @@ namespace Webhooks.API
return services;
}
}
}