123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using UnivateProperties_API.Context;
- using UnivateProperties_API.Helpers;
- using UnivateProperties_API.Model.Users;
-
- namespace UnivateProperties_API.Repository.Users
- {
- public class UserRepository : IRepository<User>
- {
- private readonly DataContext _dbContext;
-
- public UserRepository(DataContext dbContext)
- {
- _dbContext = dbContext;
- }
-
- public List<User> Get(Func<User, bool> where)
- {
- return _dbContext.Users.Where(where).ToList();
- }
-
- public List<User> GetAll()
- {
- return _dbContext.Users.ToList();
- }
-
- public User GetDetailed(Func<User, bool> 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)
- {
- item.Id = NewId();
- _dbContext.Add(item);
- Save();
- }
-
- public void Insert(IEnumerable<User> item)
- {
- int id = NewId();
- foreach (var i in item)
- {
- i.Id = id;
- _dbContext.Add(i);
- id += 1;
- }
- Save();
- }
-
- public void Remove(User item)
- {
- var i = _dbContext.Users.Find(item);
- _dbContext.Users.Remove(i);
- Save();
- }
-
- public void Remove(IEnumerable<User> 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 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()))
- {
- CreatePasswordHash(userParam.PasswordHash.ToString(), out byte[] passwordHash, out byte[] 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<User> GetDetailedAll()
- {
- throw new NotImplementedException();
- }
-
- public int NewId()
- {
- int id = 0;
- if (_dbContext.Users.Count() > 0)
- {
- id = _dbContext.Users.Max(x => x.Id);
- }
- id += 1;
- return id;
- }
- }
- }
|