Skip to content

Commit 7a3409b

Browse files
committed
TD-5761: Latest changes.
1 parent a53b976 commit 7a3409b

File tree

13 files changed

+710
-444
lines changed

13 files changed

+710
-444
lines changed

LearningHub.Nhs.WebUI/Controllers/MyAccountController.cs

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
using System;
44
using System.Collections.Generic;
5+
using System.Drawing;
56
using System.Linq;
67
using System.Net.Http;
78
using System.Threading.Tasks;
@@ -20,11 +21,14 @@
2021
using Microsoft.AspNetCore.Authorization;
2122
using Microsoft.AspNetCore.Hosting;
2223
using Microsoft.AspNetCore.Mvc;
24+
using Microsoft.AspNetCore.Mvc.Rendering;
2325
using Microsoft.AspNetCore.Routing;
2426
using Microsoft.Extensions.Configuration;
2527
using Microsoft.Extensions.Logging;
2628
using Microsoft.Extensions.Options;
29+
using Newtonsoft.Json.Linq;
2730
using NHSUKViewComponents.Web.ViewModels;
31+
using static IdentityModel.ClaimComparer;
2832
using ChangePasswordViewModel = LearningHub.Nhs.WebUI.Models.UserProfile.ChangePasswordViewModel;
2933
using IConfiguration = Microsoft.Extensions.Configuration.IConfiguration;
3034

