From f503b8143f8caf92f12cb7e81042346319521c97 Mon Sep 17 00:00:00 2001 From: codemazeblog Date: Tue, 24 Apr 2018 21:08:18 +0200 Subject: [PATCH 01/11] Initial commit --- .gitignore | 5 ++ AccountOwnerServer.sln | 63 ++++++++++++++++- AccountOwnerServer/AccountOwnerServer.csproj | 1 + .../Controllers/AccountController.cs | 67 +++++++++++++++++++ .../Controllers/OwnerController.cs | 4 +- .../Extensions/ServiceExtensions.cs | 7 +- AccountOwnerServer/Startup.cs | 16 ++++- AccountOwnerServer/appsettings.json | 2 +- AccountOwnerServer/nlog.config | 4 +- Contracts/IAccountRepository.cs | 3 + README.md | 2 +- Repository/AccountRepository.cs | 16 +++++ Tests/OwnerRepositoryTests.cs | 42 ++++++++++++ Tests/Tests.csproj | 21 ++++++ 14 files changed, 236 insertions(+), 17 deletions(-) create mode 100644 AccountOwnerServer/Controllers/AccountController.cs create mode 100644 Tests/OwnerRepositoryTests.cs create mode 100644 Tests/Tests.csproj diff --git a/.gitignore b/.gitignore index b14f7ce..6c3ac39 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ /AccountOwnerServer/obj /AccountOwnerServer/bin +/AccountOwnerServer/Projects /Contracts/obj /Contracts/bin /Entities/obj @@ -8,3 +9,7 @@ /LoggerService/bin /Repository/obj /Repository/bin +/Tests/bin +/Tests/obj +/.vs + diff --git a/AccountOwnerServer.sln b/AccountOwnerServer.sln index 7620715..601977b 100644 --- a/AccountOwnerServer.sln +++ b/AccountOwnerServer.sln @@ -1,7 +1,6 @@ - Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26730.3 +VisualStudioVersion = 15.0.27130.2010 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AccountOwnerServer", "AccountOwnerServer\AccountOwnerServer.csproj", "{73FB08B8-4033-4116-B3AC-EC8253B024D4}" EndProject @@ -11,34 +10,92 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LoggerService", "LoggerServ EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Entities", "Entities\Entities.csproj", "{8CD97B3F-C616-469C-80A2-A1B366611487}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Repository", "Repository\Repository.csproj", "{CC0B1C81-CB8A-412C-BD48-BCBC3F1A1CD0}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Repository", "Repository\Repository.csproj", "{CC0B1C81-CB8A-412C-BD48-BCBC3F1A1CD0}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests", "Tests\Tests.csproj", "{89CF5113-7F84-40B8-A81C-B918F00183AD}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {73FB08B8-4033-4116-B3AC-EC8253B024D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {73FB08B8-4033-4116-B3AC-EC8253B024D4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {73FB08B8-4033-4116-B3AC-EC8253B024D4}.Debug|x64.ActiveCfg = Debug|Any CPU + {73FB08B8-4033-4116-B3AC-EC8253B024D4}.Debug|x64.Build.0 = Debug|Any CPU + {73FB08B8-4033-4116-B3AC-EC8253B024D4}.Debug|x86.ActiveCfg = Debug|Any CPU + {73FB08B8-4033-4116-B3AC-EC8253B024D4}.Debug|x86.Build.0 = Debug|Any CPU {73FB08B8-4033-4116-B3AC-EC8253B024D4}.Release|Any CPU.ActiveCfg = Release|Any CPU {73FB08B8-4033-4116-B3AC-EC8253B024D4}.Release|Any CPU.Build.0 = Release|Any CPU + {73FB08B8-4033-4116-B3AC-EC8253B024D4}.Release|x64.ActiveCfg = Release|Any CPU + {73FB08B8-4033-4116-B3AC-EC8253B024D4}.Release|x64.Build.0 = Release|Any CPU + {73FB08B8-4033-4116-B3AC-EC8253B024D4}.Release|x86.ActiveCfg = Release|Any CPU + {73FB08B8-4033-4116-B3AC-EC8253B024D4}.Release|x86.Build.0 = Release|Any CPU {52725034-B791-4DF1-A227-02EDB5BF78E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {52725034-B791-4DF1-A227-02EDB5BF78E0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {52725034-B791-4DF1-A227-02EDB5BF78E0}.Debug|x64.ActiveCfg = Debug|Any CPU + {52725034-B791-4DF1-A227-02EDB5BF78E0}.Debug|x64.Build.0 = Debug|Any CPU + {52725034-B791-4DF1-A227-02EDB5BF78E0}.Debug|x86.ActiveCfg = Debug|Any CPU + {52725034-B791-4DF1-A227-02EDB5BF78E0}.Debug|x86.Build.0 = Debug|Any CPU {52725034-B791-4DF1-A227-02EDB5BF78E0}.Release|Any CPU.ActiveCfg = Release|Any CPU {52725034-B791-4DF1-A227-02EDB5BF78E0}.Release|Any CPU.Build.0 = Release|Any CPU + {52725034-B791-4DF1-A227-02EDB5BF78E0}.Release|x64.ActiveCfg = Release|Any CPU + {52725034-B791-4DF1-A227-02EDB5BF78E0}.Release|x64.Build.0 = Release|Any CPU + {52725034-B791-4DF1-A227-02EDB5BF78E0}.Release|x86.ActiveCfg = Release|Any CPU + {52725034-B791-4DF1-A227-02EDB5BF78E0}.Release|x86.Build.0 = Release|Any CPU {A190AF01-84DC-40A8-BEF3-C015825C8B30}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {A190AF01-84DC-40A8-BEF3-C015825C8B30}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A190AF01-84DC-40A8-BEF3-C015825C8B30}.Debug|x64.ActiveCfg = Debug|Any CPU + {A190AF01-84DC-40A8-BEF3-C015825C8B30}.Debug|x64.Build.0 = Debug|Any CPU + {A190AF01-84DC-40A8-BEF3-C015825C8B30}.Debug|x86.ActiveCfg = Debug|Any CPU + {A190AF01-84DC-40A8-BEF3-C015825C8B30}.Debug|x86.Build.0 = Debug|Any CPU {A190AF01-84DC-40A8-BEF3-C015825C8B30}.Release|Any CPU.ActiveCfg = Release|Any CPU {A190AF01-84DC-40A8-BEF3-C015825C8B30}.Release|Any CPU.Build.0 = Release|Any CPU + {A190AF01-84DC-40A8-BEF3-C015825C8B30}.Release|x64.ActiveCfg = Release|Any CPU + {A190AF01-84DC-40A8-BEF3-C015825C8B30}.Release|x64.Build.0 = Release|Any CPU + {A190AF01-84DC-40A8-BEF3-C015825C8B30}.Release|x86.ActiveCfg = Release|Any CPU + {A190AF01-84DC-40A8-BEF3-C015825C8B30}.Release|x86.Build.0 = Release|Any CPU {8CD97B3F-C616-469C-80A2-A1B366611487}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {8CD97B3F-C616-469C-80A2-A1B366611487}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8CD97B3F-C616-469C-80A2-A1B366611487}.Debug|x64.ActiveCfg = Debug|Any CPU + {8CD97B3F-C616-469C-80A2-A1B366611487}.Debug|x64.Build.0 = Debug|Any CPU + {8CD97B3F-C616-469C-80A2-A1B366611487}.Debug|x86.ActiveCfg = Debug|Any CPU + {8CD97B3F-C616-469C-80A2-A1B366611487}.Debug|x86.Build.0 = Debug|Any CPU {8CD97B3F-C616-469C-80A2-A1B366611487}.Release|Any CPU.ActiveCfg = Release|Any CPU {8CD97B3F-C616-469C-80A2-A1B366611487}.Release|Any CPU.Build.0 = Release|Any CPU + {8CD97B3F-C616-469C-80A2-A1B366611487}.Release|x64.ActiveCfg = Release|Any CPU + {8CD97B3F-C616-469C-80A2-A1B366611487}.Release|x64.Build.0 = Release|Any CPU + {8CD97B3F-C616-469C-80A2-A1B366611487}.Release|x86.ActiveCfg = Release|Any CPU + {8CD97B3F-C616-469C-80A2-A1B366611487}.Release|x86.Build.0 = Release|Any CPU {CC0B1C81-CB8A-412C-BD48-BCBC3F1A1CD0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {CC0B1C81-CB8A-412C-BD48-BCBC3F1A1CD0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CC0B1C81-CB8A-412C-BD48-BCBC3F1A1CD0}.Debug|x64.ActiveCfg = Debug|Any CPU + {CC0B1C81-CB8A-412C-BD48-BCBC3F1A1CD0}.Debug|x64.Build.0 = Debug|Any CPU + {CC0B1C81-CB8A-412C-BD48-BCBC3F1A1CD0}.Debug|x86.ActiveCfg = Debug|Any CPU + {CC0B1C81-CB8A-412C-BD48-BCBC3F1A1CD0}.Debug|x86.Build.0 = Debug|Any CPU {CC0B1C81-CB8A-412C-BD48-BCBC3F1A1CD0}.Release|Any CPU.ActiveCfg = Release|Any CPU {CC0B1C81-CB8A-412C-BD48-BCBC3F1A1CD0}.Release|Any CPU.Build.0 = Release|Any CPU + {CC0B1C81-CB8A-412C-BD48-BCBC3F1A1CD0}.Release|x64.ActiveCfg = Release|Any CPU + {CC0B1C81-CB8A-412C-BD48-BCBC3F1A1CD0}.Release|x64.Build.0 = Release|Any CPU + {CC0B1C81-CB8A-412C-BD48-BCBC3F1A1CD0}.Release|x86.ActiveCfg = Release|Any CPU + {CC0B1C81-CB8A-412C-BD48-BCBC3F1A1CD0}.Release|x86.Build.0 = Release|Any CPU + {89CF5113-7F84-40B8-A81C-B918F00183AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {89CF5113-7F84-40B8-A81C-B918F00183AD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {89CF5113-7F84-40B8-A81C-B918F00183AD}.Debug|x64.ActiveCfg = Debug|Any CPU + {89CF5113-7F84-40B8-A81C-B918F00183AD}.Debug|x64.Build.0 = Debug|Any CPU + {89CF5113-7F84-40B8-A81C-B918F00183AD}.Debug|x86.ActiveCfg = Debug|Any CPU + {89CF5113-7F84-40B8-A81C-B918F00183AD}.Debug|x86.Build.0 = Debug|Any CPU + {89CF5113-7F84-40B8-A81C-B918F00183AD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {89CF5113-7F84-40B8-A81C-B918F00183AD}.Release|Any CPU.Build.0 = Release|Any CPU + {89CF5113-7F84-40B8-A81C-B918F00183AD}.Release|x64.ActiveCfg = Release|Any CPU + {89CF5113-7F84-40B8-A81C-B918F00183AD}.Release|x64.Build.0 = Release|Any CPU + {89CF5113-7F84-40B8-A81C-B918F00183AD}.Release|x86.ActiveCfg = Release|Any CPU + {89CF5113-7F84-40B8-A81C-B918F00183AD}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/AccountOwnerServer/AccountOwnerServer.csproj b/AccountOwnerServer/AccountOwnerServer.csproj index beffa6b..b0d65a5 100644 --- a/AccountOwnerServer/AccountOwnerServer.csproj +++ b/AccountOwnerServer/AccountOwnerServer.csproj @@ -11,6 +11,7 @@ + diff --git a/AccountOwnerServer/Controllers/AccountController.cs b/AccountOwnerServer/Controllers/AccountController.cs new file mode 100644 index 0000000..0675f09 --- /dev/null +++ b/AccountOwnerServer/Controllers/AccountController.cs @@ -0,0 +1,67 @@ +using Contracts; +using Entities.Extensions; +using Entities.Models; +using Microsoft.AspNetCore.Mvc; +using System; + +namespace AccountOwnerServer.Controllers +{ + [Route("api/[controller]")] + public class AccountController : Controller + { + private ILoggerManager _logger; + private IRepositoryWrapper _repository; + + public AccountController(ILoggerManager logger, IRepositoryWrapper repository) + { + _logger = logger; + _repository = repository; + } + + [HttpGet] + public IActionResult GetAllAccounts() + { + try + { + var accounts = _repository.Account.GetAllAccounts(); + + _logger.LogInfo($"Returned all accounts from database."); + + return Ok(accounts); + } + catch (Exception ex) + { + _logger.LogError($"Something went wrong inside GetAllAccounts action: {ex}"); + return StatusCode(500, "Internal server error"); + } + } + + [HttpPost] + public IActionResult CreateOwner([FromBody]Account account) + { + try + { + if (account.IsObjectNull()) + { + _logger.LogError("Object sent from client is null."); + return BadRequest("Object is null"); + } + + if (!ModelState.IsValid) + { + _logger.LogError("Invalid object sent from client."); + return BadRequest("Invalid model object"); + } + + _repository.Account.CreateAccount(account); + + return CreatedAtRoute("AccountById", new { id = account.Id }, account); + } + catch (Exception ex) + { + _logger.LogError($"Something went wrong inside CreateAccount action: {ex}"); + return StatusCode(500, "Internal server error"); + } + } + } +} diff --git a/AccountOwnerServer/Controllers/OwnerController.cs b/AccountOwnerServer/Controllers/OwnerController.cs index dc2850c..e26f275 100644 --- a/AccountOwnerServer/Controllers/OwnerController.cs +++ b/AccountOwnerServer/Controllers/OwnerController.cs @@ -31,7 +31,7 @@ public IActionResult GetAllOwners() } catch (Exception ex) { - _logger.LogError($"Something went wrong inside GetAllOwners action: {ex.Message}"); + _logger.LogError($"Something went wrong inside GetAllOwners action: {ex}"); return StatusCode(500, "Internal server error"); } } @@ -62,7 +62,7 @@ public IActionResult GetOwnerById(Guid id) } [HttpGet("{id}/account")] - public IActionResult GetOwnerWithDetails(Guid id) + public IActionResult GetOwnerAccounts(Guid id) { try { diff --git a/AccountOwnerServer/Extensions/ServiceExtensions.cs b/AccountOwnerServer/Extensions/ServiceExtensions.cs index 89ac3a5..3acce2b 100644 --- a/AccountOwnerServer/Extensions/ServiceExtensions.cs +++ b/AccountOwnerServer/Extensions/ServiceExtensions.cs @@ -6,10 +6,6 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Repository; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; namespace AccountOwnerServer.Extensions { @@ -42,8 +38,7 @@ public static void ConfigureLoggerService(this IServiceCollection services) public static void ConfigureMySqlContext(this IServiceCollection services, IConfiguration config) { - var connectionString = config["mysqlconnection:connectionString"]; - services.AddDbContext(o => o.UseMySql(connectionString)); + services.AddDbContext(o => o.UseInMemoryDatabase("accountowner")); } public static void ConfigureRepositoryWrapper(this IServiceCollection services) diff --git a/AccountOwnerServer/Startup.cs b/AccountOwnerServer/Startup.cs index 3a86fc4..fa74191 100644 --- a/AccountOwnerServer/Startup.cs +++ b/AccountOwnerServer/Startup.cs @@ -12,7 +12,7 @@ using Microsoft.AspNetCore.HttpOverrides; using System.IO; using NLog.Extensions.Logging; -using Contracts; +using Swashbuckle.AspNetCore.Swagger; namespace AccountOwnerServer { @@ -39,6 +39,11 @@ public void ConfigureServices(IServiceCollection services) services.ConfigureRepositoryWrapper(); services.AddMvc(); + + services.AddSwaggerGen(c => + { + c.SwaggerDoc("v1", new Info { Title = "AccountOwner API", Version = "v1" }); + }); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) @@ -47,7 +52,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseDeveloperExceptionPage(); } - + app.UseCors("CorsPolicy"); app.UseForwardedHeaders(new ForwardedHeadersOptions @@ -70,6 +75,13 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env) app.UseStaticFiles(); app.UseMvc(); + + app.UseSwagger(); + + app.UseSwaggerUI(c => + { + c.SwaggerEndpoint("/swagger/v1/swagger.json", "AccountOwner API V1"); + }); } } } diff --git a/AccountOwnerServer/appsettings.json b/AccountOwnerServer/appsettings.json index 934ec46..900bf6b 100644 --- a/AccountOwnerServer/appsettings.json +++ b/AccountOwnerServer/appsettings.json @@ -13,6 +13,6 @@ } }, "mysqlconnection": { - "connectionString": "server=localhost;userid=dbuser;password=pass;database=accountowner;" + "connectionString": "server=localhost;port=3306;userid=root;password=root;database=accountowner;" } } diff --git a/AccountOwnerServer/nlog.config b/AccountOwnerServer/nlog.config index 91c2174..69efc94 100644 --- a/AccountOwnerServer/nlog.config +++ b/AccountOwnerServer/nlog.config @@ -3,7 +3,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true" internalLogLevel="Trace" - internalLogFile="d:Projects\Blog-AccountOwner\Project\internal_logs\internallog.txt"> + internalLogFile="internallog.txt"> @@ -11,7 +11,7 @@ diff --git a/Contracts/IAccountRepository.cs b/Contracts/IAccountRepository.cs index 825a4fb..945b7f5 100644 --- a/Contracts/IAccountRepository.cs +++ b/Contracts/IAccountRepository.cs @@ -1,8 +1,11 @@ using Entities.Models; +using System.Collections.Generic; namespace Contracts { public interface IAccountRepository { + IEnumerable GetAllAccounts(); + void CreateAccount(Account account); } } diff --git a/README.md b/README.md index 226a577..80c22d1 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -# Docker Series +# Docker Kubernetes Series diff --git a/Repository/AccountRepository.cs b/Repository/AccountRepository.cs index 9dcb2d5..0cd0be0 100644 --- a/Repository/AccountRepository.cs +++ b/Repository/AccountRepository.cs @@ -1,6 +1,9 @@ using Contracts; using Entities; using Entities.Models; +using System; +using System.Collections.Generic; +using System.Linq; namespace Repository { @@ -10,5 +13,18 @@ public AccountRepository(RepositoryContext repositoryContext) :base(repositoryContext) { } + + public IEnumerable GetAllAccounts() + { + return FindAll() + .OrderBy(ac => ac.DateCreated); + } + + public void CreateAccount(Account account) + { + account.Id = Guid.NewGuid(); + Create(account); + Save(); + } } } diff --git a/Tests/OwnerRepositoryTests.cs b/Tests/OwnerRepositoryTests.cs new file mode 100644 index 0000000..81d3133 --- /dev/null +++ b/Tests/OwnerRepositoryTests.cs @@ -0,0 +1,42 @@ +using Contracts; +using Entities.Models; +using Moq; +using System; +using System.Collections.Generic; +using System.Linq; +using Xunit; + +namespace Tests +{ + public class OwnerRepositoryTests + { + [Fact] + public void GetAllOwners_ReturnsListOfOwners_WithSingleOwner() + { + // Arrange + var mockRepo = new Mock(); + mockRepo.Setup(repo => (repo.GetAllOwners())).Returns(GetOwners()); + + // Act + var result = mockRepo.Object.GetAllOwners().ToList(); + + // Assert + Assert.IsType>(result); + Assert.Single(result); + } + + public List GetOwners() + { + return new List + { + new Owner + { + Id = Guid.NewGuid(), + Name = "John Keen", + DateOfBirth = new DateTime(1980, 12, 05), + Address = "61 Wellfield Road" + } + }; + } + } +} diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj new file mode 100644 index 0000000..114ad4b --- /dev/null +++ b/Tests/Tests.csproj @@ -0,0 +1,21 @@ + + + + netcoreapp2.0 + + false + + + + + + + + + + + + + + + From 4d7a6b56165b5a785333caab2d245593dc0444d9 Mon Sep 17 00:00:00 2001 From: codemazeblog Date: Tue, 24 Apr 2018 21:11:35 +0200 Subject: [PATCH 02/11] connection string changes --- AccountOwnerServer/appsettings.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AccountOwnerServer/appsettings.json b/AccountOwnerServer/appsettings.json index 900bf6b..934ec46 100644 --- a/AccountOwnerServer/appsettings.json +++ b/AccountOwnerServer/appsettings.json @@ -13,6 +13,6 @@ } }, "mysqlconnection": { - "connectionString": "server=localhost;port=3306;userid=root;password=root;database=accountowner;" + "connectionString": "server=localhost;userid=dbuser;password=pass;database=accountowner;" } } From e67ebb94158bce1d4bd5e6bdbce20e65fc316c8e Mon Sep 17 00:00:00 2001 From: codemazeblog Date: Sat, 5 May 2018 20:11:14 +0200 Subject: [PATCH 03/11] Initial Commit --- .dockerignore | 7 +++++ .gitignore | 2 +- .../Controllers/AccountController.cs | 2 +- Dockerfile | 27 +++++++++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..341d071 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,7 @@ +**/bin/ +**/obj/ +**/global.json +**/Dockerfile* +**/.dockerignore* +**/*.user + diff --git a/.gitignore b/.gitignore index 6c3ac39..dab9784 100644 --- a/.gitignore +++ b/.gitignore @@ -11,5 +11,5 @@ /Repository/bin /Tests/bin /Tests/obj +publish /.vs - diff --git a/AccountOwnerServer/Controllers/AccountController.cs b/AccountOwnerServer/Controllers/AccountController.cs index 0675f09..4404b93 100644 --- a/AccountOwnerServer/Controllers/AccountController.cs +++ b/AccountOwnerServer/Controllers/AccountController.cs @@ -10,7 +10,7 @@ namespace AccountOwnerServer.Controllers public class AccountController : Controller { private ILoggerManager _logger; - private IRepositoryWrapper _repository; + private IRepositoryWrapper _repository; public AccountController(ILoggerManager logger, IRepositoryWrapper repository) { diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..98ac7b8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,27 @@ +FROM microsoft/aspnetcore-build as build-image + +WORKDIR /home/app + +COPY ./AccountOwnerServer/AccountOwnerServer.csproj ./AccountOwnerServer/ +COPY ./Contracts/Contracts.csproj ./Contracts/ +COPY ./Repository/Repository.csproj ./Repository/ +COPY ./Entities/Entities.csproj ./Entities/ +COPY ./LoggerService/LoggerService.csproj ./LoggerService/ +COPY ./Tests/Tests.csproj ./Tests/ +COPY ./AccountOwnerServer.sln . + +RUN dotnet restore + +COPY . . + +RUN dotnet test ./Tests/Tests.csproj + +RUN dotnet publish ./AccountOwnerServer/AccountOwnerServer.csproj -o /publish/ + +FROM microsoft/aspnetcore + +WORKDIR /publish + +COPY --from=build-image /publish . + +ENTRYPOINT ["dotnet", "AccountOwnerServer.dll"] \ No newline at end of file From 1d9c49289939fae369ba044119366c7c39caec1b Mon Sep 17 00:00:00 2001 From: codemazeblog Date: Sun, 6 May 2018 19:59:44 +0200 Subject: [PATCH 04/11] Optimizing Dockerfile --- Dockerfile | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/Dockerfile b/Dockerfile index 98ac7b8..e055560 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,27 +1,23 @@ FROM microsoft/aspnetcore-build as build-image - + WORKDIR /home/app - -COPY ./AccountOwnerServer/AccountOwnerServer.csproj ./AccountOwnerServer/ -COPY ./Contracts/Contracts.csproj ./Contracts/ -COPY ./Repository/Repository.csproj ./Repository/ -COPY ./Entities/Entities.csproj ./Entities/ -COPY ./LoggerService/LoggerService.csproj ./LoggerService/ -COPY ./Tests/Tests.csproj ./Tests/ -COPY ./AccountOwnerServer.sln . - + +COPY ./*.sln ./ +COPY ./*/*.csproj ./ +RUN for file in $(ls *.csproj); do mkdir -p ./${file%.*}/ && mv $file ./${file%.*}/; done + RUN dotnet restore - + COPY . . - + RUN dotnet test ./Tests/Tests.csproj - + RUN dotnet publish ./AccountOwnerServer/AccountOwnerServer.csproj -o /publish/ - + FROM microsoft/aspnetcore - + WORKDIR /publish - + COPY --from=build-image /publish . - + ENTRYPOINT ["dotnet", "AccountOwnerServer.dll"] \ No newline at end of file From e65081f6761cf865d71f33e24275b1ebb582e5ac Mon Sep 17 00:00:00 2001 From: codemazeblog Date: Sun, 13 May 2018 15:14:42 +0200 Subject: [PATCH 05/11] Initial commit --- .gitignore | 1 + .../Extensions/ServiceExtensions.cs | 3 ++- AccountOwnerServer/appsettings.json | 2 +- docker-compose.yml | 26 +++++++++++++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 docker-compose.yml diff --git a/.gitignore b/.gitignore index dab9784..e32fc58 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ /Tests/obj publish /.vs +/AccountOwnerServer/internallog.txt diff --git a/AccountOwnerServer/Extensions/ServiceExtensions.cs b/AccountOwnerServer/Extensions/ServiceExtensions.cs index 3acce2b..ca6eb09 100644 --- a/AccountOwnerServer/Extensions/ServiceExtensions.cs +++ b/AccountOwnerServer/Extensions/ServiceExtensions.cs @@ -38,7 +38,8 @@ public static void ConfigureLoggerService(this IServiceCollection services) public static void ConfigureMySqlContext(this IServiceCollection services, IConfiguration config) { - services.AddDbContext(o => o.UseInMemoryDatabase("accountowner")); + var connectionString = config["mysqlconnection:connectionString"]; + services.AddDbContext(o => o.UseMySql(connectionString)); } public static void ConfigureRepositoryWrapper(this IServiceCollection services) diff --git a/AccountOwnerServer/appsettings.json b/AccountOwnerServer/appsettings.json index 934ec46..5935f76 100644 --- a/AccountOwnerServer/appsettings.json +++ b/AccountOwnerServer/appsettings.json @@ -13,6 +13,6 @@ } }, "mysqlconnection": { - "connectionString": "server=localhost;userid=dbuser;password=pass;database=accountowner;" + "connectionString": "server=db;port=3306;userid=dbuser;password=dbuserpassword;database=accountowner;" } } diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..a591245 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,26 @@ +version: '3.0' + +services: + db: + image: mysql:5.7 + environment: + MYSQL_RANDOM_ROOT_PASSWORD: 1 + MYSQL_DATABASE: accountowner + MYSQL_USER: dbuser + MYSQL_PASSWORD: dbuserpassword + volumes: + - dbdata:/var/lib/mysql + - ./_MySQL_Init_Script:/docker-entrypoint-initdb.d + restart: always + + accountownerapp: + depends_on: + - db + image: codemazeblog/accountownerapp + build: + context: . + ports: + - "8080:80" + +volumes: + dbdata: \ No newline at end of file From cf17ec26a32ab9dcc49698c2c3fbed8fb7813d6f Mon Sep 17 00:00:00 2001 From: Code Maze Date: Mon, 14 May 2018 07:15:30 +0200 Subject: [PATCH 06/11] Updated readme.MD --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 80c22d1..0599cc8 100644 --- a/README.md +++ b/README.md @@ -1 +1,5 @@ -# Docker Kubernetes Series +# Docker Series +https://code-maze.com/docker-series/ + +## Part 3 of the Docker Series on CodeMaze blog +https://code-maze.com/mysql-aspnetcore-docker-compose/ From b9247398008610309899e872cdf44fc3124e0f6a Mon Sep 17 00:00:00 2001 From: Code Maze Date: Mon, 14 May 2018 07:16:04 +0200 Subject: [PATCH 07/11] Updated README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0599cc8..a6d49a5 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Docker Series https://code-maze.com/docker-series/ -## Part 3 of the Docker Series on CodeMaze blog +## Part 4 of the Docker Series on CodeMaze blog https://code-maze.com/mysql-aspnetcore-docker-compose/ From cdbe0cc9011f4b34a15d09168825936134185f9a Mon Sep 17 00:00:00 2001 From: Code Maze Date: Tue, 12 Jun 2018 15:26:17 +0200 Subject: [PATCH 08/11] Created .travis.yml --- .travis.yml | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..da77466 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,9 @@ +sudo: required +language: csharp +mono: none +dotnet: 2.0 +services: + - docker +script: + - docker build -t codemazeblog/aspnetcoreapp . + - docker images From 2b8b1f6a8a8b23dcf69485d766ec949f52fe1c25 Mon Sep 17 00:00:00 2001 From: Code Maze Date: Tue, 12 Jun 2018 15:33:06 +0200 Subject: [PATCH 09/11] .travis.yml optimization --- .travis.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index da77466..8e6a77b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,4 @@ sudo: required -language: csharp -mono: none -dotnet: 2.0 services: - docker script: From a9d83828ae6d525a3240bc9353829b4581cffc23 Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Thu, 24 Jan 2019 15:30:02 +0100 Subject: [PATCH 10/11] Fixed a security issue in ASP.NET Core --- AccountOwnerServer/AccountOwnerServer.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AccountOwnerServer/AccountOwnerServer.csproj b/AccountOwnerServer/AccountOwnerServer.csproj index b0d65a5..cac8709 100644 --- a/AccountOwnerServer/AccountOwnerServer.csproj +++ b/AccountOwnerServer/AccountOwnerServer.csproj @@ -9,7 +9,7 @@ - + From f5e6c4e7c25f323af6f9dabe985adcab7190908f Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Wed, 18 Dec 2019 19:18:28 +0100 Subject: [PATCH 11/11] Project upgrade and updated .NET Core to versio 3.1 --- .dockerignore | 1 + .gitignore | 9 +- .travis.yml | 6 - AccountOwnerServer.sln | 119 ++++-------- AccountOwnerServer/AccountOwnerServer.csproj | 28 ++- .../Controllers/AccountController.cs | 115 ++++++------ .../Controllers/OwnerController.cs | 175 ++++++++++-------- .../Controllers/ValuesController.cs | 39 ---- .../Extensions/ServiceExtensions.cs | 17 +- AccountOwnerServer/MappingProfile.cs | 27 +++ AccountOwnerServer/Program.cs | 17 +- .../Properties/launchSettings.json | 55 +++--- AccountOwnerServer/Startup.cs | 152 ++++++++------- .../appsettings.Development.json | 3 +- AccountOwnerServer/appsettings.json | 29 ++- AccountOwnerServer/nlog.config | 8 +- Contracts/Contracts.csproj | 2 +- Contracts/IAccountRepository.cs | 10 +- Contracts/ILoggerManager.cs | 6 +- Contracts/IOwnerRepository.cs | 9 +- Contracts/IRepositoryBase.cs | 10 +- Contracts/IRepositoryWrapper.cs | 9 +- Dockerfile | 26 +-- Entities/DataTransferObjects/AccountDto.cs | 11 ++ .../AccountForCreationDto.cs | 10 + .../AccountForUpdateDto.cs | 10 + Entities/DataTransferObjects/OwnerDto.cs | 17 ++ .../OwnerForCreationDto.cs | 19 ++ .../DataTransferObjects/OwnerForUpdateDto.cs | 19 ++ Entities/Entities.csproj | 4 +- Entities/Enumerations/AccountType.cs | 13 -- Entities/ExtendedModels/OwnerExtended.cs | 29 --- Entities/Extensions/IEntityExtensions.cs | 19 -- Entities/Extensions/OwnerExtensions.cs | 17 -- Entities/IEntity.cs | 11 -- Entities/Models/Account.cs | 19 +- Entities/Models/Owner.cs | 20 +- Entities/RepositoryContext.cs | 8 +- LoggerService/LoggerManager.cs | 5 - LoggerService/LoggerService.csproj | 4 +- Repository/AccountRepository.cs | 11 +- Repository/OwnerRepository.cs | 44 ++--- Repository/Repository.csproj | 7 +- Repository/RepositoryBase.cs | 60 +++--- Repository/RepositoryWrapper.cs | 72 +++---- Tests/OwnerRepositoryTests.cs | 56 +++--- Tests/Tests.csproj | 12 +- docker-compose.yml | 4 +- 48 files changed, 644 insertions(+), 729 deletions(-) delete mode 100644 .travis.yml delete mode 100644 AccountOwnerServer/Controllers/ValuesController.cs create mode 100644 AccountOwnerServer/MappingProfile.cs create mode 100644 Entities/DataTransferObjects/AccountDto.cs create mode 100644 Entities/DataTransferObjects/AccountForCreationDto.cs create mode 100644 Entities/DataTransferObjects/AccountForUpdateDto.cs create mode 100644 Entities/DataTransferObjects/OwnerDto.cs create mode 100644 Entities/DataTransferObjects/OwnerForCreationDto.cs create mode 100644 Entities/DataTransferObjects/OwnerForUpdateDto.cs delete mode 100644 Entities/Enumerations/AccountType.cs delete mode 100644 Entities/ExtendedModels/OwnerExtended.cs delete mode 100644 Entities/Extensions/IEntityExtensions.cs delete mode 100644 Entities/Extensions/OwnerExtensions.cs delete mode 100644 Entities/IEntity.cs diff --git a/.dockerignore b/.dockerignore index 341d071..6d0c20f 100644 --- a/.dockerignore +++ b/.dockerignore @@ -4,4 +4,5 @@ **/Dockerfile* **/.dockerignore* **/*.user +**/.vs/ diff --git a/.gitignore b/.gitignore index e32fc58..46c22b0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ /AccountOwnerServer/obj /AccountOwnerServer/bin -/AccountOwnerServer/Projects /Contracts/obj /Contracts/bin /Entities/obj @@ -9,8 +8,8 @@ /LoggerService/bin /Repository/obj /Repository/bin -/Tests/bin +/.vs/AccountOwnerServer/v16 +/AccountOwnerServer/*.user /Tests/obj -publish -/.vs -/AccountOwnerServer/internallog.txt +/Tests/bin +/.vs/AccountOwnerServer/DesignTimeBuild/*.dtbcache diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 8e6a77b..0000000 --- a/.travis.yml +++ /dev/null @@ -1,6 +0,0 @@ -sudo: required -services: - - docker -script: - - docker build -t codemazeblog/aspnetcoreapp . - - docker images diff --git a/AccountOwnerServer.sln b/AccountOwnerServer.sln index 601977b..13c6893 100644 --- a/AccountOwnerServer.sln +++ b/AccountOwnerServer.sln @@ -1,106 +1,55 @@ + Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.27130.2010 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29424.173 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AccountOwnerServer", "AccountOwnerServer\AccountOwnerServer.csproj", "{73FB08B8-4033-4116-B3AC-EC8253B024D4}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AccountOwnerServer", "AccountOwnerServer\AccountOwnerServer.csproj", "{D9142A6E-9687-4BA4-9CF9-A9AFF006E3DF}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Contracts", "Contracts\Contracts.csproj", "{52725034-B791-4DF1-A227-02EDB5BF78E0}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Contracts", "Contracts\Contracts.csproj", "{E75B1842-10BD-4BF3-A96A-830220E57332}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LoggerService", "LoggerService\LoggerService.csproj", "{A190AF01-84DC-40A8-BEF3-C015825C8B30}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LoggerService", "LoggerService\LoggerService.csproj", "{DA66E4D3-1CDD-407D-9900-FC27F5AF8F06}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Entities", "Entities\Entities.csproj", "{8CD97B3F-C616-469C-80A2-A1B366611487}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Entities", "Entities\Entities.csproj", "{3466888D-D523-4180-B3E4-69953F45F520}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Repository", "Repository\Repository.csproj", "{CC0B1C81-CB8A-412C-BD48-BCBC3F1A1CD0}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Repository", "Repository\Repository.csproj", "{7EB45A88-65DE-4A64-852A-3B6921B32D2E}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests", "Tests\Tests.csproj", "{89CF5113-7F84-40B8-A81C-B918F00183AD}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{F45A20D5-FE64-49EB-AB95-2C90A0B76164}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU - Debug|x64 = Debug|x64 - Debug|x86 = Debug|x86 Release|Any CPU = Release|Any CPU - Release|x64 = Release|x64 - Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {73FB08B8-4033-4116-B3AC-EC8253B024D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {73FB08B8-4033-4116-B3AC-EC8253B024D4}.Debug|Any CPU.Build.0 = Debug|Any CPU - {73FB08B8-4033-4116-B3AC-EC8253B024D4}.Debug|x64.ActiveCfg = Debug|Any CPU - {73FB08B8-4033-4116-B3AC-EC8253B024D4}.Debug|x64.Build.0 = Debug|Any CPU - {73FB08B8-4033-4116-B3AC-EC8253B024D4}.Debug|x86.ActiveCfg = Debug|Any CPU - {73FB08B8-4033-4116-B3AC-EC8253B024D4}.Debug|x86.Build.0 = Debug|Any CPU - {73FB08B8-4033-4116-B3AC-EC8253B024D4}.Release|Any CPU.ActiveCfg = Release|Any CPU - {73FB08B8-4033-4116-B3AC-EC8253B024D4}.Release|Any CPU.Build.0 = Release|Any CPU - {73FB08B8-4033-4116-B3AC-EC8253B024D4}.Release|x64.ActiveCfg = Release|Any CPU - {73FB08B8-4033-4116-B3AC-EC8253B024D4}.Release|x64.Build.0 = Release|Any CPU - {73FB08B8-4033-4116-B3AC-EC8253B024D4}.Release|x86.ActiveCfg = Release|Any CPU - {73FB08B8-4033-4116-B3AC-EC8253B024D4}.Release|x86.Build.0 = Release|Any CPU - {52725034-B791-4DF1-A227-02EDB5BF78E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {52725034-B791-4DF1-A227-02EDB5BF78E0}.Debug|Any CPU.Build.0 = Debug|Any CPU - {52725034-B791-4DF1-A227-02EDB5BF78E0}.Debug|x64.ActiveCfg = Debug|Any CPU - {52725034-B791-4DF1-A227-02EDB5BF78E0}.Debug|x64.Build.0 = Debug|Any CPU - {52725034-B791-4DF1-A227-02EDB5BF78E0}.Debug|x86.ActiveCfg = Debug|Any CPU - {52725034-B791-4DF1-A227-02EDB5BF78E0}.Debug|x86.Build.0 = Debug|Any CPU - {52725034-B791-4DF1-A227-02EDB5BF78E0}.Release|Any CPU.ActiveCfg = Release|Any CPU - {52725034-B791-4DF1-A227-02EDB5BF78E0}.Release|Any CPU.Build.0 = Release|Any CPU - {52725034-B791-4DF1-A227-02EDB5BF78E0}.Release|x64.ActiveCfg = Release|Any CPU - {52725034-B791-4DF1-A227-02EDB5BF78E0}.Release|x64.Build.0 = Release|Any CPU - {52725034-B791-4DF1-A227-02EDB5BF78E0}.Release|x86.ActiveCfg = Release|Any CPU - {52725034-B791-4DF1-A227-02EDB5BF78E0}.Release|x86.Build.0 = Release|Any CPU - {A190AF01-84DC-40A8-BEF3-C015825C8B30}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {A190AF01-84DC-40A8-BEF3-C015825C8B30}.Debug|Any CPU.Build.0 = Debug|Any CPU - {A190AF01-84DC-40A8-BEF3-C015825C8B30}.Debug|x64.ActiveCfg = Debug|Any CPU - {A190AF01-84DC-40A8-BEF3-C015825C8B30}.Debug|x64.Build.0 = Debug|Any CPU - {A190AF01-84DC-40A8-BEF3-C015825C8B30}.Debug|x86.ActiveCfg = Debug|Any CPU - {A190AF01-84DC-40A8-BEF3-C015825C8B30}.Debug|x86.Build.0 = Debug|Any CPU - {A190AF01-84DC-40A8-BEF3-C015825C8B30}.Release|Any CPU.ActiveCfg = Release|Any CPU - {A190AF01-84DC-40A8-BEF3-C015825C8B30}.Release|Any CPU.Build.0 = Release|Any CPU - {A190AF01-84DC-40A8-BEF3-C015825C8B30}.Release|x64.ActiveCfg = Release|Any CPU - {A190AF01-84DC-40A8-BEF3-C015825C8B30}.Release|x64.Build.0 = Release|Any CPU - {A190AF01-84DC-40A8-BEF3-C015825C8B30}.Release|x86.ActiveCfg = Release|Any CPU - {A190AF01-84DC-40A8-BEF3-C015825C8B30}.Release|x86.Build.0 = Release|Any CPU - {8CD97B3F-C616-469C-80A2-A1B366611487}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {8CD97B3F-C616-469C-80A2-A1B366611487}.Debug|Any CPU.Build.0 = Debug|Any CPU - {8CD97B3F-C616-469C-80A2-A1B366611487}.Debug|x64.ActiveCfg = Debug|Any CPU - {8CD97B3F-C616-469C-80A2-A1B366611487}.Debug|x64.Build.0 = Debug|Any CPU - {8CD97B3F-C616-469C-80A2-A1B366611487}.Debug|x86.ActiveCfg = Debug|Any CPU - {8CD97B3F-C616-469C-80A2-A1B366611487}.Debug|x86.Build.0 = Debug|Any CPU - {8CD97B3F-C616-469C-80A2-A1B366611487}.Release|Any CPU.ActiveCfg = Release|Any CPU - {8CD97B3F-C616-469C-80A2-A1B366611487}.Release|Any CPU.Build.0 = Release|Any CPU - {8CD97B3F-C616-469C-80A2-A1B366611487}.Release|x64.ActiveCfg = Release|Any CPU - {8CD97B3F-C616-469C-80A2-A1B366611487}.Release|x64.Build.0 = Release|Any CPU - {8CD97B3F-C616-469C-80A2-A1B366611487}.Release|x86.ActiveCfg = Release|Any CPU - {8CD97B3F-C616-469C-80A2-A1B366611487}.Release|x86.Build.0 = Release|Any CPU - {CC0B1C81-CB8A-412C-BD48-BCBC3F1A1CD0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {CC0B1C81-CB8A-412C-BD48-BCBC3F1A1CD0}.Debug|Any CPU.Build.0 = Debug|Any CPU - {CC0B1C81-CB8A-412C-BD48-BCBC3F1A1CD0}.Debug|x64.ActiveCfg = Debug|Any CPU - {CC0B1C81-CB8A-412C-BD48-BCBC3F1A1CD0}.Debug|x64.Build.0 = Debug|Any CPU - {CC0B1C81-CB8A-412C-BD48-BCBC3F1A1CD0}.Debug|x86.ActiveCfg = Debug|Any CPU - {CC0B1C81-CB8A-412C-BD48-BCBC3F1A1CD0}.Debug|x86.Build.0 = Debug|Any CPU - {CC0B1C81-CB8A-412C-BD48-BCBC3F1A1CD0}.Release|Any CPU.ActiveCfg = Release|Any CPU - {CC0B1C81-CB8A-412C-BD48-BCBC3F1A1CD0}.Release|Any CPU.Build.0 = Release|Any CPU - {CC0B1C81-CB8A-412C-BD48-BCBC3F1A1CD0}.Release|x64.ActiveCfg = Release|Any CPU - {CC0B1C81-CB8A-412C-BD48-BCBC3F1A1CD0}.Release|x64.Build.0 = Release|Any CPU - {CC0B1C81-CB8A-412C-BD48-BCBC3F1A1CD0}.Release|x86.ActiveCfg = Release|Any CPU - {CC0B1C81-CB8A-412C-BD48-BCBC3F1A1CD0}.Release|x86.Build.0 = Release|Any CPU - {89CF5113-7F84-40B8-A81C-B918F00183AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {89CF5113-7F84-40B8-A81C-B918F00183AD}.Debug|Any CPU.Build.0 = Debug|Any CPU - {89CF5113-7F84-40B8-A81C-B918F00183AD}.Debug|x64.ActiveCfg = Debug|Any CPU - {89CF5113-7F84-40B8-A81C-B918F00183AD}.Debug|x64.Build.0 = Debug|Any CPU - {89CF5113-7F84-40B8-A81C-B918F00183AD}.Debug|x86.ActiveCfg = Debug|Any CPU - {89CF5113-7F84-40B8-A81C-B918F00183AD}.Debug|x86.Build.0 = Debug|Any CPU - {89CF5113-7F84-40B8-A81C-B918F00183AD}.Release|Any CPU.ActiveCfg = Release|Any CPU - {89CF5113-7F84-40B8-A81C-B918F00183AD}.Release|Any CPU.Build.0 = Release|Any CPU - {89CF5113-7F84-40B8-A81C-B918F00183AD}.Release|x64.ActiveCfg = Release|Any CPU - {89CF5113-7F84-40B8-A81C-B918F00183AD}.Release|x64.Build.0 = Release|Any CPU - {89CF5113-7F84-40B8-A81C-B918F00183AD}.Release|x86.ActiveCfg = Release|Any CPU - {89CF5113-7F84-40B8-A81C-B918F00183AD}.Release|x86.Build.0 = Release|Any CPU + {D9142A6E-9687-4BA4-9CF9-A9AFF006E3DF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D9142A6E-9687-4BA4-9CF9-A9AFF006E3DF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D9142A6E-9687-4BA4-9CF9-A9AFF006E3DF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D9142A6E-9687-4BA4-9CF9-A9AFF006E3DF}.Release|Any CPU.Build.0 = Release|Any CPU + {E75B1842-10BD-4BF3-A96A-830220E57332}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E75B1842-10BD-4BF3-A96A-830220E57332}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E75B1842-10BD-4BF3-A96A-830220E57332}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E75B1842-10BD-4BF3-A96A-830220E57332}.Release|Any CPU.Build.0 = Release|Any CPU + {DA66E4D3-1CDD-407D-9900-FC27F5AF8F06}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DA66E4D3-1CDD-407D-9900-FC27F5AF8F06}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DA66E4D3-1CDD-407D-9900-FC27F5AF8F06}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DA66E4D3-1CDD-407D-9900-FC27F5AF8F06}.Release|Any CPU.Build.0 = Release|Any CPU + {3466888D-D523-4180-B3E4-69953F45F520}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3466888D-D523-4180-B3E4-69953F45F520}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3466888D-D523-4180-B3E4-69953F45F520}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3466888D-D523-4180-B3E4-69953F45F520}.Release|Any CPU.Build.0 = Release|Any CPU + {7EB45A88-65DE-4A64-852A-3B6921B32D2E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7EB45A88-65DE-4A64-852A-3B6921B32D2E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7EB45A88-65DE-4A64-852A-3B6921B32D2E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7EB45A88-65DE-4A64-852A-3B6921B32D2E}.Release|Any CPU.Build.0 = Release|Any CPU + {F45A20D5-FE64-49EB-AB95-2C90A0B76164}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F45A20D5-FE64-49EB-AB95-2C90A0B76164}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F45A20D5-FE64-49EB-AB95-2C90A0B76164}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F45A20D5-FE64-49EB-AB95-2C90A0B76164}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {86DE07EE-BE1C-4AAB-ADAA-224170CF42B0} + SolutionGuid = {12A94BCC-D511-439A-9D3B-C1253CB1C879} EndGlobalSection EndGlobal diff --git a/AccountOwnerServer/AccountOwnerServer.csproj b/AccountOwnerServer/AccountOwnerServer.csproj index cac8709..3d26b60 100644 --- a/AccountOwnerServer/AccountOwnerServer.csproj +++ b/AccountOwnerServer/AccountOwnerServer.csproj @@ -1,26 +1,24 @@ - + - netcoreapp2.0 + netcoreapp3.1 - + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + - - - - - - - - - - - - diff --git a/AccountOwnerServer/Controllers/AccountController.cs b/AccountOwnerServer/Controllers/AccountController.cs index 4404b93..d9dbd67 100644 --- a/AccountOwnerServer/Controllers/AccountController.cs +++ b/AccountOwnerServer/Controllers/AccountController.cs @@ -1,67 +1,78 @@ -using Contracts; -using Entities.Extensions; +using AutoMapper; +using Contracts; +using Entities.DataTransferObjects; using Entities.Models; using Microsoft.AspNetCore.Mvc; using System; +using System.Collections.Generic; namespace AccountOwnerServer.Controllers { - [Route("api/[controller]")] - public class AccountController : Controller - { - private ILoggerManager _logger; - private IRepositoryWrapper _repository; + [ApiController] + [Route("api/[controller]")] + public class AccountController : Controller + { + private ILoggerManager _logger; + private IRepositoryWrapper _repository; + private IMapper _mapper; - public AccountController(ILoggerManager logger, IRepositoryWrapper repository) - { - _logger = logger; - _repository = repository; - } + public AccountController(ILoggerManager logger, IRepositoryWrapper repository, IMapper mapper) + { + _logger = logger; + _repository = repository; + _mapper = mapper; + } - [HttpGet] - public IActionResult GetAllAccounts() - { - try - { - var accounts = _repository.Account.GetAllAccounts(); + [HttpGet] + public IActionResult GetAllAccounts() + { + try + { + var accounts = _repository.Account.GetAllAccounts(); - _logger.LogInfo($"Returned all accounts from database."); + _logger.LogInfo($"Returned all accounts from database."); - return Ok(accounts); - } - catch (Exception ex) - { - _logger.LogError($"Something went wrong inside GetAllAccounts action: {ex}"); - return StatusCode(500, "Internal server error"); - } - } + var accountDtos = _mapper.Map>(accounts); + return Ok(accountDtos); + } + catch (Exception ex) + { + _logger.LogError($"Something went wrong inside GetAllAccounts action: {ex}"); + return StatusCode(500, "Internal server error"); + } + } - [HttpPost] - public IActionResult CreateOwner([FromBody]Account account) - { - try - { - if (account.IsObjectNull()) - { - _logger.LogError("Object sent from client is null."); - return BadRequest("Object is null"); - } + [HttpPost] + public IActionResult CreateAccount([FromBody]AccountForCreationDto account) + { + try + { + if (account == null) + { + _logger.LogError("Object sent from client is null."); + return BadRequest("Object is null"); + } - if (!ModelState.IsValid) - { - _logger.LogError("Invalid object sent from client."); - return BadRequest("Invalid model object"); - } + if (!ModelState.IsValid) + { + _logger.LogError("Invalid object sent from client."); + return BadRequest("Invalid model object"); + } - _repository.Account.CreateAccount(account); + var accountEntity = _mapper.Map(account); - return CreatedAtRoute("AccountById", new { id = account.Id }, account); - } - catch (Exception ex) - { - _logger.LogError($"Something went wrong inside CreateAccount action: {ex}"); - return StatusCode(500, "Internal server error"); - } - } - } + _repository.Account.CreateAccount(accountEntity); + _repository.Save(); + + var createdAccount = _mapper.Map(accountEntity); + + return CreatedAtRoute("AccountById", new { id = createdAccount.Id }, createdAccount); + } + catch (Exception ex) + { + _logger.LogError($"Something went wrong inside CreateAccount action: {ex}"); + return StatusCode(500, "Internal server error"); + } + } + } } diff --git a/AccountOwnerServer/Controllers/OwnerController.cs b/AccountOwnerServer/Controllers/OwnerController.cs index e26f275..1578424 100644 --- a/AccountOwnerServer/Controllers/OwnerController.cs +++ b/AccountOwnerServer/Controllers/OwnerController.cs @@ -1,97 +1,105 @@ -using Contracts; -using Entities.Extensions; +using AutoMapper; +using Contracts; +using Entities.DataTransferObjects; using Entities.Models; using Microsoft.AspNetCore.Mvc; using System; +using System.Collections.Generic; +using System.Linq; namespace AccountOwnerServer.Controllers { - [Route("api/owner")] - public class OwnerController : Controller - { - private ILoggerManager _logger; + [Route("api/owner")] + [ApiController] + public class OwnerController : ControllerBase + { + private ILoggerManager _logger; private IRepositoryWrapper _repository; - - public OwnerController(ILoggerManager logger, IRepositoryWrapper repository) - { - _logger = logger; + private IMapper _mapper; + + public OwnerController(ILoggerManager logger, IRepositoryWrapper repository, IMapper mapper) + { + _logger = logger; _repository = repository; + _mapper = mapper; } - - [HttpGet] - public IActionResult GetAllOwners() - { - try - { - var owners = _repository.Owner.GetAllOwners(); - + + [HttpGet] + public IActionResult GetAllOwners() + { + try + { + var owners = _repository.Owner.GetAllOwners(); _logger.LogInfo($"Returned all owners from database."); - return Ok(owners); - } - catch (Exception ex) - { - _logger.LogError($"Something went wrong inside GetAllOwners action: {ex}"); - return StatusCode(500, "Internal server error"); - } + var ownersResult = _mapper.Map>(owners); + return Ok(ownersResult); + } + catch (Exception ex) + { + _logger.LogError($"Something went wrong inside GetAllOwners action: {ex.Message}"); + return StatusCode(500, "Internal server error"); + } } - [HttpGet("{id}", Name = "OwnerById")] - public IActionResult GetOwnerById(Guid id) - { - try - { - var owner = _repository.Owner.GetOwnerById(id); - - if (owner.IsEmptyObject()) - { - _logger.LogError($"Owner with id: {id}, hasn't been found in db."); - return NotFound(); - } - else - { + [HttpGet("{id}", Name = "OwnerById")] + public IActionResult GetOwnerById(Guid id) + { + try + { + var owner = _repository.Owner.GetOwnerById(id); + if (owner == null) + { + _logger.LogError($"Owner with id: {id}, hasn't been found in db."); + return NotFound(); + } + else + { _logger.LogInfo($"Returned owner with id: {id}"); - return Ok(owner); - } - } - catch (Exception ex) - { - _logger.LogError($"Something went wrong inside GetOwnerById action: {ex.Message}"); - return StatusCode(500, "Internal server error"); - } - } - [HttpGet("{id}/account")] - public IActionResult GetOwnerAccounts(Guid id) - { - try - { - var owner = _repository.Owner.GetOwnerWithDetails(id); + var ownerResult = _mapper.Map(owner); + return Ok(ownerResult); + } + } + catch (Exception ex) + { + _logger.LogError($"Something went wrong inside GetOwnerById action: {ex.Message}"); + return StatusCode(500, "Internal server error"); + } + } - if (owner.IsEmptyObject()) - { - _logger.LogError($"Owner with id: {id}, hasn't been found in db."); - return NotFound(); - } - else - { + [HttpGet("{id}/account")] + public IActionResult GetOwnerWithDetails(Guid id) + { + try + { + var owner = _repository.Owner.GetOwnerWithDetails(id); + if (owner == null) + { + _logger.LogError($"Owner with id: {id}, hasn't been found in db."); + return NotFound(); + } + else + { _logger.LogInfo($"Returned owner with details for id: {id}"); - return Ok(owner); - } - } - catch (Exception ex) - { - _logger.LogError($"Something went wrong inside GetOwnerWithDetails action: {ex.Message}"); - return StatusCode(500, "Internal server error"); + + var ownerResult = _mapper.Map(owner); + return Ok(ownerResult); + } + } + catch (Exception ex) + { + _logger.LogError($"Something went wrong inside GetOwnerWithDetails action: {ex.Message}"); + return StatusCode(500, "Internal server error"); } } [HttpPost] - public IActionResult CreateOwner([FromBody]Owner owner) + public IActionResult CreateOwner([FromBody]OwnerForCreationDto owner) { try { - if (owner.IsObjectNull()) + if (owner == null) { _logger.LogError("Owner object sent from client is null."); return BadRequest("Owner object is null"); @@ -103,9 +111,14 @@ public IActionResult CreateOwner([FromBody]Owner owner) return BadRequest("Invalid model object"); } - _repository.Owner.CreateOwner(owner); + var ownerEntity = _mapper.Map(owner); + + _repository.Owner.CreateOwner(ownerEntity); + _repository.Save(); - return CreatedAtRoute("OwnerById", new { id = owner.Id }, owner); + var createdOwner = _mapper.Map(ownerEntity); + + return CreatedAtRoute("OwnerById", new { id = createdOwner.Id }, createdOwner); } catch (Exception ex) { @@ -115,11 +128,11 @@ public IActionResult CreateOwner([FromBody]Owner owner) } [HttpPut("{id}")] - public IActionResult UpdateOwner(Guid id, [FromBody]Owner owner) + public IActionResult UpdateOwner(Guid id, [FromBody]OwnerForUpdateDto owner) { try { - if (owner.IsObjectNull()) + if (owner == null) { _logger.LogError("Owner object sent from client is null."); return BadRequest("Owner object is null"); @@ -131,14 +144,17 @@ public IActionResult UpdateOwner(Guid id, [FromBody]Owner owner) return BadRequest("Invalid model object"); } - var dbOwner = _repository.Owner.GetOwnerById(id); - if (dbOwner.IsEmptyObject()) + var ownerEntity = _repository.Owner.GetOwnerById(id); + if (ownerEntity == null) { _logger.LogError($"Owner with id: {id}, hasn't been found in db."); return NotFound(); } - _repository.Owner.UpdateOwner(dbOwner, owner); + _mapper.Map(owner, ownerEntity); + + _repository.Owner.UpdateOwner(ownerEntity); + _repository.Save(); return NoContent(); } @@ -155,13 +171,14 @@ public IActionResult DeleteOwner(Guid id) try { var owner = _repository.Owner.GetOwnerById(id); - if (owner.IsEmptyObject()) + if (owner == null) { _logger.LogError($"Owner with id: {id}, hasn't been found in db."); return NotFound(); } _repository.Owner.DeleteOwner(owner); + _repository.Save(); return NoContent(); } @@ -172,4 +189,4 @@ public IActionResult DeleteOwner(Guid id) } } } -} +} \ No newline at end of file diff --git a/AccountOwnerServer/Controllers/ValuesController.cs b/AccountOwnerServer/Controllers/ValuesController.cs deleted file mode 100644 index 71841b7..0000000 --- a/AccountOwnerServer/Controllers/ValuesController.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; -using Contracts; -using Entities; -using Entities.Enumerations; -using Entities.Models; - -namespace AccountOwnerServer.Controllers -{ - [Route("api/[controller]")] - public class ValuesController : Controller - { - private ILoggerManager _logger; - private IRepositoryWrapper _repoWrapper; - - public ValuesController(ILoggerManager logger, IRepositoryWrapper repoWrapper) - { - _logger = logger; - _repoWrapper = repoWrapper; - } - // GET api/values - [HttpGet] - public IActionResult Get() - { - //var domesticAccounts = _repoWrapper.Account.FindByCondition(x => x.AccountType.Equals("Domestic")); - var owners = _repoWrapper.Owner.GetAllOwners(); - - _logger.LogInfo("Here is info message from our values controller."); - _logger.LogDebug("Here is debug message from our values controller."); - _logger.LogWarn("Here is warn message from our values controller."); - _logger.LogError("Here is error message from our values controller."); - - return Ok(owners); - } - } -} diff --git a/AccountOwnerServer/Extensions/ServiceExtensions.cs b/AccountOwnerServer/Extensions/ServiceExtensions.cs index ca6eb09..7e31db1 100644 --- a/AccountOwnerServer/Extensions/ServiceExtensions.cs +++ b/AccountOwnerServer/Extensions/ServiceExtensions.cs @@ -18,16 +18,15 @@ public static void ConfigureCors(this IServiceCollection services) options.AddPolicy("CorsPolicy", builder => builder.AllowAnyOrigin() .AllowAnyMethod() - .AllowAnyHeader() - .AllowCredentials()); + .AllowAnyHeader()); }); } public static void ConfigureIISIntegration(this IServiceCollection services) { - services.Configure(options => + services.Configure(options => { - + }); } @@ -36,15 +35,15 @@ public static void ConfigureLoggerService(this IServiceCollection services) services.AddSingleton(); } - public static void ConfigureMySqlContext(this IServiceCollection services, IConfiguration config) + public static void ConfigureMySqlContext(this IServiceCollection services, IConfiguration config) { - var connectionString = config["mysqlconnection:connectionString"]; + var connectionString = config["mysqlconnection:connectionString"]; services.AddDbContext(o => o.UseMySql(connectionString)); } - public static void ConfigureRepositoryWrapper(this IServiceCollection services) - { - services.AddScoped(); + public static void ConfigureRepositoryWrapper(this IServiceCollection services) + { + services.AddScoped(); } } } diff --git a/AccountOwnerServer/MappingProfile.cs b/AccountOwnerServer/MappingProfile.cs new file mode 100644 index 0000000..8bf056e --- /dev/null +++ b/AccountOwnerServer/MappingProfile.cs @@ -0,0 +1,27 @@ +using AutoMapper; +using Entities.DataTransferObjects; +using Entities.Models; +using System.Linq; + +namespace AccountOwnerServer +{ + public class MappingProfile : Profile + { + public MappingProfile() + { + CreateMap(); + + CreateMap(); + + CreateMap(); + + CreateMap(); + + CreateMap(); + + CreateMap(); + + CreateMap(); + } + } +} diff --git a/AccountOwnerServer/Program.cs b/AccountOwnerServer/Program.cs index 0be0d38..7131aeb 100644 --- a/AccountOwnerServer/Program.cs +++ b/AccountOwnerServer/Program.cs @@ -1,11 +1,10 @@ -using System; +using System; using System.Collections.Generic; -using System.IO; using System.Linq; using System.Threading.Tasks; -using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; namespace AccountOwnerServer @@ -14,12 +13,14 @@ public class Program { public static void Main(string[] args) { - BuildWebHost(args).Run(); + CreateHostBuilder(args).Build().Run(); } - public static IWebHost BuildWebHost(string[] args) => - WebHost.CreateDefaultBuilder(args) - .UseStartup() - .Build(); + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); } } diff --git a/AccountOwnerServer/Properties/launchSettings.json b/AccountOwnerServer/Properties/launchSettings.json index 348af13..4b82d54 100644 --- a/AccountOwnerServer/Properties/launchSettings.json +++ b/AccountOwnerServer/Properties/launchSettings.json @@ -1,27 +1,28 @@ -{ - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:5000/", - "sslPort": 0 - } - }, - "profiles": { - "IIS Express": { - "commandName": "IISExpress", - "launchBrowser": false, - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - }, - "AccountOwnerServer": { - "commandName": "Project", - "launchBrowser": false, - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - }, - "applicationUrl": "http://localhost:5000/" - } - } -} +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://0.0.0.0:5000", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": false, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "AccountOwnerServer": { + "commandName": "Project", + "launchBrowser": false, + "applicationUrl": "http://0.0.0.0:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} \ No newline at end of file diff --git a/AccountOwnerServer/Startup.cs b/AccountOwnerServer/Startup.cs index fa74191..c132c1b 100644 --- a/AccountOwnerServer/Startup.cs +++ b/AccountOwnerServer/Startup.cs @@ -1,87 +1,83 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +using AccountOwnerServer.Extensions; +using AutoMapper; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.HttpOverrides; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; -using AccountOwnerServer.Extensions; -using Microsoft.AspNetCore.HttpOverrides; +using Microsoft.Extensions.Hosting; +using Microsoft.OpenApi.Models; +using NLog; using System.IO; -using NLog.Extensions.Logging; -using Swashbuckle.AspNetCore.Swagger; namespace AccountOwnerServer { - public class Startup - { - public Startup(IConfiguration configuration, ILoggerFactory loggerFactory) - { - loggerFactory.ConfigureNLog(String.Concat(Directory.GetCurrentDirectory(), "/nlog.config")); - Configuration = configuration; - } - - public IConfiguration Configuration { get; } - - public void ConfigureServices(IServiceCollection services) - { - services.ConfigureCors(); - - services.ConfigureIISIntegration(); - - services.ConfigureLoggerService(); - - services.ConfigureMySqlContext(Configuration); - - services.ConfigureRepositoryWrapper(); - - services.AddMvc(); - - services.AddSwaggerGen(c => - { - c.SwaggerDoc("v1", new Info { Title = "AccountOwner API", Version = "v1" }); - }); - } - - public void Configure(IApplicationBuilder app, IHostingEnvironment env) - { - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - } - - app.UseCors("CorsPolicy"); - - app.UseForwardedHeaders(new ForwardedHeadersOptions - { - ForwardedHeaders = ForwardedHeaders.All - }); - - app.Use(async (context, next) => - { - await next(); - - if (context.Response.StatusCode == 404 - && !Path.HasExtension(context.Request.Path.Value)) - { - context.Request.Path = "/index.html"; - await next(); - } - }); - - app.UseStaticFiles(); - - app.UseMvc(); - - app.UseSwagger(); - - app.UseSwaggerUI(c => - { - c.SwaggerEndpoint("/swagger/v1/swagger.json", "AccountOwner API V1"); - }); - } - } + public class Startup + { + public Startup(IConfiguration configuration) + { + LogManager.LoadConfiguration(string.Concat(Directory.GetCurrentDirectory(), "/nlog.config")); + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + public void ConfigureServices(IServiceCollection services) + { + services.ConfigureCors(); + services.ConfigureIISIntegration(); + services.ConfigureLoggerService(); + services.ConfigureMySqlContext(Configuration); + services.ConfigureRepositoryWrapper(); + services.AddAutoMapper(typeof(Startup)); + + services.AddSwaggerGen(c => + { + c.SwaggerDoc("v1", new OpenApiInfo + { + Title = "AccountOwner API", + Version = "v1" + }); + }); + + services.AddControllers(); + } + + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + + app.UseHttpsRedirection(); + app.UseStaticFiles(); + + app.UseCors("CorsPolicy"); + + app.UseForwardedHeaders(new ForwardedHeadersOptions + { + ForwardedHeaders = ForwardedHeaders.All + }); + + app.UseRouting(); + + app.UseAuthorization(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); + + // Enable middleware to serve generated Swagger as a JSON endpoint. + app.UseSwagger(); + + // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), + // specifying the Swagger JSON endpoint. + app.UseSwaggerUI(c => + { + c.SwaggerEndpoint("/swagger/v1/swagger.json", "AccountOwner API V1"); + }); + } + } } diff --git a/AccountOwnerServer/appsettings.Development.json b/AccountOwnerServer/appsettings.Development.json index fa8ce71..e203e94 100644 --- a/AccountOwnerServer/appsettings.Development.json +++ b/AccountOwnerServer/appsettings.Development.json @@ -1,6 +1,5 @@ -{ +{ "Logging": { - "IncludeScopes": false, "LogLevel": { "Default": "Debug", "System": "Information", diff --git a/AccountOwnerServer/appsettings.json b/AccountOwnerServer/appsettings.json index 5935f76..2c0f849 100644 --- a/AccountOwnerServer/appsettings.json +++ b/AccountOwnerServer/appsettings.json @@ -1,18 +1,13 @@ -{ - "Logging": { - "IncludeScopes": false, - "Debug": { - "LogLevel": { - "Default": "Warning" - } - }, - "Console": { - "LogLevel": { - "Default": "Warning" - } - } - }, - "mysqlconnection": { - "connectionString": "server=db;port=3306;userid=dbuser;password=dbuserpassword;database=accountowner;" - } +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "mysqlconnection": { + "connectionString": "server=db;port=3306;userid=dbuser;password=dbuserpassword;database=accountowner;" + }, + "AllowedHosts": "*" } diff --git a/AccountOwnerServer/nlog.config b/AccountOwnerServer/nlog.config index 69efc94..47744e0 100644 --- a/AccountOwnerServer/nlog.config +++ b/AccountOwnerServer/nlog.config @@ -3,15 +3,11 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true" internalLogLevel="Trace" - internalLogFile="internallog.txt"> - - - - + internalLogFile="d:Projects\Blog-AccountOwner\Project\internal_logs\internallog.txt"> diff --git a/Contracts/Contracts.csproj b/Contracts/Contracts.csproj index ce507b4..96a3e45 100644 --- a/Contracts/Contracts.csproj +++ b/Contracts/Contracts.csproj @@ -1,7 +1,7 @@ - netcoreapp2.0 + netcoreapp3.1 diff --git a/Contracts/IAccountRepository.cs b/Contracts/IAccountRepository.cs index 945b7f5..776ae31 100644 --- a/Contracts/IAccountRepository.cs +++ b/Contracts/IAccountRepository.cs @@ -3,9 +3,9 @@ namespace Contracts { - public interface IAccountRepository - { - IEnumerable GetAllAccounts(); - void CreateAccount(Account account); - } + public interface IAccountRepository : IRepositoryBase + { + IEnumerable GetAllAccounts(); + void CreateAccount(Account account); + } } diff --git a/Contracts/ILoggerManager.cs b/Contracts/ILoggerManager.cs index 7dcd872..ee1f081 100644 --- a/Contracts/ILoggerManager.cs +++ b/Contracts/ILoggerManager.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace Contracts +namespace Contracts { public interface ILoggerManager { diff --git a/Contracts/IOwnerRepository.cs b/Contracts/IOwnerRepository.cs index da31e25..d82d2a8 100644 --- a/Contracts/IOwnerRepository.cs +++ b/Contracts/IOwnerRepository.cs @@ -1,17 +1,16 @@ -using Entities.ExtendedModels; -using Entities.Models; +using Entities.Models; using System; using System.Collections.Generic; namespace Contracts { - public interface IOwnerRepository + public interface IOwnerRepository : IRepositoryBase { IEnumerable GetAllOwners(); Owner GetOwnerById(Guid ownerId); - OwnerExtended GetOwnerWithDetails(Guid ownerId); + Owner GetOwnerWithDetails(Guid ownerId); void CreateOwner(Owner owner); - void UpdateOwner(Owner dbOwner, Owner owner); + void UpdateOwner(Owner owner); void DeleteOwner(Owner owner); } } diff --git a/Contracts/IRepositoryBase.cs b/Contracts/IRepositoryBase.cs index 336894d..7a2b84b 100644 --- a/Contracts/IRepositoryBase.cs +++ b/Contracts/IRepositoryBase.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Linq.Expressions; using System.Text; @@ -7,11 +8,10 @@ namespace Contracts { public interface IRepositoryBase { - IEnumerable FindAll(); - IEnumerable FindByCondition(Expression> expression); - void Create(T entity); - void Update(T entity); + IQueryable FindAll(); + IQueryable FindByCondition(Expression> expression); + void Create(T entity); + void Update(T entity); void Delete(T entity); - void Save(); } } diff --git a/Contracts/IRepositoryWrapper.cs b/Contracts/IRepositoryWrapper.cs index f4275a1..445d2f7 100644 --- a/Contracts/IRepositoryWrapper.cs +++ b/Contracts/IRepositoryWrapper.cs @@ -4,9 +4,10 @@ namespace Contracts { - public interface IRepositoryWrapper - { - IOwnerRepository Owner { get; } - IAccountRepository Account { get; } + public interface IRepositoryWrapper + { + IOwnerRepository Owner { get; } + IAccountRepository Account { get; } + void Save(); } } diff --git a/Dockerfile b/Dockerfile index e055560..edc400e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,23 +1,25 @@ -FROM microsoft/aspnetcore-build as build-image - +FROM mcr.microsoft.com/dotnet/core/sdk:3.1 as build-image + WORKDIR /home/app - + COPY ./*.sln ./ COPY ./*/*.csproj ./ RUN for file in $(ls *.csproj); do mkdir -p ./${file%.*}/ && mv $file ./${file%.*}/; done - + RUN dotnet restore - + COPY . . - + RUN dotnet test ./Tests/Tests.csproj - + RUN dotnet publish ./AccountOwnerServer/AccountOwnerServer.csproj -o /publish/ - -FROM microsoft/aspnetcore - + +FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 + WORKDIR /publish - + COPY --from=build-image /publish . - + +ENV ASPNETCORE_URLS="http://0.0.0.0:5000" + ENTRYPOINT ["dotnet", "AccountOwnerServer.dll"] \ No newline at end of file diff --git a/Entities/DataTransferObjects/AccountDto.cs b/Entities/DataTransferObjects/AccountDto.cs new file mode 100644 index 0000000..ef800f3 --- /dev/null +++ b/Entities/DataTransferObjects/AccountDto.cs @@ -0,0 +1,11 @@ +using System; + +namespace Entities.DataTransferObjects +{ + public class AccountDto + { + public Guid Id { get; set; } + public DateTime DateCreated { get; set; } + public string AccountType { get; set; } + } +} diff --git a/Entities/DataTransferObjects/AccountForCreationDto.cs b/Entities/DataTransferObjects/AccountForCreationDto.cs new file mode 100644 index 0000000..8fde44f --- /dev/null +++ b/Entities/DataTransferObjects/AccountForCreationDto.cs @@ -0,0 +1,10 @@ +using System; + +namespace Entities.DataTransferObjects +{ + public class AccountForCreationDto + { + public DateTime DateCreated { get; set; } + public string AccountType { get; set; } + } +} diff --git a/Entities/DataTransferObjects/AccountForUpdateDto.cs b/Entities/DataTransferObjects/AccountForUpdateDto.cs new file mode 100644 index 0000000..9d75fa3 --- /dev/null +++ b/Entities/DataTransferObjects/AccountForUpdateDto.cs @@ -0,0 +1,10 @@ +using System; + +namespace Entities.DataTransferObjects +{ + public class AccountForUpdateDto + { + public DateTime DateCreated { get; set; } + public string AccountType { get; set; } + } +} diff --git a/Entities/DataTransferObjects/OwnerDto.cs b/Entities/DataTransferObjects/OwnerDto.cs new file mode 100644 index 0000000..cdbdddc --- /dev/null +++ b/Entities/DataTransferObjects/OwnerDto.cs @@ -0,0 +1,17 @@ +using Entities.Models; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Entities.DataTransferObjects +{ + public class OwnerDto + { + public Guid Id { get; set; } + public string Name { get; set; } + public DateTime DateOfBirth { get; set; } + public string Address { get; set; } + + public IEnumerable Accounts { get; set; } + } +} diff --git a/Entities/DataTransferObjects/OwnerForCreationDto.cs b/Entities/DataTransferObjects/OwnerForCreationDto.cs new file mode 100644 index 0000000..18595f6 --- /dev/null +++ b/Entities/DataTransferObjects/OwnerForCreationDto.cs @@ -0,0 +1,19 @@ +using System; +using System.ComponentModel.DataAnnotations; + +namespace Entities.DataTransferObjects +{ + public class OwnerForCreationDto + { + [Required(ErrorMessage = "Name is required")] + [StringLength(60, ErrorMessage = "Name can't be longer than 60 characters")] + public string Name { get; set; } + + [Required(ErrorMessage = "Date of birth is required")] + public DateTime DateOfBirth { get; set; } + + [Required(ErrorMessage = "Address is required")] + [StringLength(100, ErrorMessage = "Address cannot be loner then 100 characters")] + public string Address { get; set; } + } +} diff --git a/Entities/DataTransferObjects/OwnerForUpdateDto.cs b/Entities/DataTransferObjects/OwnerForUpdateDto.cs new file mode 100644 index 0000000..5bd7702 --- /dev/null +++ b/Entities/DataTransferObjects/OwnerForUpdateDto.cs @@ -0,0 +1,19 @@ +using System; +using System.ComponentModel.DataAnnotations; + +namespace Entities.DataTransferObjects +{ + public class OwnerForUpdateDto + { + [Required(ErrorMessage = "Name is required")] + [StringLength(60, ErrorMessage = "Name can't be longer than 60 characters")] + public string Name { get; set; } + + [Required(ErrorMessage = "Date of birth is required")] + public DateTime DateOfBirth { get; set; } + + [Required(ErrorMessage = "Address is required")] + [StringLength(100, ErrorMessage = "Address cannot be loner then 100 characters")] + public string Address { get; set; } + } +} diff --git a/Entities/Entities.csproj b/Entities/Entities.csproj index 4de557e..01886ec 100644 --- a/Entities/Entities.csproj +++ b/Entities/Entities.csproj @@ -1,11 +1,11 @@ - netcoreapp2.0 + netcoreapp3.1 - + diff --git a/Entities/Enumerations/AccountType.cs b/Entities/Enumerations/AccountType.cs deleted file mode 100644 index 7cad9c9..0000000 --- a/Entities/Enumerations/AccountType.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace Entities.Enumerations -{ - public enum AccountType - { - Domestic, - Savings, - Foreign - } -} diff --git a/Entities/ExtendedModels/OwnerExtended.cs b/Entities/ExtendedModels/OwnerExtended.cs deleted file mode 100644 index 87b8126..0000000 --- a/Entities/ExtendedModels/OwnerExtended.cs +++ /dev/null @@ -1,29 +0,0 @@ -using Entities.Models; -using System; -using System.Collections.Generic; -using System.Text; - -namespace Entities.ExtendedModels -{ - public class OwnerExtended: IEntity - { - public Guid Id { get; set; } - public string Name { get; set; } - public DateTime DateOfBirth { get; set; } - public string Address { get; set; } - - public IEnumerable Accounts { get; set; } - - public OwnerExtended() - { - } - - public OwnerExtended(Owner owner) - { - Id = owner.Id; - Name = owner.Name; - DateOfBirth = owner.DateOfBirth; - Address = owner.Address; - } - } -} diff --git a/Entities/Extensions/IEntityExtensions.cs b/Entities/Extensions/IEntityExtensions.cs deleted file mode 100644 index 992fca0..0000000 --- a/Entities/Extensions/IEntityExtensions.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace Entities.Extensions -{ - public static class IEntityExtensions - { - public static bool IsObjectNull(this IEntity entity) - { - return entity == null; - } - - public static bool IsEmptyObject(this IEntity entity) - { - return entity.Id.Equals(Guid.Empty); - } - } -} diff --git a/Entities/Extensions/OwnerExtensions.cs b/Entities/Extensions/OwnerExtensions.cs deleted file mode 100644 index 03e8a0a..0000000 --- a/Entities/Extensions/OwnerExtensions.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Entities.Models; -using System; -using System.Collections.Generic; -using System.Text; - -namespace Entities.Extensions -{ - public static class OwnerExtensions - { - public static void Map(this Owner dbOwner, Owner owner) - { - dbOwner.Name = owner.Name; - dbOwner.Address = owner.Address; - dbOwner.DateOfBirth = owner.DateOfBirth; - } - } -} diff --git a/Entities/IEntity.cs b/Entities/IEntity.cs deleted file mode 100644 index 8dcadc4..0000000 --- a/Entities/IEntity.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace Entities -{ - public interface IEntity - { - Guid Id { get; set; } - } -} diff --git a/Entities/Models/Account.cs b/Entities/Models/Account.cs index 105d588..073d248 100644 --- a/Entities/Models/Account.cs +++ b/Entities/Models/Account.cs @@ -1,26 +1,25 @@ -using Entities.Enumerations; -using System; -using System.Collections.Generic; +using System; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; -using System.Text; namespace Entities.Models { - [Table("account")] - public class Account : IEntity + [Table("account")] + public class Account { - [Key] [Column("AccountId")] public Guid Id { get; set; } - [Required(ErrorMessage = "Date created is required")] + [Required(ErrorMessage = "Date created is required")] public DateTime DateCreated { get; set; } - [Required(ErrorMessage = "Account type is required")] + [Required(ErrorMessage = "Account type is required")] public string AccountType { get; set; } - [Required(ErrorMessage = "Owner Id is required")] + [Required(ErrorMessage = "Owner Id is required")] + + [ForeignKey(nameof(Owner))] public Guid OwnerId { get; set; } + public Owner Owner { get; set; } } } diff --git a/Entities/Models/Owner.cs b/Entities/Models/Owner.cs index d52a756..36c3b07 100644 --- a/Entities/Models/Owner.cs +++ b/Entities/Models/Owner.cs @@ -2,26 +2,26 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; -using System.Text; namespace Entities.Models { - [Table("owner")] - public class Owner: IEntity - { - [Key] + [Table("owner")] + public class Owner + { [Column("OwnerId")] public Guid Id { get; set; } - [Required(ErrorMessage = "Name is required")] - [StringLength(60, ErrorMessage = "Name can't be longer than 60 characters")] + [Required(ErrorMessage = "Name is required")] + [StringLength(60, ErrorMessage = "Name can't be longer than 60 characters")] public string Name { get; set; } - [Required(ErrorMessage = "Date of birth is required")] + [Required(ErrorMessage = "Date of birth is required")] public DateTime DateOfBirth { get; set; } - [Required(ErrorMessage = "Address is required")] - [StringLength(100, ErrorMessage = "Address can not be loner then 100 characters")] + [Required(ErrorMessage = "Address is required")] + [StringLength(100, ErrorMessage = "Address cannot be loner then 100 characters")] public string Address { get; set; } + + public ICollection Accounts { get; set; } } } diff --git a/Entities/RepositoryContext.cs b/Entities/RepositoryContext.cs index 5687078..14a2d7b 100644 --- a/Entities/RepositoryContext.cs +++ b/Entities/RepositoryContext.cs @@ -3,11 +3,11 @@ namespace Entities { - public class RepositoryContext: DbContext + public class RepositoryContext : DbContext { - public RepositoryContext(DbContextOptions options) - :base(options) - { + public RepositoryContext(DbContextOptions options) + : base(options) + { } public DbSet Owners { get; set; } diff --git a/LoggerService/LoggerManager.cs b/LoggerService/LoggerManager.cs index b5d93f1..bf2f7be 100644 --- a/LoggerService/LoggerManager.cs +++ b/LoggerService/LoggerManager.cs @@ -1,6 +1,5 @@ using Contracts; using NLog; -using System; namespace LoggerService { @@ -8,10 +7,6 @@ public class LoggerManager : ILoggerManager { private static ILogger logger = LogManager.GetCurrentClassLogger(); - public LoggerManager() - { - } - public void LogDebug(string message) { logger.Debug(message); diff --git a/LoggerService/LoggerService.csproj b/LoggerService/LoggerService.csproj index 885b749..f24e41a 100644 --- a/LoggerService/LoggerService.csproj +++ b/LoggerService/LoggerService.csproj @@ -1,11 +1,11 @@ - netcoreapp2.0 + netcoreapp3.1 - + diff --git a/Repository/AccountRepository.cs b/Repository/AccountRepository.cs index 0cd0be0..4fa3623 100644 --- a/Repository/AccountRepository.cs +++ b/Repository/AccountRepository.cs @@ -7,11 +7,11 @@ namespace Repository { - public class AccountRepository: RepositoryBase, IAccountRepository - { - public AccountRepository(RepositoryContext repositoryContext) - :base(repositoryContext) - { + public class AccountRepository : RepositoryBase, IAccountRepository + { + public AccountRepository(RepositoryContext repositoryContext) + : base(repositoryContext) + { } public IEnumerable GetAllAccounts() @@ -24,7 +24,6 @@ public void CreateAccount(Account account) { account.Id = Guid.NewGuid(); Create(account); - Save(); } } } diff --git a/Repository/OwnerRepository.cs b/Repository/OwnerRepository.cs index 60422c8..ac72bb2 100644 --- a/Repository/OwnerRepository.cs +++ b/Repository/OwnerRepository.cs @@ -1,61 +1,53 @@ using Contracts; using Entities; using Entities.Models; +using Microsoft.EntityFrameworkCore; +using System; using System.Collections.Generic; using System.Linq; -using System; -using Entities.ExtendedModels; -using Entities.Extensions; namespace Repository { - public class OwnerRepository: RepositoryBase, IOwnerRepository - { - public OwnerRepository(RepositoryContext repositoryContext) - :base(repositoryContext) - { + public class OwnerRepository : RepositoryBase, IOwnerRepository + { + public OwnerRepository(RepositoryContext repositoryContext) + : base(repositoryContext) + { } - public IEnumerable GetAllOwners() - { + public IEnumerable GetAllOwners() + { return FindAll() - .OrderBy(ow => ow.Name); + .OrderBy(ow => ow.Name) + .ToList(); } public Owner GetOwnerById(Guid ownerId) { return FindByCondition(owner => owner.Id.Equals(ownerId)) - .DefaultIfEmpty(new Owner()) - .FirstOrDefault(); + .FirstOrDefault(); } - public OwnerExtended GetOwnerWithDetails(Guid ownerId) + public Owner GetOwnerWithDetails(Guid ownerId) { - return new OwnerExtended(GetOwnerById(ownerId)) - { - Accounts = RepositoryContext.Accounts - .Where(a => a.OwnerId == ownerId) - }; + return FindByCondition(owner => owner.Id.Equals(ownerId)) + .Include(ac => ac.Accounts) + .FirstOrDefault(); } public void CreateOwner(Owner owner) { - owner.Id = Guid.NewGuid(); Create(owner); - Save(); } - public void UpdateOwner(Owner dbOwner, Owner owner) + public void UpdateOwner(Owner owner) { - dbOwner.Map(owner); - Update(dbOwner); - Save(); + Update(owner); } public void DeleteOwner(Owner owner) { Delete(owner); - Save(); } } } diff --git a/Repository/Repository.csproj b/Repository/Repository.csproj index e1eecf3..a2fe898 100644 --- a/Repository/Repository.csproj +++ b/Repository/Repository.csproj @@ -1,16 +1,11 @@ - netcoreapp2.0 + netcoreapp3.1 - - - - - diff --git a/Repository/RepositoryBase.cs b/Repository/RepositoryBase.cs index 64aa6af..5fdacee 100644 --- a/Repository/RepositoryBase.cs +++ b/Repository/RepositoryBase.cs @@ -1,7 +1,7 @@ using Contracts; using Entities; +using Microsoft.EntityFrameworkCore; using System; -using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; @@ -9,41 +9,35 @@ namespace Repository { public abstract class RepositoryBase : IRepositoryBase where T : class { - protected RepositoryContext RepositoryContext { get; set; } - - public RepositoryBase(RepositoryContext repositoryContext) - { - this.RepositoryContext = repositoryContext; - } - - public IEnumerable FindAll() - { - return this.RepositoryContext.Set(); - } - - public IEnumerable FindByCondition(Expression> expression) - { - return this.RepositoryContext.Set().Where(expression); - } - - public void Create(T entity) + protected RepositoryContext RepositoryContext { get; set; } + public RepositoryBase(RepositoryContext repositoryContext) { - this.RepositoryContext.Set().Add(entity); - } - - public void Update(T entity) + RepositoryContext = repositoryContext; + } + + public IQueryable FindAll() + { + return RepositoryContext.Set().AsNoTracking(); + } + + public IQueryable FindByCondition(Expression> expression) + { + return RepositoryContext.Set().Where(expression).AsNoTracking(); + } + + public void Create(T entity) + { + RepositoryContext.Set().Add(entity); + } + + public void Update(T entity) { - this.RepositoryContext.Set().Update(entity); - } - + RepositoryContext.Set().Update(entity); + } + public void Delete(T entity) - { - this.RepositoryContext.Set().Remove(entity); - } - - public void Save() - { - this.RepositoryContext.SaveChanges(); + { + RepositoryContext.Set().Remove(entity); } } } diff --git a/Repository/RepositoryWrapper.cs b/Repository/RepositoryWrapper.cs index d8be3f9..01a8b10 100644 --- a/Repository/RepositoryWrapper.cs +++ b/Repository/RepositoryWrapper.cs @@ -3,41 +3,43 @@ namespace Repository { - public class RepositoryWrapper : IRepositoryWrapper - { - private RepositoryContext _repoContext; - private IOwnerRepository _owner; - private IAccountRepository _account; - - public IOwnerRepository Owner - { - get - { - if (_owner == null) - { - _owner = new OwnerRepository(_repoContext); - } - - return _owner; - } - } - - public IAccountRepository Account - { - get - { - if (_account == null) - { - _account = new AccountRepository(_repoContext); - } - - return _account; - } - } - - public RepositoryWrapper(RepositoryContext repositoryContext) + public class RepositoryWrapper : IRepositoryWrapper + { + private RepositoryContext _repoContext; + private IOwnerRepository _owner; + private IAccountRepository _account; + public IOwnerRepository Owner + { + get + { + if (_owner == null) + { + _owner = new OwnerRepository(_repoContext); + } + return _owner; + } + } + + public IAccountRepository Account + { + get + { + if (_account == null) + { + _account = new AccountRepository(_repoContext); + } + return _account; + } + } + + public RepositoryWrapper(RepositoryContext repositoryContext) + { + _repoContext = repositoryContext; + } + + public void Save() { - _repoContext = repositoryContext; - } + _repoContext.SaveChanges(); + } } } diff --git a/Tests/OwnerRepositoryTests.cs b/Tests/OwnerRepositoryTests.cs index 81d3133..0d5e4bf 100644 --- a/Tests/OwnerRepositoryTests.cs +++ b/Tests/OwnerRepositoryTests.cs @@ -8,35 +8,35 @@ namespace Tests { - public class OwnerRepositoryTests - { - [Fact] - public void GetAllOwners_ReturnsListOfOwners_WithSingleOwner() - { - // Arrange - var mockRepo = new Mock(); - mockRepo.Setup(repo => (repo.GetAllOwners())).Returns(GetOwners()); + public class OwnerRepositoryTests + { + [Fact] + public void GetAllOwners_ReturnsListOfOwners_WithSingleOwner() + { + // Arrange + var mockRepo = new Mock(); + mockRepo.Setup(repo => (repo.GetAllOwners())).Returns(GetOwners()); - // Act - var result = mockRepo.Object.GetAllOwners().ToList(); + // Act + var result = mockRepo.Object.GetAllOwners().ToList(); - // Assert - Assert.IsType>(result); - Assert.Single(result); - } + // Assert + Assert.IsType>(result); + Assert.Single(result); + } - public List GetOwners() - { - return new List - { - new Owner - { - Id = Guid.NewGuid(), - Name = "John Keen", - DateOfBirth = new DateTime(1980, 12, 05), - Address = "61 Wellfield Road" - } - }; - } - } + public List GetOwners() + { + return new List + { + new Owner + { + Id = Guid.NewGuid(), + Name = "John Keen", + DateOfBirth = new DateTime(1980, 12, 05), + Address = "61 Wellfield Road" + } + }; + } + } } diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 114ad4b..61e4405 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -1,17 +1,17 @@ - netcoreapp2.0 + netcoreapp3.1 false - - - - - + + + + + diff --git a/docker-compose.yml b/docker-compose.yml index a591245..6d95a75 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -16,11 +16,11 @@ services: accountownerapp: depends_on: - db - image: codemazeblog/accountownerapp + image: codemazeblog/accountowner:runtime build: context: . ports: - - "8080:80" + - "8080:5000" volumes: dbdata: \ No newline at end of file