Pārlūkot izejas kodu

Property Search Changes

master
George Williams 5 gadus atpakaļ
vecāks
revīzija
badd145992

+ 2
- 1
UnivateProperties_API/Containers/Property/PropertySearch.cs Parādīt failu

@@ -6,7 +6,8 @@
6 6
         public int UserID { get; set; }
7 7
         public string Keyword { get; set; }
8 8
         public string SalesType { get; set; }
9
-        public string PropertyType { get; set; }
9
+        public string PropertyUsageType { get; set; }
10
+        public string PropertyType { get; set; }        
10 11
         public string Province { get; set; }
11 12
         public string City { get; set; }
12 13
         public string Suburb { get; set; }

+ 8
- 13
UnivateProperties_API/Controllers/Properties/PropertyController.cs Parādīt failu

@@ -2,6 +2,7 @@
2 2
 using System.Collections.Generic;
3 3
 using System.Linq;
4 4
 using System.Transactions;
5
+using UnivateProperties_API.Containers.Property;
5 6
 using UnivateProperties_API.Model.Properties;
6 7
 using UnivateProperties_API.Repository.Properties;
7 8
 
@@ -29,19 +30,7 @@ namespace UnivateProperties_API.Controllers.Properties
29 30
         public IActionResult Get(int id)
30 31
         {
31 32
             return new OkObjectResult(_Repo.GetDetailed(x => x.Id == id));
32
-        }
33
-
34
-        [HttpGet("search/{keyword}")]
35
-        public IActionResult Search(string Keyword)
36
-        {
37
-            return new OkObjectResult(_Repo.GetDisplay(Keyword));
38
-        }
39
-
40
-        [HttpGet("search/{type}/{propertytype}/{province}/{city}/{suburb}/{proptype}")]
41
-        public IActionResult Get(string type, string propertyType, string province, string city, string suburb, string propType)
42
-        {
43
-            return new OkObjectResult(_Repo.GetDisplay(type, propertyType, province, city, suburb, propType));
44
-        }
33
+        }          
45 34
 
46 35
         [HttpGet("latestProperties")]
47 36
         public IActionResult GetLatestProperties()
@@ -89,6 +78,12 @@ namespace UnivateProperties_API.Controllers.Properties
89 78
         }
90 79
         #endregion
91 80
 
81
+        [HttpPost("search")]
82
+        public IActionResult Search([FromBody] PropertySearch search)
83
+        {
84
+            return new OkObjectResult(_Repo.GetDisplay(search));
85
+        }
86
+
92 87
         [HttpPost]
93 88
         public IActionResult Post([FromBody] Property property)
