fronted: login
This commit is contained in:
@@ -6,7 +6,7 @@ namespace back.context;
|
||||
public class EventContext : DbContext
|
||||
{
|
||||
private DbSet<EventModel> Events { get; set; }
|
||||
public EventContext(DbContextOptions<EventContext> options)
|
||||
public EventContext(DbContextOptions<EventContext> options) : base(options)
|
||||
{
|
||||
Database.EnsureCreated();
|
||||
}
|
||||
|
@@ -6,8 +6,9 @@ namespace back.context;
|
||||
public class PersonContext : DbContext
|
||||
{
|
||||
private DbSet<PersonModel> Persons { get; set; }
|
||||
public PersonContext(DbContextOptions<PersonContext> options)
|
||||
public PersonContext(DbContextOptions<PersonContext> options) : base(options)
|
||||
{
|
||||
// Ensure database is created
|
||||
Database.EnsureCreated();
|
||||
}
|
||||
|
||||
|
@@ -162,36 +162,71 @@ public class PhotoContext : DbContext
|
||||
|
||||
public async Task<PhotoModel?> GetById(Guid id)
|
||||
{
|
||||
return await Photos.FindAsync(id);
|
||||
try
|
||||
{
|
||||
return await Photos.FindAsync(id);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<int> GetTotalItems()
|
||||
{
|
||||
return await Photos.CountAsync();
|
||||
try
|
||||
{
|
||||
return await Photos.CountAsync();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<PhotoModel>> GetPage(int page = 1, int pageSize = 20)
|
||||
public async Task<IEnumerable<PhotoModel>?> GetPage(int page = 1, int pageSize = 20)
|
||||
{
|
||||
if (page < 1) page = 1;
|
||||
if (pageSize < 1) pageSize = 20;
|
||||
|
||||
return await Photos
|
||||
try
|
||||
{
|
||||
return await Photos
|
||||
.OrderByDescending(p => p.CreatedAt)
|
||||
.Skip((page - 1) * pageSize)
|
||||
.Take(pageSize)
|
||||
.ToListAsync();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> Exists(PhotoModel? photo)
|
||||
{
|
||||
if (photo == null) return false;
|
||||
if (string.IsNullOrEmpty(photo.Id)) return false;
|
||||
return await Photos.AnyAsync(p => p.Id == photo.Id);
|
||||
try
|
||||
{
|
||||
if (photo == null) return false;
|
||||
if (string.IsNullOrEmpty(photo.Id)) return false;
|
||||
return await Photos.AnyAsync(p => p.Id == photo.Id);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false; // Handle exceptions gracefully
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> Exists(string id)
|
||||
{
|
||||
return await Photos.AnyAsync(p => p.Id == id);
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrEmpty(id)) return false;
|
||||
return await Photos.AnyAsync(p => p.Id == id);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false; // Handle exceptions gracefully
|
||||
}
|
||||
}
|
||||
|
||||
public async Task Delete(PhotoModel photo)
|
||||
|
@@ -6,7 +6,7 @@ namespace back.context;
|
||||
public class TagContext : DbContext
|
||||
{
|
||||
private DbSet<TagModel> Tags { get; set; }
|
||||
public TagContext(DbContextOptions<TagContext> options)
|
||||
public TagContext(DbContextOptions<TagContext> options) : base(options)
|
||||
{
|
||||
Database.EnsureCreated();
|
||||
}
|
||||
|
@@ -1,17 +1,111 @@
|
||||
using back.DataModels;
|
||||
using back.services.Crypto;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Net;
|
||||
|
||||
namespace back.context;
|
||||
|
||||
public class UserContext : DbContext
|
||||
{
|
||||
public DbSet<UserModel> Users { get; set; }
|
||||
public UserContext(DbContextOptions<UserContext> options) : base(options)
|
||||
public record HttpErrorMap(HttpStatusCode Code, string Description);
|
||||
|
||||
public static class Errors
|
||||
{
|
||||
public static readonly HttpErrorMap Unauthorized =
|
||||
new(HttpStatusCode.Unauthorized, "Invalid user data. Email or password are wrong.");
|
||||
public static readonly HttpErrorMap BadRequest =
|
||||
new(HttpStatusCode.BadRequest, "Missing user data.");
|
||||
}
|
||||
|
||||
public DbSet<UserModel> Users { get; set; }
|
||||
private readonly ICryptoService _cryptoService;
|
||||
public UserContext(
|
||||
DbContextOptions<UserContext> options,
|
||||
ICryptoService cryptoService
|
||||
) : base(options)
|
||||
{
|
||||
_cryptoService = cryptoService ?? throw new ArgumentNullException(nameof(cryptoService));
|
||||
// Ensure database is created
|
||||
Database.EnsureCreated();
|
||||
}
|
||||
|
||||
public async Task<UserModel?> Create(string clientId, UserModel user)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(user);
|
||||
|
||||
if (await Exists(user))
|
||||
{
|
||||
return await GetById(Guid.Parse(user.Id)) ?? null;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(user.Id))
|
||||
{
|
||||
user.Id = Guid.NewGuid().ToString();
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(user.Salt))
|
||||
{
|
||||
user.Salt = _cryptoService.Salt();
|
||||
}
|
||||
user.Password = _cryptoService.Decrypt(clientId, user.Password) ?? string.Empty;
|
||||
user.Password = _cryptoService.Hash(user.Password + user.Salt + _cryptoService.Pepper()) ?? string.Empty;
|
||||
|
||||
user.CreatedAt = DateTime.UtcNow;
|
||||
Users.Add(user);
|
||||
await SaveChangesAsync();
|
||||
return user;
|
||||
}
|
||||
|
||||
public async Task<UserModel?> Update(UserModel user)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(user);
|
||||
if (!await Exists(user))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
var existingUser = await GetById(Guid.Parse(user.Id));
|
||||
if (existingUser == null) return null;
|
||||
existingUser.Name = user.Name;
|
||||
existingUser.Email = user.Email;
|
||||
existingUser.UpdatedAt = DateTime.UtcNow;
|
||||
Users.Update(existingUser);
|
||||
await SaveChangesAsync();
|
||||
return existingUser;
|
||||
}
|
||||
|
||||
public async Task<bool> Delete(Guid id)
|
||||
{
|
||||
var user = await GetById(id);
|
||||
if (user == null) return false;
|
||||
Users.Remove(user);
|
||||
await SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<UserModel?> GetByEmail(string email)
|
||||
{
|
||||
if (string.IsNullOrEmpty(email)) return null;
|
||||
return await Users.FirstOrDefaultAsync(u => u.Email == email);
|
||||
}
|
||||
|
||||
public async Task<string> GetUserSaltByEmail(string email)
|
||||
{
|
||||
if (string.IsNullOrEmpty(email)) return string.Empty;
|
||||
var user = await Users.FirstOrDefaultAsync(u => u.Email == email);
|
||||
return user?.Salt ?? string.Empty;
|
||||
}
|
||||
|
||||
public async Task<UserModel?> Login(string email, string password, string clientId)
|
||||
{
|
||||
if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password)) return null;
|
||||
|
||||
var pass = _cryptoService.Decrypt(clientId, password) + await GetUserSaltByEmail(email) + _cryptoService.Pepper();
|
||||
var hashedPassword = _cryptoService.Hash(pass);
|
||||
var user = await Users
|
||||
.FirstOrDefaultAsync(u => u.Email == email && u.Password == hashedPassword);
|
||||
return user;
|
||||
}
|
||||
|
||||
public async Task<UserModel?> GetById(Guid id)
|
||||
{
|
||||
return await Users.FindAsync(id);
|
||||
|
Reference in New Issue
Block a user