Переглянути джерело

Template and attributes

master
Kobus 5 роки тому
джерело
коміт
f1933ef0a7

+ 33
- 0
UnivateProperties_API/Containers/Timeshare/WeekDto.cs Переглянути файл

@@ -1,4 +1,7 @@
1 1
 using System;
2
+using System.Collections.Generic;
3
+using System.Linq;
4
+using UnivateProperties_API.Helpers;
2 5
 using UnivateProperties_API.Model.Timeshare;
3 6
 
4 7
 namespace UnivateProperties_API.Containers.Timeshare
@@ -10,6 +13,36 @@ namespace UnivateProperties_API.Containers.Timeshare
10 13
 
11 14
         }
12 15
 
16
+        public WeekDto(string line)
17
+        {
18
+            List<string> split = line.Split(",").ToList();
19
+            AgentAsRep = false;
20
+            Resort = new ResortDto(split[0].Trim(), split[0].Trim());
21
+            UnitNumber = split[1].Trim();
22
+            WeekNumber = split[2].Trim();
23
+            var size = split[3].Trim();
24
+            if(size.Length == 3 && !size.ToLower().StartsWith('s'))
25
+            {
26
+                int.TryParse(size.Substring(0, 1), out int temp);
27
+                Bedrooms = temp;
28
+                int.TryParse(size.Substring(2, 1), out temp);
29
+                MaxSleep = temp;
30
+            }
31
+            bool currentYear = split[9].Trim() == "Y";
32
+            LevyAmount = Convert.ToDouble(split[5].Trim());
33
+            DateTime tempDate = MyCommon.GetDateFromString(currentYear ? split[6].Trim() : split[13].Trim());
34
+            if(tempDate != DateTime.MinValue)
35
+            {
36
+                ArrivalDate = tempDate;
37
+            }
38
+            tempDate = MyCommon.GetDateFromString(currentYear ? split[7].Trim() : split[14].Trim());
39
+            if(tempDate != DateTime.MinValue)
40
+            {
41
+                DepartureDate = tempDate;
42
+            }
43
+            Region = new RegionDto() { RegionCode = split[24] };
44
+        }
45
+
13 46
         public WeekDto(TimeshareWeek week)