@@ -197,25 +201,66 @@ public async Task<IActionResult> UpdatePersonalDetails(MyAccountPersonalDetailsV
197201
/// <summary>
198202
/// ChangeLocation.
199203
/// </summary>
200-
/// <param name="selectedCountryId">country id.</param>
201204
/// <returns>ActionResult.</returns>
202205
[HttpGet]
203206
[Route("myaccount/ChangeLocation")]
204-
public async Task<IActionResult> ChangeLocation(int? selectedCountryId)
207+
public async Task<IActionResult> ChangeLocation()
205208
{
206209
this.TempData.Clear();
207-
var userLocationViewModel = await this.userService.GetUserLocationDetailsAsync();
208-
var userProfileSummary = await this.userService.GetUserProfileSummaryAsync();
209-
if (selectedCountryId.HasValue)
210+
var userLocationViewModel = await this.userService.GetMyAccountLocationDetailsAsync();
211+
var allUKcountries = await this.countryService.GetAllUKCountries();
212+
var allUKnoncountries = await this.countryService.GetAllNonUKCountries();
213+
var regions = await this.regionService.GetAllAsync();
214+
215+
List<RadiosItemViewModel> radio = new List<RadiosItemViewModel>();
216+
foreach (var country in allUKcountries)
210217
{
211-
userLocationViewModel.SelectedCountryId = selectedCountryId;
218+
var newradio = new RadiosItemViewModel(country.Id.ToString(), country.Name, false, country.Name);
219+
radio.Add(newradio);
212220
}
213221

214-
await this.multiPageFormService.SetMultiPageFormData(
215-
userLocationViewModel,
216-
MultiPageFormDataFeature.AddRegistrationPrompt,
217-
this.TempData);
218-
return this.View("ChangeLocation", new Tuple<UserProfileSummaryViewModel, UserLocationViewModel>(userProfileSummary, userLocationViewModel));
222+
if (allUKnoncountries.Any(v => v.Id == userLocationViewModel.SelectedCountryId))
223+
{
224+
////userLocationViewModel.SelectedOtherCountry = true;
225+
userLocationViewModel.SelectedOtherCountryId = userLocationViewModel.SelectedCountryId;
226+
userLocationViewModel.SelectedCountryId = 0;
227+
}
228+
229+
userLocationViewModel.Country = radio;
230+
userLocationViewModel.OtherCountryOptions = SelectListHelper.MapOptionsToSelectListItems(allUKnoncountries, userLocationViewModel.SelectedOtherCountryId);
231+
232+
userLocationViewModel.RegionOptions = SelectListHelper.MapOptionsToSelectListItems(regions, userLocationViewModel.SelectedRegionId);
233+
return this.View("MyAccountChangeLocation", userLocationViewModel);
234+
}
235+
236+
/// <summary>
237+
/// To update location details.
238+
/// </summary>
239+
/// <param name="model">model.</param>
240+
/// <returns>The <see cref="Task{IActionResult}"/>.</returns>
241+
[HttpPost]
242+
public async Task<IActionResult> UpdateMyAccountLocationDetails(MyAccountLocationViewModel model)
243+
{
244+
if (this.ModelState.IsValid)
245+
{
246+
if (model?.SelectedCountryId == 0)
247+
{
248+
model.SelectedCountryId = model.SelectedOtherCountryId;
249+
}
250+
251+
if (model.SelectedCountryId != 1)
252+
{
253+
model.SelectedRegionId = null;
254+
}
255+
256+
await this.userService.UpdateMyAccountLocationDetailsAsync(this.CurrentUserId, model);
257+
this.ViewBag.SuccessMessage = CommonValidationErrorMessages.CountrySuccessMessage;
258+
return this.View("SuccessMessage");
259+
}
260+
else
261+
{
262+
return this.View("MyAccountChangeLocation", model);
263+
}
219264
}
220265

221266
/// <summary>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace LearningHub.Nhs.WebUI.Helpers
2+
{
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using elfhHub.Nhs.Models.Common;
6+
using Microsoft.AspNetCore.Mvc.Rendering;
7+
8+
/// <summary>
9+
/// SelectListHelper.
10+
/// </summary>
11+
public static class SelectListHelper
12+
{
13+
/// <summary>
14+
/// MapOptionsToSelectListItems.
15+
/// </summary>
16+
/// <param name="options">options.</param>
17+
/// <param name="selectedId">selectedId.</param>
18+
/// <returns>SelectListItem.</returns>
19+
public static IEnumerable<SelectListItem> MapOptionsToSelectListItems(IEnumerable<GenericListViewModel> options, int? selectedId = null)
20+
{
21+
return options.Select(o => new SelectListItem(o.Name, o.Id.ToString(), o.Id == selectedId)).ToList();
22+
}
23+
}
24+
}

LearningHub.Nhs.WebUI/Interfaces/ICountryService.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,17 @@ public interface ICountryService
2929
/// </summary>
3030
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
3131
Task<List<Country>> GetAllAsync();
32+
33+
/// <summary>
34+
/// The GetAllUKCountries.
35+
/// </summary>
36+
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
37+
Task<List<GenericListViewModel>> GetAllUKCountries();
38+
39+
/// <summary>
40+
/// The GetAllNonUKCountries.
41+
/// </summary>
42+
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
43+
Task<List<GenericListViewModel>> GetAllNonUKCountries();
3244
}
3345
}

