API
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TemplateRepository.cs 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using Microsoft.EntityFrameworkCore;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnivateProperties_API.Containers.Communication;
  6. using UnivateProperties_API.Containers.Timeshare;
  7. using UnivateProperties_API.Context;
  8. using UnivateProperties_API.Model;
  9. using UnivateProperties_API.Model.Communication;
  10. using UnivateProperties_API.Model.Timeshare;
  11. using UnivateProperties_API.Model.Users;
  12. namespace UnivateProperties_API.Repository.Communication
  13. {
  14. public class TemplateRepository : IRepository<Template>
  15. {
  16. private readonly DataContext _dbContext;
  17. public TemplateRepository(DataContext dbContext)
  18. {
  19. _dbContext = dbContext;
  20. }
  21. public List<Template> Get(Func<Template, bool> where)
  22. {
  23. return _dbContext.Templates.Where(where).ToList();
  24. }
  25. public List<Template> GetAll()
  26. {
  27. return _dbContext.Templates.ToList();
  28. }
  29. public Template GetDetailed(Func<Template, bool> first)
  30. {
  31. var item = _dbContext.Templates.FirstOrDefault(first);
  32. return item;
  33. }
  34. public List<Template> GetDetailedAll()
  35. {
  36. var list = GetAll();
  37. return list;
  38. }
  39. public void Insert(Template item)
  40. {
  41. _dbContext.Add(item);
  42. Save();
  43. }
  44. public void Insert(IEnumerable<Template> items)
  45. {
  46. foreach (var item in items)
  47. {
  48. _dbContext.Add(item);
  49. }
  50. Save();
  51. }
  52. public void Remove(Template item)
  53. {
  54. var i = _dbContext.Templates.Find(item);
  55. _dbContext.Templates.Remove(i);
  56. Save();
  57. }
  58. public void Remove(IEnumerable<Template> items)
  59. {
  60. foreach (var item in items)
  61. {
  62. Template i = _dbContext.Templates.Find(item);
  63. _dbContext.Templates.Remove(i);
  64. }
  65. Save();
  66. }
  67. public void RemoveAtId(int item)
  68. {
  69. var i = _dbContext.Templates.Find(item);
  70. _dbContext.Templates.Remove(i);
  71. Save();
  72. }
  73. public void Update(Template item)
  74. {
  75. var itemList = _dbContext.PlaceHolders.Where(x => x.TemplateId == item.Id).ToList();
  76. CheckListChange(itemList, item.PlaceHolders.ToList());
  77. _dbContext.Entry(item).State = EntityState.Modified;
  78. Save();
  79. }
  80. private void CheckListChange(List<PlaceHolder> holderOld, List<PlaceHolder> holderNew)
  81. {
  82. foreach (var item in holderOld)
  83. {
  84. if (!holderNew.Any(x => x.Id == item.Id))
  85. {
  86. var i = _dbContext.PlaceHolders.FirstOrDefault(x => x.Id == item.Id);
  87. if (item != null)
  88. {
  89. _dbContext.PlaceHolders.Remove(i);
  90. }
  91. }
  92. }
  93. foreach (var item in holderNew.Where(x => x.Id == 0))
  94. {
  95. _dbContext.Add(item);
  96. }
  97. }
  98. public void Save()
  99. {
  100. _dbContext.SaveChanges();
  101. }
  102. public List<TemplateDto> GetSimpleAll()
  103. {
  104. List<TemplateDto> list = new List<TemplateDto>();
  105. foreach (var item in GetAll())
  106. {
  107. list.Add(new TemplateDto()
  108. {
  109. Id = item.Id,
  110. Name = item.Name,
  111. Subject = item.Subject,
  112. Body = item.Body,
  113. PlaceHolders = GetPlaceHolders(item.Id)
  114. });
  115. }
  116. return list;
  117. }
  118. private List<PlaceHolderDto> GetPlaceHolders(int id)
  119. {
  120. List<PlaceHolderDto> list = new List<PlaceHolderDto>();
  121. foreach (var item in _dbContext.PlaceHolders.Where(x => x.TemplateId == id).ToList())
  122. {
  123. list.Add(new PlaceHolderDto()
  124. {
  125. Id = item.Id,
  126. Name = item.Name,
  127. BoundTo = item.BoundTo,
  128. BoundToClass = item.BoundToClass,
  129. BoundToClassDisplay = item.BoundToClassDisplay
  130. });
  131. }
  132. return list;
  133. }
  134. public void SendEmailTemplate(Template template, Person sendTo, List<BaseEntity> args)
  135. {
  136. if (template != null)
  137. {
  138. if (template.SenderId == null)
  139. {
  140. var acc = _dbContext.Accounts.FirstOrDefault(x => x.Default);
  141. if (acc != null)
  142. {
  143. template.SenderId = acc.Id;
  144. template.Sender = acc;
  145. }
  146. }
  147. else template.Sender = _dbContext.Accounts.FirstOrDefault(x => x.Id == template.SenderId);
  148. template.PlaceHolders = _dbContext.PlaceHolders.Where(x => x.TemplateId == template.Id).ToList();
  149. if (sendTo != null)
  150. {
  151. try
  152. {
  153. /*Email email = new Email(template, sendTo, args);
  154. EmailRepository emailRepo = new EmailRepository(_dbContext);
  155. emailRepo.Insert(email);*/
  156. }
  157. catch (Exception)
  158. {
  159. }
  160. }
  161. }
  162. }
  163. public void SendEmailTemplateWeek(Template template, Person sendTo, TimeshareWeekDto sellItem)
  164. {
  165. if (template != null)
  166. {
  167. if (template.SenderId == null)
  168. {
  169. var acc = _dbContext.Accounts.FirstOrDefault(x => x.Default);
  170. if (acc != null)
  171. {
  172. template.SenderId = acc.Id;
  173. template.Sender = acc;
  174. }
  175. }
  176. else template.Sender = _dbContext.Accounts.FirstOrDefault(x => x.Id == template.SenderId);
  177. template.PlaceHolders = _dbContext.PlaceHolders.Where(x => x.TemplateId == template.Id).ToList();
  178. if (sendTo != null)
  179. {
  180. try
  181. {
  182. Email email = new Email(template, sendTo, sellItem, _dbContext);
  183. EmailRepository emailRepo = new EmailRepository(_dbContext);
  184. emailRepo.Insert(email);
  185. }
  186. catch (Exception)
  187. {
  188. }
  189. }
  190. }
  191. }
  192. public int NewId()
  193. {
  194. throw new NotImplementedException();
  195. }
  196. }
  197. }