Skip to content

Commit

Permalink
[#8] added User, UserController and othe related functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavlo Kyrylenko committed Nov 20, 2020
1 parent cecbf3f commit 4bcf9ad
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 6 deletions.
10 changes: 10 additions & 0 deletions SafeCity.Core/Entities/Role.cs
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
}
}
18 changes: 18 additions & 0 deletions SafeCity.Core/Entities/User.cs
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; }
}
}
12 changes: 12 additions & 0 deletions SafeCity.Core/Repositories/IUserRepository.cs
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();
}
}
46 changes: 46 additions & 0 deletions SafeCity.Core/Repositories/UserRepository.cs
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;
}
}
}
18 changes: 13 additions & 5 deletions SafeCity.Core/SafeCityContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class SafeCityContext : DbContext
{
public DbSet<Project> Projects { get; set; }
public DbSet<Donation> Donations { get; set; }
public DbSet<User> Users { get; set; }

public SafeCityContext(DbContextOptions<SafeCityContext> options) : base(options)
{
Expand All @@ -16,11 +17,9 @@ public SafeCityContext(DbContextOptions<SafeCityContext> options) : base(options

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Project>()
.HasData(Seed.Projects);

modelBuilder.Entity<Donation>()
.HasData(Seed.Donations);
modelBuilder.Entity<User>()
.HasIndex(u => u.Email)
.IsUnique();

modelBuilder.Entity<Project>()
.Property(e => e.Images)
Expand All @@ -34,6 +33,15 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
v => string.Join(',', v),
v => v.Split(',', StringSplitOptions.RemoveEmptyEntries));

modelBuilder.Entity<Project>()
.HasData(Seed.Projects);

modelBuilder.Entity<Donation>()
.HasData(Seed.Donations);

modelBuilder.Entity<User>()
.HasData(Seed.Users);

base.OnModelCreating(modelBuilder);
}
}
Expand Down
13 changes: 13 additions & 0 deletions SafeCity.Core/Seed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
};
}
}
41 changes: 41 additions & 0 deletions SafeCity/Controllers/UsersController.cs
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);
}
}
}
3 changes: 2 additions & 1 deletion SafeCity/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public void ConfigureServices(IServiceCollection services)

services.AddScoped<IProjectRepository, ProjectRepository>();
services.AddScoped<IDonationRepository, DonationRepository>();

services.AddScoped<IUserRepository, UserRepository>();

services.AddScoped<ILiqPayService>(x =>
new LiqPayService(Configuration["LiqPay:PublicKey"], Configuration["LiqPay:PrivateKey"]));

Expand Down

0 comments on commit 4bcf9ad

Please sign in to comment.