Kobus 5 年之前
父節點
當前提交
4b23626a6c

+ 59
- 0
UnivateProperties_API/Containers/Property/ImageFormatter.cs 查看文件

1
+using System;
2
+using System.Drawing;
3
+using System.IO;
4
+
5
+namespace UnivateProperties_API.Containers.Property
6
+{
7
+    public class ImageFormatter
8
+    {
9
+        public static string Base64ToImage(string binData, string path, string name)
10
+        {
11
+            string extention = ".jpg";
12
+            if (binData.StartsWith("data:image/gif;base64,"))
13
+            {
14
+                extention = ".gif";
15
+                binData = binData.Replace("data:image/gif;base64,", "");
16
+            }
17
+            if (binData.StartsWith("data:image/jpeg;base64,"))
18
+            {
19
+                extention = ".jpg";
20
+                binData = binData.Replace("data:image/jpeg;base64,", "");
21
+            }
22
+            if (binData.StartsWith("data:image/png;base64,"))
23
+            {
24
+                extention = ".png";
25
+                binData = binData.Replace("data:image/png;base64,", "");
26
+            }
27
+
28
+            if (!Directory.Exists(path))
29
+                Directory.CreateDirectory(path);
30
+
31
+            string filePath = string.Format(@"{0}\{1}{2}", path, name, extention);
32
+            File.WriteAllBytes(filePath, Convert.FromBase64String(binData));
33
+            return filePath;
34
+        }
35
+
36
+        public static string ImageToBase64(string Path)
37
+        {
38
+            using (Image image = Image.FromFile(Path))
39
+            {
40
+                using (MemoryStream m = new MemoryStream())
41
+                {
42
+                    image.Save(m, image.RawFormat);
43
+                    byte[] imageBytes = m.ToArray();
44
+
45
+                    string base64String = Convert.ToBase64String(imageBytes);
46
+
47
+                    if (Path.EndsWith(".jpg") || Path.EndsWith(".jpeg"))
48
+                        base64String = "data:image/jpeg;base64," + base64String;
49
+                    if (Path.EndsWith(".gif"))
50
+                        base64String = "data:image/gif;base64," + base64String;
51
+                    if (base64String.EndsWith(".png"))
52
+                        base64String = "data:image/png;base64," + base64String;
53
+
54
+                    return base64String;
55
+                }
56
+            }
57
+        }
58
+    }
59
+}

+ 2
- 0
UnivateProperties_API/Containers/Property/PropertyList.cs 查看文件

7
         public int Id { get; set; }
7
         public int Id { get; set; }
8
         public string Size { get; set; }
8
         public string Size { get; set; }
9
         public string Price { get; set; }
9
         public string Price { get; set; }
10
+        public string UsageType { get; set;  }
10
         public string Type { get; set; }
11
         public string Type { get; set; }
12
+        public string SaleType { get; set; }
11
         public string Publish { get; set; }
13
         public string Publish { get; set; }
12
         public string Status { get; set; }
14
         public string Status { get; set; }
13
         #endregion
15
         #endregion

+ 5
- 1
UnivateProperties_API/Context/DataContext.cs 查看文件

68
 
68
 
69
         #region Logs
69
         #region Logs
70
         public DbSet<SearchLog> SearchLogs { get; set; }
70
         public DbSet<SearchLog> SearchLogs { get; set; }
71
-        #endregion 
71
+        #endregion
72
+
73
+        #region Misc
74
+        public DbSet<Location> Location { get; set; }
75
+        #endregion
72
 
76
 
73
         public override int SaveChanges()
77
         public override int SaveChanges()
74
         {
78
         {

+ 3
- 35
UnivateProperties_API/Controllers/Properties/PropertyController.cs 查看文件

39
         }
39
         }
40
 
40
 
41
         [HttpGet("{type}/{by}")]
41
         [HttpGet("{type}/{by}")]
42
-        public IActionResult SearchBy(string type, string by)
42
+        public IActionResult SearchBy(string type, int by)
43
         {
43
         {
44
-            PropertyUsageType pType = PropertyUsageType.Both;
45
-            switch (type.ToUpper())
46
-            {
47
-                case "RESIDENTIAL":
48
-                    pType = PropertyUsageType.Residential;
49
-                    break;
50
-                case "COMMERCIAL":
51
-                    pType = PropertyUsageType.Commercial;
52
-                    break;
53
-            }
54
-
55
-            if (pType != PropertyUsageType.Both)
56
-            {
57
-                List<int> proptypeIds = (from pt in _Repo.GetPropertyTypes(p => p.UsageType == pType)
58
-                                         select pt.Id).ToList();
59
-
60
-                if (string.IsNullOrEmpty(by) || by.ToUpper() == "ALL")
61
-                {
62
-                    return new OkObjectResult(_Repo.GetPropertyList(x => proptypeIds.Contains(x.PropertyTypeId)));
63
-                }
64
-                else
65
-                {
66
-                    //Needs to change to search on individule/Agent
67
-                    return new OkObjectResult(_Repo.GetPropertyList(x => proptypeIds.Contains(x.PropertyTypeId)));
68
-                }
69
-            }
70
-            else
71
-                return new NoContentResult();
72
-        }
73
-
74
-        [HttpGet("GetEditDisplay/{id}")]
75
-        public IActionResult GetEditDisplay(int id)
76
-        {
77
-            return new OkObjectResult(_Repo.GetPropertyList(x => x.Id == id));
44
+            return new OkObjectResult(_Repo.GetPropertyList(type, by));
78
         }
45
         }
