Skip to content

Commit 10d5ed7

Browse files
committed
Initial additiosn
0 parents  commit 10d5ed7

34 files changed

+891
-0
lines changed

.gitignore

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
*.swp
2+
*.*~
3+
project.lock.json
4+
.DS_Store
5+
*.pyc
6+
nupkg/
7+
8+
# Visual Studio Code
9+
.vscode
10+
11+
# Rider
12+
.idea
13+
14+
# User-specific files
15+
*.suo
16+
*.user
17+
*.userosscache
18+
*.sln.docstates
19+
20+
# Build results
21+
[Dd]ebug/
22+
[Dd]ebugPublic/
23+
[Rr]elease/
24+
[Rr]eleases/
25+
x64/
26+
x86/
27+
build/
28+
bld/
29+
[Bb]in/
30+
[Oo]bj/
31+
[Oo]ut/
32+
msbuild.log
33+
msbuild.err
34+
msbuild.wrn
35+
36+
# Visual Studio 2015
37+
.vs/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using Customers.API.Models;
2+
using Customers.API.Services;
3+
using Microsoft.AspNetCore.Mvc;
4+
5+
namespace Customers.API.Controllers
6+
{
7+
[ApiController]
8+
[Route("api/[controller]")]
9+
public class CustomersController : ControllerBase
10+
{
11+
private readonly ICustomerService _customerService;
12+
13+
public CustomersController(ICustomerService customerService)
14+
{
15+
_customerService = customerService;
16+
}
17+
18+
// GET: api/customers
19+
[HttpGet]
20+
public async Task<IActionResult> Get()
21+
{
22+
var result = await _customerService.FindAllAsync();
23+
return Ok(result);
24+
}
25+
26+
// POST: api/customers
27+
[HttpPost]
28+
public async Task<IActionResult> Post([FromBody] Customer customer)
29+
{
30+
var result = await _customerService.InsertAsync(customer);
31+
return Ok(result);
32+
}
33+
34+
// PUT: api/customers
35+
[HttpPut]
36+
public async Task<IActionResult> Put([FromBody] Customer customer)
37+
{
38+
var result = await _customerService.UpdateAsync(customer);
39+
return Ok(result);
40+
}
41+
42+
// DELETE api/tasks?id=5
43+
[HttpDelete]
44+
public async Task<IActionResult> Delete([FromQuery] int id)
45+
{
46+
var result = await _customerService.DeleteAsync(id);
47+
return Ok(result);
48+
}
49+
}
50+
}

Customers.API/Customers.API.csproj

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.0" />
11+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
12+
</ItemGroup>
13+
14+
</Project>

Customers.API/Data/MariaDbContext.cs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Customers.API.Models;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
namespace Customers.API.Data
5+
{
6+
public partial class MariaDbContext : DbContext
7+
{
8+
public MariaDbContext(DbContextOptions<MariaDbContext> options)
9+
: base(options)
10+
{ }
11+
12+
public virtual DbSet<Customer>? Customers { get; set; }
13+
14+
protected override void OnModelCreating(ModelBuilder builder)
15+
{
16+
builder.Entity<Customer>(b =>
17+
{
18+
b.ToTable("customers");
19+
});
20+
}
21+
}
22+
}

Customers.API/Models/Customer.cs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Customers.API.Models
2+
{
3+
public class Customer
4+
{
5+
public int Id { get; set; }
6+
public string Name { get; set; }
7+
public string Email { get; set; }
8+
}
9+
}

