Exported from Notepad++
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Threading.Tasks;
5 using Microsoft.AspNetCore.Mvc;
6 using Microsoft.AspNetCore.Mvc.Rendering;
7 using Microsoft.EntityFrameworkCore;
8 using RealEstateMarket.Data;
9 using RealEstateMarket.Models;
10 using RealEstateMarket.Models.ViewModels;
11 using Microsoft.AspNetCore.Mvc.Routing;
12
13 namespace RealEstateMarket.Controllers
14 {
15 public class ClientsController : Controller
16 {
17 private readonly MarketDbContext _context;
18
19 public ClientsController(MarketDbContext context)
20 {
21 _context = context;
22 }
23
24 // GET: Clients
25 public async Task<IActionResult> Index(int? id)
26 {
27 var viewModel = new BrokeragesViewModel
28 {
29 Clients = await _context.Clients
30 .Include(Client => Client.Subscriptions)
31 .ThenInclude(Subscription => Subscription.Brokerage)
32 .AsNoTracking()
33 .OrderBy(Client => Client.LastName)
34 .ToListAsync()
35 };
36
37 if (id != null)
38 {
39 viewModel.Subscriptions = viewModel.Clients
40 .Where(Client => Client.Id == id).Single().Subscriptions;
41 }
42
43 return View(viewModel);
44 }
45
46 // GET: Clients/Details/Id
47 public async Task<IActionResult> Details(int? id)
48 {
49 if (id == null || _context.Clients == null)
50 {
51 return NotFound();
52 }
53
54 var client = await _context.Clients
55 .FirstOrDefaultAsync(m => m.Id == id);
56 if (client == null)
57 {
58 return NotFound();
59 }
60
61 return View(client);
62 }
63
64 // GET: Clients/Create
65 public IActionResult Create()
66 {
67 return View();
68 }
69
70 // POST: Clients/Create
71 [HttpPost]
72 [ValidateAntiForgeryToken]
73 public async Task<IActionResult> Create([Bind("LastName,FirstName,BirthDate")] Client client)
74 {
75 if (ModelState.IsValid)
76 {
77 _context.Add(client);
78 await _context.SaveChangesAsync();
79 return RedirectToAction(nameof(Index));
80 } else
81 {
82 throw new Exception("Invalid Client model");
83 }
84 // return View(client);
85 }
86
87 // GET: Clients/Edit/Id
88 public async Task<IActionResult> Edit(int? id)
89 {
90 if (id == null || _context.Clients == null)
91 {
92 return NotFound();
93 }
94
95 var client = await _context.Clients.FindAsync(id);
96 if (client == null)
97 {
98 return NotFound();
99 }
100 return View(client);
101 }
102
103 // POST: Clients/Edit/Id
104 [HttpPost]
105 [ValidateAntiForgeryToken]
106 public async Task<IActionResult> Edit(int id, [Bind("Id,LastName,FirstName,BirthDate")] Client client)
107 {
108 if (id != client.Id)
109 {
110 return NotFound();
111 }
112
113 if (ModelState.IsValid)
114 {
115 try
116 {
117 _context.Update(client);
118 await _context.SaveChangesAsync();
119 }
120 catch (DbUpdateConcurrencyException)
121 {
122 if (!ClientExists(client.Id))
123 {
124 return NotFound();
125 }
126 else
127 {
128 throw;
129 }
130 }
131 return RedirectToAction(nameof(Index));
132 }
133 return View(client);
134 }
135
136 // GET: Clients/Delete/Id
137 public async Task<IActionResult> Delete(int? id)
138 {
139 if (id == null || _context.Clients == null)
140 {
141 return NotFound();
142 }
143
144 var client = await _context.Clients
145 .FirstOrDefaultAsync(m => m.Id == id);
146 if (client == null)
147 {
148 return NotFound();
149 }
150
151 return View(client);
152 }
153
154 // POST: Clients/Delete/Id
155 [HttpPost, ActionName("Delete")]
156 [ValidateAntiForgeryToken]
157 public async Task<IActionResult> DeleteConfirmed(int id)
158 {
159 if (_context.Clients == null)
160 {
161 return Problem("Entity set 'MarketDbContext.Clients' is null.");
162 }
163 var client = await _context.Clients.FindAsync(id);
164 if (client != null)
165 {
166 _context.Clients.Remove(client);
167 }
168
169 await _context.SaveChangesAsync();
170 return RedirectToAction(nameof(Index));
171 }
172
173 // GET: Clients/Edit/Id
174 public async Task<IActionResult> EditSubscriptions(int? id)
175 {
176 if (id == null || _context.Clients == null)
177 {
178 return NotFound();
179 }
180
181 var viewModel = new ClientSubscriptionsViewModel
182 {
183 Client = await _context.Clients
184 .Include(Client => Client.Subscriptions)
185 .ThenInclude(Subscription => Subscription.Brokerage)
186 .ThenInclude(Brokerage => Brokerage.Subscriptions)
187 .SingleAsync(Client => Client.Id == id),
188 NonSubscribed = await _context.Brokerages
189 .Include(Brokerage=> Brokerage.Subscriptions)
190 .AsNoTracking()
191 .OrderBy(Brokerage => Brokerage.Title)
192 .ToListAsync()
193 };
194
195 if (viewModel.Client == null)
196 {
197 return NotFound();
198 }
199
200 // split brokerages into subscribed and unsubscribed
201 viewModel.Subscribed = viewModel.Client.Subscriptions.Select(Subscription => Subscription.Brokerage).OrderBy(Brokerage => Brokerage.Title);
202 viewModel.NonSubscribed = viewModel.NonSubscribed.Except(viewModel.Subscribed);
203
204 return View(viewModel);
205 }
206
207 public async Task<ActionResult> ChangeRegistration(int? clientId, string? brokerageId, bool? register)
208 {
209 if (clientId == null || brokerageId == null || register == null)
210 {
211 return NotFound();
212 }
213
214 if (register == false)
215 {
216 var subscription = await _context.Subscriptions
217 .FindAsync(clientId, brokerageId);
218
219 if (subscription == null)
220 {
221 return NotFound();
222 }
223
224 _context.Subscriptions.Remove(subscription);
225 }
226 else
227 {
228 // find client and brokerage
229 var brokerage = await _context.Brokerages.FindAsync(brokerageId);
230 var client = await _context.Clients.FindAsync(clientId);
231
232 if (brokerage == null || client == null)
233 {
234 return NotFound();
235 }
236
237 var subscription = new Subscription();
238 subscription.ClientId = client.Id;
239 subscription.BrokerageId = brokerage.Id;
240 subscription.Client = client;
241 subscription.Brokerage = brokerage;
242
243 // add to context
244 _context.Subscriptions.Add(subscription);
245 }
246
247 await _context.SaveChangesAsync();
248
249 return RedirectToAction("EditSubscriptions", "Clients", new { id = clientId });
250 }
251
252 private bool ClientExists(int id)
253 {
254 return _context.Clients.Any(e => e.Id == id);
255 }
256 }
257 }
258