46
+        
79
         #endregion
47
         #endregion
80
 
48
 
81
         [HttpGet("search")]
49
         [HttpGet("search")]

+ 8
- 0
UnivateProperties_API/Model/Misc/Location.cs 查看文件

1
+namespace UnivateProperties_API.Model.Misc
2
+{
3
+    public class Location : BaseEntity
4
+    {
5
+        public bool IsTesting { get; set; }
6
+        public string PropertyImageLocation { get; set; }
7
+    }
8
+}

+ 1
- 1
UnivateProperties_API/Repository/Properties/IPropertyRepository.cs 查看文件

12
         List<PropertyDisplay> GetDisplay(PropertySearch search);        
12
         List<PropertyDisplay> GetDisplay(PropertySearch search);        
13
         List<PropertyDisplay> GetLatestDisplay();
13
         List<PropertyDisplay> GetLatestDisplay();
14
         List<PropertyType> GetPropertyTypes(Func<PropertyType, bool> where);
14
         List<PropertyType> GetPropertyTypes(Func<PropertyType, bool> where);
15
-        List<PropertyList> GetPropertyList(Func<Property, bool> where);
15
+        List<PropertyList> GetPropertyList(string Type, int By);
16
     }
16
     }
17
 }
17
 }

+ 12
- 1
UnivateProperties_API/Repository/Properties/PropertyImageRepository.cs 查看文件

2
 using System;
2
 using System;
3
 using System.Collections.Generic;
3
 using System.Collections.Generic;
4
 using System.Linq;
4
 using System.Linq;
5
+using UnivateProperties_API.Containers.Property;
5
 using UnivateProperties_API.Context;
6
 using UnivateProperties_API.Context;
6
 using UnivateProperties_API.Model.Properties;
7
 using UnivateProperties_API.Model.Properties;
7
 
8
 
41
                           where p.PropertyId == PropertyId
42
                           where p.PropertyId == PropertyId
42
                           select p.Image).ToList();
43
                           select p.Image).ToList();
43
 
44
 
44
-            return images;
45
+            List<string> formated = new List<string>();
46
+
47
+            foreach (string img in images)
48
+            {
49
+                if (!img.StartsWith("data:image"))
50
+                    formated.Add(ImageFormatter.ImageToBase64(img));
51
+                else
52
+                    formated.Add(img);
53
+            }
54
+
55
+            return formated;
45
         }
56
         }
46
 
57
 
47
         public void Insert(PropertyImage item)
58
         public void Insert(PropertyImage item)

+ 49
- 5
UnivateProperties_API/Repository/Properties/PropertyRepository.cs 查看文件

1
 using Microsoft.EntityFrameworkCore;
1
 using Microsoft.EntityFrameworkCore;
2
 using Newtonsoft.Json;
2
 using Newtonsoft.Json;
3
 using System;
3
 using System;
4
+using System.IO;
4
 using System.Collections.Generic;
5
 using System.Collections.Generic;
5
 using System.Linq;
6
 using System.Linq;
6
 using UnivateProperties_API.Containers.Property;
7
 using UnivateProperties_API.Containers.Property;
8
 using UnivateProperties_API.Context;
9
 using UnivateProperties_API.Context;
9
 using UnivateProperties_API.Model.Logging;
10
 using UnivateProperties_API.Model.Logging;
10
 using UnivateProperties_API.Model.Properties;
11
 using UnivateProperties_API.Model.Properties;
12
+using System.Drawing;
11
 
13
 
12
 namespace UnivateProperties_API.Repository.Properties
14
 namespace UnivateProperties_API.Repository.Properties
13
 {
15
 {
161
                               orderby p.Id descending
163
                               orderby p.Id descending
162
                               select p.Id).FirstOrDefault();
164
                               select p.Id).FirstOrDefault();
163
 
165
 
166
+                bool saveFiles = false;
167
+                var loc = dBContext.Location.FirstOrDefault().PropertyImageLocation;
168
+                if (!string.IsNullOrEmpty(loc))
169
+                {
170
+                    saveFiles = true;
171
+                    loc += string.Format("\\{0}", item.Id);
172
+                    if (Directory.Exists(loc))
173
+                    {
174
+                        Directory.CreateDirectory(loc);
175
+                    }
176
+                }
177
+                
164
                 foreach (PropertyImage image in images)
178
                 foreach (PropertyImage image in images)
