123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- using Microsoft.EntityFrameworkCore;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using UnivateProperties_API.Containers.ProcessFlow;
- using UnivateProperties_API.Context;
- using UnivateProperties_API.Helpers;
- using UnivateProperties_API.Model;
- using UnivateProperties_API.Model.Communication;
- using UnivateProperties_API.Model.ProcessFlow;
- using UnivateProperties_API.Repository.Communication;
- using UnivateProperties_API.Repository.Timeshare;
-
- namespace UnivateProperties_API.Repository.ProccessFlow
- {
- public class BidRepository : IBidRepository
- {
- private readonly DataContext _dbContext;
-
- public BidRepository(DataContext dbContext)
- {
- _dbContext = dbContext;
- }
-
- public List<BidItem> Get(Func<BidItem, bool> where)
- {
- return _dbContext.BidItems.Where(where).ToList();
- }
-
- public List<BidItem> GetAll()
- {
- return _dbContext.BidItems.ToList();
- }
-
- public BidItem GetDetailed(Func<BidItem, bool> first)
- {
- var item = _dbContext.BidItems.FirstOrDefault(first);
- return item;
- }
-
- public List<BidItem> GetDetailedAll()
- {
- return GetAll();
- }
-
- public List<BidItemDisplay> GetAllBid()
- {
- List<BidItem> bids = _dbContext.BidItems
- .Include("Property")
- .Include("TimeshareWeek")
- .Include("BidMaker")
- .Include("Status")
- .Include("Property.Owner")
- .Include("Property.Agent")
- .ToList();
-
- return LoadDisplay(bids);
- }
-
- public List<BidItemDisplay> GetMyBid(Func<BidItem, bool> where)
- {
- List<BidItem> bids = _dbContext.BidItems
- .Include("Property")
- .Include("TimeshareWeek")
- .Include("BidMaker")
- .Include("Status")
- .Where(where)
- .ToList();
-
- return LoadDisplay(bids);
- }
-
- private List<BidItemDisplay> LoadDisplay(List<BidItem> bids)
- {
- List<BidItemDisplay> list = new List<BidItemDisplay>();
- foreach (BidItem item in bids)
- {
- BidItemDisplay bid = new BidItemDisplay()
- {
- Id = item.Id,
- Offer = (decimal)item.Amount,
- Comment = item.Comment,
- DeclineReason = item.DeclinedReason
- };
-
- if (item.PropertyId != null)
- {
- bid.Type = "Property";
- bid.ShortDescription = item.Property.ShortDescription;
- bid.Description = item.Property.Description;
- bid.Price = item.Property.Price;
- }
- if (item.TimeshareWeekId != null)
- {
- bid.Type = "Timeshare";
- bid.ShortDescription = string.Format("{0} {1} {2}", item.TimeshareWeek.ResortCode, item.TimeshareWeek.WeekNumber, item.TimeshareWeek.UnitNumber);
- bid.Price = (decimal)item.TimeshareWeek.SellPrice;
- bid.Resort = item.TimeshareWeek.ResortName;
- bid.Unit = item.TimeshareWeek.UnitNumber;
- bid.Module = item.TimeshareWeek.Module;
- }
- if (item.Status != null)
- {
- bid.StatusCode = item.Status.Code;
- bid.Status = string.Format("{0} - {1}", item.Status.Code, item.Status.Description);
- }
- if (item.BidMaker != null)
- bid.MadeBy = item.BidMaker.Name + " " + item.BidMaker.Surname;
-
- bid.MadeBy = "Bob";
-
- list.Add(bid);
-
- }
- return list;
- }
-
- public void Insert(BidItem item)
- {
- var week = _dbContext.Weeks.Find(item.TimeshareWeekId);
- if (!(item.TimeshareWeek == null && week == null))
- {
- if (week == null && item.TimeshareWeek != null)
- {
- MyCommon.PostToConsoft(item.TimeshareWeek);
- WeekRepository weekRepository = new WeekRepository(_dbContext);
- weekRepository.Insert(item.TimeshareWeek);
- }
- var status = _dbContext.Status.Where(x => x.Code == "E1").FirstOrDefault();
- if (status != null)
- {
- List<BaseEntity> list = new List<BaseEntity>() { item, week, week.Owner };
- item.StatusId = status.Id;
- if (item.TimeshareWeekId != null)
- {
- if (week != null)
- {
- TemplateRepository templateRepository = new TemplateRepository(_dbContext);
- var template = _dbContext.Templates.FirstOrDefault(x => x.Name == "WeekOfferMade-Owner");
- if (template != null)
- {
- templateRepository.SendEmailTemplate(template, week.Owner, list);
- }
- template = _dbContext.Templates.FirstOrDefault(x => x.Name == "WeekOfferMade-User");
- if (template != null)
- {
- var bidMaker = _dbContext.Individuals.FirstOrDefault(x => x.Id == item.BidMakerId);
- if (bidMaker != null)
- {
- templateRepository.SendEmailTemplate(template, bidMaker, list);
- }
- }
- }
- }
- }
- _dbContext.Add(item);
- Save();
- }
- }
-
- public void Insert(IEnumerable<BidItem> items)
- {
- foreach (var item in items)
- {
- _dbContext.Add(item);
- }
- Save();
- }
-
- public void Remove(BidItem item)
- {
- var i = _dbContext.BidItems.Find(item);
- _dbContext.BidItems.Remove(i);
- Save();
- }
-
- public void Remove(IEnumerable<BidItem> items)
- {
- foreach (var item in items)
- {
- var i = _dbContext.BidItems.Find(item);
- _dbContext.BidItems.Remove(i);
- }
- Save();
- }
-
- public void RemoveAtId(int item)
- {
- var i = _dbContext.BidItems.Find(item);
- _dbContext.BidItems.Remove(i);
- Save();
- }
-
- public void Save()
- {
- _dbContext.SaveChanges();
- }
-
- public void Update(BidItem item)
- {
- _dbContext.Entry(item).State = EntityState.Modified;
- Save();
- }
-
- public BidItemDisplay AcceptBid(int id)
- {
- var item = _dbContext.BidItems
- .Include("Property")
- .Include("TimeshareWeek")
- .Include("BidMaker")
- .Include("Status")
- .Where(x => x.Id == id).FirstOrDefault();
-
- var status = (from s in _dbContext.Status
- where s.Code == "E2"
- select s).FirstOrDefault();
-
- if (status != null)
- {
- item.StatusId = status.Id;
- }
-
- _dbContext.Entry(item).State = EntityState.Modified;
- Save();
-
- List<BidItem> bids = new List<BidItem>() { item };
- return LoadDisplay(bids).Find(x => x.Id == item.Id);
- }
-
- public BidItemDisplay DecineBid(BitItemDecline item)
- {
- var bid = _dbContext.BidItems
- .Include("Property")
- .Include("TimeshareWeek")
- .Include("BidMaker")
- .Include("Status")
- .Where(x => x.Id == item.Id).FirstOrDefault();
-
- var status = (from s in _dbContext.Status
- where s.Code == "E3"
- select s).FirstOrDefault();
-
- if (status != null)
- {
- bid.StatusId = status.Id;
- }
-
- bid.DeclinedReason = item.Comment;
-
- _dbContext.Entry(bid).State = EntityState.Modified;
- Save();
-
- List<BidItem> bids = new List<BidItem>() { bid };
- return LoadDisplay(bids).Find(x => x.Id == bid.Id);
- }
-
- public int NewId()
- {
- // Not sure if properties need it
- return 0;
- }
- }
- }
|