| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 | using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using UnivateProperties_API.Containers.Property;
using UnivateProperties_API.Containers.Timeshare;
using UnivateProperties_API.Context;
using UnivateProperties_API.Model.Logging;
namespace UnivateProperties_API.Repository.Logging
{
    public class SearchLogRepository : ISearchLogRepository
    {
        private readonly DataContext _dbContext;
        public SearchLogRepository(DataContext dbContext)
        {
            _dbContext = dbContext;
        }
        public List<SearchLog> Get(Func<SearchLog, bool> where)
        {
            return _dbContext.SearchLogs.Where(where).ToList();
        }
        public List<SearchLog> GetAll()
        {
            return _dbContext.SearchLogs.ToList();
        }
        public SearchLog GetDetailed(Func<SearchLog, bool> first)
        {
            var item = _dbContext.SearchLogs.FirstOrDefault(first);
            return item;
        }
        public List<SearchLog> GetDetailedAll()
        {
            return _dbContext.SearchLogs.ToList();
        }
        public List<PropertySearchDispaly> GetPropertySearches()
        {
            var list = new List<PropertySearchDispaly>();
            var logs = Get(x => x.Type == "Property");
            foreach (SearchLog log in logs)
            {
                var propSearch = JsonConvert.DeserializeObject<PropertySearch>(log.Search);
                list.Add(new PropertySearchDispaly()
                {
                    Date = log.Created,
                    UserName = propSearch.UserName,
                    Keyword = propSearch.Keyword,
                    SalesType = propSearch.SalesType,
                    PropertyUsageType = propSearch.PropertyUsageType,
                    PropertyType = propSearch.PropertyType,
                    Province = propSearch.Province,
                    City = propSearch.City,
                    Suburb = propSearch.Suburb
                });
                Debug.WriteLine(propSearch);
            }
            return list;            
        }
        public List<TimeshareSearchDisplay> GetTimeshareSearches()
        {
            var list = new List<TimeshareSearchDisplay>();
            var logs = Get(x => x.Type == "Timeshare");
            foreach (SearchLog log in logs)
            {
                var timeshareSearch = JsonConvert.DeserializeObject<TimeshareSearch>(log.Search);
                list.Add(new TimeshareSearchDisplay() {
                    Date = log.Created,
                    UserName = timeshareSearch.UserName,
                    Property = timeshareSearch.Property,
                    Value = timeshareSearch.Value
                });
            }
            return list;
        }
        public void Insert(SearchLog item)
        {
            _dbContext.SearchLogs.Add(item);
            Save();
        }
        public void Insert(IEnumerable<SearchLog> items)
        {
            foreach (var item in items)
            {
                _dbContext.SearchLogs.Add(item);
                Save();
            }
        }
        public void Remove(SearchLog item)
        {
            _dbContext.SearchLogs.Remove(item);
            Save();
        }
        public void Remove(IEnumerable<SearchLog> items)
        {
            foreach (var item in items)
            {
                _dbContext.SearchLogs.Remove(item);
                Save();
            }
        }
        public void RemoveAtId(int item)
        {
            var searchLog = Get(x => x.Id == item).FirstOrDefault();
            if (searchLog != null)
            {
               _dbContext.SearchLogs.Remove(searchLog);
                Save();
            }
        }
        public void Save()
        {
            _dbContext.SaveChanges();
        }
        public void SaveTimeshareSearch(TimeshareSearch item)
        {
            var searchLog = new SearchLog
            {
                Type = "Timeshare",
                Search = JsonConvert.SerializeObject(item)
            };
            _dbContext.SearchLogs.Remove(searchLog);
            Save();
        }
        public void Update(SearchLog item)
        {
            _dbContext.Entry(item).State = EntityState.Modified;
            Save();
        }
    }
}
 |