Skip to content

Development #2

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 7 commits into from
Mar 11, 2020
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
Binary file modified .DS_Store
Binary file not shown.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
################################################################################
# This .gitignore file was automatically created by Microsoft(R) Visual Studio.
################################################################################

/src/.vs/GOC.ApiGateway/v15/sqlite3/storage.ide
50 changes: 50 additions & 0 deletions src/GOC.ApiGateway/AppSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Collections.Generic;
using Microphone.Consul;

namespace GOC.ApiGateway
{
public class AppSettings
{
public CircuitBreakerSettings CircuitBreaker {get;set;}
public WaitAndRetrySettings WaitAndRetry { get; set; }
public ConsulOptions Consul { get; set; }
public IdentitySettings Identity { get; set; }
public RabbitMQSettings Rabbit { get; set; }
}
public class CircuitBreakerSettings
{
public double FailureThreshold { get; set; }
public int SamplingDurationInSeconds { get; set; }
public int MinimumThroughput { get; set; }
public int DurationOfBreakInSeconds { get; set; }
}
public class WaitAndRetrySettings
{
public int RetryAttempts { get; set; }
}
public class IdentitySettings
{
public string Authority { get; set; }
public string ApiName { get; set; }
public string ApiSecret { get; set; }
public string TokenEndpoint { get; set; }
public string ApiClientId { get; set; }
public string ApiClientSecret { get; set; }
public string TokenEndpointUrl
{
get => $"{Authority}/{TokenEndpoint}";
}
public IEnumerable<ApiResource> Resources { get; set; }
}
//TODO do this in json config file
public class ApiResource
{
public string ResourceFriendlyName { get; set; }
public string ResourceName { get; set; }
}
public class RabbitMQSettings
{
public string Host { get; set; }
}

}
15 changes: 15 additions & 0 deletions src/GOC.ApiGateway/Controllers/BaseController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace GOC.ApiGateway.Controllers
{
public class BaseController<T> : Controller where T : class
{
protected ILogger Logger;

public BaseController(ILoggerFactory loggerFactory)
{
Logger = loggerFactory.CreateLogger<T>();
}
}
}
29 changes: 29 additions & 0 deletions src/GOC.ApiGateway/Controllers/CompaniesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Threading.Tasks;
using GOC.ApiGateway.Dtos;
using GOC.ApiGateway.Interfaces;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace GOC.ApiGateway.Controllers
{
[Authorize]
[Route("api/[controller]")]
public class CompaniesController : BaseController<CompaniesController>
{
private readonly ICompanyService _companyService;
public CompaniesController(ICompanyService company, ILoggerFactory loggerFactory) : base(loggerFactory)
{
_companyService = company;
}

[HttpPost]
public async Task<IActionResult> Post([FromBody] CompanyPostDto company)
{
var result = await _companyService.CreateCompanyAsync(company);
return Ok(result.Value);
}
}
}
45 changes: 45 additions & 0 deletions src/GOC.ApiGateway/Controllers/InventoriesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using GOC.ApiGateway.Dtos;
using GOC.ApiGateway.Interfaces;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace GOC.ApiGateway.Controllers
{
[Authorize]
[Route("api/companies/{companyId}/[controller]")]
public class InventoriesController : BaseController<InventoriesController>
{
private IInventoryService _inventoryService;
public InventoriesController(IInventoryService inventoryService, ILoggerFactory loggerFactory) : base(loggerFactory)
{
_inventoryService = inventoryService;
}

[HttpGet]
public async Task<IActionResult> Get()
{
return View();
}

[HttpGet("{id}")]
public async Task<IActionResult> Get(Guid id)
{
return View();
}

[HttpPost]
public async Task<IActionResult> Post(Guid companyId, InventoryPostDto inventory)
{
inventory.CompanyId = companyId;
var result = await _inventoryService.CreateInventoryAsync(inventory);
return Ok(result.Value);
}
}
}
44 changes: 0 additions & 44 deletions src/GOC.ApiGateway/Controllers/ValuesController.cs

This file was deleted.

