Browse Source

Roles Update Part 1

master
30117125 4 years ago
parent
commit
b047557ad7

+ 3
- 0
UnivateProperties_API/Containers/Timeshare/Detailed/DetailedOwner.cs View File

59
                     HowMarried = "Partner";
59
                     HowMarried = "Partner";
60
                     break;
60
                     break;
61
             }
61
             }
62
+            User = individual.User;
63
+
62
             if (individual.Address != null)
64
             if (individual.Address != null)
63
             {
65
             {
64
                 Address = individual.Address != null ? new DetailedAddress(individual.Address) : new DetailedAddress();
66
                 Address = individual.Address != null ? new DetailedAddress(individual.Address) : new DetailedAddress();
100
         public string SpouseCellnumber { get; set; }
102
         public string SpouseCellnumber { get; set; }
101
         public DetailedAddress Address { get; set; }
103
         public DetailedAddress Address { get; set; }
102
         public DetailedBankDetails BankingDetails { get; set; }
104
         public DetailedBankDetails BankingDetails { get; set; }
105
+        public User User { get; set; }
103
     }
106
     }
104
 }
107
 }

+ 1
- 0
UnivateProperties_API/Context/DataContext.cs View File

36
         public virtual DbSet<User> Users { get; set; }
36
         public virtual DbSet<User> Users { get; set; }
37
         public virtual DbSet<Individual> Individuals { get; set; }
37
         public virtual DbSet<Individual> Individuals { get; set; }
38
         public virtual DbSet<Address> Addresses { get; set; }
38
         public virtual DbSet<Address> Addresses { get; set; }
39
+        public virtual DbSet<UserRole> Roles { get; set; }
39
         #endregion User
40
         #endregion User
40
 
41
 
41
         #region Communication
42
         #region Communication

+ 17
- 0
UnivateProperties_API/Controllers/Users/AgentController.cs View File

37
             return new OkObjectResult(_Repo.Get(x => x.Id == id));
37
             return new OkObjectResult(_Repo.Get(x => x.Id == id));
38
         }
38
         }
39
 
39
 
40
+        [HttpGet("single/{userId}")]
41
+        public IActionResult GetByUserId(int userId)
42
+        {
43
+            return new OkObjectResult(_Repo.Get(x => x.UserId == userId));
44
+        }
45
+
40
         [HttpPost()]
46
         [HttpPost()]
41
         public IActionResult Post([FromBody] AgentDto agentDto)
47
         public IActionResult Post([FromBody] AgentDto agentDto)
42
         {
48
         {
53
             }
59
             }
54
         }
60
         }
55
 
61
 
62
+        [HttpPost("AgentFromUser")]
63
+        public IActionResult Post(int id, [FromBody] Agent agent)
64
+        {
65
+            using (var scope = new TransactionScope())
66
+            {
67
+                _Repo.Insert(agent);
68
+                scope.Complete();
69
+                return CreatedAtAction(nameof(Get), new { id = agent.Id }, agent);
70
+            }
71
+        }
72
+
56
         [HttpPut()]
73
         [HttpPut()]
57
         public IActionResult Put([FromBody] Agent agent)
74
         public IActionResult Put([FromBody] Agent agent)
