123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- using Microsoft.EntityFrameworkCore;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using UnivateProperties_API.Containers.Users;
- using UnivateProperties_API.Context;
- using UnivateProperties_API.Model.Users;
-
- namespace UnivateProperties_API.Repository.Users
- {
- public class AgentRepository : IRepository<Agent>
- {
- private readonly DataContext _dbContext;
-
- public AgentRepository(DataContext dbContext)
- {
- _dbContext = dbContext;
- }
-
- public List<Agent> Get(Func<Agent, bool> where)
- {
- return _dbContext.Agents.Where(where).ToList();
- }
-
- public List<Agent> GetAll()
- {
- return _dbContext.Agents.Include("User").Where(x => x.IsDeleted == false).ToList();
- }
-
- public Agent GetDetailed(Func<Agent, bool> first)
- {
-
- var item = _dbContext.Agents.FirstOrDefault(first);
- //AgentRepository account = new AgentRepository(_dbContext);
- //item = GetDetailedObject(item, account);
- return item;
- }
-
- public void Insert(Agent item)
- {
- //item.Id = NewId();
- //item.User.Role = Role.Agent;
- /*if (_dbContext.Agents.Any(a => a.AgencyId == null))
- {
- item.AgencyId = 10;
- }*/
- var agent = _dbContext.Agents.Where(x => x.UserId == item.User.Id).FirstOrDefault();
- var individual = _dbContext.Individuals.Where(x => x.UserId == item.User.Id).FirstOrDefault();
- var user = _dbContext.Users.Where(x => x.Id == item.User.Id).FirstOrDefault();
-
- if (item.User.Role == "Agent" || item.User.Role == "Agency" || item.User.Role == "Managing Agent")
- {
- if (agent == null)
- {
- if (individual != null)
- {
- individual.IsDeleted = true;
- _dbContext.Individuals.Update(individual);
- }
- item.Id = 0;
- _dbContext.Agents.Add(item);
- if (item.User.Role != user.Role)
- {
- user.Role = item.User.Role;
- }
- _dbContext.Users.Update(user);
-
- }
- else
- {
- if (agent.Name != item.Name)
- {
- agent.Name = item.Name;
- }
- if (agent.Surname != item.Surname)
- {
- agent.Surname = item.Surname;
- }
- if (agent.Email != item.Email)
- {
- agent.Email = item.Email;
- }
- if (agent.Telephone != item.Telephone)
- {
- agent.Telephone = item.Telephone;
- }
- if (agent.CellNumber != item.CellNumber)
- {
- agent.CellNumber = item.CellNumber;
- }
- if (agent.AgencyId != item.AgencyId)
- {
- agent.AgencyId = item.AgencyId;
- }
- agent.IsDeleted = false;
- if (individual != null)
- {
- individual.IsDeleted = true;
- _dbContext.Individuals.Update(individual);
- }
- _dbContext.Agents.Update(agent);
- if (item.User.Role != user.Role)
- {
- user.Role = item.User.Role;
- }
- _dbContext.Users.Update(user);
- }
- }
- else
- {
- agent.IsDeleted = true;
- individual.IsDeleted = false;
- user.Role = item.User.Role;
-
- if (individual.Name != item.Name)
- {
- individual.Name = item.Name;
- }
- if (individual.Surname != item.Surname)
- {
- individual.Surname = item.Surname;
- }
- if (individual.Email != item.Email)
- {
- individual.Email = item.Email;
- }
- if (individual.Telephone != item.Telephone)
- {
- individual.Telephone = item.Telephone;
- }
- if (individual.CellNumber != item.CellNumber)
- {
- individual.CellNumber = item.CellNumber;
- }
-
- _dbContext.Agents.Update(agent);
- _dbContext.Users.Update(user);
- _dbContext.Individuals.Update(individual);
-
- }
-
-
- Save();
- }
-
- public void Insert(IEnumerable<Agent> item)
- {
- int id = NewId();
- foreach (var i in item)
- {
- i.Id = id;
- _dbContext.Add(i);
- id += 1;
- }
- Save();
- }
-
- public void Remove(Agent item)
- {
- var i = _dbContext.Agents.Find(item);
- _dbContext.Agents.Remove(i);
- Save();
- }
-
- public void Remove(IEnumerable<Agent> items)
- {
- foreach (var item in items)
- {
- Agent i = _dbContext.Agents.Find(item);
- _dbContext.Agents.Remove(i);
- }
- Save();
- }
-
- public void RemoveAtId(int item)
- {
- var i = _dbContext.Agents.Find(item);
- _dbContext.Agents.Remove(i);
- Save();
- }
-
- public void Update(Agent item)
- {
- _dbContext.Entry(item).State = EntityState.Modified;
- Save();
- }
-
- public void Save()
- {
- _dbContext.SaveChanges();
- }
-
- public List<Agent> GetDetailedAll()
- {
- // TODO: GetDetailed
- throw new NotImplementedException();
- }
-
- public int NewId()
- {
- int id = 0;
- if (_dbContext.Agents.Count() > 0)
- {
- id = _dbContext.Agents.Max(x => x.Id);
- }
- id += 1;
- return id;
- }
- }
- }
|