Kobus 5 gadus atpakaļ
vecāks
revīzija
6b2b21e6f7

+ 0
- 11
UnivateProperties_API/Containers/Property/PropertyDisplay.cs Parādīt failu

@@ -29,15 +29,4 @@
29 29
         public string DisplayImage { get; set; }
30 30
         #endregion 
31 31
     }
32
-
33
-    public class PropertySearch
34
-    {
35
-        #region Properties
36
-        public string Type { get; set; }
37
-        public string PropertyType { get; set; }
38
-        public string Province { get; set; }
39
-        public string City { get; set; }
40
-        public string Suburb { get; set; }
41
-        #endregion
42
-    }
43 32
 }

+ 34
- 2
UnivateProperties_API/Controllers/Properties/PropertyController.cs Parādīt failu

@@ -1,7 +1,7 @@
1 1
 using Microsoft.AspNetCore.Mvc;
2
-using System;
2
+using System.Collections.Generic;
3
+using System.Linq;
3 4
 using System.Transactions;
4
-using UnivateProperties_API.Containers.Property;
5 5
 using UnivateProperties_API.Model.Property;
6 6
 using UnivateProperties_API.Repository.Properties;
7 7
 
@@ -42,6 +42,38 @@ namespace UnivateProperties_API.Controllers.Properties
42 42
             return new OkObjectResult(_Repo.GetDisplay(type, propertyType, province, city, suburb, propType));
43 43
         }
44 44
 
45
+        [HttpGet("{type}/{by}")]
46
+        public IActionResult SearchBy(string type, string by)
47
+        {
48
+            PropertyUsageType pType = PropertyUsageType.Both;
49
+            switch (type.ToUpper())
50
+            {
51
+                case "RESIDENTIAL":
52
+                    pType = PropertyUsageType.Residential;
53
+                    break;
54
+                case "COMMERCIAL":
55
+                    pType = PropertyUsageType.Commercial;
56
+                    break;
57
+            }
58
+
59
+            if (pType != PropertyUsageType.Both)
60
+            {
61
+                List<int> proptypeIds = (from pt in _Repo.GetPropertyTypes(p => p.UsageType == pType)
62
+                                         select pt.Id).ToList();
63
+
64
+                if (string.IsNullOrEmpty(by) || by.ToUpper() == "ALL")
65
+                {
66
+                    return new OkObjectResult(_Repo.Get(x => proptypeIds.Contains(x.PropertyTypeId)));
67
+                }
68
+                else
69
+                {
70
+                    return new OkObjectResult(_Repo.Get(x => proptypeIds.Contains(x.PropertyTypeId) && x.CreatedBy == by));
71
+                }
72
+            }
73
+            else
74
+                return new NoContentResult();
75
+        }
76
+
45 77
         [HttpPost]
46 78
         public IActionResult Post([FromBody] Property property)
47 79
         {

+ 4
- 11
UnivateProperties_API/Controllers/Users/IndividualController.cs Parādīt failu

@@ -17,29 +17,21 @@ namespace User_API.Controllers
17 17
             _Repo = repo;
18 18
         }
19 19
 
20
+        //Gets data from the DB
20 21
         [HttpGet]
21 22
         public IActionResult Get()
22 23
         {
23 24
             return new OkObjectResult(_Repo.GetAll());
24 25
         }
25 26
 
27
+        //Gets data from DB by Id
26 28
         [HttpGet("{id}")]
27 29
         public IActionResult Get(int id)
28 30
         {
29 31
             return new OkObjectResult(_Repo.Get(x => x.Id == id));
30 32
         }
31 33
 
32
-        [HttpPost()]
33
-        public IActionResult Post([FromBody] Individual individual)
34
-        {
35
-            using (var scope = new TransactionScope())
36
-            {
37
-                _Repo.Insert(individual);
38
-                scope.Complete();
39
-                return CreatedAtAction(nameof(Get), new { id = individual.Id }, individual);
40
-            }
41
-        }
42
-
34
+        //Updates to DB
43 35
         [HttpPut()]
