Browse Source

Changes to images to store to file, return as URLs

master
GJWilliams87 4 years ago
parent
commit
adcc282303

+ 19
- 0
UnivateProperties_API/Containers/Property/ImageFormatter.cs View File

60
             else
60
             else
61
                 return "";
61
                 return "";
62
         }
62
         }
63
+
64
+        public static string ImageToURL(string Path, string URL)
65
+        {
66
+            string url = URL;
67
+
68
+            string[] folders = Path.Split(new char[] { '\\' });
69
+            bool startAdd = false;
70
+            foreach(var folder in folders)
71
+            {
72
+                if (!startAdd && folder == "img")
73
+                    startAdd = true;
74
+
75
+                if (startAdd)
76
+                    url += folder + "/";
77
+            }
78
+
79
+
80
+            return url.Substring(0, url.Length - 1);
81
+        }
63
     }
82
     }
64
 }
83
 }

+ 24
- 20
UnivateProperties_API/Context/DataContext.cs View File

1
 using Microsoft.EntityFrameworkCore;
1
 using Microsoft.EntityFrameworkCore;
2
-using Npgsql;
2
+using System.Data.SqlClient;
3
 using System.Linq;
3
 using System.Linq;
4
 using UnivateProperties_API.Model;
4
 using UnivateProperties_API.Model;
5
 using UnivateProperties_API.Model.Banks;
5
 using UnivateProperties_API.Model.Banks;
27
                 {
27
                 {
28
                     connectionString = ((Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure.Internal.NpgsqlOptionsExtension)extention).ConnectionString;
28
                     connectionString = ((Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure.Internal.NpgsqlOptionsExtension)extention).ConnectionString;
29
                 }
29
                 }
30
+                else if (extention.GetType().ToString() == "Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal.SqlServerOptionsExtension")
31
+                {
32
+                    connectionString = ((Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal.SqlServerOptionsExtension)extention).ConnectionString;
33
+                }
30
             }
34
             }
31
         }
35
         }
32
 
36
 
207
             modelBuilder.Entity<PlaceHolderFormat>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
211
             modelBuilder.Entity<PlaceHolderFormat>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
208
         }
212
         }
209
 
213
 
210
-    
211
-        //public int GetMaxID(string tableName)
212
-        //{
213
-        //    NpgsqlConnection connection = new NpgsqlConnection(connectionString);
214
-        //    connection.Open();
215
-
216
-        //    NpgsqlCommand cmd = connection.CreateCommand();
217
-        //    cmd.CommandText = string.Format("select MAX(\"Id\") from \"{0}\"", tableName);
218
-        //    NpgsqlDataReader reader = cmd.ExecuteReader();
219
-        //    int returnValue = 0; 
220
-
221
-        //    while(reader.Read())
222
-        //    {
223
-        //        returnValue = int.Parse(reader[0] == null ? "0" : reader[0].ToString() == "" ? "0" : reader[0].ToString());
224
-        //    }
225
-
226
-        //    connection.Close();
227
-        //    return returnValue;
228
-        //}
214
+
215
+        public int GetMaxID(string tableName)
216
+        {
217
+            SqlConnection connection = new SqlConnection(connectionString);
218
+            connection.Open();
219
+
220
+            SqlCommand cmd = connection.CreateCommand();
221
+            cmd.CommandText = string.Format("select MAX(\"Id\") from \"{0}\"", tableName);
222
+            SqlDataReader reader = cmd.ExecuteReader();
223
+            int returnValue = 0;
224
+
225
+            while (reader.Read())
226
+            {
227
+                returnValue = int.Parse(reader[0] == null ? "0" : reader[0].ToString() == "" ? "0" : reader[0].ToString());
228
+            }
229
+
230
+            connection.Close();
231
+            return returnValue;
232
+        }
229
     }
233
     }
230
 }
234
 }

+ 7
- 0
UnivateProperties_API/Controllers/Properties/PropertyImageController.cs View File

41
             return new OkObjectResult(_Repo.Get(x => x.PropertyId == PropertyId));
41
             return new OkObjectResult(_Repo.Get(x => x.PropertyId == PropertyId));
42
         }
42
         }
43
 