19 changes: 19 additions & 0 deletions src/GOC.ApiGateway/Controllers/VendorsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace GOC.ApiGateway.Controllers
{
public class VendorsController : Controller
{
// GET: /<controller>/
public IActionResult Index()
{
return View();
}
}
}
17 changes: 17 additions & 0 deletions src/GOC.ApiGateway/Dtos/AddressDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
namespace GOC.ApiGateway.Dtos
{
public class AddressDto
{
public string AddressLine1 { get; set; }

public string AddressLine2 { get; set; }

public string City { get; set; }

public string State { get; set; }

public string ZipCode { get; set; }

}
}
16 changes: 16 additions & 0 deletions src/GOC.ApiGateway/Dtos/CompanyDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
namespace GOC.ApiGateway.Dtos
{
public class CompanyDto
{
public Guid Id { get; set; }

public AddressDto Address { get; set; }

public string Name { get; set; }

public int UserId { get; set; }

public string PhoneNumber { get; set; }
}
}
13 changes: 13 additions & 0 deletions src/GOC.ApiGateway/Dtos/CompanyPostDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace GOC.ApiGateway.Dtos
{
public class CompanyPostDto
{
public AddressDto Address { get; set; }

public string Name { get; set; }

public int UserId { get; set; }

public string PhoneNumber { get; set; }
}
}
14 changes: 14 additions & 0 deletions src/GOC.ApiGateway/Dtos/InventoryDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
namespace GOC.ApiGateway.Dtos
{
public class InventoryDto
{
public Guid Id { get; set; }

public string Name { get; set; }

public Guid CompanyId { get; set; }

public int UserId { get; set; }
}
}
12 changes: 12 additions & 0 deletions src/GOC.ApiGateway/Dtos/InventoryPostDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
namespace GOC.ApiGateway.Dtos
{
public class InventoryPostDto
{
public string Name { get; set; }

public Guid CompanyId { get; set; }

public int UserId { get; set; }
}
}
8 changes: 8 additions & 0 deletions src/GOC.ApiGateway/Enums/ServiceNameTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace GOC.ApiGateway.Enums
{
public enum ServiceNameTypes
{
InventoryService,
CrmService
}
}
56 changes: 56 additions & 0 deletions src/GOC.ApiGateway/Extensions/ConsulExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Linq;
using Consul;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace GOC.ApiGateway.Extensions
{
public static class ConsulExtension
{
//public static IApplicationBuilder RegisterWithConsul(this IApplicationBuilder app,
//IApplicationLifetime lifetime)
//{
// // Retrieve Consul client from DI
// var consulClient = app.ApplicationServices
// .GetRequiredService<IConsulClient>();
// var consulConfig = app.ApplicationServices
// .GetRequiredService<IOptions<ConsulConfig>>();
// // Setup logger
// var loggingFactory = app.ApplicationServices
// .GetRequiredService<ILoggerFactory>();
// var logger = loggingFactory.CreateLogger<IApplicationBuilder>();

// // Get server IP address
// var features = app.Properties["server.Features"] as FeatureCollection;
// var addresses = features.Get<IServerAddressesFeature>();
// var address = addresses.Addresses.First();

// // Register service with consul
// var uri = new Uri(address);
// var registration = new AgentServiceRegistration()
// {
// ID = $"{consulConfig.Value.ServiceID}-{uri.Port}",
// Name = consulConfig.Value.ServiceName,
// Address = $"{uri.Scheme}://{uri.Host}",
// Port = uri.Port,
// Tags = new[] { "Students", "Courses", "School" }
// };

// logger.LogInformation("Registering with Consul");
// consulClient.Agent.ServiceDeregister(registration.ID).Wait();
// consulClient.Agent.ServiceRegister(registration).Wait();

// lifetime.ApplicationStopping.Register(() => {
// logger.LogInformation("Deregistering from Consul");
// consulClient.Agent.ServiceDeregister(registration.ID).Wait();
// });
// return app;
//}
}
}
23 changes: 23 additions & 0 deletions src/GOC.ApiGateway/Extensions/HttpExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;

namespace GOC.ApiGateway.Extensions
{
public static class HttpExtensions
{
public static bool IsAjaxRequest(this HttpRequest request)
{
if (request == null) throw new ArgumentNullException("request");
if (request.Headers != null) return request.Headers["X-Requested-With"] == "XMLHttpRequest";
return false;
}

public static async Task<T> ReadAsAsync<T>(this HttpResponseMessage response)
{
return JsonConvert.DeserializeObject<T>(await response.Content.ReadAsStringAsync());
}
}
}
Loading