44 36
         public IActionResult Put([FromBody] Individual individual)
45 37
         {
@@ -55,6 +47,7 @@ namespace User_API.Controllers
55 47
             return new NoContentResult();
56 48
         }
57 49
 
50
+        //Updates DB by removing said Id
58 51
         [HttpDelete("{id}")]
59 52
         public IActionResult Delete(int id)
60 53
         {

+ 63
- 103
UnivateProperties_API/Controllers/Users/RegisterController.cs Parādīt failu

@@ -33,6 +33,7 @@ namespace UnivateProperties_API.Controllers.Users
33 33
             _appSettings = appSettings.Value;
34 34
         }
35 35
 
36
+        //Works
36 37
         [AllowAnonymous]
37 38
         [HttpPost("authenticate")]
38 39
         public IActionResult Authenticate([FromBody]UserDto userDto)
@@ -68,26 +69,25 @@ namespace UnivateProperties_API.Controllers.Users
68 69
             });
69 70
         }
70 71
 
72
+        //Writes to DB
71 73
         [AllowAnonymous]
72 74
         [HttpPost("register")]
73
-        public IActionResult Register([FromBody]UserDto userDto)
75
+        public IActionResult Register([FromBody]UserDto individual)
74 76
         {
75
-            // map dto to entity
76
-            var user = _mapper.Map<User>(userDto);
77
+            _mapper.Map<Individual>(individual);
77 78
 
78 79
             try
79 80
             {
80
-                // save 
81
-                _Repo.Create(user, userDto.Password, true);
81
+                _Repo.CreatePerson(individual, PersonType.Individual, true, null);
82 82
                 return Ok();
83 83
             }
84 84
             catch (AppException ex)
85 85
             {
86
-                // return error message if there was an exception
87
-                return BadRequest(new { message = ex.Message });
86
+                return BadRequest(new { messge = ex.Message });
88 87
             }
89 88
         }
90 89
 
90
+        //Writes to DB
91 91
         [AllowAnonymous]
92 92
         [HttpPost("registeragency")]
93 93
         public IActionResult RegisterAgency([FromBody]AgencyDto agency)
@@ -108,101 +108,61 @@ namespace UnivateProperties_API.Controllers.Users
108 108
             }
109 109
         }
110 110
 
111
-        [HttpPut("{id}")]
112
-        public IActionResult Update(int id, [FromBody]UserDto userDto)
113
-        {
114
-            // map dto to entity and set id
115
-            var user = _mapper.Map<User>(userDto);
116
-            user.Id = id;
117
-
118
-            try
119
-            {
120
-                // save 
121
-                _Repo.Update(user, userDto.Password);
122
-                return Ok();
123
-            }
124
-            catch (AppException ex)
125
-            {
126
-                // return error message if there was an exception
127
-                return BadRequest(new { message = ex.Message });
128
-            }
129
-        }
130
-
131
-        [HttpPut("{id}")]
132
-        public IActionResult UpdateAgency(int id, [FromBody]UserDto userDto)
133
-        {
134
-            // map dto to entity and set id
135
-            var agency = _mapper.Map<Agency>(userDto);
136
-            agency.Id = id;
137
-
138
-            try
139
-            {
140
-                // save 
141
-                _Repo.UpdateAgency(agency, userDto.Password);
142
-                return Ok();
143
-            }
144
-            catch (AppException ex)
145
-            {
146
-                // return error message if there was an exception
147
-                return BadRequest(new { message = ex.Message });
148
-            }
149
-        }
150
-
151
-        [HttpGet("{id}")]
152
-        public IActionResult GetById(int id)
153
-        {
154
-            var user = _Repo.GetById(id);
155
-            var userDto = _mapper.Map<UserDto>(user);
156
-
157
-            if (user == null)
158
-            {
159
-                return NotFound();
160
-            }
161
-
162
-            // Only allow SuperAdmins to access other user records
163
-            var currentUserId = int.Parse(User.Identity.Name);
164
-            if (id != currentUserId && !User.IsInRole(Role.SuperAdmin))
165
-            {
166
-                return Forbid();
167
-            }
168
-
169
-            return Ok(userDto);
170
-        }
171
-
172
-        [HttpGet("{id}")]
173
-        public IActionResult GetByAgencyId(int id)
174
-        {
175
-            var agency = _Repo.GetByAgencyId(id);
176
-            var agencyDto = _mapper.Map<AgencyDto>(agency);
177
-
178
-            if (agency == null)
179
-            {
180
-                return NotFound();
181
-            }
182
-
183
-            var currentAgencyId = int.Parse(User.Identity.Name);
184
-            if (id != currentAgencyId && !User.IsInRole(Role.Agency))
185
-            {
186
-                return Forbid();
187
-            }
188
-
189
-            return Ok(agencyDto);
190
-        }
191
-
192
-        [Authorize(Roles = Role.SuperAdmin)]
193
-        [HttpDelete("{id}")]
194
-        public IActionResult Delete(User user)
195
-        {
196
-            _Repo.Delete(user.Id);
197
-            return Ok();
198
-        }
199
-
200
-        [Authorize(Roles = Role.SuperAdmin)]
201
-        [HttpDelete("{id}")]
202
-        public IActionResult DeleteAgency(Agency agency)
203
-        {
204
-            _Repo.DeleteAgency(agency.Id);
205
-            return Ok();
206
-        }
111
+        //[HttpGet("{id}")]
112
+        //public IActionResult GetById(int id)
113
+        //{
114
+        //    var user = _Repo.GetById(id);
115
+        //    var userDto = _mapper.Map<UserDto>(user);
116
+
117
+        //    if (user == null)
118
+        //    {
119
+        //        return NotFound();
120
+        //    }
121
+
122
+        //    // Only allow SuperAdmins to access other user records
123
+        //    var currentUserId = int.Parse(User.Identity.Name);
124
+        //    if (id != currentUserId && !User.IsInRole(Role.SuperAdmin))
125
+        //    {
126
+        //        return Forbid();
127
+        //    }
128
+
129
+        //    return Ok(userDto);
130
+        //}
131
+
132
+        //[HttpGet("{id}")]
133
+        //public IActionResult GetByAgencyId(int id)
134
+        //{
135
+        //    var agency = _Repo.GetByAgencyId(id);
136
+        //    var agencyDto = _mapper.Map<AgencyDto>(agency);
137
+
138
+        //    if (agency == null)
139
+        //    {
140
+        //        return NotFound();
141
+        //    }
142
+
143
+        //    var currentAgencyId = int.Parse(User.Identity.Name);
144
+        //    if (id != currentAgencyId && !User.IsInRole(Role.Agency))
145
+        //    {
146
+        //        return Forbid();
147
+        //    }
148
+
149
+        //    return Ok(agencyDto);
150
+        //}
151
+
152
+        //[Authorize(Roles = Role.SuperAdmin)]
153
+        //[HttpDelete("{id}")]
154
+        //public IActionResult Delete(User user)
155
+        //{
156
+        //    _Repo.Delete(user.Id);
157
+        //    return Ok();
158
+        //}
159
+
160
+        //[Authorize(Roles = Role.SuperAdmin)]
161
+        //[HttpDelete("{id}")]
162
+        //public IActionResult DeleteAgency(Agency agency)
163
+        //{
164
+        //    _Repo.DeleteAgency(agency.Id);
165
+        //    return Ok();
166
+        //}
207 167
     }
208 168
 }

+ 0
- 1
UnivateProperties_API/Controllers/Users/UserController.cs Parādīt failu

@@ -7,7 +7,6 @@ using UnivateProperties_API.Repository.Users;
7 7
 
8 8
 namespace User_API.Controllers
9 9
 {
10
-    [Authorize]
11 10
     [Route("api/[controller]")]
12 11
     [ApiController]
13 12
     public class UserController : ControllerBase

+ 4
- 0
UnivateProperties_API/Helpers/AutoMapperProfile.cs Parādīt failu

@@ -12,6 +12,10 @@ namespace UnivateProperties_API.Helpers
12 12
             CreateMap<UserDto, User>();
13 13
             CreateMap<Agency, AgencyDto>();
14 14
             CreateMap<AgencyDto, Agency>();
15
+            CreateMap<Individual, UserDto>();
16
+            CreateMap<UserDto, Individual>();
17
+            CreateMap<Individual, Person>();
18
+            CreateMap<Person, Individual>();
15 19
         }