LearningHub.Nhs.WebUI/Interfaces/IUserService.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,5 +504,19 @@ public interface IUserService
504504
/// </summary>
505505
/// <returns>The <see cref="Task{MyAccountSecurityViewModel}"/>.</returns>
506506
Task<MyAccountSecurityViewModel> GetMyAccountSecurityDetailsAsync();
507+
508+
/// <summary>
509+
/// Get MyAccount Location Details Async.
510+
/// </summary>
511+
/// <returns>The <see cref="Task{MyAccountLocationViewModel}"/>.</returns>
512+
Task<MyAccountLocationViewModel> GetMyAccountLocationDetailsAsync();
513+
514+
/// <summary>
515+
/// Update MyAccount Location Details Async.
516+
/// </summary>
517+
/// <param name="userId">userId.</param>
518+
/// <param name="model">MyAccountLocationViewModel.</param>
519+
/// <returns>The <see cref="Task"/>.</returns>
520+
Task UpdateMyAccountLocationDetailsAsync(int userId, MyAccountLocationViewModel model);
507521
}
508522
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
namespace LearningHub.Nhs.WebUI.Models.UserProfile
2+
{
3+
using System.Collections.Generic;
4+
using System.ComponentModel;
5+
using System.ComponentModel.DataAnnotations;
6+
using elfhHub.Nhs.Models.Common;
7+
using Microsoft.AspNetCore.Mvc.Rendering;
8+
using NHSUKViewComponents.Web.ViewModels;
9+
10+
/// <summary>
11+
/// Defines the <see cref="MyAccountLocationViewModel" />.
12+
/// </summary>
13+
public class MyAccountLocationViewModel
14+
{
15+
/// <summary>
16+
/// Gets or sets the country id.
17+
/// </summary>
18+
[Required(ErrorMessage = "Select a country.")]
19+
[DisplayName("Country")]
20+
public int? SelectedCountryId { get; set; }
21+
22+
/// <summary>
23+
/// Gets or sets the region id.
24+
/// </summary>
25+
[Required(ErrorMessage = "Select a region.")]
26+
[DisplayName("Region")]
27+
public int? SelectedRegionId { get; set; }
28+
29+
/// <summary>
30+
/// Gets or sets selected country name.
31+
/// </summary>
32+
public string SelectedCountryName { get; set; }
33+
34+
/// <summary>
35+
/// Gets or sets selected region name.
36+
/// </summary>
37+
public string SelectedRegionName { get; set; }
38+
39+
/// <summary>
40+
/// Gets or sets the country id.
41+
/// </summary>
42+
////[Required(ErrorMessage = "Select a country.")]
43+
////[DisplayName("Country")]
44+
public int? SelectedOtherCountryId { get; set; }
45+
46+
/// <summary>
47+
/// Gets or sets a value indicating whether SelectedOtherCountry.
48+
/// </summary>
49+
public bool SelectedOtherCountry { get; set; }
50+
51+
/// <summary>
52+
/// Gets or sets the Country.
53+
/// </summary>
54+
public List<RadiosItemViewModel> Country { get; set; }
55+
56+
/// <summary>
57+
/// Gets or sets the OtherCountryOptions.
58+
/// </summary>
59+
public IEnumerable<SelectListItem> OtherCountryOptions { get; set; }
60+
61+
/// <summary>
62+
/// Gets or sets the RegionOptions.
63+
/// </summary>
64+
public IEnumerable<SelectListItem> RegionOptions { get; set; }
65+
}
66+
}

LearningHub.Nhs.WebUI/Services/CountryService.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,61 @@ public async Task<List<Country>> GetAllAsync()
118118

119119
return viewmodel;
120120
}
121+
122+
/// <summary>
123+
/// Get a list of Uk Country records..
124+
/// </summary>
125+
/// <returns>The <see cref="T:Task{List{GenericListViewModel}}"/>.</returns>
126+
public async Task<List<GenericListViewModel>> GetAllUKCountries()
127+
{
128+
List<GenericListViewModel> viewmodel = null;
129+
130+
var client = await this.userApiHttpClient.GetClientAsync();
131+
132+
var request = $"Country/GetAllUKCountries";
133+
var response = await client.GetAsync(request).ConfigureAwait(false);
134+
135+
if (response.IsSuccessStatusCode)
136+
{
137+
var result = response.Content.ReadAsStringAsync().Result;
138+
viewmodel = JsonConvert.DeserializeObject<List<GenericListViewModel>>(result);
139+
}
140+
else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized
141+
||
142+
response.StatusCode == System.Net.HttpStatusCode.Forbidden)
143+
{
144+
throw new Exception("AccessDenied");
145+
}
146+
147+
return viewmodel;
148+
}
149+
150+
/// <summary>
151+
/// Get a list of non Uk Country records.
152+
/// </summary>
153+
/// <returns>The <see cref="T:Task{List{GenericListViewModel}}"/>.</returns>
154+
public async Task<List<GenericListViewModel>> GetAllNonUKCountries()
155+
{
156+
List<GenericListViewModel> viewmodel = null;
157+
158+
var client = await this.userApiHttpClient.GetClientAsync();
159+
160+
var request = $"Country/GetAllNonUKCountries";
161+
var response = await client.GetAsync(request).ConfigureAwait(false);
162+
163+
if (response.IsSuccessStatusCode)
164+
{
165+
var result = response.Content.ReadAsStringAsync().Result;
166+
viewmodel = JsonConvert.DeserializeObject<List<GenericListViewModel>>(result);
167+
}
168+
else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized
169+
||
170+
response.StatusCode == System.Net.HttpStatusCode.Forbidden)
171+
{
172+
throw new Exception("AccessDenied");
173+
}
174+
175+
return viewmodel;
176+
}
121177
}
122178
}