94 89
         {

+ 1
- 2
UnivateProperties_API/Repository/Properties/IPropertyRepository.cs Parādīt failu

@@ -9,8 +9,7 @@ namespace UnivateProperties_API.Repository.Properties
9 9
     {
10 10
         List<PropertyDisplay> GetDisplay();
11 11
         List<PropertyDisplay> GetDisplay(Func<Property, bool> where);
12
-        List<PropertyDisplay> GetDisplay(string Keyword);
13
-        List<PropertyDisplay> GetDisplay(string type, string propertyType, string province, string city, string suburb, string propType);
12
+        List<PropertyDisplay> GetDisplay(PropertySearch search);        
14 13
         List<PropertyDisplay> GetLatestDisplay();
15 14
         List<PropertyType> GetPropertyTypes(Func<PropertyType, bool> where);
16 15
         List<PropertyList> GetPropertyList(Func<Property, bool> where);

+ 78
- 67
UnivateProperties_API/Repository/Properties/PropertyRepository.cs Parādīt failu

@@ -1,4 +1,5 @@
1 1
 using Microsoft.EntityFrameworkCore;
2
+using Newtonsoft.Json;
2 3
 using System;
3 4
 using System.Collections.Generic;
4 5
 using System.Linq;
@@ -241,84 +242,94 @@ namespace UnivateProperties_API.Repository.Properties
241 242
             return GetDisplayDetails(props);
242 243
         }
243 244
 
244
-        public List<PropertyDisplay> GetDisplay(string Keyword)
245
+        public List<PropertyDisplay> GetDisplay(PropertySearch search)
245 246
         {
246
-            Keyword = Keyword.ToLower();
247
-
248
-            List<Property> props = (from p in dBContext.Properties
249
-                                    join pr in dBContext.Provinces on p.ProvinceId equals pr.Id
250
-                                    join c in dBContext.Cities on p.CityId equals c.Id
251
-                                    join s in dBContext.Suburbs on p.SuburbId equals s.Id
252
-                                    join pt in dBContext.PropertyTypes on p.PropertyTypeId equals pt.Id
253
-                                    where EF.Functions.Like(p.PropertyName.ToLower(), $"%{Keyword}%")
254
-                                    || EF.Functions.Like(pr.Description.ToLower(), $"%{Keyword}%")
255
-                                    || EF.Functions.Like(c.Description.ToLower(), $"%{Keyword}%")
256
-                                    || EF.Functions.Like(s.Description.ToLower(), $"%{Keyword}%")
257
-                                    || EF.Functions.Like(pt.Description.ToLower(), $"%{Keyword}%")
258
-                                    select p).ToList();
259
-
260
-            return GetDisplayDetails(props);
261
-        }
262
-
263
-        public List<PropertyDisplay> GetDisplay(string type, string propertyType, string province, string city, string suburb, string propType)
264
-        {
265
-            List<Property> props;
266
-            PropertyUsageType uType = PropertyUsageType.Both;
267
-
268
-            if (propertyType != "" && propertyType != "undefined")
247
+            //Save to Log
248
+            dBContext.SearchLogs.Add(new Model.Logging.SearchLog()
269 249
             {
270
-                if (propertyType.ToUpper() == "COMMERCIAL")
271
-                    uType = PropertyUsageType.Commercial;
272
-                else
273
-                    uType = PropertyUsageType.Residential;
274
-            }
275
-            props = (from p in dBContext.Properties
276
-                     join pt in dBContext.PropertyTypes on p.PropertyTypeId equals pt.Id
277
-                     where pt.UsageType == uType
278
-                     select p).ToList();
250
+                UserID = search.UserID,
251
+                Search = JsonConvert.SerializeObject(search)
252
+            });
253
+            Save();
279 254
 
280
-            if (type != "" && type != "undefined")
255
+            if (!string.IsNullOrEmpty(search.Keyword))
281 256
             {
282
-                if (type.ToUpper() == "SALE")
283
-                    props = props.Where(p => p.IsSale).ToList();
284
-                else
285
-                    props = props.Where(p => !p.IsSale).ToList();
257
+                string keyword = search.Keyword.ToLower();
258
+
259
+                List<Property> props = (from p in dBContext.Properties
260
+                                        join pr in dBContext.Provinces on p.ProvinceId equals pr.Id
261
+                                        join c in dBContext.Cities on p.CityId equals c.Id
262
+                                        join s in dBContext.Suburbs on p.SuburbId equals s.Id
263
+                                        join pt in dBContext.PropertyTypes on p.PropertyTypeId equals pt.Id
264
+                                        where EF.Functions.Like(p.PropertyName.ToLower(), $"%{keyword}%")
265
+                                        || EF.Functions.Like(pr.Description.ToLower(), $"%{keyword}%")
266
+                                        || EF.Functions.Like(c.Description.ToLower(), $"%{keyword}%")
267
+                                        || EF.Functions.Like(s.Description.ToLower(), $"%{keyword}%")
268
+                                        || EF.Functions.Like(pt.Description.ToLower(), $"%{keyword}%")
269
+                                        select p).ToList();
270
+
271
+                return GetDisplayDetails(props);
286 272
             }
287
-
288
-            if (province != "" && province != "undefined" && province.ToUpper() != "ALL")
273
+            else
289 274
             {
290
-                props = (from p in props
291
-                         join pp in dBContext.Provinces on p.ProvinceId equals pp.Id
292
-                         where pp.Description.ToUpper() == province.ToUpper()
293
-                         select p).ToList();
294
-            }
275
+                List<Property> props;
276
+                PropertyUsageType uType = PropertyUsageType.Both;
295 277
 
296
-            if (city != "" && city != "undefined" && city.ToUpper() != "ALL")
297
-            {
298
-                props = (from p in props
299
-                         join c in dBContext.Cities on p.CityId equals c.Id
300
-                         where c.Description.ToUpper() == city.ToUpper()
278
+                if (!string.IsNullOrEmpty(search.PropertyUsageType) && search.PropertyUsageType != "undefined")
279
+                {
280
+                    if (search.PropertyUsageType.ToUpper() == "COMMERCIAL")
281
+                        uType = PropertyUsageType.Commercial;
282
+                    else
283
+                        uType = PropertyUsageType.Residential;
284
+                }
285
+                props = (from p in dBContext.Properties
286
+                         join pt in dBContext.PropertyTypes on p.PropertyTypeId equals pt.Id
287
+                         where pt.UsageType == uType
301 288
                          select p).ToList();
302
-            }
303 289
 
304
-            if (suburb != "" && suburb != "undefined" && suburb.ToUpper() != "ALL")
305
-            {
306
-                props = (from p in props
307
-                         join s in dBContext.Suburbs on p.SuburbId equals s.Id
308
-                         where s.Description.ToUpper() == suburb.ToUpper()
309
-                         select p).ToList();
310
-            }
311
-            if (propType != "" && propType != "Undefined" && propType.ToUpper() != "ALL")
312
-            {
313
-                var pType = dBContext.PropertyTypes.Where(t => t.Description == propType).FirstOrDefault();
314
-                if (pType != null)
290
+                if (!string.IsNullOrEmpty(search.SalesType) && search.SalesType != "undefined" && search.SalesType.ToUpper() != "ALL")
315 291
                 {
316
-                    props = props.Where(p => p.PropertyTypeId == pType.Id).ToList();
292
+                    if (search.SalesType.ToUpper() == "SALE")
293
+                        props = props.Where(p => p.IsSale).ToList();
294
+                    else
295
+                        props = props.Where(p => !p.IsSale).ToList();
317 296
                 }
318
-            }
319 297
 
320
-            return GetDisplayDetails(props);
321
-        }
298
+                if (!string.IsNullOrEmpty(search.Province) && search.Province != "undefined" && search.Province.ToUpper() != "ALL")
299
+                {
300
+                    props = (from p in props
301
+                             join pp in dBContext.Provinces on p.ProvinceId equals pp.Id
302
+                             where pp.Description.ToUpper() == search.Province.ToUpper()
303
+                             select p).ToList();
304
+                }
305
+
306
+                if (!string.IsNullOrEmpty(search.City) && search.City != "undefined" && search.City.ToUpper() != "ALL")
307
+                {
308
+                    props = (from p in props
309
+                             join c in dBContext.Cities on p.CityId equals c.Id
310
+                             where c.Description.ToUpper() == search.City.ToUpper()
311
+                             select p).ToList();
312
+                }
313
+
314
+                if (!string.IsNullOrEmpty(search.Suburb) && search.Suburb != "undefined" && search.Suburb.ToUpper() != "ALL")
315
+                {
316
+                    props = (from p in props
317
+                             join s in dBContext.Suburbs on p.SuburbId equals s.Id
318
+                             where s.Description.ToUpper() == search.Suburb.ToUpper()
319
+                             select p).ToList();
320
+                }
321
+                if (!string.IsNullOrEmpty(search.PropertyType) && search.PropertyType != "Undefined" && search.PropertyType.ToUpper() != "ALL")
322
+                {
323
+                    var pType = dBContext.PropertyTypes.Where(t => t.Description == search.PropertyType).FirstOrDefault();
324
+                    if (pType != null)
325
+                    {
326
+                        props = props.Where(p => p.PropertyTypeId == pType.Id).ToList();
327
+                    }
328
+                }
329
+
330
+                return GetDisplayDetails(props);
331
+            }
332
+        }        
322 333
 
323 334
         private List<PropertyDisplay> GetDisplayDetails(List<Property> props)
324 335
         {
@@ -428,6 +439,6 @@ namespace UnivateProperties_API.Repository.Properties
428 439
             }
429 440
                 
430 441
             return list;
431
-        }
442
+        }       
432 443
     }
433 444
 }

Notiek ielāde…
Atcelt
Saglabāt