43
 
44
+        [HttpGet("SavePropImages")]
45
+        public IActionResult SavePropImages()
46
+        {
47
+            _Repo.UpdateToPhysical();
48
+            return new OkResult();
49
+        }
50
+
44
         [HttpPost]
51
         [HttpPost]
45
         public IActionResult Post([FromBody] NewPropertyImages propertyImage)
52
         public IActionResult Post([FromBody] NewPropertyImages propertyImage)
46
         {
53
         {

+ 1694
- 0
UnivateProperties_API/Migrations/20201110092829_URL added to location.Designer.cs
File diff suppressed because it is too large
View File


+ 22
- 0
UnivateProperties_API/Migrations/20201110092829_URL added to location.cs View File

1
+using Microsoft.EntityFrameworkCore.Migrations;
2
+
3
+namespace UnivateProperties_API.Migrations
4
+{
5
+    public partial class URLaddedtolocation : Migration
6
+    {
7
+        protected override void Up(MigrationBuilder migrationBuilder)
8
+        {
9
+            migrationBuilder.AddColumn<string>(
10
+                name: "SiteURL",
11
+                table: "Location",
12
+                nullable: true);
13
+        }
14
+
15
+        protected override void Down(MigrationBuilder migrationBuilder)
16
+        {
17
+            migrationBuilder.DropColumn(
18
+                name: "SiteURL",
19
+                table: "Location");
20
+        }
21
+    }
22
+}

+ 2
- 0
UnivateProperties_API/Migrations/DataContextModelSnapshot.cs View File

556
 
556
 
557
                     b.Property<string>("PropertyImageLocation");
557
                     b.Property<string>("PropertyImageLocation");
558
 
558
 
559
+                    b.Property<string>("SiteURL");
560
+
559
                     b.HasKey("Id");
561
                     b.HasKey("Id");
560
 
562
 
561
                     b.ToTable("Location");
563
                     b.ToTable("Location");

+ 1
- 0
UnivateProperties_API/Model/Misc/Location.cs View File

4
     {
4
     {
5
         public bool IsTesting { get; set; }
5
         public bool IsTesting { get; set; }
6
         public string PropertyImageLocation { get; set; }
6
         public string PropertyImageLocation { get; set; }
7
+        public string SiteURL { get; set; }
7
     }
8
     }
8
 }
9
 }

+ 1
- 0
UnivateProperties_API/Repository/Properties/IPropertyImageRepository.cs View File

8
     {
8
     {
9
         List<string> GetImages(int PropertyId);
9
         List<string> GetImages(int PropertyId);
10
         void Update(NewPropertyImages propertyImages);
10
         void Update(NewPropertyImages propertyImages);
11
+        void UpdateToPhysical();
11
     }
12
     }
12
 }
13
 }

+ 53
- 3
UnivateProperties_API/Repository/Properties/PropertyImageRepository.cs View File