LearningHub.Nhs.WebUI/Services/UserService.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,6 +2029,41 @@ public async Task<MyAccountEmploymentDetailsViewModel> GetMyEmploymentDetailsAsy
20292029
return viewModel;
20302030
}
20312031

2032+
/// <summary>
2033+
/// GetMyAccountLocationDetailsAsync.
2034+
/// </summary>
2035+
/// <returns>The <see cref="Task"/>.</returns>
2036+
public async Task<MyAccountLocationViewModel> GetMyAccountLocationDetailsAsync()
2037+
{
2038+
MyAccountLocationViewModel viewModel = null;
2039+
2040+
var personalDetailsModel = await this.GetCurrentUserPersonalDetailsAsync();
2041+
var employmentViewModel = await this.GetPrimaryUserEmploymentForUser(personalDetailsModel.UserId);
2042+
2043+
if (personalDetailsModel != null)
2044+
{
2045+
viewModel = new MyAccountLocationViewModel
2046+
{
2047+
SelectedCountryId = personalDetailsModel.CountryId,
2048+
SelectedRegionId = personalDetailsModel.RegionId,
2049+
};
2050+
2051+
if (personalDetailsModel.CountryId.HasValue)
2052+
{
2053+
var country = await this.countryService.GetByIdAsync(personalDetailsModel.CountryId.Value);
2054+
viewModel.SelectedCountryName = country.Name;
2055+
}
2056+
2057+
if (personalDetailsModel.RegionId.HasValue)
2058+
{
2059+
var region = await this.regionService.GetByIdAsync(personalDetailsModel.RegionId.Value);
2060+
viewModel.SelectedRegionName = region.Name;
2061+
}
2062+
}
2063+
2064+
return viewModel;
2065+
}
2066+
20322067
/// <summary>
20332068
/// Get MyAccount Security Details Async.
20342069
/// </summary>
@@ -2056,5 +2091,38 @@ public async Task<MyAccountSecurityViewModel> GetMyAccountSecurityDetailsAsync()
20562091

20572092
return viewModel;
20582093
}
2094+
2095+
/// <summary>
2096+
/// Update MyAccount Location Details Async.
2097+
/// </summary>
2098+
/// <param name="userId">userId.</param>
2099+
/// <param name="model">MyAccountLocationViewModel.</param>
2100+
/// <returns>The <see cref="Task"/>.</returns>
2101+
public async Task UpdateMyAccountLocationDetailsAsync(int userId, MyAccountLocationViewModel model)
2102+
{
2103+
PersonalDetailsViewModel personalDetailsViewModel = new PersonalDetailsViewModel
2104+
{
2105+
UserId = userId,
2106+
CountryId = model.SelectedCountryId,
2107+
RegionId = model.SelectedRegionId,
2108+
};
2109+
2110+
var json = JsonConvert.SerializeObject(personalDetailsViewModel);
2111+
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
2112+
2113+
var client = await this.userApiHttpClient.GetClientAsync();
2114+
var request = $"ElfhUser/UpdateLocationDetails";
2115+
var response = await client.PutAsync(request, stringContent).ConfigureAwait(false);
2116+
2117+
if (response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.Forbidden)
2118+
{
2119+
throw new Exception("AccessDenied");
2120+
}
2121+
2122+
if (!response.IsSuccessStatusCode)
2123+
{
2124+
throw new Exception("Update user location details failed!");
2125+
}
2126+
}
20592127
}
20602128
}

0 commit comments

Comments
 (0)