16 20
     }
17 21
 }

+ 3
- 1
UnivateProperties_API/Model/Property/Property.cs Parādīt failu

@@ -8,7 +8,8 @@ namespace UnivateProperties_API.Model.Property
8 8
     public class Property : BaseEntity
9 9
     {
10 10
         #region Properties
11
-        [ForeignKey("PropertyType")]
11
+        public string CreatedBy { get; set; }
12
+        [ForeignKey("PropertyType")]        
12 13
         public int PropertyTypeId { get; set; }
13 14
         public string PropertyName { get; set; }
14 15
         public string Unit { get; set; }
@@ -24,6 +25,7 @@ namespace UnivateProperties_API.Model.Property
24 25
         public int SuburbId { get; set; }
25 26
         public int CityId { get; set; }
26 27
         public int ProvinceId { get; set; }
28
+        public bool Published { get; set; }
27 29
 
28 30
         public virtual PropertyType PropertyType { get; set; }
29 31
         public virtual Province Province { get; set; }

+ 6
- 6
UnivateProperties_API/Properties/launchSettings.json Parādīt failu

@@ -1,13 +1,13 @@
1
-{
2
-  "$schema": "http://json.schemastore.org/launchsettings.json",
1
+{
3 2
   "iisSettings": {
4
-    "windowsAuthentication": false, 
5
-    "anonymousAuthentication": true, 
3
+    "windowsAuthentication": false,
4
+    "anonymousAuthentication": true,
6 5
     "iisExpress": {
7 6
       "applicationUrl": "http://localhost:57260",
8 7
       "sslPort": 0
9 8
     }
10 9
   },
10
+  "$schema": "http://json.schemastore.org/launchsettings.json",
11 11
   "profiles": {
12 12
     "IIS Express": {
13 13
       "commandName": "IISExpress",
@@ -21,10 +21,10 @@
21 21
       "commandName": "Project",
22 22
       "launchBrowser": true,
23 23
       "launchUrl": "api/values",
24
-      "applicationUrl": "http://localhost:5000",
25 24
       "environmentVariables": {
26 25
         "ASPNETCORE_ENVIRONMENT": "Development"
27
-      }
26
+      },
27
+      "applicationUrl": "http://localhost:5000"
28 28
     }
29 29
   }
30 30
 }

+ 1
- 0
UnivateProperties_API/Repository/Properties/IPropertyRepository.cs Parādīt failu

@@ -11,5 +11,6 @@ namespace UnivateProperties_API.Repository.Properties
11 11
         List<PropertyDisplay> GetDisplay(Func<Property, bool> where);
12 12
         List<PropertyDisplay> GetDisplay(string Keyword);
13 13
         List<PropertyDisplay> GetDisplay(string type, string propertyType, string province, string city, string suburb, string propType);
14
+        List<PropertyType> GetPropertyTypes(Func<PropertyType, bool> where);
14 15
     }
15 16
 }

+ 17
- 1
UnivateProperties_API/Repository/Properties/PropertyRepository.cs Parādīt failu

@@ -19,7 +19,12 @@ namespace UnivateProperties_API.Repository.Properties
19 19
 
20 20
         public List<Property> Get(Func<Property, bool> where)
21 21
         {
22
-            return dBContext.Properties.Where(where).ToList();
22
+            return dBContext.Properties
23
+                .Include("PropertyType")
24
+                .Include("Province")
25
+                .Include("City")
26
+                .Include("Suburb")
27
+                .Where(where).ToList();
23 28
         }
24 29
 
25 30
         public List<Property> GetAll()
@@ -336,5 +341,16 @@ namespace UnivateProperties_API.Repository.Properties
336 341
 
337 342
             return properties;
