Skip to content
This repository has been archived by the owner on Nov 29, 2022. It is now read-only.

Commit

Permalink
[DDD]: registering services; fixing logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jackinf committed Nov 18, 2017
1 parent 25aee08 commit 586e9ce
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 43 deletions.
14 changes: 14 additions & 0 deletions Idontknow.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Idontknow.Domain", "backend
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Idontknow.Service", "backend\Idontknow.Service\Idontknow.Service.csproj", "{A8A03E09-B6DB-4ADF-A27A-20D6676D5EAA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Idontknow.IntegrationTests", "backend\Idontknow.IntegrationTests\Idontknow.IntegrationTests.csproj", "{14A6D965-3346-4C05-8F3A-671CC482E5AD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -83,6 +85,18 @@ Global
{A8A03E09-B6DB-4ADF-A27A-20D6676D5EAA}.Release|x64.Build.0 = Release|x64
{A8A03E09-B6DB-4ADF-A27A-20D6676D5EAA}.Release|x86.ActiveCfg = Release|x86
{A8A03E09-B6DB-4ADF-A27A-20D6676D5EAA}.Release|x86.Build.0 = Release|x86
{14A6D965-3346-4C05-8F3A-671CC482E5AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{14A6D965-3346-4C05-8F3A-671CC482E5AD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{14A6D965-3346-4C05-8F3A-671CC482E5AD}.Debug|x64.ActiveCfg = Debug|x64
{14A6D965-3346-4C05-8F3A-671CC482E5AD}.Debug|x64.Build.0 = Debug|x64
{14A6D965-3346-4C05-8F3A-671CC482E5AD}.Debug|x86.ActiveCfg = Debug|x86
{14A6D965-3346-4C05-8F3A-671CC482E5AD}.Debug|x86.Build.0 = Debug|x86
{14A6D965-3346-4C05-8F3A-671CC482E5AD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{14A6D965-3346-4C05-8F3A-671CC482E5AD}.Release|Any CPU.Build.0 = Release|Any CPU
{14A6D965-3346-4C05-8F3A-671CC482E5AD}.Release|x64.ActiveCfg = Release|x64
{14A6D965-3346-4C05-8F3A-671CC482E5AD}.Release|x64.Build.0 = Release|x64
{14A6D965-3346-4C05-8F3A-671CC482E5AD}.Release|x86.ActiveCfg = Release|x86
{14A6D965-3346-4C05-8F3A-671CC482E5AD}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
7 changes: 5 additions & 2 deletions backend/Idontknow.Api/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@
namespace Idontknow.Api.Controllers
{
[Authorize]
[Route("Account")]
public class AccountController : BaseController
{
private readonly UserManager<ApplicationUser> _userManager;

public AccountController(ILogger logger, UserManager<ApplicationUser> userManager): base(logger)
public AccountController(
ILogger<AccountController> logger,
UserManager<ApplicationUser> userManager): base(logger)
{
_userManager = userManager;
}

//
// POST: /Account/Register
[HttpPost]
[HttpPost("Register")]
[AllowAnonymous]
public async Task<IActionResult> Register([FromBody] RegisterViewModel model)
{
Expand Down
4 changes: 3 additions & 1 deletion backend/Idontknow.Api/Controllers/BlogController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public class BlogController : BaseController
{
private readonly IBloggingService _service;

public BlogController(IBloggingService service, ILogger logger) : base(logger)
public BlogController(
ILogger<BlogController> logger,
IBloggingService service) : base(logger)
{
_service = service;
}
Expand Down
25 changes: 24 additions & 1 deletion backend/Idontknow.Api/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
using System;
using AspNet.Security.OpenIdConnect.Primitives;
using Idontknow.DAL;
using Idontknow.DAL.Repository;
using Idontknow.DAL.UnitOfWork;
using Idontknow.Domain.Repository;
using Idontknow.Domain.Service;
using Idontknow.Domain.UnitOfWork;
using Idontknow.Service;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
Expand Down Expand Up @@ -123,6 +129,13 @@ public void ConfigureServices(IServiceCollection services)
// options.ClientSecret = "875sqd4s5d748z78z7ds1ff8zz8814ff88ed8ea4z4zzd";
// options.RequireHttpsMetadata = false;
// });

services.AddTransient<IBloggingService, BloggingService>();

services.AddTransient<IBlogRepository, BlogRepository>();
services.AddTransient<IPostRepository, PostRepository>();

services.AddTransient<IBloggingUnitOfWork, BloggingUnitOfWork>();

// Register the Swagger generator, defining one or more Swagger documents
services.AddSwaggerGen(c =>
Expand All @@ -143,7 +156,8 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, RoleMana

app.UseAuthentication();

app.UseMvc();
// TODO: Don't allow everything in production
app.UseCors(b => b.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().AllowCredentials());

app.UseMvcWithDefaultRoute();

Expand All @@ -158,6 +172,15 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, RoleMana
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});

//Create Database
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
var dbContext = serviceScope.ServiceProvider.GetService<ApplicationDbContext>();

// run Migrations
dbContext.Database.Migrate();
}

InitializeRoles(roleManager);
}

Expand Down
4 changes: 2 additions & 2 deletions backend/Idontknow.Domain/Factory/ServiceResultFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public static ServiceResult<TPayload> Success<TPayload>(TPayload payload, string
return result;
}

public static ServiceResult<List<TPayload>> SuccessWithPaginator<TPayload>(PaginatedListResult<TPayload> paginatedListResult, string message = "")
public static ListServiceResult<List<TPayload>> SuccessWithPaginator<TPayload>(PaginatedListResult<TPayload> paginatedListResult, string message = "")
{
var result = new ServiceResult<List<TPayload>>();
var result = new ListServiceResult<List<TPayload>>();
result.IsSuccessful = true;
result.Payload = paginatedListResult.ContextObjects;
result.Pages = paginatedListResult.Pages;
Expand Down
34 changes: 0 additions & 34 deletions backend/Idontknow.Domain/ViewModels/Result/ServiceBaseResult.cs

This file was deleted.

35 changes: 32 additions & 3 deletions backend/Idontknow.Domain/ViewModels/Result/ServiceResult.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
namespace Idontknow.Domain.ViewModels.Result
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using FluentValidation.Results;

namespace Idontknow.Domain.ViewModels.Result
{
public class ServiceResult<TPayload> : ServiceBaseResult<TPayload>
public class ServiceResult<TPayload>
{
public bool IsSuccessful { get; set; }

public ValidationResult Validation { get; set; }

public TPayload Payload { get; set; }

public string Message { get; set; }

public HttpStatusCode StatusCode { get; set; }

public void AddErrors(ValidationResult validation)
{
if (Validation == null)
Validation = new ValidationResult();

foreach (var error in validation.Errors)
{
IsSuccessful = false;
Validation.Errors.Add(error);
}
}
}

public class ServiceResult : ServiceBaseResult<object>
public class ListServiceResult<TPayload> : ServiceResult<TPayload>
{
public int Pages { get; set; } = 1;
public int TotalItems { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using Idontknow.Api;
using Idontknow.Domain.ViewModels.Service.Account;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Xunit;

namespace Idontknow.IntegrationTests.Feature.Authentification
{
public class when_user_is_registered
{
private TestServer _server;
private HttpClient _client;

public when_user_is_registered()
{
_server = new TestServer(new WebHostBuilder()
.ConfigureLogging(options => options.AddConsole())
.UseStartup<Startup>());
_client = _server.CreateClient();

// Register
var requestDataOne = new { Email = "test1@test.com", Password = "123456aA!" };
var contentOne = new StringContent(JsonConvert.SerializeObject(requestDataOne), Encoding.UTF8, "application/json");
var resultOne = _client.PostAsync("/Account/Register", contentOne).Result;
resultOne.EnsureSuccessStatusCode();

// Login
var requestDataTwo = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", "test@test.com"),
new KeyValuePair<string, string>("password", "123456aA!")
};
var contentTwo = new FormUrlEncodedContent(requestDataTwo);
var resultTwo = _client.PostAsync("/connect/token", contentTwo).Result;
resultTwo.EnsureSuccessStatusCode();
var responseContent = resultTwo.Content.ReadAsStringAsync().Result;
var token = requestDataOne;

_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", $"Bearer {token}");
}

[Fact]
public void user_should_be_able_to_access_restricted_paths()
{
var resultThree = _client.GetAsync("/api/blogs").Result;
resultThree.EnsureSuccessStatusCode();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Net.Http;
using Microsoft.AspNetCore.TestHost;
using Xunit;

namespace Idontknow.IntegrationTests.Feature.Blogging
{
public class when_blog_is_added
{
private TestServer _server;
private HttpClient _client;

public when_blog_is_added()
{
// TODO: post
}

[Fact]
public void then_blog_should_be_in_database()
{
// TODO: get
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170628-02" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Idontknow.Api\Idontknow.Api.csproj" />
</ItemGroup>
</Project>

0 comments on commit 586e9ce

Please sign in to comment.