using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Options; using Microsoft.IdentityModel.Tokens; using System; using System.Collections.Generic; using System.IdentityModel.Tokens.Jwt; using System.Linq; using System.Security.Claims; using System.Text; using UnivateProperties_API.Context; using UnivateProperties_API.Helpers; using UnivateProperties_API.Model.Users; namespace UnivateProperties_API.Repository.Users { public class UserRepository : IRepository { private readonly DataContext _dbContext; public UserRepository(DataContext dbContext) { _dbContext = dbContext; } public List Get(Func where) { return _dbContext.Users.Where(where).ToList(); } public List GetAll() { return _dbContext.Users.ToList(); } public User GetDetailed(Func first) { var item = _dbContext.Users.FirstOrDefault(first); UserRepository account = new UserRepository(_dbContext); item = GetDetailedObject(item, account); return item; } private User GetDetailedObject(User item, UserRepository repo) { item = repo.GetDetailed(x => x.Id == item.Id); return item; } public void Insert(User item) { _dbContext.Add(item); Save(); } public void Insert(IEnumerable item) { _dbContext.Add(item); Save(); } public void Remove(User item) { var i = _dbContext.Users.Find(item); _dbContext.Users.Remove(i); Save(); } public void Remove(IEnumerable items) { foreach (var item in items) { User i = _dbContext.Users.Find(item); _dbContext.Users.Remove(i); } Save(); } public void RemoveAtId(int item) { var i = _dbContext.Users.Find(item); _dbContext.Users.Remove(i); Save(); } //public void Update(User item) //{ // _dbContext.Entry(item).State = EntityState.Modified; // Save(); //} public void Update(User userParam) { var user = _dbContext.Users.Find(userParam.Id); if (user == null) throw new AppException("User not found"); if (userParam.Username != user.Username) { // username has changed so check if the new username is already taken if (_dbContext.Users.Any(x => x.Username == userParam.Username)) throw new AppException("Username " + userParam.Username + " is already taken"); } // update user properties user.Username = userParam.Username; // update password if it was entered if (!string.IsNullOrWhiteSpace(userParam.PasswordHash.ToString())) { byte[] passwordHash, passwordSalt; CreatePasswordHash(userParam.PasswordHash.ToString(), out passwordHash, out passwordSalt); user.PasswordHash = passwordHash; user.PasswordSalt = passwordSalt; } _dbContext.Users.Update(user); _dbContext.SaveChanges(); } private static void CreatePasswordHash(string password, out byte[] passwordHash, out byte[] passwordSalt) { if (password == null) throw new ArgumentNullException("password"); if (string.IsNullOrWhiteSpace(password)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "password"); using (var hmac = new System.Security.Cryptography.HMACSHA512()) { passwordSalt = hmac.Key; passwordHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password)); } } public void Save() { _dbContext.SaveChanges(); } public List GetDetailedAll() { throw new NotImplementedException(); } } }