George Williams 5 lat temu
rodzic
commit
314f22dc2f

+ 7
- 0
UnivateProperties_API/Containers/Timeshare/RegionDto.cs Wyświetl plik

@@ -29,6 +29,13 @@ namespace UnivateProperties_API.Containers.Timeshare
29 29
         public string RegionName { get; set; }
30 30
         public string RegionCode { get; set; }
31 31
         public List<ResortDto> Resorts { get; set; }
32
+        public int Available
33
+        {
34
+            get
35
+            {
36
+                return Resorts != null ? Resorts.Sum(x => x.Available) : 0;
37
+            }
38
+        }
32 39
 
33 40
         public void TryAddResort(string resortCode, string resortName)
34 41
         {

+ 1
- 0
UnivateProperties_API/Containers/Timeshare/ResortDto.cs Wyświetl plik

@@ -15,5 +15,6 @@
15 15
 
16 16
         public string ResortCode { get; set; }
17 17
         public string ResortName { get; set; }
18
+        public int Available { get; set; }
18 19
     }
19 20
 }

+ 7
- 1
UnivateProperties_API/Containers/Timeshare/WeekDto.cs Wyświetl plik

@@ -15,9 +15,12 @@ namespace UnivateProperties_API.Containers.Timeshare
15 15
             Id = week.Id;
16 16
             AgentAsRep = week.AgentAsRep;
17 17
             OtherResort = week.OtherResort;
18
+            Agency = week.Agency?.AgencyName;
19
+            Agent = $"{week.Agent?.Name} {week.Agent?.Surname}";
20
+            Owner = $"{week.Owner?.Name} {week.Owner?.Surname}";
18 21
             Resort = new ResortDto(week.ResortCode, week.ResortName);
19 22
             Region = new RegionDto(week.Region != null ? week.Region.Id : 0, week.Region?.Description, week.Region?.Code);
20
-            Status = new StatusDto(week.Status.Id, week.Status.Code, week.Status.Description);
23
+            Status = new StatusDto(week.Status.Id, week.Status?.Code, week.Status?.Description);
21 24
             Bedrooms = week.Bedrooms;
22 25
             MaxSleep = week.MaxSleep;
23 26
             UnitNumber = week.UnitNumber;
@@ -32,6 +35,9 @@ namespace UnivateProperties_API.Containers.Timeshare
32 35
         public int Id { get; set; }
33 36
         public bool AgentAsRep { get; set; }
34 37
         public bool OtherResort { get; set; }
38
+        public string Agency { get; set; }
39
+        public string Agent { get; set; }
40
+        public string Owner { get; set; }
35 41
         public ResortDto Resort { get; set; }
36 42
         public RegionDto Region { get; set; }
37 43
         public StatusDto Status { get; set; }

+ 8
- 0
UnivateProperties_API/Containers/Users/Simple/SimpleAgencyDto.cs Wyświetl plik

@@ -0,0 +1,8 @@
1
+namespace UnivateProperties_API.Containers.Users.Simple
2
+{
3
+    public class SimpleAgencyDto
4
+    {
5
+        public int Id { get; set; }
6
+        public string Name { get; set; }
7
+    }
8
+}

+ 10
- 0
UnivateProperties_API/Containers/Users/Simple/SimpleItem.cs Wyświetl plik

@@ -0,0 +1,10 @@
1
+namespace UnivateProperties_API.Containers.Users.Simple
2
+{
3
+    public class SimpleItem
4
+    {
5
+        public SimpleUserDto User { get; set; }
6
+        public SimplePersonDto Person { get; set; }
7
+        public SimpleTokenDto Token { get; set; }
8
+        public SimpleAgencyDto Agency { get; set; }
9
+    }
10
+}

+ 13
- 0
UnivateProperties_API/Containers/Users/Simple/SimplePersonDto.cs Wyświetl plik

@@ -0,0 +1,13 @@
1
+namespace UnivateProperties_API.Containers.Users.Simple
2
+{
3
+    public class SimplePersonDto
4
+    {
5
+        public string Name { get; set; }
6
+        public string Surname { get; set; }
7
+        public string FullName 
8
+        {
9
+            get { return $"{Name} {Surname}"; }
10
+        }
11
+        public string Email { get; set; }
12
+    }
13
+}

+ 10
- 0
UnivateProperties_API/Containers/Users/Simple/SimpleTokenDto.cs Wyświetl plik