Customers.API/Program.cs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Customers.API.Data;
2+
using Customers.API.Services;
3+
using Microsoft.EntityFrameworkCore;
4+
5+
var builder = WebApplication.CreateBuilder(args);
6+
7+
// Add services to the container.
8+
9+
builder.Services.AddControllers();
10+
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
11+
builder.Services.AddEndpointsApiExplorer();
12+
builder.Services.AddSwaggerGen();
13+
builder.Services.AddDbContextPool<MariaDbContext>(options => options
14+
.UseMySql(builder.Configuration.GetConnectionString("CustomerDatabase"), new MariaDbServerVersion(new Version(10, 6, 5)))
15+
);
16+
builder.Services.AddScoped<ICustomerService, CustomerService>();
17+
18+
var app = builder.Build();
19+
20+
// Configure the HTTP request pipeline.
21+
if (app.Environment.IsDevelopment())
22+
{
23+
app.UseSwagger();
24+
app.UseSwaggerUI();
25+
}
26+
27+
app.UseAuthorization();
28+
29+
app.MapControllers();
30+
31+
app.Run();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:4671",
8+
"sslPort": 0
9+
}
10+
},
11+
"profiles": {
12+
"Customers.API": {
13+
"commandName": "Project",
14+
"dotnetRunMessages": true,
15+
"launchBrowser": true,
16+
"launchUrl": "swagger",
17+
"applicationUrl": "http://localhost:5016",
18+
"environmentVariables": {
19+
"ASPNETCORE_ENVIRONMENT": "Development"
20+
}
21+
},
22+
"IIS Express": {
23+
"commandName": "IISExpress",
24+
"launchBrowser": true,
25+
"launchUrl": "swagger",
26+
"environmentVariables": {
27+
"ASPNETCORE_ENVIRONMENT": "Development"
28+
}
29+
}
30+
}
31+
}
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using Customers.API.Data;
2+
using Customers.API.Models;
3+
using Microsoft.EntityFrameworkCore;
4+
5+
namespace Customers.API.Services
6+
{
7+
public sealed class CustomerService : ICustomerService
8+
{
9+
private readonly MariaDbContext _dbContext;
10+
11+
public CustomerService(MariaDbContext dbContext)
12+
{
13+
_dbContext = dbContext;
14+
}
15+
16+
public async Task<int> DeleteAsync(int id)
17+
{
18+
try
19+
{
20+
_dbContext.Customers.Remove(
21+
new Customer
22+
{
23+
Id = id
24+
}
25+
);
26+
27+
return await _dbContext.SaveChangesAsync();
28+
}
29+
catch (DbUpdateConcurrencyException)
30+
{
31+
return 0;
32+
}
33+
}
34+
35+
public Task<List<Customer>> FindAllAsync() => _dbContext.Customers.ToListAsync();
36+
37+
public Task<Customer> FindOneAsync(int id) => _dbContext.Customers.FirstOrDefaultAsync(x => x.Id == id);
38+
39+
public Task<int> InsertAsync(Customer customer)
40+
{
41+
_dbContext.Add(customer);
42+
return _dbContext.SaveChangesAsync();
43+
}
44+
45+
public async Task<int> UpdateAsync(Customer customer)
46+
{
47+
try
48+
{
49+
_dbContext.Update(customer);
50+
return await _dbContext.SaveChangesAsync();
51+
}
52+
catch (DbUpdateConcurrencyException)
53+
{
54+
return 0;
55+
}
56+
}
57+
}
58+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Customers.API.Models;
2+
3+
namespace Customers.API.Services
4+
{
5+
public interface ICustomerService
6+
{
7+
Task<int> DeleteAsync(int id);
8+
Task<List<Customer>> FindAllAsync();
9+
Task<Customer> FindOneAsync(int id);
10+
Task<int> InsertAsync(Customer customer);
11+
Task<int> UpdateAsync(Customer customer);
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}

Customers.API/appsettings.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"ConnectionStrings": {
9+
"CustomerDatabase": "host=127.0.0.1;port=3306;user id=root;password=RootPassword123!;database=customer_db;"
10+
},
11+
"AllowedHosts": "*"
12+
}

Demo.sln

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31919.166
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gateway.API", "Gateway.API\Gateway.API.csproj", "{73784A6F-6335-4389-8719-E573F0080FD6}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Products.API", "Products.API\Products.API.csproj", "{07B1BA94-C119-4114-823B-BE257A695F7F}"
9+
EndProject
10+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Microservices", "Microservices", "{88707F7A-2C84-45B1-AACD-9C3690C67033}"
11+
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Customers.API", "Customers.API\Customers.API.csproj", "{4E77697B-B402-441A-A822-F732C9AAFA76}"
13+
EndProject
14+
Global
15+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
16+
Debug|Any CPU = Debug|Any CPU
17+
Release|Any CPU = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
20+
{73784A6F-6335-4389-8719-E573F0080FD6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{73784A6F-6335-4389-8719-E573F0080FD6}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{73784A6F-6335-4389-8719-E573F0080FD6}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{73784A6F-6335-4389-8719-E573F0080FD6}.Release|Any CPU.Build.0 = Release|Any CPU
24+
{07B1BA94-C119-4114-823B-BE257A695F7F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
25+
{07B1BA94-C119-4114-823B-BE257A695F7F}.Debug|Any CPU.Build.0 = Debug|Any CPU
26+
{07B1BA94-C119-4114-823B-BE257A695F7F}.Release|Any CPU.ActiveCfg = Release|Any CPU
27+
{07B1BA94-C119-4114-823B-BE257A695F7F}.Release|Any CPU.Build.0 = Release|Any CPU
28+
{4E77697B-B402-441A-A822-F732C9AAFA76}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
29+
{4E77697B-B402-441A-A822-F732C9AAFA76}.Debug|Any CPU.Build.0 = Debug|Any CPU
30+
{4E77697B-B402-441A-A822-F732C9AAFA76}.Release|Any CPU.ActiveCfg = Release|Any CPU
31+
{4E77697B-B402-441A-A822-F732C9AAFA76}.Release|Any CPU.Build.0 = Release|Any CPU
32+
EndGlobalSection
33+
GlobalSection(SolutionProperties) = preSolution
34+
HideSolutionNode = FALSE
35+
EndGlobalSection
36+
GlobalSection(NestedProjects) = preSolution
37+
{07B1BA94-C119-4114-823B-BE257A695F7F} = {88707F7A-2C84-45B1-AACD-9C3690C67033}
38+
{4E77697B-B402-441A-A822-F732C9AAFA76} = {88707F7A-2C84-45B1-AACD-9C3690C67033}
39+
EndGlobalSection
40+
GlobalSection(ExtensibilityGlobals) = postSolution
41+
SolutionGuid = {12587228-5078-426B-94F6-DFCA4EFC3E7C}
42+
EndGlobalSection
43+
EndGlobal

Gateway.API/Gateway.API.csproj

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Ocelot" Version="17.0.0" />
11+
</ItemGroup>
12+
13+
</Project>

Gateway.API/Program.cs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Ocelot.DependencyInjection;
2+
using Ocelot.Middleware;
3+
4+
var builder = WebApplication.CreateBuilder(args);
5+
6+
builder.Configuration.SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("ocelot.json");
7+
builder.Services.AddOcelot();
8+
9+
var app = builder.Build();
10+
11+
app.UseRouting();
12+
13+
app.UseEndpoints(endpoints =>
14+
{
15+
endpoints.MapControllers();
16+
});
17+
18+
await app.UseOcelot();
19+
20+
app.Run();

0 commit comments

Comments
 (0)