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.

SearchLogRepository.cs 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using Microsoft.EntityFrameworkCore;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using UnivateProperties_API.Containers.Property;
  8. using UnivateProperties_API.Containers.Timeshare;
  9. using UnivateProperties_API.Context;
  10. using UnivateProperties_API.Model.Logging;
  11. namespace UnivateProperties_API.Repository.Logging
  12. {
  13. public class SearchLogRepository : ISearchLogRepository
  14. {
  15. private readonly DataContext _dbContext;
  16. public SearchLogRepository(DataContext dbContext)
  17. {
  18. _dbContext = dbContext;
  19. }
  20. public List<SearchLog> Get(Func<SearchLog, bool> where)
  21. {
  22. return _dbContext.SearchLogs.Where(where).ToList();
  23. }
  24. public List<SearchLog> GetAll()
  25. {
  26. return _dbContext.SearchLogs.ToList();
  27. }
  28. public SearchLog GetDetailed(Func<SearchLog, bool> first)
  29. {
  30. var item = _dbContext.SearchLogs.FirstOrDefault(first);
  31. return item;
  32. }
  33. public List<SearchLog> GetDetailedAll()
  34. {
  35. return _dbContext.SearchLogs.ToList();
  36. }
  37. public List<PropertySearchDispaly> GetPropertySearches()
  38. {
  39. var list = new List<PropertySearchDispaly>();
  40. var logs = Get(x => x.Type == "Property");
  41. foreach (SearchLog log in logs)
  42. {
  43. var propSearch = JsonConvert.DeserializeObject<PropertySearch>(log.Search);
  44. list.Add(new PropertySearchDispaly()
  45. {
  46. Date = log.Created,
  47. UserName = propSearch.UserName,
  48. Keyword = propSearch.Keyword,
  49. SalesType = propSearch.SalesType,
  50. PropertyUsageType = propSearch.PropertyUsageType,
  51. PropertyType = propSearch.PropertyType,
  52. Province = propSearch.Province,
  53. City = propSearch.City,
  54. Suburb = propSearch.Suburb
  55. });
  56. Debug.WriteLine(propSearch);
  57. }
  58. return list;
  59. }
  60. public List<TimeshareSearchDisplay> GetTimeshareSearches()
  61. {
  62. var list = new List<TimeshareSearchDisplay>();
  63. var logs = Get(x => x.Type == "Timeshare");
  64. foreach (SearchLog log in logs)
  65. {
  66. var timeshareSearch = JsonConvert.DeserializeObject<TimeshareSearch>(log.Search);
  67. list.Add(new TimeshareSearchDisplay() {
  68. Date = log.Created,
  69. UserName = timeshareSearch.UserName,
  70. Property = timeshareSearch.Property,
  71. Value = timeshareSearch.Value
  72. });
  73. }
  74. return list;
  75. }
  76. public void Insert(SearchLog item)
  77. {
  78. _dbContext.SearchLogs.Add(item);
  79. Save();
  80. }
  81. public void Insert(IEnumerable<SearchLog> items)
  82. {
  83. foreach (var item in items)
  84. {
  85. _dbContext.SearchLogs.Add(item);
  86. Save();
  87. }
  88. }
  89. public void Remove(SearchLog item)
  90. {
  91. _dbContext.SearchLogs.Remove(item);
  92. Save();
  93. }
  94. public void Remove(IEnumerable<SearchLog> items)
  95. {
  96. foreach (var item in items)
  97. {
  98. _dbContext.SearchLogs.Remove(item);
  99. Save();
  100. }
  101. }
  102. public void RemoveAtId(int item)
  103. {
  104. var searchLog = Get(x => x.Id == item).FirstOrDefault();
  105. if (searchLog != null)
  106. {
  107. _dbContext.SearchLogs.Remove(searchLog);
  108. Save();
  109. }
  110. }
  111. public void Save()
  112. {
  113. _dbContext.SaveChanges();
  114. }
  115. public void SaveTimeshareSearch(TimeshareSearch item)
  116. {
  117. var searchLog = new SearchLog
  118. {
  119. Type = "Timeshare",
  120. Search = JsonConvert.SerializeObject(item)
  121. };
  122. _dbContext.SearchLogs.Remove(searchLog);
  123. Save();
  124. }
  125. public void Update(SearchLog item)
  126. {
  127. _dbContext.Entry(item).State = EntityState.Modified;
  128. Save();
  129. }
  130. }
  131. }