@@ -0,0 +1,10 @@
1
+using System;
2
+
3
+namespace UnivateProperties_API.Containers.Users.Simple
4
+{
5
+    public class SimpleTokenDto
6
+    {
7
+        public string Token { get; set; }
8
+        public DateTime Expires { get; set; }
9
+    }
10
+}

+ 9
- 0
UnivateProperties_API/Containers/Users/Simple/SimpleUserDto.cs Wyświetl plik

@@ -0,0 +1,9 @@
1
+namespace UnivateProperties_API.Containers.Users.Simple
2
+{
3
+    public class SimpleUserDto
4
+    {
5
+        public int Id { get; set; }
6
+        public string Username { get; set; }
7
+        public string Role { get; set; }
8
+    }
9
+}

+ 6
- 1
UnivateProperties_API/Context/DataContext.cs Wyświetl plik

@@ -117,6 +117,9 @@ namespace UnivateProperties_API.Context
117 117
                 b.HasKey(e => e.Id);
118 118
                 b.Property(e => e.Id).ValueGeneratedOnAdd();
119 119
             });
120
+            modelBuilder.Entity<Individual>()
121
+            .HasIndex(i => new { i.Telephone, i.CellNumber, i.Email })
122
+            .IsUnique(true);
120 123
             modelBuilder.Entity<Agent>(b =>
121 124
             {
122 125
                 b.HasKey(e => e.Id);
@@ -132,6 +135,9 @@ namespace UnivateProperties_API.Context
132 135
                 b.HasKey(e => e.Id);
133 136
                 b.Property(e => e.Id).ValueGeneratedOnAdd();
134 137
             });
138
+            modelBuilder.Entity<User>()
139
+            .HasIndex(u => u.Username)
140
+            .IsUnique(true);
135 141
             modelBuilder.Entity<Email>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
136 142
             modelBuilder.Entity<SMTPAccount>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
137 143
             modelBuilder.Entity<SMTPHost>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
@@ -158,7 +164,6 @@ namespace UnivateProperties_API.Context
158 164
             modelBuilder.Entity<Address>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
159 165
             modelBuilder.Entity<BidItem>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
160 166
             modelBuilder.Entity<ProcessFlow>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
161
-
162 167
         }
163 168
     }
164 169
 }

+ 10
- 0
UnivateProperties_API/Controllers/Timeshare/TimeshareWeekController.cs Wyświetl plik

@@ -44,7 +44,17 @@ namespace UnivateProperties_API.Controllers.Timeshare
44 44
                 return new OkObjectResult(item);
45 45
             }
46 46
             else return new OkResult();
47
+        }
47 48
 
49
+        [HttpGet("getMyWeek/{id}")]
50
+        public IActionResult GetMyWeek(int id)
51
+        {
52
+            if (_Repo is WeekRepository)
53
+            {
54
+                var item = (_Repo as WeekRepository).GetMyWeeks(id);
55
+                return new OkObjectResult(item);
56
+            }
57
+            else return new OkResult();
48 58
         }
49 59
 
50 60
         [HttpGet("getBy")]

+ 38
- 23
UnivateProperties_API/Controllers/Users/RegisterController.cs Wyświetl plik

@@ -8,6 +8,7 @@ using System.IdentityModel.Tokens.Jwt;
8 8
 using System.Security.Claims;
9 9
 using System.Text;
10 10
 using UnivateProperties_API.Containers.Users;
11
+using UnivateProperties_API.Containers.Users.Simple;
11 12
 using UnivateProperties_API.Helpers;
12 13
 using UnivateProperties_API.Model.Users;
13 14
 using UnivateProperties_API.Repository.Users;
@@ -34,36 +35,50 @@ namespace UnivateProperties_API.Controllers.Users
34 35
         [HttpPost("authenticate")]
35 36
         public IActionResult Authenticate([FromBody]UserDto userDto)
