-
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.
[#8] added User, UserController and othe related functionality
- Loading branch information
Pavlo Kyrylenko
committed
Nov 20, 2020
1 parent
cecbf3f
commit 4bcf9ad
Showing
8 changed files
with
155 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace SafeCity.Core.Entities | ||
{ | ||
public enum Role | ||
{ | ||
Guest = 0, | ||
UnAuthorized = 1, | ||
Authorized = 2, | ||
Admin = 3 | ||
} | ||
} |
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,18 @@ | ||
using System; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace SafeCity.Core.Entities | ||
{ | ||
public class User | ||
{ | ||
[Key] | ||
public Guid Id { get; set; } | ||
[Required] | ||
[EmailAddress] | ||
public string Email { get; set; } | ||
public string GivenName { get; set; } | ||
public string FamilyName { get; set; } | ||
public string Picture { get; set; } | ||
public Role Role { get; set; } | ||
} | ||
} |
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,12 @@ | ||
using System.Threading.Tasks; | ||
using SafeCity.Core.Entities; | ||
|
||
namespace SafeCity.Core.Repositories | ||
{ | ||
public interface IUserRepository | ||
{ | ||
Task<User> GetUserByEmailAsync(string email); | ||
Task<User> CreateUserAsync(string email, string givenName, string familyName, string picture, Role role = Role.UnAuthorized); | ||
Task<bool> SaveAsync(); | ||
} | ||
} |
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,46 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.EntityFrameworkCore; | ||
using SafeCity.Core.Entities; | ||
|
||
namespace SafeCity.Core.Repositories | ||
{ | ||
public class UserRepository: IUserRepository | ||
{ | ||
private readonly SafeCityContext _context; | ||
|
||
public UserRepository(SafeCityContext context) | ||
{ | ||
_context = context ?? throw new ArgumentException(nameof(context)); | ||
} | ||
public async Task<User> GetUserByEmailAsync(string email) | ||
{ | ||
return await _context.Users | ||
//.AsNoTracking() | ||
.FirstOrDefaultAsync(x => x.Email == email); | ||
} | ||
|
||
public async Task<User> CreateUserAsync(string email, string givenName, string familyName, string picture, | ||
Role role = Role.UnAuthorized) | ||
{ | ||
var user = new User() | ||
{ | ||
Id = Guid.NewGuid(), | ||
Email = email, | ||
GivenName = givenName, | ||
FamilyName = familyName, | ||
Picture = picture, | ||
Role = role | ||
}; | ||
var result = await _context.Users.AddAsync(user); | ||
|
||
return result.Entity; | ||
} | ||
|
||
public async Task<bool> SaveAsync() | ||
{ | ||
var result = await _context.SaveChangesAsync(); | ||
return result >= 0; | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -104,5 +104,18 @@ class Seed | |
Ip = "92.253.252.0" | ||
} | ||
}; | ||
|
||
public static User[] Users { get; } = | ||
{ | ||
new User() | ||
{ | ||
Id = Guid.NewGuid(), | ||
Email = "[email protected]", | ||
FamilyName = "Pavlo", | ||
GivenName = "Kyrylenko", | ||
Picture = "https://lh3.googleusercontent.com/a-/AOh14GhhOenSg9NkV2y8V170GpLKP-8Hzn5wncPxGqvkkg=s96-c", | ||
Role = Role.Admin | ||
} | ||
}; | ||
} | ||
} |
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,41 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IdentityModel.Tokens.Jwt; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using SafeCity.Core.Entities; | ||
using SafeCity.Core.Repositories; | ||
using SafeCity.Services; | ||
|
||
namespace SafeCity.Controllers | ||
{ | ||
[Route("api/v1/auth")] | ||
[ApiController] | ||
public class UsersController : ControllerBase | ||
{ | ||
private readonly IUserRepository _userRepository; | ||
private readonly IHttpContextAccessor _httpContextAccessor; | ||
public UsersController(IUserRepository userRepository, | ||
IHttpContextAccessor httpContextAccessor) | ||
{ | ||
_userRepository = userRepository; | ||
_httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor)); | ||
} | ||
|
||
[HttpGet] | ||
public async Task<IActionResult> Me() | ||
{ | ||
var email = _httpContextAccessor.HttpContext.User.FindFirst(JwtRegisteredClaimNames.Email).Value; | ||
var givenName = _httpContextAccessor.HttpContext.User.FindFirst(JwtRegisteredClaimNames.GivenName).Value; | ||
var familyName = _httpContextAccessor.HttpContext.User.FindFirst(JwtRegisteredClaimNames.FamilyName).Value; | ||
var picture = _httpContextAccessor.HttpContext.User.FindFirst("picture").Value; | ||
|
||
var user = await _userRepository.GetUserByEmailAsync(email) | ||
?? await _userRepository.CreateUserAsync(email, givenName, familyName, picture); | ||
|
||
return Ok(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