165
                 {
179
                 {
166
                     lastID++;
180
                     lastID++;
167
                     image.Id = lastID;
181
                     image.Id = lastID;
168
                     image.PropertyId = item.Id;
182
                     image.PropertyId = item.Id;
183
+                    if (saveFiles)
184
+                    {
185
+                        string path = ImageFormatter.Base64ToImage(image.Image, loc, lastID.ToString());
186
+                        image.Image = path;
187
+                    }
169
                     dBContext.PropertyImages.Add(image);
188
                     dBContext.PropertyImages.Add(image);
170
                     Save();
189
                     Save();
171
                 }
190
                 }
258
                 Type = "Property"
277
                 Type = "Property"
259
             };
278
             };
260
 
279
 
261
-            if (!string.IsNullOrEmpty(search.Keyword) && search.Keyword.ToUpper() != "ALL")
280
+            if (!string.IsNullOrEmpty(search.Keyword) && search.Keyword.ToUpper() != "ALL" && search.Keyword.ToUpper() != "UNDEFINED")
262
             {
281
             {
263
                 string keyword = search.Keyword.ToLower();
282
                 string keyword = search.Keyword.ToLower();
264
 
283
 
442
                                          select p.UsageType.ToString()).FirstOrDefault()
461
                                          select p.UsageType.ToString()).FirstOrDefault()
443
                 };
462
                 };
444
 
463
 
464
+                if (!display.DisplayImage.StartsWith("data:image"))
465
+                {
466
+                    display.DisplayImage = ImageFormatter.ImageToBase64(display.DisplayImage);
467
+                }
468
+
445
                 if (!string.IsNullOrEmpty(display.Area) && display.Area.EndsWith("2"))
469
                 if (!string.IsNullOrEmpty(display.Area) && display.Area.EndsWith("2"))
446
                 {
470
                 {
447
                     display.Area = display.Area.Substring(0, display.Area.Length - 1) + "<sup>" + display.Area.Last() + "</sup>";
471
                     display.Area = display.Area.Substring(0, display.Area.Length - 1) + "<sup>" + display.Area.Last() + "</sup>";
470
             return GetDisplayDetails(props);
494
             return GetDisplayDetails(props);
471
         }
495
         }
472
 
496
 
473
-        public List<PropertyList> GetPropertyList(Func<Property, bool> where)
497
+        public List<PropertyList> GetPropertyList(string Type, int By)
474
         {
498
         {
475
-            var properties = Get(where);
476
-
499
+            var individual = dBContext.Individuals.Where(x => x.UserId == By).FirstOrDefault();
500
+            var agent = dBContext.Agents.Where(x => x.UserId == By).FirstOrDefault();
501
+
502
+            List<Property> properties = new List<Property>();
503
+            if (Type.ToUpper() == "MY")
504
+            {                
505
+                if (individual != null)
506
+                    properties = Get(x => x.OwnerId == individual.Id);
507
+                if (agent != null)
508
+                    properties = Get(x => x.AgentId == agent.Id);
509
+            }
510
+            else
511
+            {
512
+                if (individual != null)
513
+                    properties = Get(x => x.OwnerId == individual.Id);
514
+                if (agent != null)
515
+                    properties = Get(x => x.AgencyId == agent.AgencyId);
516
+            }
517
+            
477
             List<PropertyList> list = new List<PropertyList>();
518
             List<PropertyList> list = new List<PropertyList>();
478
 
519
 
479
             foreach (Property p in properties)
520
             foreach (Property p in properties)
498
                     prop.Size = prop.Size.Substring(0, prop.Size.Length - 1) + "<sup>" + prop.Size.Last() + "</sup>";
539
                     prop.Size = prop.Size.Substring(0, prop.Size.Length - 1) + "<sup>" + prop.Size.Last() + "</sup>";
499
                 }
540
                 }
500
 
541
 
542
+                prop.UsageType = (dBContext.PropertyTypes.Find(p.PropertyTypeId).UsageType == PropertyUsageType.Residential ? "Residential" : "Commercial");
543
+                prop.SaleType = p.IsSale ? "Sale" : "Rental";
544
+
501
                 list.Add(prop);
545
                 list.Add(prop);
502
             }
546
             }
503
                 
547
                 
508
         {
552
         {
509
             // Not sure if properties need it
553
             // Not sure if properties need it
510
             return 0;
554
             return 0;
511
-        }
555
+        }       
512
     }
556
     }
513
 }
557
 }

+ 1
- 0
UnivateProperties_API/UnivateProperties_API.csproj 查看文件

18
     <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.4" />
18
     <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.4" />
19
     <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />
19
     <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />
20
     <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.2.4" />
20
     <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.2.4" />
21
+    <PackageReference Include="System.Drawing.Common" Version="4.6.0" />
21
   </ItemGroup>
22
   </ItemGroup>
22
 
23
 
23
 </Project>
24
 </Project>

Loading…
取消
儲存