36 37
         {
37
-            var user = _Repo.Authenticate(userDto.Username, userDto.Password);
38
-
38
+            try
39
+            {
40
+                var user = _Repo.Authenticate(userDto.Username, userDto.Password);
39 41
 
40
-            if (user == null)
41
-                return BadRequest(new { message = "Username or password is incorrect" });
42 42
 
43
-            var name = _Repo.UserDetails(user.Id);
43
+                if (user == null)
44
+                    return BadRequest(new { message = "Username or password is incorrect" }); 
44 45
 
45
-            var tokenHandler = new JwtSecurityTokenHandler();
46
-            var key = Encoding.ASCII.GetBytes(_appSettings.Secret);
47
-            var tokenDescriptor = new SecurityTokenDescriptor
48
-            {
49
-                Subject = new ClaimsIdentity(new Claim[]
46
+                var tokenHandler = new JwtSecurityTokenHandler();
47
+                var key = Encoding.ASCII.GetBytes(_appSettings.Secret);
48
+                var tokenDescriptor = new SecurityTokenDescriptor
50 49
                 {
50
+                    Subject = new ClaimsIdentity(new Claim[]
51
+                    {
51 52
                     new Claim(ClaimTypes.Name, user.Id.ToString()),
52 53
                     new Claim(ClaimTypes.Role, user.Role)
53
-                }),
54
-                Expires = DateTime.UtcNow.AddMinutes(15),
55
-                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
56
-            };
57
-            var token = tokenHandler.CreateToken(tokenDescriptor);
58
-            var tokenString = tokenHandler.WriteToken(token);
59
-            // return basic user info (without password) and token to store client side
60
-            return Ok(new
54
+                    }),
55
+                    Expires = DateTime.UtcNow.AddMinutes(15),
56
+                    SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
57
+                };
58
+                var token = tokenHandler.CreateToken(tokenDescriptor);
59
+                SimpleItem item = new SimpleItem()
60
+                {
61
+                    User = new SimpleUserDto()
62
+                    {
63
+                        Id = user.Id,
64
+                        Role = user.Role,
65
+                        Username = user.Username
66
+                    },
67
+                    Person = _Repo.UserDetails(user.Id),
68
+                    Token = new SimpleTokenDto()
69
+                    {
70
+                        Token = tokenHandler.WriteToken(token),
71
+                        Expires = tokenDescriptor.Expires.Value
72
+                    }
73
+                };
74
+                // return basic user info (without password) and token to store client side
75
+                return Ok(item);
76
+            }
77
+            catch(Exception)
61 78
             {
62
-                user.Id,
63
-                user.Username,
64
-                Token = tokenString,
65
-                name
66
-            });
79
+                return new UnauthorizedResult();
80
+            }
81
+            
67 82
         }
68 83
 
69 84
         //Writes to DB

+ 1
- 0
UnivateProperties_API/Model/Users/Person.cs Wyświetl plik

@@ -17,6 +17,7 @@ namespace UnivateProperties_API.Model.Users
17 17
         public int? UserId { get; set; }
18 18
         public string Name { get; set; }
19 19
         public string Surname { get; set; }
20
+        [DataType(DataType.EmailAddress)]
20 21
         public string Email { get; set; }
21 22
         [Phone]
22 23
         public string Telephone { get; set; }

+ 7
- 0
UnivateProperties_API/Model/Users/User.cs Wyświetl plik

@@ -32,5 +32,12 @@ namespace UnivateProperties_API.Model.Users
32 32
         public bool Verified { get; set; }
33 33
         public string Token { get; set; }
34 34
         #endregion Properties
35
+
36
+        #region Methods
37
+        public bool IsUserInRole(string role)
38
+        {
39
+            return Role == role;
40
+        }
41
+        #endregion
35 42
     }
36 43
 }

+ 47
- 2
UnivateProperties_API/Repository/Timeshare/WeekRepository.cs Wyświetl plik

@@ -3,6 +3,7 @@ using System;
3 3
 using System.Collections.Generic;
4 4
 using System.Linq;
5 5
 using UnivateProperties_API.Containers.Timeshare;
6
+using UnivateProperties_API.Containers.Users;
6 7
 using UnivateProperties_API.Context;
7 8
 using UnivateProperties_API.Model.Timeshare;
8 9
 using UnivateProperties_API.Repository.Region;
@@ -46,10 +47,47 @@ namespace UnivateProperties_API.Repository.Timeshare
46 47
             return list;
47 48
         }
48 49
 
