using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Linq; using System.Net; using System.Text; using UnivateProperties_API.Containers.Timeshare; using UnivateProperties_API.Containers.Timeshare.Detailed; using UnivateProperties_API.Containers.Users; using UnivateProperties_API.Context; using UnivateProperties_API.Helpers; using UnivateProperties_API.Model.Region; using UnivateProperties_API.Model.Timeshare; using UnivateProperties_API.Repository.Region; using UnivateProperties_API.Repository.Users; namespace UnivateProperties_API.Repository.Timeshare { public class WeekRepository : IRepository { private readonly DataContext _dbContext; public WeekRepository(DataContext dbContext) { _dbContext = dbContext; } public List Get(Func where) { return _dbContext.Weeks.Where(where).ToList(); } public List GetAll() { return _dbContext.Weeks.ToList(); } public TimeshareWeek GetDetailed(Func first) { var item = _dbContext.Weeks.FirstOrDefault(first); item = GetDetailedWeek(item); return item; } public DetailedWeekDto GetMyDetailed(Func first) { var item = GetDetailed(first); if(item != null) { return new DetailedWeekDto(item); } return null; } public List GetDtoListAll() { List list = new List(); foreach (var item in GetDetailedAll()) { list.Add(new WeekDto(item)); } foreach (var item in GetTenderWeeks()) { list.Add(item); } return list; } public List GetMyWeeks(int userId) { List list = new List(); UserRepository userRepository = new UserRepository(_dbContext); var user = userRepository.Get(x => x.Id == userId).FirstOrDefault(); if (user.IsUserInRole(Role.SuperAdmin)) { foreach (var item in GetDetailedAll()) { list.Add(new WeekDto(item)); } } else if (user.IsUserInRole(Role.Agency)) { var agent = _dbContext.Agents.FirstOrDefault(x => x.UserId == userId); if(agent != null) { foreach (var item in GetDetailedAll().Where(x => x.AgencyId == agent.AgencyId)) { list.Add(new WeekDto(item)); } } } else if (user.IsUserInRole(Role.Agent) || user.IsUserInRole(Role.ManagingAgent)) { var agent = _dbContext.Agents.FirstOrDefault(x => x.UserId == userId); if(agent != null) { foreach (var item in GetDetailedAll().Where(x => x.AgentId == agent.Id)) { list.Add(new WeekDto(item)); } } } else { var individual = _dbContext.Individuals.FirstOrDefault(x => x.UserId == userId); if(individual != null) { foreach (var item in GetDetailedAll().Where(x => x.OwnerId == userId)) { list.Add(new WeekDto(item)); } } } return list; } public List GetAvailResort() { List list = new List(); var allItems = GetDtoListAll(); foreach (var item in allItems) { if (item.Region != null) { if (!list.Any(x => x.RegionCode == item.Region.RegionCode)) { list.Add(new RegionDto(item.Region.RegionCode, item.Region.RegionName)); } foreach (var i in list.Where(x => x.RegionCode == item.Region.RegionCode)) { i.TryAddResort(item.Resort.ResortCode, item.Resort.ResortName); } } } list = list.OrderBy(x => x.RegionName).ToList(); foreach (var region in list) { region.OrderResorts(); foreach (var resort in region.Resorts) { resort.Available = allItems.Count(x => x.Resort.ResortCode == resort.ResortCode); } } return list; } public List GetDetailedAll() { var list = GetAll(); List weeklist = new List(); foreach (var item in list) { weeklist.Add(GetDetailedWeek(item)); } return weeklist; } private TimeshareWeek GetDetailedWeek(TimeshareWeek week) { if (week.AgencyId != null) { AgencyRepository agency = new AgencyRepository(_dbContext); week.Agency = agency.Get(x => x.Id == week.AgencyId).FirstOrDefault(); } if (week.AgentId != null) { AgentRepository agent = new AgentRepository(_dbContext); week.Agent = agent.Get(x => x.Id == week.AgentId).FirstOrDefault(); } if (week.StatusId != 0) { StatusRepository status = new StatusRepository(_dbContext); week.Status = status.Get(x => x.Id == week.StatusId).FirstOrDefault(); } else { StatusRepository repo = new StatusRepository(_dbContext); week.Status = repo.GetDetailed(s => s.Code == "A1"); } if (week.RegionId != 0) { ProvinceRepository province = new ProvinceRepository(_dbContext); week.Region = province.Get(x => x.Id == week.RegionId).FirstOrDefault(); } if (week.OwnerId != 0) { IndividualRepository individual = new IndividualRepository(_dbContext); week.Owner = individual.Get(x => x.Id == week.OwnerId).FirstOrDefault(); } return week; } public void Insert(TimeshareWeek item) { item = GetDetailedWeek(item); item.Id = _dbContext.Weeks.Max(x => x.Id) + 1; if (item.Owner != null && item.Owner.Id == 0) { item.Owner.Id = _dbContext.Individuals.Max(x => x.Id) + 1; } // Set starting Status of A1 StatusRepository repo = new StatusRepository(_dbContext); item.Status = repo.GetDetailed(s => s.Code == "A1"); if (item.Status != null) { //Create initial item.Status = new Status() { Code = "A1", Description = "Pending verification", StatusType = StatusType.Timeshare, ModifiedBy = "KobusB" }; } item.Id = NewId(); UnivateProperties_API.Model.ProcessFlow.ProcessFlow flowItem = new Model.ProcessFlow.ProcessFlow() { Status = item.Status, Timeshare = item }; _dbContext.Add(item); _dbContext.Add(flowItem); Save(); } public void Insert(IEnumerable items) { int id = NewId(); foreach (var item in items) { item.Id = id; _dbContext.Add(item); id += 1; } Save(); } public void Remove(TimeshareWeek item) { var i = _dbContext.Weeks.Find(item); _dbContext.Weeks.Remove(i); Save(); } public void Remove(IEnumerable items) { foreach (var item in items) { var i = _dbContext.Weeks.Find(item); _dbContext.Weeks.Remove(i); } Save(); } public void RemoveAtId(int item) { var i = _dbContext.Weeks.Find(item); _dbContext.Weeks.Remove(i); Save(); } public void Save() { _dbContext.SaveChanges(); } public void Update(TimeshareWeek item) { _dbContext.Entry(item).State = EntityState.Modified; Save(); } public List GetBy(WeekFilterDto week) { List item = GetDetailedAll(); if (!string.IsNullOrEmpty(week.RegionCode)) { item = item.Where(x => x.Region != null && x.Region.Code?.ToLower() == week.RegionCode.ToLower()).ToList(); } if (!string.IsNullOrEmpty(week.ResortCode)) { item = item.Where(x => x.ResortCode?.ToLower() == week.ResortCode).ToList(); } if (week.Date != null) { item = item.Where(x => x.ArrivalDate >= week.Date.Value.AddDays(-7) && x.DepartureDate <= week.Date.Value.AddDays(7)).ToList(); } if (week.MinAmount != null && week.MinAmount != 0) { item = item.Where(x => x.SellPrice >= week.MinAmount).ToList(); } if (week.MaxAmount != null && week.MaxAmount != 0) { item.Where(x => x.SellPrice <= week.MaxAmount).ToList(); } return item; } public int NewId() { int id = 0; if (_dbContext.Weeks.Count() > 0) { id = _dbContext.Weeks.Max(x => x.Id); } id += 1; return id; } private int _TenderId = 10000; private int GetTenderId() { return _TenderId++; } private List GetTenderWeeks() { List list = new List(); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(MyCommon.TenderUrl); request.Method = "GET"; WebResponse response = request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { string result = reader.ReadToEnd(); if (result.Length > 0) { string cleanLine; string[] lines = result.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); foreach (string line in lines) { cleanLine = line.Replace("
", ""); cleanLine = cleanLine.Replace("
", ""); cleanLine = cleanLine.Replace("
", ""); cleanLine = cleanLine.Replace("
", ""); list.Add(new WeekDto(GetTenderId(),cleanLine)); } } } // Check that all regions are same as other list .Where(x => x.Region != null && x.Region.RegionCode == "FN") .ToList() .ForEach(x => x.Region.RegionCode = ChangeRegion(x.Region.RegionCode)); list = GetRegion(list); return list; } private List GetRegion(List list) { ProvinceRepository province = new ProvinceRepository(_dbContext); Province prov = null; foreach(var item in list) { prov = province.GetDetailed(x => x.Code == item.Region.RegionCode); if(prov != null) { item.Region = new RegionDto(prov.Id, prov.Code, prov.Description); } } return list; } private string ChangeRegion(string value) { switch(value) { case "FN": return "KZN"; case "L": return "LI"; default: return value; } } } }