Kobus пре 5 година
родитељ
комит
4b23626a6c

+ 59
- 0
UnivateProperties_API/Containers/Property/ImageFormatter.cs Прегледај датотеку

@@ -0,0 +1,59 @@
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,7 +7,9 @@
7 7
         public int Id { get; set; }
8 8
         public string Size { get; set; }
9 9
         public string Price { get; set; }
10
+        public string UsageType { get; set;  }
10 11
         public string Type { get; set; }
12
+        public string SaleType { get; set; }
11 13
         public string Publish { get; set; }
12 14
         public string Status { get; set; }
13 15
         #endregion

+ 5
- 1
UnivateProperties_API/Context/DataContext.cs Прегледај датотеку

@@ -68,7 +68,11 @@ namespace UnivateProperties_API.Context
68 68
 
69 69
         #region Logs
70 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 77
         public override int SaveChanges()
74 78
         {

+ 3
- 35
UnivateProperties_API/Controllers/Properties/PropertyController.cs Прегледај датотеку

@@ -39,43 +39,11 @@ namespace UnivateProperties_API.Controllers.Properties
39 39
         }
40 40
 
41 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 47
         #endregion
80 48
 
81 49
         [HttpGet("search")]

+ 8
- 0
UnivateProperties_API/Model/Misc/Location.cs Прегледај датотеку

@@ -0,0 +1,8 @@
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,6 +12,6 @@ namespace UnivateProperties_API.Repository.Properties
12 12
         List<PropertyDisplay> GetDisplay(PropertySearch search);        
13 13
         List<PropertyDisplay> GetLatestDisplay();
14 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,6 +2,7 @@
2 2
 using System;
3 3
 using System.Collections.Generic;
4 4
 using System.Linq;
5
+using UnivateProperties_API.Containers.Property;
5 6
 using UnivateProperties_API.Context;
6 7
 using UnivateProperties_API.Model.Properties;
7 8
 
@@ -41,7 +42,17 @@ namespace UnivateProperties_API.Repository.Properties
41 42
                           where p.PropertyId == PropertyId
42 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 58
         public void Insert(PropertyImage item)

+ 49
- 5
UnivateProperties_API/Repository/Properties/PropertyRepository.cs Прегледај датотеку

@@ -1,6 +1,7 @@
1 1
 using Microsoft.EntityFrameworkCore;
2 2
 using Newtonsoft.Json;
3 3
 using System;
4
+using System.IO;
4 5
 using System.Collections.Generic;
5 6
 using System.Linq;
6 7
 using UnivateProperties_API.Containers.Property;
@@ -8,6 +9,7 @@ using UnivateProperties_API.Containers.Timeshare;
8 9
 using UnivateProperties_API.Context;
9 10
 using UnivateProperties_API.Model.Logging;
10 11
 using UnivateProperties_API.Model.Properties;
12
+using System.Drawing;
11 13
 
12 14
 namespace UnivateProperties_API.Repository.Properties
13 15
 {
@@ -161,11 +163,28 @@ namespace UnivateProperties_API.Repository.Properties
161 163
                               orderby p.Id descending
162 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 178
                 foreach (PropertyImage image in images)
165 179
                 {
166 180
                     lastID++;
167 181
                     image.Id = lastID;
168 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 188
                     dBContext.PropertyImages.Add(image);
170 189
                     Save();
171 190
                 }
@@ -258,7 +277,7 @@ namespace UnivateProperties_API.Repository.Properties
258 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 282
                 string keyword = search.Keyword.ToLower();
264 283
 
@@ -442,6 +461,11 @@ namespace UnivateProperties_API.Repository.Properties
442 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 469
                 if (!string.IsNullOrEmpty(display.Area) && display.Area.EndsWith("2"))
446 470
                 {
447 471
                     display.Area = display.Area.Substring(0, display.Area.Length - 1) + "<sup>" + display.Area.Last() + "</sup>";
@@ -470,10 +494,27 @@ namespace UnivateProperties_API.Repository.Properties
470 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 518
             List<PropertyList> list = new List<PropertyList>();
478 519
 
479 520
             foreach (Property p in properties)
@@ -498,6 +539,9 @@ namespace UnivateProperties_API.Repository.Properties
498 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 545
                 list.Add(prop);
502 546
             }
503 547
                 
@@ -508,6 +552,6 @@ namespace UnivateProperties_API.Repository.Properties
508 552
         {
509 553
             // Not sure if properties need it
510 554
             return 0;
511
-        }
555
+        }       
512 556
     }
513 557
 }

+ 1
- 0
UnivateProperties_API/UnivateProperties_API.csproj Прегледај датотеку

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

Loading…
Откажи
Сачувај