50
+        public List<WeekDto> GetMyWeeks(int userId)
51
+        {
52
+            List<WeekDto> list = new List<WeekDto>();
53
+            UserRepository userRepository = new UserRepository(_dbContext);
54
+            var user = userRepository.Get(x => x.Id == userId).FirstOrDefault();
55
+            if(user.IsUserInRole(Role.SuperAdmin))
56
+            {
57
+                foreach (var item in GetDetailedAll())
58
+                {
59
+                    list.Add(new WeekDto(item));
60
+                }
61
+            }
62
+            else if(user.IsUserInRole(Role.Agency))
63
+            {
64
+                foreach (var item in GetDetailedAll().Where(x => x.AgencyId == userId))
65
+                {
66
+                    list.Add(new WeekDto(item));
67
+                }
68
+            }
69
+            else if (user.IsUserInRole(Role.Agent) || user.IsUserInRole(Role.ManagingAgent))
70
+            {
71
+                foreach (var item in GetDetailedAll().Where(x => x.AgentId == userId))
72
+                {
73
+                    list.Add(new WeekDto(item));
74
+                }
75
+            }
76
+            else
77
+            {
78
+                foreach (var item in GetDetailedAll().Where(x => x.OwnerId == userId))
79
+                {
80
+                    list.Add(new WeekDto(item));
81
+                }
82
+            }
83
+            return list;
84
+        }
85
+
49 86
         public List<RegionDto> GetAvailResort()
50 87
         {
51 88
             List<RegionDto> list = new List<RegionDto>();
52
-            foreach (var item in GetDetailedAll())
89
+            var allItems = GetDetailedAll();
90
+            foreach (var item in allItems)
53 91
             {
54 92
                 if (item.Region != null)
55 93
                 {
@@ -64,7 +102,14 @@ namespace UnivateProperties_API.Repository.Timeshare
64 102
                 }
65 103
             }
66 104
             list = list.OrderBy(x => x.RegionName).ToList();
67
-            list.ForEach(x => x.OrderResorts());
105
+            foreach(var region in list)
106
+            {
107
+                region.OrderResorts();
108
+                foreach(var resort in region.Resorts)
109
+                {
110
+                    resort.Available = allItems.Count(x => x.ResortCode == resort.ResortCode);
111
+                }
112
+            }
68 113
             return list;
69 114
         }
70 115
 

+ 2
- 1
UnivateProperties_API/Repository/Users/IRegisterRepository.cs Wyświetl plik

@@ -1,5 +1,6 @@
1 1
 using System.Collections.Generic;
2 2
 using UnivateProperties_API.Containers.Users;
3
+using UnivateProperties_API.Containers.Users.Simple;
3 4
 using UnivateProperties_API.Model.Users;
4 5
 
5 6
 namespace UnivateProperties_API.Repository.Users
@@ -20,6 +21,6 @@ namespace UnivateProperties_API.Repository.Users
20 21
         void Delete(int id);
21 22
         void DeleteAgency(int id);
22 23
         void DeleteIndividual(int id);
23
-        string UserDetails(int userId);
24
+        SimplePersonDto UserDetails(int userId);
24 25
     }
25 26
 }

+ 15
- 4
UnivateProperties_API/Repository/Users/RegisterRepository.cs Wyświetl plik

@@ -2,6 +2,7 @@
2 2
 using System.Collections.Generic;
3 3
 using System.Linq;
4 4
 using UnivateProperties_API.Containers.Users;
5
+using UnivateProperties_API.Containers.Users.Simple;
5 6
 using UnivateProperties_API.Context;
6 7
 using UnivateProperties_API.Helpers;
7 8
 using UnivateProperties_API.Model.Users;
@@ -275,7 +276,7 @@ namespace UnivateProperties_API.Repository.Users
275 276
             return id;
276 277
         }
277 278
 
278
-        public string UserDetails(int userId)
279
+        public SimplePersonDto UserDetails(int userId)
279 280
         {
280 281
             var individual = _dbContext.Individuals.Where(i => i.UserId == userId).FirstOrDefault();
281 282
 
@@ -284,16 +285,26 @@ namespace UnivateProperties_API.Repository.Users
284 285
                 var agent = _dbContext.Agents.Where(i => i.UserId == userId).FirstOrDefault();
285 286
                 if (agent != null)
286 287
                 {
287
-                    return agent.Name + " " + agent.Surname;
288
+                    return new SimplePersonDto()
289
+                    {
290
+                        Name = agent.Name,
291
+                        Surname = agent.Surname,
292
+                        Email = agent.Email
293
+                    };
288 294
                 }
289 295
                 else
290 296
                 {
291
-                    return "";
297
+                    return new SimplePersonDto(); ;
292 298
                 }
293 299
             }
294 300
             else
295 301
             {
296
-                return individual.Name + " " + individual.Surname;
302
+                return new SimplePersonDto()
303
+                {
304
+                    Name = individual.Name,
305
+                    Surname = individual.Surname,
306
+                    Email = individual.Email
307
+                };
297 308
             }
298 309
         }
299 310
     }

Ładowanie…
Anuluj
Zapisz