-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wireframe of hierarchy of services and classes
and how to use them in DI
- Loading branch information
Showing
13 changed files
with
229 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Mvc; | ||
using N2N.Api.Services; | ||
using N2N.Core.Entities; | ||
using N2N.Infrastructure.Models; | ||
using N2N.Services.Users; | ||
|
||
namespace N2N.Api.Controllers | ||
{ | ||
[Produces("application/json")] | ||
[Route("User")] | ||
public class UserController : Controller | ||
{ | ||
private N2NApiUserService _apiUserService; | ||
|
||
public UserController(N2NApiUserService apiUserService) | ||
{ | ||
this._apiUserService = apiUserService; | ||
} | ||
|
||
// GET: api/User | ||
[HttpGet] | ||
public async Task<IActionResult> Get() | ||
{ | ||
var result = await this._apiUserService.CreateUserAsync(new N2NUser() | ||
{ | ||
NickName = "Test", | ||
Email = "[email protected]" | ||
}, | ||
"Password" | ||
); | ||
|
||
if (!result.Success) | ||
{ | ||
return BadRequest( "Baaad"); | ||
} | ||
|
||
return Ok(); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Identity; | ||
using N2N.Core.Entities; | ||
using N2N.Infrastructure.Models; | ||
using N2N.Services.Users; | ||
|
||
namespace N2N.Api.Services | ||
{ | ||
public class N2NApiUserService | ||
{ | ||
private IN2NUserService _userservice; | ||
private UserManager<N2NIdentityUser> _userManager; | ||
|
||
public N2NApiUserService(IN2NUserService userService, UserManager<N2NIdentityUser> userManager) | ||
{ | ||
this._userservice = userService; | ||
this._userManager = userManager; | ||
} | ||
|
||
public async Task<OperationResult> CreateUserAsync(N2NUser user, string password) | ||
{ | ||
// this all should be a transaction ↓ | ||
|
||
var result = this._userservice.CreateUser(user); | ||
var identityResult = await this._userManager.CreateAsync(new N2NIdentityUser() | ||
{ | ||
N2NUserId = user.Id | ||
}, password); | ||
|
||
//if operation fails, we should delete N2NUser | ||
|
||
return result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using N2N.Core.Entities; | ||
using N2N.Infrastructure.Models; | ||
|
||
namespace N2N.Services.Users | ||
{ | ||
public interface IN2NUserService | ||
{ | ||
OperationResult CreateUser(N2NUser user); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Security.Principal; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace N2N.Services | ||
{ | ||
public interface ISecurityService | ||
{ | ||
bool HasAccess(); | ||
bool HasAccess(IIdentity identity); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using N2N.Core.Entities; | ||
using N2N.Data.Repositories; | ||
using N2N.Infrastructure.Models; | ||
using N2N.Services.Users; | ||
|
||
namespace N2N.Services | ||
{ | ||
public class N2NUserService : IN2NUserService | ||
{ | ||
private IRepository<N2NUser> _userRepo; | ||
private ISecurityService _security; | ||
|
||
|
||
public N2NUserService(IRepository<N2NUser> userRepo, ISecurityService security) | ||
{ | ||
this._userRepo = userRepo; | ||
this._security = security; | ||
} | ||
|
||
public OperationResult CreateUser(N2NUser user) | ||
{ | ||
var result = new OperationResult() | ||
{ | ||
Success = false | ||
}; | ||
|
||
// add verification of user fields | ||
|
||
if (this._security.HasAccess()) | ||
{ | ||
//add user here | ||
_userRepo.Add(user); | ||
|
||
result.Data = user; | ||
result.Success = true; | ||
result.Messages = new[] { $"User was created with Id = ${user.Id}" }; | ||
} | ||
else | ||
{ | ||
result.Messages = new []{ "You dont have permissions to create user" }; | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Security.Principal; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Threading; | ||
|
||
namespace N2N.Services | ||
{ | ||
public class SecurityService : ISecurityService | ||
{ | ||
public bool HasAccess() | ||
{ | ||
var principal = System.Threading.Thread.CurrentPrincipal; | ||
|
||
return true; | ||
} | ||
|
||
public bool HasAccess(IIdentity identity) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |