From 93fa738d724fedb980acc990a52b291916254174 Mon Sep 17 00:00:00 2001 From: Kham Date: Fri, 2 Dec 2022 15:53:33 +1100 Subject: [PATCH] update to .NET 6.0 --- CarParkValidationAPI.csproj | 2 +- Controllers/UsersController.cs | 10 ++ Entities/Role.cs | 8 -- Interface/IConcessionType.cs | 1 + Interface/IConcessionValidation.cs | 2 +- Interface/IUser.cs | 1 + Interface/IValidationPoint.cs | 1 + Repository/ConcessionTypeRepository.cs | 30 ++++- Repository/UserFakeRepository.cs | 27 ++-- Repository/UserRepository.cs | 131 +++++++++++++++++++- Repository/ValidationPointFackRepository.cs | 14 ++- Repository/ValidationPointRepository.cs | 31 ++++- 12 files changed, 235 insertions(+), 23 deletions(-) delete mode 100644 Entities/Role.cs diff --git a/CarParkValidationAPI.csproj b/CarParkValidationAPI.csproj index e2b9b9d..19d5129 100644 --- a/CarParkValidationAPI.csproj +++ b/CarParkValidationAPI.csproj @@ -1,7 +1,7 @@  - net5.0 + net6.0 diff --git a/Controllers/UsersController.cs b/Controllers/UsersController.cs index 6e9750a..a557786 100644 --- a/Controllers/UsersController.cs +++ b/Controllers/UsersController.cs @@ -5,6 +5,7 @@ using CarParkValidationAPI.Models.Users; using CarParkValidationAPI.Interface; using System.Threading.Tasks; using CarParkValidationAPI.Models; +using System; namespace CarParkValidationAPI.Controllers { @@ -78,5 +79,14 @@ namespace CarParkValidationAPI.Controllers var user = await _userService.GetById(id); return Ok(user); } + /* download the excel file */ + [HttpGet("[action]")] + public async Task GetReportFile() + { + // this.Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition"); + var result = await _userService.GetAllUserReport(); // .GetReport(fromDate, toDate); + string fileContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; //Map(result.Data.FileName); + return File(result.Data.Content, fileContentType, result.Data.FileName); + } } } diff --git a/Entities/Role.cs b/Entities/Role.cs deleted file mode 100644 index f000a23..0000000 --- a/Entities/Role.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace CarParkValidationAPI.Entities -{ - public enum Role - { - Admin, - User - } -} \ No newline at end of file diff --git a/Interface/IConcessionType.cs b/Interface/IConcessionType.cs index e3f2a02..3eaebae 100644 --- a/Interface/IConcessionType.cs +++ b/Interface/IConcessionType.cs @@ -14,6 +14,7 @@ namespace CarParkValidationAPI.Interface Task> SaveConcessionTypeAsync(ConcessionTypeDto item); Task> LoadConcessionTypeByIdAsync(int id); Task> DeleteConcessionTypeAsync(int id); + Task>> GetConcessionTypeDicAsync(); void SetCurrentUser(string username); } } diff --git a/Interface/IConcessionValidation.cs b/Interface/IConcessionValidation.cs index ccf00f8..ab7adde 100644 --- a/Interface/IConcessionValidation.cs +++ b/Interface/IConcessionValidation.cs @@ -16,7 +16,7 @@ namespace CarParkValidationAPI.Interface Task> SaveConcessionValidationAsync(ConcessionValidationDto item); Task> LoadConcessionValidationByIdAsync(int id); Task> DeleteConcessionValidationAsync(int id); - Task> GetReport(DateTime fromDate, DateTime toDate); + void SetCurrentUser(string username); } } diff --git a/Interface/IUser.cs b/Interface/IUser.cs index 8a6b9fd..392939b 100644 --- a/Interface/IUser.cs +++ b/Interface/IUser.cs @@ -17,5 +17,6 @@ namespace CarParkValidationAPI.Interface Task> GetById(int id); Task> SearchApiStaff(string staffLinkNo); Task> SearchADStaff(string staffLinkNo); + Task> GetAllUserReport(); } } diff --git a/Interface/IValidationPoint.cs b/Interface/IValidationPoint.cs index 68671e0..485e0b6 100644 --- a/Interface/IValidationPoint.cs +++ b/Interface/IValidationPoint.cs @@ -14,6 +14,7 @@ namespace CarParkValidationAPI.Interface Task> SaveValidationPointAsync(ValidationPointDto item); Task> LoadValidationPointByIdAsync(int id); Task> DeleteValidationPointAsync(int id); + Task>> GetValidationPointDicAsync(); void SetCurrentUser(string username); } } diff --git a/Repository/ConcessionTypeRepository.cs b/Repository/ConcessionTypeRepository.cs index 3bab604..4b9d951 100644 --- a/Repository/ConcessionTypeRepository.cs +++ b/Repository/ConcessionTypeRepository.cs @@ -195,7 +195,35 @@ namespace CarParkValidationAPI.Repository Message = error }; } + public async Task>> GetConcessionTypeDicAsync() + { + Dictionary resultList = new(); + + int statusCode = 0; + string error = ""; + try + { + resultList = await _context.ConcessionTypes.Where(x => x.Active == true) + .Select(item => new LookupCodeDto { Id = item.ConcessionTypeID, Description = item.ConcessionDesc ?? "" }) + .ToDictionaryAsync(x => x.Id); - } + statusCode = 1; + } + catch (Exception ex) + { + error = ex.ToString(); + statusCode = 0; + throw ex; + } + return new ResultModel>() + { + Data = resultList, + StatusCode = statusCode, + Message = error + }; + } + + + } } \ No newline at end of file diff --git a/Repository/UserFakeRepository.cs b/Repository/UserFakeRepository.cs index 785d336..f26d9c0 100644 --- a/Repository/UserFakeRepository.cs +++ b/Repository/UserFakeRepository.cs @@ -13,6 +13,7 @@ using CarParkValidationAPI.Interface; using Microsoft.EntityFrameworkCore; using CommonAD.Models; using CommonAD; +using Microsoft.AspNetCore.Http; namespace CarParkValidationAPI.Repository { @@ -30,7 +31,7 @@ namespace CarParkValidationAPI.Repository _context = context; _jwtUtils = jwtUtils; _appSettings = appSettings.Value; - + } public AuthenticateResponse Authenticate(AuthenticateRequest model) { @@ -84,7 +85,7 @@ namespace CarParkValidationAPI.Repository AuthenticateResponse retval = null; int statuscode = 0; string webAPIUrl = _appSettings.LoginWebAPI; - + var loginInfo = await CommonAD.WebAPIUtil.GetLoginADAsync(webAPIUrl, "CheckADCredentials", model.Username, model.Password); if (loginInfo != null && loginInfo.result) { @@ -132,13 +133,13 @@ namespace CarParkValidationAPI.Repository LDAPUser = _appSettings.LDAPUser, ADTimeOutStr = _appSettings.ADTimeOut }; - myUser = await Task.Run(() => LoginADStaff(adConfig,model.Username, model.Password)); + myUser = await Task.Run(() => LoginADStaff(adConfig, model.Username, model.Password)); if (!string.IsNullOrEmpty(myUser.Username)) - { - statuscode = 1; + { + statuscode = 1; // authentication successful so generate jwt token var jwtToken = _jwtUtils.GenerateJwtToken(myUser); - + retval = new AuthenticateResponse(myUser, jwtToken); } return new ResultModel() @@ -159,7 +160,7 @@ namespace CarParkValidationAPI.Repository } - public async Task> GetById(int id) + public async Task> GetById(int id) { var user = await _context.Users.FindAsync(id); if (user == null) throw new KeyNotFoundException("User not found"); @@ -190,7 +191,7 @@ namespace CarParkValidationAPI.Repository Data = myUser, StatusCode = statuscode }; - // return myUser; + // return myUser; } @@ -236,7 +237,7 @@ namespace CarParkValidationAPI.Repository { ADStaffLink staffLink = new ADStaffLink(adConfig); - MyADObject myADObj = staffLink.CheckADCredentials(username,password); + MyADObject myADObj = staffLink.CheckADCredentials(username, password); User user = new() { Username = myADObj.stafflinkNo, @@ -249,5 +250,13 @@ namespace CarParkValidationAPI.Repository // return null; } + public async Task> GetAllUserReport() + { + return new ResultModel + { + Data = null, + StatusCode = 1 + }; + } } } \ No newline at end of file diff --git a/Repository/UserRepository.cs b/Repository/UserRepository.cs index bd8f6d8..b1e957e 100644 --- a/Repository/UserRepository.cs +++ b/Repository/UserRepository.cs @@ -13,6 +13,9 @@ using CarParkValidationAPI.Interface; using Microsoft.EntityFrameworkCore; using CommonAD.Models; using CommonAD; +using SpreadsheetLight; +using System.IO; +using System; namespace CarParkValidationAPI.Repository { @@ -21,14 +24,18 @@ namespace CarParkValidationAPI.Repository private DataContext _context; private IJwtUtils _jwtUtils; private readonly AppSettings _appSettings; + private readonly IValidationPoint _validationPoint; + private Dictionary _validationPointDic; public UserRepository( DataContext context, IJwtUtils jwtUtils, + IValidationPoint validationPoint, IOptions appSettings) { _context = context; _jwtUtils = jwtUtils; + _validationPoint = validationPoint; _appSettings = appSettings.Value; } @@ -53,6 +60,7 @@ namespace CarParkValidationAPI.Repository User myUser = null; AuthenticateResponse retval = null; int statuscode = 0; + string error = ""; string webAPIUrl = _appSettings.LoginWebAPI; var loginInfo = await CommonAD.WebAPIUtil.PostLoginADAsync(webAPIUrl, "CheckCredentials", model.Username, model.Password); @@ -77,6 +85,7 @@ namespace CarParkValidationAPI.Repository else //not allow { statuscode = 0; + error = "username is not exist in admimuser."; myUser.Role = AdminRole.Normal; } // validate @@ -88,11 +97,14 @@ namespace CarParkValidationAPI.Repository retval = new AuthenticateResponse(myUser, jwtToken); } + else + error = "error username or password is invalid"; return new ResultModel() { Data = retval, - StatusCode = statuscode + StatusCode = statuscode, + Message = error }; } @@ -225,5 +237,122 @@ namespace CarParkValidationAPI.Repository // return null; } + private string GetVPDesc(int? idn) + { + string result = ""; + int id = idn ?? 1; + if (_validationPointDic.ContainsKey(id)) + { + result = _validationPointDic[id].Description; + } + return result; + } + + public async Task> GetAllUserReport() + { + //excel SLDocument sl = new SLDocument(); + int statusCode = 1; + AdminUser item; + int col = 1; + int row = 1; + SLDocument sl = new SLDocument(); + var ret = await _validationPoint.GetValidationPointDicAsync(); + if (ret.StatusCode == 1) + _validationPointDic = ret.Data; + //first sheetName + sl.RenameWorksheet(SLDocument.DefaultFirstSheetName, "All User"); + SLStyle style = sl.CreateStyle(); + style.SetFontUnderline(DocumentFormat.OpenXml.Spreadsheet.UnderlineValues.Single); + style.SetFontBold(true); + style.SetFont("Arial Black", 16); + sl.SetCellStyle(1, 5, style); + sl.SetRowHeight(1, 1, 25); + sl.SetCellValue(row++, 5, "The User Report"); + + sl.SetCellValue(1, 6, " " + DateTime.Now.ToString("ddd dd/MM/yyyy HH:mm")); + + int startTitleRow = 3; + row = startTitleRow; + //make the title bold + style = sl.CreateStyle(); + style.Font.Bold = true; + sl.SetRowStyle(startTitleRow, style); + //set it height + sl.SetRowHeight(startTitleRow, startTitleRow, 25); + // add the title first + sl.SetCellValue(row, col++, "Stafflink ID"); + sl.SetCellValue(row, col++, "Name"); + sl.SetCellValue(row, col++, "Role"); + sl.SetCellValue(row, col++, "Validation Point"); + sl.SetCellValue(row, col++, "Active"); + + sl.FreezePanes(row,0); //frozen the row. + //wrap text on is concession Expiry + style = sl.CreateStyle(); + style.SetWrapText(true); + sl.SetColumnStyle(4, style); + + //format date + + style = sl.CreateStyle(); + style.FormatCode = "dd/MM/yyyy HH:MM"; + sl.SetColumnStyle(1, style); + //make column width + sl.SetColumnWidth(1, 20); + sl.SetColumnWidth(2, 50); + sl.SetColumnWidth(3, 20); + sl.SetColumnWidth(4, 30); + sl.SetColumnWidth(5, 20); + /* + public enum AdminRole + { + Normal = 1, + Admin = 2 + } + */ + int startRowFrom = startTitleRow + 1; + var list = await _context.AdminUsers.ToListAsync(); + for (int i = 0; i < list.Count; i++) + { + col = 1; + item = list[i]; + row = i + startRowFrom; + sl.SetCellValue(row, col++, item.StafflinkNo); + sl.SetCellValue(row, col++, item.Name); + sl.SetCellValue(row, col++, (int) item.RoleType == 1 ? "Normal": "Admin"); + sl.SetCellValue(row, col++, GetVPDesc(item.ValidationPointID) ); + sl.SetCellValue(row, col++, item.Active ? "Yes" : "No"); + + } + byte[] array; + // sl.SaveAs("C:\\Temp\\Report\\ConcessionValidationDate" + DateTime.Now.ToString("yyyyMMddHHmm") + ".xlsx"); + using (MemoryStream ws = new MemoryStream()) + { + sl.SaveAs(ws); + array = ws.ToArray(); + /* + using (FileStream fs = new FileStream("c:\\temp\\Report\\conReport.xlsx",FileMode.Create,FileAccess.Write)) + { + ws.WriteTo(fs); + fs.Close(); + } + */ + ws.Close(); + } + + var result = new FileContent + { + Content = array, + FileName = "UserReport" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm") + ".xlsx", + + }; + + return new ResultModel + { + Data = result, + StatusCode = statusCode + }; + + } } } \ No newline at end of file diff --git a/Repository/ValidationPointFackRepository.cs b/Repository/ValidationPointFackRepository.cs index 7e1fa79..aef94e5 100644 --- a/Repository/ValidationPointFackRepository.cs +++ b/Repository/ValidationPointFackRepository.cs @@ -2,6 +2,8 @@ using CarParkValidationAPI.Entities; using CarParkValidationAPI.Interface; using CarParkValidationAPI.Models; +using DocumentFormat.OpenXml.VariantTypes; +using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Options; using System; @@ -189,7 +191,17 @@ namespace CarParkValidationAPI.Repository } } + public async Task>> GetValidationPointDicAsync() + { + Dictionary resultList = new(); + return new ResultModel>() + { + Data = resultList, + StatusCode = 1, + Message = "" + }; + } - } + } } diff --git a/Repository/ValidationPointRepository.cs b/Repository/ValidationPointRepository.cs index e353766..97fe6c6 100644 --- a/Repository/ValidationPointRepository.cs +++ b/Repository/ValidationPointRepository.cs @@ -184,5 +184,34 @@ namespace CarParkValidationAPI.Repository Message = error }; } - } + + public async Task>> GetValidationPointDicAsync() + { + Dictionary resultList = new(); + + int statusCode = 0; + string error = ""; + try + { + resultList = await _context.Validations.Where(x => x.Active == true) + .Select(item => new LookupCodeDto { Id = item.ValidationPointID, Description = item.Description ?? "" }) + .ToDictionaryAsync(x => x.Id); + + + statusCode = 1; + } + catch (Exception ex) + { + error = ex.ToString(); + statusCode = 0; + throw ex; + } + return new ResultModel>() + { + Data = resultList, + StatusCode = statusCode, + Message = error + }; + } + } }