using OFBButte.Application.Database; using OFBButte.Application.Exceptions; using OFBButte.Entities; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace OFBButte.Application.Users { public class AddUser { private readonly IOFBContext context; private readonly IPasswordHasher hasher; public AddUser(IOFBContext context, IPasswordHasher hasher) { this.context = context; this.hasher = hasher; } public User Handle(string email, string password) { // Check to see if email already exists var existingUser = context.Users.FirstOrDefault(u => u.Email == email); if (existingUser != null) throw new ValidationException("User with email already exists"); // Create the new user var user = new Entities.User() { Email = email, CreatedDate = DateTime.Now, Password = hasher.HashPassword(password) }; // Save to db context.Users.Add(user); context.SaveChanges(); return user; } } }