21
         {
21
         {
22
             var images = dBContext.PropertyImages.Where(where).OrderByDescending(i => i.IsDefault).ThenBy(i => i.Id).ToList();
22
             var images = dBContext.PropertyImages.Where(where).OrderByDescending(i => i.IsDefault).ThenBy(i => i.Id).ToList();
23
 
23
 
24
+            var url = dBContext.Location.FirstOrDefault()?.SiteURL;
25
+
24
             foreach (PropertyImage img in images)
26
             foreach (PropertyImage img in images)
25
             {
27
             {
26
                 if (!img.Image.StartsWith("data:image"))
28
                 if (!img.Image.StartsWith("data:image"))
27
-                    img.Image = ImageFormatter.ImageToBase64(img.Image);                
29
+                {
30
+                    if (!string.IsNullOrEmpty(url))
31
+                        img.Image = ImageFormatter.ImageToURL(img.Image, url);
32
+                    else
33
+                        img.Image = ImageFormatter.ImageToBase64(img.Image);
34
+                }
28
             }
35
             }
29
 
36
 
30
             return images;
37
             return images;
54
 
61
 
55
             List<string> formated = new List<string>();
62
             List<string> formated = new List<string>();
56
 
63
 
64
+            var url = dBContext.Location.FirstOrDefault()?.SiteURL;
65
+
57
             foreach (string img in images)
66
             foreach (string img in images)
58
             {
67
             {
59
                 if (!img.StartsWith("data:image"))
68
                 if (!img.StartsWith("data:image"))
60
-                    formated.Add(ImageFormatter.ImageToBase64(img));
69
+                {
70
+                    if (!string.IsNullOrEmpty(url))
71
+                        formated.Add(ImageFormatter.ImageToURL(img, url));
72
+                    else
73
+                        formated.Add(ImageFormatter.ImageToBase64(img));
74
+                }
61
                 else
75
                 else
62
                     formated.Add(img);
76
                     formated.Add(img);
63
             }
77
             }
131
                 if (!string.IsNullOrEmpty(loc))
145
                 if (!string.IsNullOrEmpty(loc))
132
                 {
146
                 {
133
                     saveFiles = true;
147
                     saveFiles = true;
148
+                    if (loc.EndsWith("\\"))
149
+                    {
150
+                        loc = loc.Substring(0, loc.Length - 1);
151
+                    }
134
                     loc += string.Format("\\{0}", propertyImages.PropertyId);
152
                     loc += string.Format("\\{0}", propertyImages.PropertyId);
135
-                    if (Directory.Exists(loc))
153
+                    if (!Directory.Exists(loc))
136
                     {
154
                     {
137
                         Directory.CreateDirectory(loc);
155
                         Directory.CreateDirectory(loc);
138
                     }
156
                     }
162
                 Save();
180
                 Save();
163
             }
181
             }
164
         }
182
         }
183
+
184
+        public void UpdateToPhysical()
185
+        {            
186
+            bool saveFiles = false;
187
+            var loc = dBContext.Location.FirstOrDefault()?.PropertyImageLocation;
188
+            if (!string.IsNullOrEmpty(loc))
189
+            {
190
+                saveFiles = true;
191
+                if (loc.EndsWith("\\"))
192
+                {
193
+                    loc = loc.Substring(0, loc.Length - 1);
194
+                }
195
+            }
196
+
197
+            if (saveFiles)
198
+            {
199
+                var images = dBContext.PropertyImages.ToList();
200
+                foreach (var image in images)
201
+                {
202
+                    string filePath = string.Format("{0}\\{1}", loc, image.PropertyId );
203
+                    if (!Directory.Exists(filePath))
204
+                    {
205
+                        Directory.CreateDirectory(filePath);
206
+                    }
207
+
208
+                    string path = ImageFormatter.Base64ToImage(image.Image, filePath, image.Id.ToString());
209
+                    image.Image = path;
210
+                }
211
+
212
+                Save();
213
+            }
214
+        }
165
     }
215
     }
166
 }
216
 }

+ 48
- 36
UnivateProperties_API/Repository/Properties/PropertyRepository.cs View File

27
         public List<Property> Get(Func<Property, bool> where)
27
         public List<Property> Get(Func<Property, bool> where)
28
         {
28
         {
29
             return dBContext.Properties
29
             return dBContext.Properties
30
-                .Include("PropertyType")                
30
+                .Include("PropertyType")
31
                 .Where(where).ToList();
31
                 .Where(where).ToList();
32
         }
32
         }
33
 
33
 
39
 
39
 
40
         public Property GetDetailed(Func<Property, bool> first)
40
         public Property GetDetailed(Func<Property, bool> first)
41
         {
41
         {
42
-            throw new NotImplementedException();            
42
+            throw new NotImplementedException();
43
         }
43
         }
44
 
44
 
45
         public PropertyContainer GetDetailed(int id, bool detailed)
45
         public PropertyContainer GetDetailed(int id, bool detailed)
72
         }
72
         }
73
 
73
 
74
         private PropertyContainer GetDetail(Property property, bool detailed)
74
         private PropertyContainer GetDetail(Property property, bool detailed)
75
-        {            
75
+        {
76
             int propID = property.Id;
76
             int propID = property.Id;
77
-            var propertyType = dBContext.PropertyTypes.Find(property.PropertyTypeId);           
78
-            property.DisplayData = new List<PropertyDetailGroup>();            
77
+            var propertyType = dBContext.PropertyTypes.Find(property.PropertyTypeId);
78
+            property.DisplayData = new List<PropertyDetailGroup>();
79
 
79
 
80
             if (detailed)
80
             if (detailed)
81
             {
81
             {
236
             {
236
             {
237
                 if (prop != "Item" && prop != "Display")
237
                 if (prop != "Item" && prop != "Display")
238
                     property[prop] = item[prop];
238
                     property[prop] = item[prop];
239
-            }            
239
+            }
240
 
240
 
241
             property.PropertyUserFields = null;
241
             property.PropertyUserFields = null;
242
             property.PropertyImages = null;
242
             property.PropertyImages = null;
244
             if (item.Price < item.OldPrice)
244
             if (item.Price < item.OldPrice)
245
             {
245
             {
246
                 property.PriceRedused = true;
246
                 property.PriceRedused = true;
247
-            }            
247
+            }
248
 
248
 
249
             if (!string.IsNullOrEmpty(item.StatusString))
249
             if (!string.IsNullOrEmpty(item.StatusString))
250
             {
250
             {
252
                     property.StatusDate = DateTime.Now;
252
                     property.StatusDate = DateTime.Now;
253
 
253
 
254
                 property.StatusId = dBContext.Status.Where(s => s.Description == item.StatusString && s.StatusType == StatusType.Property).FirstOrDefault()?.Id;
254
                 property.StatusId = dBContext.Status.Where(s => s.Description == item.StatusString && s.StatusType == StatusType.Property).FirstOrDefault()?.Id;
255
-                if ((item.StatusString.ToUpper() == "RENTED OUT" || item.StatusString.ToUpper() == "SOLD") && item.StatusString != item.OldStatus)  
255
+                if ((item.StatusString.ToUpper() == "RENTED OUT" || item.StatusString.ToUpper() == "SOLD") && item.StatusString != item.OldStatus)
256
                 {
256
                 {
257
                     property.CutOffDisplayDate = DateTime.Now.AddMonths(1);
257
                     property.CutOffDisplayDate = DateTime.Now.AddMonths(1);
258
                 }
258
                 }
300
                             dBContext.Entry(propField).State = EntityState.Modified;
300
                             dBContext.Entry(propField).State = EntityState.Modified;
301
                             Save();
301
                             Save();
302
                         }
302
                         }
303
-                    }                    
303
+                    }
304
                 }
304
                 }
305
             }
305
             }
306
             #endregion
306
             #endregion
329
             }
329
             }
330
 
330
 
331
             if (item.NewImages != null)
331
             if (item.NewImages != null)
332
-            {                
332
+            {
333
                 bool saveFiles = false;
333
                 bool saveFiles = false;
334
                 var loc = dBContext.Location.FirstOrDefault()?.PropertyImageLocation;
334
                 var loc = dBContext.Location.FirstOrDefault()?.PropertyImageLocation;
335
                 if (!string.IsNullOrEmpty(loc))
335
                 if (!string.IsNullOrEmpty(loc))
336
                 {
336
                 {
337
                     saveFiles = true;
337
                     saveFiles = true;
338
+                    if (loc.EndsWith("\\"))
339
+                    {
340
+                        loc = loc.Substring(0, loc.Length - 1);
341
+                    }
338
                     loc += string.Format("\\{0}", property.Id);
342
                     loc += string.Format("\\{0}", property.Id);
339
-                    if (Directory.Exists(loc))
343
+                    if (!Directory.Exists(loc))
340
                     {
344
                     {
341
                         Directory.CreateDirectory(loc);
345
                         Directory.CreateDirectory(loc);
342
                     }
346
                     }
343
                 }
347
                 }
344
 
348
 
345
-                             
349
+
346
                 var lastID = dBContext.PropertyImages.Max(i => i.Id) + 1;
350
                 var lastID = dBContext.PropertyImages.Max(i => i.Id) + 1;
347
                 foreach (var image in item.NewImages)
351
                 foreach (var image in item.NewImages)
348
                 {
352
                 {
353
+                    if (!image.Image.StartsWith("data:image")) 
354
+                        continue;
355
+
349
                     var propImage = new PropertyImage
356
                     var propImage = new PropertyImage
350
                     {
357
                     {
351
                         PropertyId = property.Id,
358
                         PropertyId = property.Id,
366
                         dBContext.PropertyImages.Add(propImage);
373
                         dBContext.PropertyImages.Add(propImage);
367
                         Save();
374
                         Save();
368
                         lastID++;
375
                         lastID++;
369
-                    }                                        
376
+                    }
370
                 }
377
                 }
371
             }
378
             }
372
 
379
 
391
 
398
 
392
             return GetDisplayDetails(props);
399
             return GetDisplayDetails(props);
393
         }
400
         }
394
-        
401
+
395
         public List<PropertyDisplay> GetDisplay(PropertySearch search)
402
         public List<PropertyDisplay> GetDisplay(PropertySearch search)
396
         {
403
         {
397
             //return GetDisplayDetails(dBContext.Properties.ToList());
404
             //return GetDisplayDetails(dBContext.Properties.ToList());
410
             {
417
             {
411
                 string keyword = search.Keyword.ToLower();
418
                 string keyword = search.Keyword.ToLower();
412
 
419
 
413
-                List<Property> props = (from p in dBContext.Properties                                       
420
+                List<Property> props = (from p in dBContext.Properties
414
                                         join pt in dBContext.PropertyTypes on p.PropertyTypeId equals pt.Id
421
                                         join pt in dBContext.PropertyTypes on p.PropertyTypeId equals pt.Id
415
                                         where EF.Functions.Like(p.PropertyName.ToLower(), $"%{keyword}%")
422
                                         where EF.Functions.Like(p.PropertyName.ToLower(), $"%{keyword}%")
416
                                         || EF.Functions.Like(p.Province.ToLower(), $"%{keyword}%")
423
                                         || EF.Functions.Like(p.Province.ToLower(), $"%{keyword}%")
474
 
481
 
475
                 if (!string.IsNullOrEmpty(search.Province) && search.Province != "undefined" && search.Province.ToUpper() != "ALL")
482
                 if (!string.IsNullOrEmpty(search.Province) && search.Province != "undefined" && search.Province.ToUpper() != "ALL")
476
                 {
483
                 {
477
-                    props = (from p in props                            
484
+                    props = (from p in props
478
                              where p.Province.ToUpper() == search.Province.ToUpper()
485
                              where p.Province.ToUpper() == search.Province.ToUpper()
479
                              select p).ToList();
486
                              select p).ToList();
480
 
487
 
485
 
492
 
486
                 if (!string.IsNullOrEmpty(search.City) && search.City != "undefined" && search.City.ToUpper() != "ALL")
493
                 if (!string.IsNullOrEmpty(search.City) && search.City != "undefined" && search.City.ToUpper() != "ALL")
487
                 {
494
                 {
488
-                    props = (from p in props                             
495
+                    props = (from p in props
489
                              where p.City.ToUpper() == search.City.ToUpper()
496
                              where p.City.ToUpper() == search.City.ToUpper()
490
                              select p).ToList();
497
                              select p).ToList();
491
 
498
 
496
 
503
 
497
                 if (!string.IsNullOrEmpty(search.Suburb) && search.Suburb != "undefined" && search.Suburb.ToUpper() != "ALL")
504
                 if (!string.IsNullOrEmpty(search.Suburb) && search.Suburb != "undefined" && search.Suburb.ToUpper() != "ALL")
498
                 {
505
                 {
499
-                    props = (from p in props                             
506
+                    props = (from p in props
500
                              where p.Suburb.ToUpper() == search.Suburb.ToUpper()
507
                              where p.Suburb.ToUpper() == search.Suburb.ToUpper()
501
                              select p).ToList();
508
                              select p).ToList();
502
 
509
 
544
 
551
 
545
                 return GetDisplayDetails(props);
552
                 return GetDisplayDetails(props);
546
             }
553
             }
547
-        }        
554
+        }
548
 
555
 
549
         private void SaveLog(SearchObject item)
556
         private void SaveLog(SearchObject item)
550
         {
557
         {
552
             {
559
             {
553
                 Type = item.Type,
560
                 Type = item.Type,
554
                 Search = JsonConvert.SerializeObject(item)
561
                 Search = JsonConvert.SerializeObject(item)
555
-            };            
562
+            };
556
             dBContext.SearchLogs.Add(searchLog);
563
             dBContext.SearchLogs.Add(searchLog);
557
             Save();
564
             Save();
558
         }
565
         }
566
                 PropertyDisplay display = new PropertyDisplay
573
                 PropertyDisplay display = new PropertyDisplay
567
                 {
574
                 {
568
                     PropertyReference = item.PropertyRef,
575
                     PropertyReference = item.PropertyRef,
569
-                    DateAvailable = item.DateAvailable,                    
576
+                    DateAvailable = item.DateAvailable,
570
                     Id = item.Id,
577
                     Id = item.Id,
571
                     ShortDescription = item.ShortDescription,
578
                     ShortDescription = item.ShortDescription,
572
                     IsSale = item.IsSale,
579
                     IsSale = item.IsSale,
686
                     break;
693
                     break;
687
             }
694
             }
688
 
695
 
689
-            List<Property> props = dBContext.Properties.Include("PropertyType").Where(x => x.Published 
690
-                        && x.PropertyType.UsageType == type 
696
+            List<Property> props = dBContext.Properties.Include("PropertyType").Where(x => x.Published
697
+                        && x.PropertyType.UsageType == type
691
                         && (x.CutOffDisplayDate == DateTime.MinValue || x.CutOffDisplayDate.Date > DateTime.Now.Date)).OrderByDescending(x => x.DatePublished).Take(4).ToList();
698
                         && (x.CutOffDisplayDate == DateTime.MinValue || x.CutOffDisplayDate.Date > DateTime.Now.Date)).OrderByDescending(x => x.DatePublished).Take(4).ToList();
692
             return GetDisplayDetails(props);
699
             return GetDisplayDetails(props);
693
         }
700
         }
724
         }
731
         }
725
 
732
 
726
         public List<PropertyList> GetPropertyList()
733
         public List<PropertyList> GetPropertyList()
727
-        {           
734
+        {
728
             return SetPropertyList(dBContext.Properties.Where(x => x.Published).ToList());
735
             return SetPropertyList(dBContext.Properties.Where(x => x.Published).ToList());
729
         }
736
         }
730
 
737
 
774
 
781
 
775
         public void Insert(PropertyContainer items)
782
         public void Insert(PropertyContainer items)
776
         {
783
         {
777
-            Property property = new Property();            
784
+            Property property = new Property();
778
             PropertyType pt = dBContext.PropertyTypes.Find(items.PropertyTypeId);
785
             PropertyType pt = dBContext.PropertyTypes.Find(items.PropertyTypeId);
779
             if (pt != null)
786
             if (pt != null)
780
             {
787
             {
809
             items.PropertyUserFields = null;
816
             items.PropertyUserFields = null;
810
 
817
 
811
             var individual = dBContext.Individuals.Where(i => i.UserId == items.UserId).FirstOrDefault();
818
             var individual = dBContext.Individuals.Where(i => i.UserId == items.UserId).FirstOrDefault();
812
-            var agent = dBContext.Agents.Where(a => a.UserId == items.UserId).FirstOrDefault();            
819
+            var agent = dBContext.Agents.Where(a => a.UserId == items.UserId).FirstOrDefault();
813
 
820
 
814
-            foreach( string prop in property.GetAllProperties())
821
+            foreach (string prop in property.GetAllProperties())
815
             {
822
             {
816
                 if (prop != "Item" && prop != "Display")
823
                 if (prop != "Item" && prop != "Display")
817
                     property[prop] = items[prop];
824
                     property[prop] = items[prop];
823
             {
830
             {
824
                 property.AgencyId = agent.AgencyId;
831
                 property.AgencyId = agent.AgencyId;
825
                 property.AgentId = agent.Id;
832
                 property.AgentId = agent.Id;
826
-            }            
833
+            }
827
 
834
 
828
-            if (property.IsSale)            
835
+            if (property.IsSale)
829
                 property.StatusId = dBContext.Status.Where(s => s.Description == "For Sale" && s.StatusType == StatusType.Property).FirstOrDefault()?.Id;
836
                 property.StatusId = dBContext.Status.Where(s => s.Description == "For Sale" && s.StatusType == StatusType.Property).FirstOrDefault()?.Id;
830
             else
837
             else
831
                 property.StatusId = dBContext.Status.Where(s => s.Description == "For Rent" && s.StatusType == StatusType.Property).FirstOrDefault()?.Id;
838
                 property.StatusId = dBContext.Status.Where(s => s.Description == "For Rent" && s.StatusType == StatusType.Property).FirstOrDefault()?.Id;
836
             if (images != null)
843
             if (images != null)
837
             {
844
             {
838
                 bool saveFiles = false;
845
                 bool saveFiles = false;
846
+                var nextPropID = dBContext.GetMaxID("Properties") + 1;
839
                 var loc = dBContext.Location.FirstOrDefault()?.PropertyImageLocation;
847
                 var loc = dBContext.Location.FirstOrDefault()?.PropertyImageLocation;
840
                 if (!string.IsNullOrEmpty(loc))
848
                 if (!string.IsNullOrEmpty(loc))
841
                 {
849
                 {
842
                     saveFiles = true;
850
                     saveFiles = true;
843
-                    loc += string.Format("\\{0}", property.Id);
844
-                    if (Directory.Exists(loc))
851
+                    if (loc.EndsWith("\\"))
852
+                    {
853
+                        loc = loc.Substring(0, loc.Length - 1);
854
+                    }
855
+                    loc += string.Format("\\{0}", nextPropID);
856
+                    if (!Directory.Exists(loc))
845
                     {
857
                     {
846
                         Directory.CreateDirectory(loc);
858
                         Directory.CreateDirectory(loc);
847
                     }
859
                     }
851
                 var lastID = 0;
863
                 var lastID = 0;
852
                 if (dBContext.PropertyImages.Count() == 0)
864
                 if (dBContext.PropertyImages.Count() == 0)
853
                     lastID = 1;
865
                     lastID = 1;
854
-                else 
866
+                else
855
                     lastID = dBContext.PropertyImages.Max(i => i.Id) + 1;
867
                     lastID = dBContext.PropertyImages.Max(i => i.Id) + 1;
856
 
868
 
857
                 foreach (PropertyImage image in images)
869
                 foreach (PropertyImage image in images)
868
             }
880
             }
869
 
881
 
870
             if (fields != null)
882
             if (fields != null)
871
-            {                
883
+            {
872
                 property.PropertyUserFields = new List<PropertyUserField>();
884
                 property.PropertyUserFields = new List<PropertyUserField>();
873
                 foreach (PropertyUserField field in fields)
885
                 foreach (PropertyUserField field in fields)
874
                 {
886
                 {
880
             dBContext.Properties.Add(property);
892
             dBContext.Properties.Add(property);
881
             Save();
893
             Save();
882
 
894
 
883
-            items.Id = property.Id;            
895
+            items.Id = property.Id;
884
         }
896
         }
885
 
897
 
886
         public bool MayEdit(int id)
898
         public bool MayEdit(int id)
912
                 }
924
                 }
913
 
925
 
914
                 foreach (PropertyImage image in Images)
926
                 foreach (PropertyImage image in Images)
915
-                {                                    
927
+                {
916
                     image.PropertyId = propertyID;
928
                     image.PropertyId = propertyID;
917
                     if (saveFiles)
929
                     if (saveFiles)
918
                     {
930
                     {
994
                     IsPublished = prop.Published,
1006
                     IsPublished = prop.Published,
995
                     Type = prop.PropertyType.UsageType.ToString()
1007
                     Type = prop.PropertyType.UsageType.ToString()
996
                 };
1008
                 };
997
-                
1009
+
998
                 if (prop.StatusId != null)
1010
                 if (prop.StatusId != null)
999
                 {
1011
                 {
1000
                     propAdmin.Status = dBContext.Status.Where(s => s.Id == prop.StatusId).FirstOrDefault()?.Description;
1012
                     propAdmin.Status = dBContext.Status.Where(s => s.Id == prop.StatusId).FirstOrDefault()?.Description;

Loading…
Cancel
Save