Skip to content

Commit 176642a

Browse files
authored
Merge pull request #2 from simcoder/development
Development
2 parents 286eabb + d7ee849 commit 176642a

37 files changed

+1114
-55
lines changed

.DS_Store

0 Bytes
Binary file not shown.

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
################################################################################
2+
# This .gitignore file was automatically created by Microsoft(R) Visual Studio.
3+
################################################################################
4+
5+
/src/.vs/GOC.ApiGateway/v15/sqlite3/storage.ide

src/GOC.ApiGateway/AppSettings.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System.Collections.Generic;
2+
using Microphone.Consul;
3+
4+
namespace GOC.ApiGateway
5+
{
6+
public class AppSettings
7+
{
8+
public CircuitBreakerSettings CircuitBreaker {get;set;}
9+
public WaitAndRetrySettings WaitAndRetry { get; set; }
10+
public ConsulOptions Consul { get; set; }
11+
public IdentitySettings Identity { get; set; }
12+
public RabbitMQSettings Rabbit { get; set; }
13+
}
14+
public class CircuitBreakerSettings
15+
{
16+
public double FailureThreshold { get; set; }
17+
public int SamplingDurationInSeconds { get; set; }
18+
public int MinimumThroughput { get; set; }
19+
public int DurationOfBreakInSeconds { get; set; }
20+
}
21+
public class WaitAndRetrySettings
22+
{
23+
public int RetryAttempts { get; set; }
24+
}
25+
public class IdentitySettings
26+
{
27+
public string Authority { get; set; }
28+
public string ApiName { get; set; }
29+
public string ApiSecret { get; set; }
30+
public string TokenEndpoint { get; set; }
31+
public string ApiClientId { get; set; }
32+
public string ApiClientSecret { get; set; }
33+
public string TokenEndpointUrl
34+
{
35+
get => $"{Authority}/{TokenEndpoint}";
36+
}
37+
public IEnumerable<ApiResource> Resources { get; set; }
38+
}
39+
//TODO do this in json config file
40+
public class ApiResource
41+
{
42+
public string ResourceFriendlyName { get; set; }
43+
public string ResourceName { get; set; }
44+
}
45+
public class RabbitMQSettings
46+
{
47+
public string Host { get; set; }
48+
}
49+
50+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Microsoft.Extensions.Logging;
3+
4+
namespace GOC.ApiGateway.Controllers
5+
{
6+
public class BaseController<T> : Controller where T : class
7+
{
8+
protected ILogger Logger;
9+
10+
public BaseController(ILoggerFactory loggerFactory)
11+
{
12+
Logger = loggerFactory.CreateLogger<T>();
13+
}
14+
}
15+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Threading.Tasks;
2+
using GOC.ApiGateway.Dtos;
3+
using GOC.ApiGateway.Interfaces;
4+
using Microsoft.AspNetCore.Authorization;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.Extensions.Logging;
7+
8+
// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
9+
10+
namespace GOC.ApiGateway.Controllers
11+
{
12+
[Authorize]
13+
[Route("api/[controller]")]
14+
public class CompaniesController : BaseController<CompaniesController>
15+
{
16+
private readonly ICompanyService _companyService;
17+
public CompaniesController(ICompanyService company, ILoggerFactory loggerFactory) : base(loggerFactory)
18+
{
19+
_companyService = company;
20+
}
21+
22+
[HttpPost]
23+
public async Task<IActionResult> Post([FromBody] CompanyPostDto company)
24+
{
25+
var result = await _companyService.CreateCompanyAsync(company);
26+
return Ok(result.Value);
27+
}
28+
}
29+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using GOC.ApiGateway.Dtos;
6+
using GOC.ApiGateway.Interfaces;
7+
using Microsoft.AspNetCore.Authorization;
8+
using Microsoft.AspNetCore.Mvc;
9+
using Microsoft.Extensions.Logging;
10+
11+
// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
12+
13+
namespace GOC.ApiGateway.Controllers
14+
{
15+
[Authorize]
16+
[Route("api/companies/{companyId}/[controller]")]
17+
public class InventoriesController : BaseController<InventoriesController>
18+
{
19+
private IInventoryService _inventoryService;
20+
public InventoriesController(IInventoryService inventoryService, ILoggerFactory loggerFactory) : base(loggerFactory)
21+
{
22+
_inventoryService = inventoryService;
23+
}
24+
25+
[HttpGet]
26+
public async Task<IActionResult> Get()
27+
{
28+
return View();
29+
}
30+
31+
[HttpGet("{id}")]
32+
public async Task<IActionResult> Get(Guid id)
33+
{
34+
return View();
35+
}
36+
37+
[HttpPost]
38+
public async Task<IActionResult> Post(Guid companyId, InventoryPostDto inventory)
39+
{
40+
inventory.CompanyId = companyId;
41+
var result = await _inventoryService.CreateInventoryAsync(inventory);
42+
return Ok(result.Value);
43+
}
44+
}
45+
}

src/GOC.ApiGateway/Controllers/ValuesController.cs

Lines changed: 0 additions & 44 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
7+
// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
8+
9+
namespace GOC.ApiGateway.Controllers
10+
{
11+
public class VendorsController : Controller
12+
{
13+
// GET: /<controller>/
14+
public IActionResult Index()
15+
{
16+
return View();
17+
}
18+
}
19+
}

src/GOC.ApiGateway/Dtos/AddressDto.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
namespace GOC.ApiGateway.Dtos
3+
{
4+
public class AddressDto
5+
{
6+
public string AddressLine1 { get; set; }
7+
8+
public string AddressLine2 { get; set; }
9+
10+
public string City { get; set; }
11+
12+
public string State { get; set; }
13+
14+
public string ZipCode { get; set; }
15+
16+
}
17+
}

src/GOC.ApiGateway/Dtos/CompanyDto.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
namespace GOC.ApiGateway.Dtos
3+
{
4+
public class CompanyDto
5+
{
6+
public Guid Id { get; set; }
7+
8+
public AddressDto Address { get; set; }
9+
10+
public string Name { get; set; }
11+
12+
public int UserId { get; set; }
13+
14+
public string PhoneNumber { get; set; }
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace GOC.ApiGateway.Dtos
2+
{
3+
public class CompanyPostDto
4+
{
5+
public AddressDto Address { get; set; }
6+
7+
public string Name { get; set; }
8+
9+
public int UserId { get; set; }
10+
11+
public string PhoneNumber { get; set; }
12+
}
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
namespace GOC.ApiGateway.Dtos
3+
{
4+
public class InventoryDto
5+
{
6+
public Guid Id { get; set; }
7+
8+
public string Name { get; set; }
9+
10+
public Guid CompanyId { get; set; }
11+
12+
public int UserId { get; set; }
13+
}
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
namespace GOC.ApiGateway.Dtos
3+
{
4+
public class InventoryPostDto
5+
{
6+
public string Name { get; set; }
7+
8+
public Guid CompanyId { get; set; }
9+
10+
public int UserId { get; set; }
11+
}
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace GOC.ApiGateway.Enums
2+
{
3+
public enum ServiceNameTypes
4+
{
5+
InventoryService,
6+
CrmService
7+
}
8+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.Linq;
3+
using Consul;
4+
using Microsoft.AspNetCore.Builder;
5+
using Microsoft.AspNetCore.Hosting;
6+
using Microsoft.AspNetCore.Hosting.Server.Features;
7+
using Microsoft.AspNetCore.Http.Features;
8+
using Microsoft.Extensions.DependencyInjection;
9+
using Microsoft.Extensions.Logging;
10+
using Microsoft.Extensions.Options;
11+
12+
namespace GOC.ApiGateway.Extensions
13+
{
14+
public static class ConsulExtension
15+
{
16+
//public static IApplicationBuilder RegisterWithConsul(this IApplicationBuilder app,
17+
//IApplicationLifetime lifetime)
18+
//{
19+
// // Retrieve Consul client from DI
20+
// var consulClient = app.ApplicationServices
21+
// .GetRequiredService<IConsulClient>();
22+
// var consulConfig = app.ApplicationServices
23+
// .GetRequiredService<IOptions<ConsulConfig>>();
24+
// // Setup logger
25+
// var loggingFactory = app.ApplicationServices
26+
// .GetRequiredService<ILoggerFactory>();
27+
// var logger = loggingFactory.CreateLogger<IApplicationBuilder>();
28+
29+
// // Get server IP address
30+
// var features = app.Properties["server.Features"] as FeatureCollection;
31+
// var addresses = features.Get<IServerAddressesFeature>();
32+
// var address = addresses.Addresses.First();
33+
34+
// // Register service with consul
35+
// var uri = new Uri(address);
36+
// var registration = new AgentServiceRegistration()
37+
// {
38+
// ID = $"{consulConfig.Value.ServiceID}-{uri.Port}",
39+
// Name = consulConfig.Value.ServiceName,
40+
// Address = $"{uri.Scheme}://{uri.Host}",
41+
// Port = uri.Port,
42+
// Tags = new[] { "Students", "Courses", "School" }
43+
// };
44+
45+
// logger.LogInformation("Registering with Consul");
46+
// consulClient.Agent.ServiceDeregister(registration.ID).Wait();
47+
// consulClient.Agent.ServiceRegister(registration).Wait();
48+
49+
// lifetime.ApplicationStopping.Register(() => {
50+
// logger.LogInformation("Deregistering from Consul");
51+
// consulClient.Agent.ServiceDeregister(registration.ID).Wait();
52+
// });
53+
// return app;
54+
//}
55+
}
56+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Net.Http;
3+
using System.Threading.Tasks;
4+
using Microsoft.AspNetCore.Http;
5+
using Newtonsoft.Json;
6+
7+
namespace GOC.ApiGateway.Extensions
8+
{
9+
public static class HttpExtensions
10+
{
11+
public static bool IsAjaxRequest(this HttpRequest request)
12+
{
13+
if (request == null) throw new ArgumentNullException("request");
14+
if (request.Headers != null) return request.Headers["X-Requested-With"] == "XMLHttpRequest";
15+
return false;
16+
}
17+
18+
public static async Task<T> ReadAsAsync<T>(this HttpResponseMessage response)
19+
{
20+
return JsonConvert.DeserializeObject<T>(await response.Content.ReadAsStringAsync());
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)