14 47
         {
15 48
             Id = week.Id;

+ 2
- 0
UnivateProperties_API/Context/DataContext.cs Переглянути файл

@@ -110,6 +110,8 @@ namespace UnivateProperties_API.Context
110 110
 
111 111
         protected override void OnModelCreating(ModelBuilder modelBuilder)
112 112
         {
113
+            modelBuilder.Entity<Individual>()
114
+                .Ignore(b => b.FullName);
113 115
             modelBuilder.Entity<SMTPHost>().ToTable("Hosts");
114 116
             modelBuilder.Entity<UnitConfiguration>()
115 117
                 .HasIndex(u => u.Code)

+ 11
- 0
UnivateProperties_API/Helpers/Attributes/BaseAttribute.cs Переглянути файл

@@ -0,0 +1,11 @@
1
+using System;
2
+
3
+namespace UnivateProperties_API.Helpers.Attributes
4
+{
5
+    public abstract class BaseAttribute : Attribute
6
+    {
7
+        protected BaseAttribute() { }
8
+
9
+        public abstract object Value { get; }
10
+    }
11
+}

+ 17
- 0
UnivateProperties_API/Helpers/Attributes/VisibleAttribute.cs Переглянути файл

@@ -0,0 +1,17 @@
1
+namespace UnivateProperties_API.Helpers.Attributes
2
+{
3
+    public class VisibleAttribute : BaseAttribute
4
+    {
5
+        public VisibleAttribute(bool value)
6
+        {
7
+            Value = value;
8
+        }
9
+
10
+        public VisibleAttribute()
11
+        {
12
+            Value = true;
13
+        }
14
+
15
+        public override object Value { get; }
16
+    }
17
+}

+ 16
- 0
UnivateProperties_API/Helpers/Attributes/VisibleInListViewAttribute.cs Переглянути файл

@@ -0,0 +1,16 @@
1
+namespace UnivateProperties_API.Helpers.Attributes
2
+{
3
+    public class VisibleInListViewAttribute : VisibleAttribute
4
+    {
5
+        public VisibleInListViewAttribute(bool value) : base(value)
6
+        {
7
+        }
8
+
9
+        public VisibleInListViewAttribute() : base()
10
+        {
11
+
12
+        }
13
+
14
+        public override object Value => base.Value;
15
+    }
16
+}

+ 48
- 0
UnivateProperties_API/Helpers/MyCommon.cs Переглянути файл

@@ -1,10 +1,58 @@
1 1
 using System;
2
+using System.Collections.Generic;
3
+using System.Linq;
2 4
 using System.Text.RegularExpressions;
5
+using UnivateProperties_API.Helpers.Attributes;
3 6
 
4 7
 namespace UnivateProperties_API.Helpers
5 8
 {
6 9
     public static class MyCommon
7 10
     {
11
+        public static List<string> GetVisibleColumns<T>(T item) where T : new()
12
+        {
13
+            List<string> list = new List<string>();
14
+            var type = item.GetType();
15
+            var properties = type.GetProperties();
16
+            var visibleProperties = properties.Where(x => x.GetCustomAttributes(false).Any(a => a.GetType() == typeof(VisibleInListViewAttribute) && ((a as VisibleInListViewAttribute).Value as bool?) == true)).ToList();
17
+            visibleProperties.ForEach(x => list.Add(x.Name));
18
+            return list;
19
+        }
20
+
21
+        public static string TenderUrl { get; set; }
22
+
23
+        public static DateTime GetDateFromString(string value)
24
+        {
25
+            if (CheckDateDayFirst(value))
26
+            {
27
+                return new DateTime(Convert.ToInt32(value.Substring(6, 4)), Convert.ToInt32(value.Substring(3, 2)), Convert.ToInt32(value.Substring(0, 2)));
28
+            }
29
+            else if (CheckDateYearFirst(value))
30
+            {
31
+                return Convert.ToDateTime(value);
32
+            }
33
+            else return DateTime.MinValue;
34
+        }
35
+
36
+        public static bool CheckDateYearFirst(string value)
37
+        {
38
+            if(value.Length >= 10)
39
+            {
40
+                value = value.Substring(0, 10);
41
+            }
42
+            Regex reggie = new Regex(@"^\d{4}-((0[1-9])|(1[012]))-((0[1-9]|[12]\d)|3[01])$");
43
+            return reggie.IsMatch(value);
44
+        }
45
+
46
+        public static bool CheckDateDayFirst(string value)
47
+        {
48
+            if (value.Length >= 10)
49
+            {
50
+                value = value.Substring(0, 10);
51
+            }
52
+            Regex reggie = new Regex(@"^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$");
53
+            return reggie.IsMatch(value);
54
+        }
55
+
8 56
         public static bool IsValidEmail(string item)
9 57
         {
10 58
             if (!string.IsNullOrEmpty(item))

+ 1
- 5
UnivateProperties_API/Model/Communication/PlaceHolder.cs Переглянути файл

@@ -1,13 +1,9 @@
1
-using System.ComponentModel.DataAnnotations;
2
-using System.ComponentModel.DataAnnotations.Schema;
1
+using System.ComponentModel.DataAnnotations.Schema;
3 2
 
4 3
 namespace UnivateProperties_API.Model.Communication
5 4
 {
6 5
     public class PlaceHolder : BaseEntity
7 6
     {
8
-        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
9
-        [Key]
10
-        public int Id { get; set; }
11 7
         public string Name { get; set; }
12 8
         public string BoundToClass { get; set; }
13 9
         public string BoundToClassDisplay { get; set; }

+ 17
- 0
UnivateProperties_API/Model/Users/Person.cs Переглянути файл

@@ -1,5 +1,6 @@
1 1
 using System.ComponentModel.DataAnnotations;
2 2
 using System.ComponentModel.DataAnnotations.Schema;
3
+using UnivateProperties_API.Helpers.Attributes;
3 4
 
4 5
 namespace UnivateProperties_API.Model.Users
5 6
 {
@@ -17,13 +18,29 @@ namespace UnivateProperties_API.Model.Users
17 18
         public int? UserId { get; set; }
18 19
         public string Name { get; set; }
19 20
         public string Surname { get; set; }
21
+        [NotMapped]
22
+        [VisibleInListView]
23
+        public string FullName
24
+        {
25
+            get
26
+            {
27
+                return $"{Name} {Surname}";
28
+            }
29
+        }
30
+        [VisibleInListView(true)]
20 31
         [DataType(DataType.EmailAddress)]
21 32
         public string Email { get; set; }
33
+        [VisibleInListView(false)]
22 34
         [Phone]
23 35
         public string Telephone { get; set; }
24 36
         [Phone]
25 37
         public string CellNumber { get; set; }
26 38
         public virtual User User { get; set; }
27 39
         #endregion Properties
40
+
41
+        public override string ToString()
42
+        {
43
+            return $"{Name} {Surname}";
44
+        }
28 45
     }
29 46
 }

+ 74
- 10
UnivateProperties_API/Repository/Timeshare/WeekRepository.cs Переглянути файл

@@ -1,10 +1,16 @@
1 1
 using Microsoft.EntityFrameworkCore;
2 2
 using System;
3 3
 using System.Collections.Generic;
4
+using System.Configuration;
5
+using System.IO;
4 6
 using System.Linq;
7
+using System.Net;
8
+using System.Text;
5 9
 using UnivateProperties_API.Containers.Timeshare;
6 10
 using UnivateProperties_API.Containers.Users;
7 11
 using UnivateProperties_API.Context;
12
+using UnivateProperties_API.Helpers;
13
+using UnivateProperties_API.Model.Region;
8 14
 using UnivateProperties_API.Model.Timeshare;
9 15
 using UnivateProperties_API.Repository.Region;
10 16
 using UnivateProperties_API.Repository.Users;
@@ -40,10 +46,14 @@ namespace UnivateProperties_API.Repository.Timeshare
40 46
         public List<WeekDto> GetDtoListAll()
41 47
         {
42 48
             List<WeekDto> list = new List<WeekDto>();
43
-            foreach(var item in GetDetailedAll())
49
+            foreach (var item in GetDetailedAll())
44 50
             {
45 51
                 list.Add(new WeekDto(item));
46 52
             }
53
+            foreach (var  item in GetTenderWeeks())
54
+            {
55
+                list.Add(item);
56
+            }
47 57
             return list;
48 58
         }
49 59
 
@@ -52,14 +62,14 @@ namespace UnivateProperties_API.Repository.Timeshare
52 62
             List<WeekDto> list = new List<WeekDto>();
53 63
             UserRepository userRepository = new UserRepository(_dbContext);
54 64
             var user = userRepository.Get(x => x.Id == userId).FirstOrDefault();
55
-            if(user.IsUserInRole(Role.SuperAdmin))
65
+            if (user.IsUserInRole(Role.SuperAdmin))
56 66
             {
57 67
                 foreach (var item in GetDetailedAll())
58 68
                 {
59 69
                     list.Add(new WeekDto(item));
60 70
                 }
61 71
             }
62
-            else if(user.IsUserInRole(Role.Agency))
72
+            else if (user.IsUserInRole(Role.Agency))
63 73
             {
64 74
                 foreach (var item in GetDetailedAll().Where(x => x.AgencyId == userId))
65 75
                 {
@@ -102,10 +112,10 @@ namespace UnivateProperties_API.Repository.Timeshare
102 112
                 }
103 113
             }
104 114
             list = list.OrderBy(x => x.RegionName).ToList();
105
-            foreach(var region in list)
115
+            foreach (var region in list)
106 116
             {
107 117
                 region.OrderResorts();
108
-                foreach(var resort in region.Resorts)
118
+                foreach (var resort in region.Resorts)
109 119
                 {
110 120
                     resort.Available = allItems.Count(x => x.ResortCode == resort.ResortCode);
111 121
                 }
@@ -117,7 +127,7 @@ namespace UnivateProperties_API.Repository.Timeshare
117 127
         {
118 128
             var list = GetAll();
119 129
             List<TimeshareWeek> weeklist = new List<TimeshareWeek>();
120
-            foreach(var item in list)
130
+            foreach (var item in list)
121 131
             {
122 132
                 weeklist.Add(GetDetailedWeek(item));
123 133
             }
@@ -126,12 +136,12 @@ namespace UnivateProperties_API.Repository.Timeshare
126 136
 
127 137
         private TimeshareWeek GetDetailedWeek(TimeshareWeek week)
128 138
         {
129
-            if(week.AgencyId != null)
139
+            if (week.AgencyId != null)
130 140
             {
131 141
                 AgencyRepository agency = new AgencyRepository(_dbContext);
132 142
                 week.Agency = agency.Get(x => x.Id == week.AgencyId).FirstOrDefault();
133 143
             }
134
-            if(week.AgentId != null)
144
+            if (week.AgentId != null)
135 145
             {
136 146
                 AgentRepository agent = new AgentRepository(_dbContext);
137 147
                 week.Agent = agent.Get(x => x.Id == week.AgentId).FirstOrDefault();
@@ -170,7 +180,7 @@ namespace UnivateProperties_API.Repository.Timeshare
170 180
             // Set starting Status of A1
171 181
             StatusRepository repo = new StatusRepository(_dbContext);
172 182
             item.Status = repo.GetDetailed(s => s.Code == "A1");
173
-            if(item.Status != null)
183
+            if (item.Status != null)
174 184
             {
175 185
                 //Create initial
176 186
                 item.Status = new Status()
@@ -224,7 +234,7 @@ namespace UnivateProperties_API.Repository.Timeshare
224 234
 
225 235
         public void Save()
226 236
         {
227
-            _dbContext.SaveChanges();            
237
+            _dbContext.SaveChanges();
228 238
         }
229 239
 
230 240
         public void Update(TimeshareWeek item)
@@ -271,5 +281,59 @@ namespace UnivateProperties_API.Repository.Timeshare
271 281
             id += 1;
272 282
             return id;
273 283
         }
284
+
285
+        private List<WeekDto> GetTenderWeeks()
286
+        {
287
+            List<WeekDto> list = new List<WeekDto>();
288
+            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(MyCommon.TenderUrl);
289
+            request.Method = "GET";
290
+            WebResponse response = request.GetResponse();
291
+            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
292
+            {
293
+                string result = reader.ReadToEnd();
294
+
295
+                if (result.Length > 0)
296
+                {
297
+                    string cleanLine;
298
+                    string[] lines = result.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
299
+                    foreach (string line in lines)
300
+                    {
301
+                        cleanLine = line.Replace("<br>", "");
302
+                        cleanLine = line.Replace("<br/>", "");
303
+                        list.Add(new WeekDto(cleanLine));
304
+                    }
305
+                }
306
+            }
307
+            // Check that all regions are same as other
308
+            list
309
+                .Where(x => x.Region != null && x.Region.RegionCode == "FN")
310
+                .ToList()
311
+                .ForEach(x => x.Region.RegionCode = ChangeRegion(x.Region.RegionCode));
312
+            list = GetRegion(list);
313
+            return list;
314
+        }
315
+
316
+        private List<WeekDto> GetRegion(List<WeekDto> list)
317
+        {
318
+            ProvinceRepository province = new ProvinceRepository(_dbContext);
319
+            Province prov = null;
320
+            foreach(var item in list)
321
+            {
322
+                prov = province.GetDetailed(x => x.Code == item.Region.RegionCode);
323
+                item.Region = new RegionDto(prov.Id, prov.Code, prov.Description);
324
+            }
325
+            return list;
326
+        }
327
+
328
+        private string ChangeRegion(string value)
329
+        {
330
+            switch(value)
331
+            {
332
+                case "FN":
333
+                    return "KZN";
334
+                default:
335
+                    return value;
336
+            }
337
+        }
274 338
     }
275 339
 }

+ 5
- 0
UnivateProperties_API/Repository/Users/IndividualRepository.cs Переглянути файл

@@ -3,6 +3,7 @@ using System;
3 3
 using System.Collections.Generic;
4 4
 using System.Linq;
5 5
 using UnivateProperties_API.Context;
6
+using UnivateProperties_API.Helpers;
6 7
 using UnivateProperties_API.Model.Users;
7 8
 
8 9
 namespace UnivateProperties_API.Repository.Users
@@ -23,6 +24,10 @@ namespace UnivateProperties_API.Repository.Users
23 24
 
24 25
         public List<Individual> GetAll()
25 26
         {
27
+            foreach(var item in _dbContext.Individuals.Include("User").ToList())
28
+            {
29
+                var list = MyCommon.GetVisibleColumns(item);
30
+            }
26 31
             return _dbContext.Individuals.Include("User").ToList();
27 32
         }
28 33
 

+ 2
- 0
UnivateProperties_API/Startup.cs Переглянути файл

@@ -11,6 +11,7 @@ using Microsoft.IdentityModel.Tokens;
11 11
 using System.Text;
12 12
 using System.Threading.Tasks;
13 13
 using UnivateProperties_API.Context;
14
+using UnivateProperties_API.Helpers;
14 15
 using UnivateProperties_API.Model.Banks;
15 16
 using UnivateProperties_API.Model.Communication;
16 17
 using UnivateProperties_API.Model.ProcessFlow;
@@ -50,6 +51,7 @@ namespace UnivateProperties_API
50 51
             }));
51 52
             services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
52 53
             services.AddDbContext<DataContext>(o => o.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));
54
+            MyCommon.TenderUrl = Configuration.GetConnectionString("TenderConnection");
53 55
 
54 56
             var appSettingsSection = Configuration.GetSection("AppSettings");
55 57
             services.Configure<AppSettings>(appSettingsSection);

+ 2
- 1
UnivateProperties_API/appsettings.json Переглянути файл

@@ -9,6 +9,7 @@
9 9
   },
10 10
   "AllowedHosts": "*",
11 11
   "ConnectionStrings": {
12
-    "DefaultConnection": "Server=localhost;Port=5432;Database=Univate;User Id=postgres;Password=prov1s1on;"
12
+    "DefaultConnection": "Server=localhost;Port=5432;Database=Univate;User Id=postgres;Password=prov1s1on;",
13
+    "TenderConnection": "http://www.unipoint-consoft.co.za/nph-srep.exe?cluvavail_test.sch&CLUB=LPA&RESORT=ALL&SUMMARY=N&HEAD=N"
13 14
   }
14 15
 }

Завантаження…
Відмінити
Зберегти