From 4e6ec2c41b6f0869326d176abf6516f8273aa154 Mon Sep 17 00:00:00 2001 From: s459315 Date: Fri, 22 Jul 2022 16:45:33 +0200 Subject: [PATCH] Added Model For Displaying LoggedInUsed and Catched Api call --- RMDataManager/Controllers/UserController.cs | 6 +- .../Api}/APIHelper.cs | 36 ++++++++++- .../Api}/IAPIHelper.cs | 3 +- .../Models/AuthenticatedUser.cs | 14 +++++ .../Models/ILoggedInUserModel.cs | 14 +++++ .../Models/LoggedInUserModel.cs | 18 ++++++ .../Properties/AssemblyInfo.cs | 36 +++++++++++ .../RMWPFInterfaceLibrary.csproj | 62 +++++++++++++++++++ RMWPFInterfaceLibrary/packages.config | 5 ++ RMWPFUserInterface/BootStrapper.cs | 3 + RMWPFUserInterface/RMWPFUserInterface.csproj | 9 ++- .../ViewModels/LoginViewModel.cs | 5 ++ Retail_manager.sln | 6 ++ 13 files changed, 208 insertions(+), 9 deletions(-) rename {RMWPFUserInterface/Helpers => RMWPFInterfaceLibrary/Api}/APIHelper.cs (52%) rename {RMWPFUserInterface/Helpers => RMWPFInterfaceLibrary/Api}/IAPIHelper.cs (69%) create mode 100644 RMWPFInterfaceLibrary/Models/AuthenticatedUser.cs create mode 100644 RMWPFInterfaceLibrary/Models/ILoggedInUserModel.cs create mode 100644 RMWPFInterfaceLibrary/Models/LoggedInUserModel.cs create mode 100644 RMWPFInterfaceLibrary/Properties/AssemblyInfo.cs create mode 100644 RMWPFInterfaceLibrary/RMWPFInterfaceLibrary.csproj create mode 100644 RMWPFInterfaceLibrary/packages.config diff --git a/RMDataManager/Controllers/UserController.cs b/RMDataManager/Controllers/UserController.cs index fa2c241..fb5a183 100644 --- a/RMDataManager/Controllers/UserController.cs +++ b/RMDataManager/Controllers/UserController.cs @@ -4,19 +4,21 @@ using System.Web.Http; using RMDataManagerLibrary.Models; using System.Web; using Microsoft.AspNet.Identity; +using System.Linq; namespace RMDataManager.Controllers { [Authorize] public class UserController : ApiController { - public List GetById() + [HttpGet] + public UserModel GetById() { string userId = RequestContext.Principal.Identity.GetUserId(); UserData data = new UserData(); - return data.GetUserById(userId); + return data.GetUserById(userId).First(); } } } diff --git a/RMWPFUserInterface/Helpers/APIHelper.cs b/RMWPFInterfaceLibrary/Api/APIHelper.cs similarity index 52% rename from RMWPFUserInterface/Helpers/APIHelper.cs rename to RMWPFInterfaceLibrary/Api/APIHelper.cs index a289a0a..ef15489 100644 --- a/RMWPFUserInterface/Helpers/APIHelper.cs +++ b/RMWPFInterfaceLibrary/Api/APIHelper.cs @@ -1,4 +1,5 @@ -using RMWPFUserInterface.Models; +using RMWPFInterfaceLibrary.Models; +using RMWPFUserInterface.Models; using System; using System.Collections.Generic; using System.Configuration; @@ -8,14 +9,16 @@ using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; -namespace RMWPFUserInterface.Helpers +namespace RMWPFInterfaceLibrary.Api { public class APIHelper : IAPIHelper { private HttpClient apiClient; - public APIHelper() + private ILoggedInUserModel _loggedInUser; + public APIHelper(ILoggedInUserModel loggedInUser) { InitializeClient(); + _loggedInUser = loggedInUser; } private void InitializeClient() { @@ -49,5 +52,32 @@ namespace RMWPFUserInterface.Helpers } } } + + public async Task GetLogedInUserInfo(string token) + { + apiClient.DefaultRequestHeaders.Clear(); + apiClient.DefaultRequestHeaders.Accept.Clear(); + apiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + apiClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}"); + + using (HttpResponseMessage response = await apiClient.GetAsync("api/User")) + { + if (response.IsSuccessStatusCode) + { + var result = await response.Content.ReadAsAsync(); + _loggedInUser.CreatedDate = result.CreatedDate; + _loggedInUser.EmailAddress = result.EmailAddress; + _loggedInUser.FirstName = result.FirstName; + _loggedInUser.Id = result.Id; + _loggedInUser.LastName = result.LastName; + _loggedInUser.Token = token; + } + else + { + throw new Exception(response.ReasonPhrase); + } + + } + } } } diff --git a/RMWPFUserInterface/Helpers/IAPIHelper.cs b/RMWPFInterfaceLibrary/Api/IAPIHelper.cs similarity index 69% rename from RMWPFUserInterface/Helpers/IAPIHelper.cs rename to RMWPFInterfaceLibrary/Api/IAPIHelper.cs index 13c68c7..73ef29a 100644 --- a/RMWPFUserInterface/Helpers/IAPIHelper.cs +++ b/RMWPFInterfaceLibrary/Api/IAPIHelper.cs @@ -1,10 +1,11 @@ using RMWPFUserInterface.Models; using System.Threading.Tasks; -namespace RMWPFUserInterface.Helpers +namespace RMWPFInterfaceLibrary.Api { public interface IAPIHelper { Task Authenticate(string username, string password); + Task GetLogedInUserInfo(string token); } } \ No newline at end of file diff --git a/RMWPFInterfaceLibrary/Models/AuthenticatedUser.cs b/RMWPFInterfaceLibrary/Models/AuthenticatedUser.cs new file mode 100644 index 0000000..a91f84c --- /dev/null +++ b/RMWPFInterfaceLibrary/Models/AuthenticatedUser.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RMWPFUserInterface.Models +{ + public class AuthenticatedUser + { + public string Access_Token { get; set; } + public string UserName { get; set; } + } +} diff --git a/RMWPFInterfaceLibrary/Models/ILoggedInUserModel.cs b/RMWPFInterfaceLibrary/Models/ILoggedInUserModel.cs new file mode 100644 index 0000000..73328b5 --- /dev/null +++ b/RMWPFInterfaceLibrary/Models/ILoggedInUserModel.cs @@ -0,0 +1,14 @@ +using System; + +namespace RMWPFInterfaceLibrary.Models +{ + public interface ILoggedInUserModel + { + DateTime CreatedDate { get; set; } + string EmailAddress { get; set; } + string FirstName { get; set; } + string Id { get; set; } + string LastName { get; set; } + string Token { get; set; } + } +} \ No newline at end of file diff --git a/RMWPFInterfaceLibrary/Models/LoggedInUserModel.cs b/RMWPFInterfaceLibrary/Models/LoggedInUserModel.cs new file mode 100644 index 0000000..0d163de --- /dev/null +++ b/RMWPFInterfaceLibrary/Models/LoggedInUserModel.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RMWPFInterfaceLibrary.Models +{ + public class LoggedInUserModel : ILoggedInUserModel + { + public string Token { get; set; } + public string Id { get; set; } + public string FirstName { get; set; } + public string LastName { get; set; } + public string EmailAddress { get; set; } + public DateTime CreatedDate { get; set; } + } +} diff --git a/RMWPFInterfaceLibrary/Properties/AssemblyInfo.cs b/RMWPFInterfaceLibrary/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..80c1c38 --- /dev/null +++ b/RMWPFInterfaceLibrary/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("RMWPFInterfaceLibrary")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("RMWPFInterfaceLibrary")] +[assembly: AssemblyCopyright("Copyright © 2022")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("10b4a580-f9cf-4483-bf8b-02c5b20f182d")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/RMWPFInterfaceLibrary/RMWPFInterfaceLibrary.csproj b/RMWPFInterfaceLibrary/RMWPFInterfaceLibrary.csproj new file mode 100644 index 0000000..14f9cdb --- /dev/null +++ b/RMWPFInterfaceLibrary/RMWPFInterfaceLibrary.csproj @@ -0,0 +1,62 @@ + + + + + Debug + AnyCPU + {10B4A580-F9CF-4483-BF8B-02C5B20F182D} + Library + Properties + RMWPFInterfaceLibrary + RMWPFInterfaceLibrary + v4.7.2 + 512 + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll + + + + + + ..\packages\Microsoft.AspNet.WebApi.Client.5.2.9\lib\net45\System.Net.Http.Formatting.dll + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/RMWPFInterfaceLibrary/packages.config b/RMWPFInterfaceLibrary/packages.config new file mode 100644 index 0000000..e7a4b49 --- /dev/null +++ b/RMWPFInterfaceLibrary/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/RMWPFUserInterface/BootStrapper.cs b/RMWPFUserInterface/BootStrapper.cs index b647721..b62ce16 100644 --- a/RMWPFUserInterface/BootStrapper.cs +++ b/RMWPFUserInterface/BootStrapper.cs @@ -1,4 +1,6 @@ using Caliburn.Micro; +using RMWPFInterfaceLibrary.Api; +using RMWPFInterfaceLibrary.Models; using RMWPFUserInterface.Helpers; using RMWPFUserInterface.ViewModels; using System; @@ -30,6 +32,7 @@ namespace RMWPFUserInterface _container .Singleton() .Singleton() + .Singleton() .Singleton(); GetType().Assembly.GetTypes() diff --git a/RMWPFUserInterface/RMWPFUserInterface.csproj b/RMWPFUserInterface/RMWPFUserInterface.csproj index 4b42ed7..cc8a1a6 100644 --- a/RMWPFUserInterface/RMWPFUserInterface.csproj +++ b/RMWPFUserInterface/RMWPFUserInterface.csproj @@ -81,8 +81,6 @@ Designer - - @@ -133,6 +131,11 @@ - + + + {10b4a580-f9cf-4483-bf8b-02c5b20f182d} + RMWPFInterfaceLibrary + + \ No newline at end of file diff --git a/RMWPFUserInterface/ViewModels/LoginViewModel.cs b/RMWPFUserInterface/ViewModels/LoginViewModel.cs index ea595e6..121b9a2 100644 --- a/RMWPFUserInterface/ViewModels/LoginViewModel.cs +++ b/RMWPFUserInterface/ViewModels/LoginViewModel.cs @@ -1,4 +1,5 @@ using Caliburn.Micro; +using RMWPFInterfaceLibrary.Api; using RMWPFUserInterface.Helpers; using System; using System.Collections.Generic; @@ -91,6 +92,10 @@ namespace RMWPFUserInterface.ViewModels { ErrorMessage = ""; var result = await _apiHelper.Authenticate(UserName, Password); + + // Capture more information + + await _apiHelper.GetLogedInUserInfo(result.Access_Token); } catch (Exception ex) { diff --git a/Retail_manager.sln b/Retail_manager.sln index 569a991..c38f31e 100644 --- a/Retail_manager.sln +++ b/Retail_manager.sln @@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RMWPFUserInterface", "RMWPF EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RMDataManagerLibrary", "RMDataManagerLibrary\RMDataManagerLibrary.csproj", "{6669F7DC-4B07-497F-BDEE-5333DB3EDBF4}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RMWPFInterfaceLibrary", "RMWPFInterfaceLibrary\RMWPFInterfaceLibrary.csproj", "{10B4A580-F9CF-4483-BF8B-02C5B20F182D}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -35,6 +37,10 @@ Global {6669F7DC-4B07-497F-BDEE-5333DB3EDBF4}.Debug|Any CPU.Build.0 = Debug|Any CPU {6669F7DC-4B07-497F-BDEE-5333DB3EDBF4}.Release|Any CPU.ActiveCfg = Release|Any CPU {6669F7DC-4B07-497F-BDEE-5333DB3EDBF4}.Release|Any CPU.Build.0 = Release|Any CPU + {10B4A580-F9CF-4483-BF8B-02C5B20F182D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {10B4A580-F9CF-4483-BF8B-02C5B20F182D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {10B4A580-F9CF-4483-BF8B-02C5B20F182D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {10B4A580-F9CF-4483-BF8B-02C5B20F182D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE