123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- using Microsoft.AspNetCore.Authorization;
- using System.Collections.Generic;
- using System.Linq;
- using UnivateProperties_API.Containers.Users;
- using UnivateProperties_API.Containers.Users.Simple;
- using UnivateProperties_API.Context;
- using UnivateProperties_API.Helpers;
- using UnivateProperties_API.Model.Communication;
- using UnivateProperties_API.Model.Users;
- using UnivateProperties_API.Repository.Communication;
-
- namespace UnivateProperties_API.Repository.Users
- {
- public class RegisterRepository : IRegisterRepository
- {
- private readonly DataContext _dbContext;
-
- public RegisterRepository(DataContext dbContext)
- {
- _dbContext = dbContext;
- }
-
- public User Authenticate(string username, string password)
- {
- if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
- return null;
-
- var user = _dbContext.Users.SingleOrDefault(x => x.Username == username);
-
- // check if username exists
- if (user == null)
- throw new AppException("Username is incorrect");
-
- // check if password is correct
- if (!MyCommon.VerifyPasswordHash(password, user.PasswordHash, user.PasswordSalt))
- throw new AppException("Password is incorrect");
-
- // authentication successful
- return user;
- }
-
- public User Create(User user, string password, bool save)
- {
- // validation
- if (string.IsNullOrWhiteSpace(password))
- throw new AppException("Password is required");
-
- if (_dbContext.Users.Any(x => x.Username == user.Username))
- throw new AppException("Username \"" + user.Username + "\" is already taken");
-
- MyCommon.CreatePasswordHash(password, out byte[] passwordHash, out byte[] passwordSalt);
-
- user.PasswordHash = passwordHash;
- user.PasswordSalt = passwordSalt;
- user.Id = NewUserId();
- _dbContext.Users.Add(user);
- if (save)
- {
- _dbContext.SaveChanges();
- }
-
- return user;
- }
-
- public Agency CreateAgency(AgencyDto agency)
- {
- // validation
- if (string.IsNullOrWhiteSpace(agency.EaabeffcNumber))
- throw new AppException("eaabeffcNumber is required");
-
- if (_dbContext.Agencies.Any(x => x.EAABEFFCNumber == agency.EaabeffcNumber))
- throw new AppException("eaabeffcNumber \"" + agency.EaabeffcNumber + "\" already exists");
-
- Agency a = new Agency()
- {
- AgencyName = agency.Name,
- EAABEFFCNumber = agency.EaabeffcNumber,
- CompanyRegNumber = agency.RegNo
- };
- a.Id = NewAgencyId();
- _dbContext.Agencies.Add(a);
- CreatePerson(agency.User, PersonType.Agent, false, a);
-
- _dbContext.SaveChanges();
-
- return a;
- }
-
- public void CreatePerson(UserDto individual, PersonType personType, bool save, Agency agency)
- {
- // validation
- if (string.IsNullOrWhiteSpace(individual.Password))
- throw new AppException("Password is required");
-
- if (_dbContext.Users.Any(x => x.Username == individual.Username))
- throw new AppException("Individual \"" + individual.Username + "\" is already taken");
-
- MyCommon.CreatePasswordHash(individual.Password, out byte[] passwordHash, out byte[] passwordSalt);
-
- User createUser = new User(individual.Username, individual.Password);
-
- Create(createUser, individual.Password, false);
-
- Person p = null;
-
- if (personType == PersonType.Agent)
- {
- Agent agent = new Agent()
- {
- Name = individual.Name,
- Surname = individual.Surname,
- User = createUser,
- Email = individual.Email,
- CellNumber = individual.CellNumber,
- Telephone = individual.Telephone,
- Agency = agency
- };
- agent.Id = NewAgentId();
- agent.User.Role = Role.Agency;
- p = agent;
- _dbContext.Agents.Add(agent);
- }
- else if (personType == PersonType.Individual)
- {
- Individual i = new Individual()
- {
- Name = individual.Name,
- Surname = individual.Surname,
- User = createUser,
- Email = individual.Email,
- CellNumber = individual.CellNumber,
- Telephone = individual.Telephone
- };
- i.Id = NewIndividualId();
- i.User.Role = Role.PrivateUser;
- p = i;
- _dbContext.Individuals.Add(i);
-
- }
- Template template = _dbContext.Templates.FirstOrDefault(x => x.Name == "IndivRegEmail");
- if (template != null && personType == PersonType.Individual)
- {
- TemplateRepository templateRepository = new TemplateRepository(_dbContext);
- templateRepository.SendEmailTemplate(template, p, new List<Model.BaseEntity>() { p });
- }
-
- Template templ = _dbContext.Templates.FirstOrDefault(x => x.Name == "AgencyRegEmail");
- if (templ != null)
- {
- TemplateRepository templateRepository = new TemplateRepository(_dbContext);
- templateRepository.SendEmailTemplate(templ, p, new List<Model.BaseEntity>() { p });
- }
-
- Template temp = _dbContext.Templates.FirstOrDefault(x => x.Name == "VerificationEmail");
- if (temp != null)
- {
- TemplateRepository templateRepository = new TemplateRepository(_dbContext);
- templateRepository.SendEmailTemplate(temp, p, new List<Model.BaseEntity>() { p });
- }
-
- if (save)
- {
- Save();
- }
- }
-
- public void Update(User userParam, string password = null)
- {
- 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(password))
- {
- MyCommon.CreatePasswordHash(password, out byte[] passwordHash, out byte[] passwordSalt);
-
- user.PasswordHash = passwordHash;
- user.PasswordSalt = passwordSalt;
- }
-
- _dbContext.Users.Update(user);
- _dbContext.SaveChanges();
- }
-
- [Authorize(Roles = Role.SuperAdmin)]
- public IEnumerable<User> GetAllUsers()
- {
- return _dbContext.Users;
- }
-
- [Authorize(Roles = Role.SuperAdmin)]
- public IEnumerable<Agency> GetAllAgencies()
- {
- return _dbContext.Agencies;
- }
-
- [Authorize(Roles = Role.SuperAdmin)]
- public IEnumerable<Individual> GetAllIndividuals()
- {
- return _dbContext.Individuals;
- }
-
- public User GetById(int id)
- {
- return _dbContext.Users.Find(id);
- }
-
- public Agency GetByAgencyId(int id)
- {
- return _dbContext.Agencies.Find(id);
- }
-
- public Individual GetByIndividualId(int id)
- {
- return _dbContext.Individuals.Find(id);
- }
-
- public void Delete(int id)
- {
- var user = _dbContext.Users.Find(id);
- if (user != null)
- {
- _dbContext.Users.Remove(user);
- _dbContext.SaveChanges();
- }
- }
-
- public void DeleteAgency(int id)
- {
- var agency = _dbContext.Agencies.Find(id);
- if (agency != null)
- {
- _dbContext.Remove(agency);
- _dbContext.SaveChanges();
- }
- }
-
- public void DeleteIndividual(int id)
- {
- var individual = _dbContext.Individuals.Find(id);
- if (individual != null)
- {
- _dbContext.Individuals.Remove(individual);
- _dbContext.SaveChanges();
- }
- }
-
- private void Save()
- {
- _dbContext.SaveChanges();
- }
-
- public int NewAgencyId()
- {
- int id = 0;
- if (_dbContext.Agencies.Count() > 0)
- {
- id = _dbContext.Agencies.Max(x => x.Id);
- }
- id += 1;
- return id;
- }
-
- public int NewAgentId()
- {
- int id = 0;
- if (_dbContext.Agents.Count() > 0)
- {
- id = _dbContext.Agents.Max(x => x.Id);
- }
- id += 1;
- return id;
- }
-
- public int NewIndividualId()
- {
- int id = 0;
- if (_dbContext.Individuals.Count() > 0)
- {
- id = _dbContext.Individuals.Max(x => x.Id);
- }
- id += 1;
- return id;
- }
-
- public int NewUserId()
- {
- int id = 0;
- if (_dbContext.Users.Count() > 0)
- {
- id = _dbContext.Users.Max(x => x.Id);
- }
- id += 1;
- return id;
- }
-
- public SimplePersonDto UserDetails(int userId)
- {
- var individual = _dbContext.Individuals.Where(i => i.UserId == userId).FirstOrDefault();
-
- if (individual == null)
- {
- var agent = _dbContext.Agents.Where(i => i.UserId == userId).FirstOrDefault();
- if (agent != null)
- {
- return new SimplePersonDto()
- {
- Name = agent.Name,
- Surname = agent.Surname,
- Email = agent.Email
- };
- }
- else
- {
- return new SimplePersonDto(); ;
- }
- }
- else
- {
- return new SimplePersonDto()
- {
- Name = individual.Name,
- Surname = individual.Surname,
- Email = individual.Email
- };
- }
- }
- }
- }
|