338 343
         }
344
+
345
+        public List<PropertyType> GetPropertyTypes(Func<PropertyType, bool> where)
346
+        {
347
+            List<PropertyType> list;
348
+            if (where != null)
349
+                list = dBContext.PropertyTypes.Where(where).ToList();
350
+            else
351
+                list = dBContext.PropertyTypes.ToList();
352
+
353
+            return list;
354
+        }
339 355
     }
340 356
 }

+ 4
- 6
UnivateProperties_API/Repository/Users/IRegisterRepository.cs Parādīt failu

@@ -7,18 +7,16 @@ namespace UnivateProperties_API.Repository.Users
7 7
     public interface IRegisterRepository
8 8
     {
9 9
         User Authenticate(string username, string password);
10
+        User Create(User user, string password, bool save);
11
+        Agency CreateAgency(AgencyDto agency);
12
+        void CreatePerson(UserDto individual, PersonType personType, bool save, Agency agency);
13
+        void Update(User userParam, string password = null);
10 14
         IEnumerable<User> GetAllUsers();
11 15
         IEnumerable<Agency> GetAllAgencies();
12 16
         IEnumerable<Individual> GetAllIndividuals();
13 17
         User GetById(int id);
14 18
         Agency GetByAgencyId(int id);
15 19
         Individual GetByIndividualId(int id);
16
-        User Create(User user, string password, bool save);
17
-        Agency CreateAgency(AgencyDto agency);
18
-        void CreatePerson(UserDto individual, PersonType personType, bool save, Agency agency);
19
-        void Update(User user, string password = null);
20
-        void UpdateAgency(Agency agency, string agencyname = null);
21
-        void UpdatePerson(Person person, PersonType personType);
22 20
         void Delete(int id);
23 21
         void DeleteAgency(int id);
24 22
         void DeleteIndividual(int id);

+ 1
- 0
UnivateProperties_API/Repository/Users/IndividualRepository.cs Parādīt failu

@@ -76,6 +76,7 @@ namespace UnivateProperties_API.Repository.Users
76 76
             Save();
77 77
         }
78 78
 
79
+        //Updates in DB
79 80
         public void Update(Individual item)
80 81
         {
81 82
             _dbContext.Entry(item).State = EntityState.Modified;

+ 0
- 40
UnivateProperties_API/Repository/Users/RegisterRepository.cs Parādīt failu

@@ -150,22 +150,6 @@ namespace UnivateProperties_API.Repository.Users
150 150
             }
151 151
         }
152 152
 
153
-        public void UpdatePerson(Person person, PersonType personType)
154
-        {
155
-            if (personType == PersonType.Agent)
156
-            {
157
-                var item = (person as Agent);
158
-                _dbContext.Entry(item).State = EntityState.Modified;
159
-                Save();
160
-            }
161
-            else if (personType == PersonType.Individual)
162
-            {
163
-                var item = (person as Individual);
164
-                _dbContext.Entry(item).State = EntityState.Modified;
165
-                Save();
166
-            }
167
-        }
168
-
169 153
         public void Update(User userParam, string password = null)
170 154
         {
171 155
             var user = _dbContext.Users.Find(userParam.Id);
@@ -199,30 +183,6 @@ namespace UnivateProperties_API.Repository.Users
199 183
             _dbContext.SaveChanges();
200 184
         }
201 185
 
202
-        public void UpdateAgency(Agency agencyParam, string agencyname = null)
203
-        {
204
-            var agency = _dbContext.Agencies.Find(agencyParam.Id);
205
-
206
-            if (agency == null)
207
-                throw new AppException("Agency not found");
208
-
209
-            if (agencyParam.AgencyName != agency.AgencyName)
210
-            {
211
-                // username has changed so check if the new username is already taken
212
-                if (_dbContext.Agencies.Any(x => x.AgencyName == agencyParam.AgencyName))
213
-                    throw new AppException("AgencyName " + agencyParam.AgencyName + " is already taken");
214
-            }
215
-
216
-            // update user properties
217
-            agency.AgencyName = agencyParam.AgencyName;
218
-            agency.EAABEFFCNumber = agencyParam.EAABEFFCNumber;
219
-            agency.CompanyRegNumber = agencyParam.CompanyRegNumber;
220
-
221
-            // update password if it was entered
222
-            _dbContext.Agencies.Update(agency);
223
-            _dbContext.SaveChanges();
224
-        }
225
-
226 186
         [Authorize(Roles = Role.SuperAdmin)]