58
         {
75
         {

+ 65
- 0
UnivateProperties_API/Controllers/Users/UserRoleController.cs View File

1
+using Microsoft.AspNetCore.Mvc;
2
+using System.Transactions;
3
+using UnivateProperties_API.Model.Users;
4
+using UnivateProperties_API.Repository;
5
+
6
+namespace UnivateProperties_API.Controllers.Users
7
+{
8
+    [Route("api/[controller]")]
9
+    [ApiController]
10
+    public class UserRoleController : ControllerBase
11
+    {
12
+        private readonly IRepository<UserRole> _Repo;
13
+
14
+        public UserRoleController(IRepository<UserRole> rp)
15
+        {
16
+            _Repo = rp;
17
+        }
18
+
19
+        [HttpGet]
20
+        public IActionResult Get()
21
+        {
22
+            var roles = _Repo.GetAll();
23
+            return new OkObjectResult(roles);
24
+        }
25
+
26
+        [HttpGet("{id}")]
27
+        public IActionResult Get(int id)
28
+        {
29
+            return new OkObjectResult(_Repo.Get(x => x.Id == id));
30
+        }
31
+
32
+        [HttpPost]
33
+        public IActionResult Post([FromBody] UserRole userRole)
34
+        {
35
+            using (var scope = new TransactionScope())
36
+            {
37
+                _Repo.Insert(userRole);
38
+                scope.Complete();
39
+                return CreatedAtAction(nameof(Get), new { id = userRole.Id }, userRole);
40
+            }
41
+        }
42
+
43
+        [HttpPut]
44
+        public IActionResult Put([FromBody] UserRole userRole)
45
+        {
46
+            if (userRole != null)
47
+            {
48
+                using (var scope = new TransactionScope())
49
+                {
50
+                    _Repo.Update(userRole);
51
+                    scope.Complete();
52
+                    return new OkResult();
53
+                }
54
+            }
55
+            return new NoContentResult();
56
+        }
57
+
58
+        [HttpDelete("{id}")]
59
+        public IActionResult Delete(int id)
60
+        {
61
+            _Repo.RemoveAtId(id);
62
+            return new OkResult();
63
+        }
64
+    }
65
+}

+ 1660
- 0
UnivateProperties_API/Migrations/20201019061752_UserRoles.Designer.cs
File diff suppressed because it is too large
View File


+ 36
- 0
UnivateProperties_API/Migrations/20201019061752_UserRoles.cs View File

1
+using System;
2
+using Microsoft.EntityFrameworkCore.Metadata;
3
+using Microsoft.EntityFrameworkCore.Migrations;
4
+
5
+namespace UnivateProperties_API.Migrations
6
+{
7
+    public partial class UserRoles : Migration
8
+    {
9
+        protected override void Up(MigrationBuilder migrationBuilder)
10
+        {
11
+            migrationBuilder.CreateTable(
12
+                name: "Roles",
13
+                columns: table => new
14
+                {
15
+                    Id = table.Column<int>(nullable: false)
16
+                        .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
17
+                    Created = table.Column<DateTime>(nullable: false),
18
+                    Modified = table.Column<DateTime>(nullable: false),
19
+                    ModifiedBy = table.Column<string>(nullable: true),
20
+                    IsDeleted = table.Column<bool>(nullable: false),
21
+                    RoleName = table.Column<string>(nullable: true),
22
+                    RoleDescription = table.Column<string>(nullable: true)
23
+                },
24
+                constraints: table =>
25
+                {
26
+                    table.PrimaryKey("PK_Roles", x => x.Id);
27
+                });
28
+        }
29
+
30
+        protected override void Down(MigrationBuilder migrationBuilder)
31
+        {
32
+            migrationBuilder.DropTable(
33
+                name: "Roles");
34
+        }
35
+    }
36
+}

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

1361
                     b.ToTable("Users");
1361
                     b.ToTable("Users");
1362
                 });
1362
                 });
1363
 
1363
 
1364
+            modelBuilder.Entity("UnivateProperties_API.Model.Users.UserRole", b =>
1365
+                {
1366
+                    b.Property<int>("Id")
1367
+                        .ValueGeneratedOnAdd()
1368
+                        .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
1369
+
1370
+                    b.Property<DateTime>("Created");
1371
+
1372
+                    b.Property<bool>("IsDeleted");
1373
+
1374
+                    b.Property<DateTime>("Modified");
1375
+
1376
+                    b.Property<string>("ModifiedBy");
1377
+
1378
+                    b.Property<string>("RoleDescription");
1379
+
1380
+                    b.Property<string>("RoleName");
1381
+
1382
+                    b.HasKey("Id");
1383
+
1384
+                    b.ToTable("Roles");
1385
+                });
1386
+
1364
             modelBuilder.Entity("UnivateProperties_API.Model.Banks.BankAccount", b =>
1387
             modelBuilder.Entity("UnivateProperties_API.Model.Banks.BankAccount", b =>
1365
                 {
1388
                 {
1366
                     b.HasOne("UnivateProperties_API.Model.Banks.Bank", "Bank")
1389
                     b.HasOne("UnivateProperties_API.Model.Banks.Bank", "Bank")

+ 6
- 0
UnivateProperties_API/Model/Users/Individual.cs View File

98
                         BankAccount.AccountHolder = owner.BankingDetails.AccountHolder;
98
                         BankAccount.AccountHolder = owner.BankingDetails.AccountHolder;
99
                     }
99
                     }
100
                 }
100
                 }
101
+
102
+                if (owner.User != null)
103
+                {
104
+                    User.Role = owner.User.Role;
105
+                    User.LoginPasswordChange = owner.User.LoginPasswordChange;
106
+                }
101
             }
107
             }
102
         }
108
         }
103
         #endregion Methods
109
         #endregion Methods

+ 8
- 0
UnivateProperties_API/Model/Users/UserRole.cs View File

1
+namespace UnivateProperties_API.Model.Users
2
+{
3
+    public class UserRole : BaseEntity
4
+    {
5
+        public string RoleName { get; set; }
6
+        public string RoleDescription { get; set; }
7
+    }
8
+}

+ 5
- 0
UnivateProperties_API/Repository/Properties/PropertyRepository.cs View File

957
             {
957
             {
958
                 props = dBContext.Properties.Include("Owner").Include("PropertyType").ToList();
958
                 props = dBContext.Properties.Include("Owner").Include("PropertyType").ToList();
959
             }
959
             }
960
+            else if (user.Role.ToUpper() == "AGENCY")
961
+            {
962
+                var agent = dBContext.Agents.Where(x => x.UserId == user.Id).FirstOrDefault();
963
+                props = dBContext.Properties.Include("Owner").Include("PropertyType").Where(x => x.AgencyId == agent.AgencyId).ToList();
964
+            }
960
             else
965
             else
961
             {
966
             {
962
                 var indiv = dBContext.Individuals.Where(i => i.UserId == user.Id).FirstOrDefault();
967
                 var indiv = dBContext.Individuals.Where(i => i.UserId == user.Id).FirstOrDefault();

+ 1
- 1
UnivateProperties_API/Repository/Timeshare/WeekRepository.cs View File

110
 
110
 
111
             var listIds = list.Select(x => x.WeekUni).ToList();
111
             var listIds = list.Select(x => x.WeekUni).ToList();
112
 
112
 
113
-            foreach (var  item in GetTenderWeeks())
113
+            foreach (var item in GetTenderWeeks())
114
             {
114
             {
115
                 if (!listIds.Contains(item.WeekUni))
115
                 if (!listIds.Contains(item.WeekUni))
116
                     list.Add(item);
116
                     list.Add(item);

+ 50
- 4
UnivateProperties_API/Repository/Users/AgentRepository.cs View File

38
 
38
 
39
         public void Insert(Agent item)
39
         public void Insert(Agent item)
40
         {
40
         {
41
-            item.Id = NewId();
42
-            item.User.Role = Role.Agent;
43
-            if (_dbContext.Agents.Any(a => a.AgencyId == null))
41
+            //item.Id = NewId();
42
+            //item.User.Role = Role.Agent;
43
+            /*if (_dbContext.Agents.Any(a => a.AgencyId == null))
44
             {
44
             {
45
                 item.AgencyId = 10;
45
                 item.AgencyId = 10;
46
+            }*/
47
+            var agent = _dbContext.Agents.Where(x => x.Name == item.Name && x.Surname == item.Surname).FirstOrDefault();
48
+            var indiv = new Individual();
49
+
50
+            if (agent == null)
51
+            {
52
+                indiv = _dbContext.Individuals.Where(x => x.UserId == item.UserId).FirstOrDefault();
53
+                if (indiv != null)
54
+                {
55
+                    indiv.IsDeleted = true;
56
+                    _dbContext.Individuals.Update(indiv);
57
+                    _dbContext.SaveChanges();
58
+                }
59
+                item.Id = 0;
60
+                _dbContext.Agents.Add(item);
61
+                _dbContext.Users.Update(item.User);
62
+
63
+            }
64
+            else
65
+            {
66
+                if (agent.Name != item.Name)
67
+                {
68
+                    agent.Name = item.Name;
69
+                }
70
+                if (agent.Surname != item.Surname)
71
+                {
72
+                    agent.Surname = item.Surname;
73
+                }
74
+                if (agent.Email != item.Email)
75
+                {
76
+                    agent.Email = item.Email;
77
+                }
78
+                if (agent.Telephone != item.Telephone)
79
+                {
80
+                    agent.Telephone = item.Telephone;
81
+                }
82
+                if (agent.CellNumber != item.CellNumber)
83
+                {
84
+                    agent.CellNumber = item.CellNumber;
85
+                }
86
+                if (agent.AgencyId != item.AgencyId)
87
+                {
88
+                    agent.AgencyId = item.AgencyId;
89
+                }
90
+                _dbContext.Agents.Update(agent);
91
+                _dbContext.Users.Update(item.User);
46
             }
92
             }
47
-            _dbContext.Add(item);
93
+            
48
             Save();
94
             Save();
49
         }
95
         }
50
 
96
 

+ 3
- 3
UnivateProperties_API/Repository/Users/IndividualRepository.cs View File

25
 
25
 
26
         public List<Individual> GetAll()
26
         public List<Individual> GetAll()
27
         {
27
         {
28
-            foreach(var item in _dbContext.Individuals.Include("User").Include("Address").Include("BankAccount").ToList())
28
+            foreach (var item in _dbContext.Individuals.Include("User").Include("Address").Include("BankAccount").ToList())
29
             {
29
             {
30
                 var list = MyCommon.GetVisibleColumns(item);
30
                 var list = MyCommon.GetVisibleColumns(item);
31
             }
31
             }
32
-            return _dbContext.Individuals.Include("User").ToList();
32
+            return _dbContext.Individuals.Include("User").Include("Address").ToList();
33
         }
33
         }
34
 
34
 
35
         public Individual GetDetailed(Func<Individual, bool> first)
35
         public Individual GetDetailed(Func<Individual, bool> first)
130
 
130
 
131
         public void Update(DetailedOwner item)
131
         public void Update(DetailedOwner item)
132
         {
132
         {
133
-            var individual = _dbContext.Individuals.Include("Address").Include("BankAccount").FirstOrDefault(x => x.Id == item.Id);
133
+            var individual = _dbContext.Individuals.Include("User").Include("Address").Include("BankAccount").FirstOrDefault(x => x.Id == item.Id);
134
             if (individual != null)
134
             if (individual != null)
135
             {
135
             {
136
                 individual.UpdateFromDetailedOwner(item);
136
                 individual.UpdateFromDetailedOwner(item);

+ 104
- 0
UnivateProperties_API/Repository/Users/UserRoleRepository.cs View File

1
+using System;
2
+using System.Collections.Generic;
3
+using System.Linq;
4
+using UnivateProperties_API.Context;
5
+using UnivateProperties_API.Model.Users;
6
+
7
+namespace UnivateProperties_API.Repository.Users
8
+{
9
+    public class UserRoleRepository : IRepository<UserRole>
10
+    {
11
+        private readonly DataContext _dbContext;
12
+
13
+        public UserRoleRepository(DataContext dbc)
14
+        {
15
+            _dbContext = dbc;
16
+        }
17
+
18
+
19
+        public List<UserRole> GetAll() 
20
+        {
21
+            return _dbContext.Roles.ToList();
22
+        }
23
+
24
+        public List<UserRole> Get(Func<UserRole, bool> where)
25
+        {
26
+            return _dbContext.Roles.Where(where).ToList();
27
+        }
28
+
29
+        public UserRole GetDetailed(Func<UserRole, bool> first)
30
+        {
31
+            return _dbContext.Roles.FirstOrDefault(first);
32
+        }
33
+
34
+        public List<UserRole> GetDetailedAll()
35
+        {
36
+            throw new NotImplementedException();
37
+        }
38
+
39
+        public void Insert(UserRole item)
40
+        {
41
+            if (item != null)
42
+            {
43
+                _dbContext.Roles.Add(item);
44
+                Save();
45
+            }
46
+        }
47
+
48
+        public void Insert(IEnumerable<UserRole> items)
49
+        {
50
+            if (items != null)
51
+            {
52
+                foreach (var item in items)
53
+                {
54
+                    _dbContext.Roles.Add(item);
55
+                }
56
+                Save();
57
+            }
58
+        }
59
+
60
+        public void Remove(UserRole item)
61
+        {
62
+            if (item != null)
63
+            {
64
+                _dbContext.Roles.Remove(item);
65
+                Save();
66
+            }
67
+        }
68
+
69
+        public void Remove(IEnumerable<UserRole> items)
70
+        {
71
+            if (items != null)
72
+            {
73
+                foreach (var item in items)
74
+                {
75
+                    _dbContext.Roles.Remove(item);
76
+                }
77
+                Save();
78
+            }
79
+        }
80
+
81
+        public void RemoveAtId(int item)
82
+        {
83
+            var role = _dbContext.Roles.Where(x => x.Id == item).FirstOrDefault();
84
+            _dbContext.Roles.Remove(role);
85
+            Save();
86
+        }
87
+
88
+        public void Update(UserRole item)
89
+        {
90
+            var curRole = _dbContext.Roles.Where(x => x.Id == item.Id).FirstOrDefault();
91
+
92
+            if (curRole != item)
93
+            {
94
+                _dbContext.Roles.Update(item);
95
+                Save();
96
+            }
97
+        }
98
+
99
+        public void Save()
100
+        {
101
+            _dbContext.SaveChanges();
102
+        }
103
+    }
104
+}

+ 1
- 0
UnivateProperties_API/Startup.cs View File

144
             #region User
144
             #region User
145
             services.AddScoped<IRegisterRepository, RegisterRepository>();
145
             services.AddScoped<IRegisterRepository, RegisterRepository>();
146
             services.AddTransient<IRepository<Agent>, AgentRepository>();
146
             services.AddTransient<IRepository<Agent>, AgentRepository>();
147
+            services.AddTransient<IRepository<UserRole>, UserRoleRepository>();
147
             services.AddTransient<IRegisterRepository, RegisterRepository>();
148
             services.AddTransient<IRegisterRepository, RegisterRepository>();
148
             services.AddTransient<IRepository<Agency>, AgencyRepository>();
149
             services.AddTransient<IRepository<Agency>, AgencyRepository>();
149
             services.AddTransient<IUserRepository, UserRepository>();
150
             services.AddTransient<IUserRepository, UserRepository>();

Loading…
Cancel
Save