227 187
         public IEnumerable<User> GetAllUsers()
228 188
         {

+ 49
- 3
UnivateProperties_API/Repository/Users/UserRepository.cs Parādīt failu

@@ -8,6 +8,7 @@ using System.Linq;
8 8
 using System.Security.Claims;
9 9
 using System.Text;
10 10
 using UnivateProperties_API.Context;
11
+using UnivateProperties_API.Helpers;
11 12
 using UnivateProperties_API.Model.Users;
12 13
 
13 14
 namespace UnivateProperties_API.Repository.Users
@@ -81,10 +82,55 @@ namespace UnivateProperties_API.Repository.Users
81 82
             Save();
82 83
         }
83 84
 
84
-        public void Update(User item)
85
+        //public void Update(User item)
86
+        //{
87
+        //    _dbContext.Entry(item).State = EntityState.Modified;
88
+        //    Save();
89
+        //}
90
+
91
+        public void Update(User userParam)
85 92
         {
86
-            _dbContext.Entry(item).State = EntityState.Modified;
87
-            Save();
93
+            var user = _dbContext.Users.Find(userParam.Id);
94
+
95
+            if (user == null)
96
+                throw new AppException("User not found");
97
+
98
+            if (userParam.Username != user.Username)
99
+            {
100
+                // username has changed so check if the new username is already taken
101
+                if (_dbContext.Users.Any(x => x.Username == userParam.Username))
102
+                    throw new AppException("Username " + userParam.Username + " is already taken");
103
+            }
104
+
105
+            // update user properties
106
+            user.Name = userParam.Name;
107
+            user.Surname = userParam.Surname;
108
+            user.Username = userParam.Username;
109
+
110
+            // update password if it was entered
111
+            if (!string.IsNullOrWhiteSpace(userParam.PasswordHash.ToString()))
112
+            {
113
+                byte[] passwordHash, passwordSalt;
114
+                CreatePasswordHash(userParam.PasswordHash.ToString(), out passwordHash, out passwordSalt);
115
+
116
+                user.PasswordHash = passwordHash;
117
+                user.PasswordSalt = passwordSalt;
118
+            }
119
+
120
+            _dbContext.Users.Update(user);
121
+            _dbContext.SaveChanges();
122
+        }
123
+
124
+        private static void CreatePasswordHash(string password, out byte[] passwordHash, out byte[] passwordSalt)
125
+        {
126
+            if (password == null) throw new ArgumentNullException("password");
127
+            if (string.IsNullOrWhiteSpace(password)) throw new ArgumentException("Value cannot be empty or whitespace only string.", "password");
128
+
129
+            using (var hmac = new System.Security.Cryptography.HMACSHA512())
130
+            {
131
+                passwordSalt = hmac.Key;
132
+                passwordHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password));
133
+            }
88 134
         }
89 135
 
90 136
         public void Save()

+ 0
- 2
UnivateProperties_API/UnivateProperties_API.csproj Parādīt failu

@@ -6,8 +6,6 @@
6 6
   </PropertyGroup>
7 7
 
8 8
   <ItemGroup>
9
-    <Compile Remove="Migrations\20190826105340_InitialCreate.cs" />
10
-    <Compile Remove="Migrations\20190826105340_InitialCreate.Designer.cs" />
11 9
     <Compile Remove="Model\Users\AgencyAgent.cs" />
12 10
   </ItemGroup>
13 11
 

+ 1
- 1
UnivateProperties_API/appsettings.json Parādīt failu

@@ -9,6 +9,6 @@
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 13
   }
14 14
 }

Notiek ielāde…
Atcelt
Saglabāt