upgrade to v8 remove automap too.

This commit is contained in:
Kham
2025-11-10 11:32:00 +11:00
parent d6bf268167
commit e318260acd
7 changed files with 116 additions and 370 deletions
+19 -9
View File
@@ -12,7 +12,7 @@ using CarParkValidationAPI.Datas;
using CarParkValidationAPI.Interface;
using Microsoft.EntityFrameworkCore;
using System;
using AutoMapper;
namespace CarParkValidationAPI.Repository
{
@@ -21,13 +21,13 @@ namespace CarParkValidationAPI.Repository
private DataContext _context;
private string _currentUser = "";
private readonly AppSettings _appSettings;
private readonly IMapper _mapper;
public ConcessionTypeRepository(
DataContext context,
IMapper mapper,
IOptions<AppSettings> appSettings)
{
_mapper = mapper;
_context = context;
_appSettings = appSettings.Value;
}
@@ -61,7 +61,7 @@ namespace CarParkValidationAPI.Repository
string error = "";
try
{
/*
result = await _context.ConcessionTypes.Select(
x => new ConcessionTypeDto()
{
@@ -70,9 +70,9 @@ namespace CarParkValidationAPI.Repository
Display = x.ConcessionDisplay,
Active = x.Active
}).ToListAsync();
*/
var olist = await _context.ConcessionTypes.ToListAsync();
result = _mapper.Map<List<ConcessionTypeDto>>(olist);
//var olist = await _context.ConcessionTypes.ToListAsync();
//result = _mapper.Map<List<ConcessionTypeDto>>(olist);
statuscode = 1;
result.Sort((x, y) => x.Description.CompareTo(y.Description));
}
@@ -100,7 +100,17 @@ namespace CarParkValidationAPI.Repository
try
{
result = await _context.ConcessionTypes.FindAsync(id);
dto = _mapper.Map<ConcessionTypeDto>(result);
dto = new();
if (result != null)
{
dto.Id = id;
dto.Active = result.Active;
dto.Description = result.ConcessionDesc;
dto.Display = result.ConcessionDisplay;
}
// dto = _mapper.Map<ConcessionTypeDto>(result);
//dto = FillConcessionTypeDto(result);
//result = _list.Find(x => x.Id == id);
//var dto = await Task.Run(() => result);
-262
View File
@@ -1,262 +0,0 @@
//using BCryptNet = BCrypt.Net.BCrypt;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using CarParkValidationAPI.Authorization;
using CarParkValidationAPI.Entities;
using CarParkValidationAPI.Helpers;
using CarParkValidationAPI.Models.Users;
using CarParkValidationAPI.Models;
using CarParkValidationAPI.Datas;
using CarParkValidationAPI.Interface;
using Microsoft.EntityFrameworkCore;
using CommonAD.Models;
using CommonAD;
using Microsoft.AspNetCore.Http;
namespace CarParkValidationAPI.Repository
{
public class UserFakeRepository : IUserService
{
private DataContext _context;
private IJwtUtils _jwtUtils;
private readonly AppSettings _appSettings;
public UserFakeRepository(
DataContext context,
IJwtUtils jwtUtils,
IOptions<AppSettings> appSettings)
{
_context = context;
_jwtUtils = jwtUtils;
_appSettings = appSettings.Value;
}
public AuthenticateResponse Authenticate(AuthenticateRequest model)
{
var user = _context.Users.SingleOrDefault(x => x.Username == model.Username);
// validate
// if (user == null || !BCryptNet.Verify(model.Password, user.PasswordHash))
if (user == null || (model.Password != user.PasswordHash))
throw new System.Exception("Username or password is incorrect");
// authentication successful so generate jwt token
var jwtToken = _jwtUtils.GenerateJwtToken(user);
return new AuthenticateResponse(user, jwtToken);
}
public async Task<ResultModel<AuthenticateResponse>> LoginApiAD(AuthenticateRequest model)
{
User user = new User();
int statusCode = 1;
if (model.Username.ToLower() == "admin")
{
user.FirstName = model.Username;
user.LastName = "LS Test";
user.Role = AdminRole.Admin;
user.Username = model.Username;
user.Email = "admin@mail.com";
}
else
{
user.Username = model.Username;
user.FirstName = "FS Normal";
user.LastName = " LS Normal";
user.Email = "Normal@mail.com";
user.Role = AdminRole.Normal;
}
// authentication successful so generate jwt token
var jwtToken = _jwtUtils.GenerateJwtToken(user);
var retval = await Task.Run(() => new AuthenticateResponse(user, jwtToken));
return new ResultModel<AuthenticateResponse>()
{
Data = retval,
StatusCode = statusCode
};
}
public async Task<ResultModel<AuthenticateResponse>> NLoginApiAD(AuthenticateRequest model)
{
User myUser = null;
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)
{
myUser = new User();
myUser.Email = loginInfo.email;
myUser.FirstName = loginInfo.firstName;
myUser.LastName = loginInfo.lastName;
myUser.Username = loginInfo.stafflinkNo;
//now check the adminuser table
var user = _context.AdminUsers.SingleOrDefault(x => x.StafflinkNo == model.Username);
if (user != null)
{
myUser.Role = user.RoleType;
myUser.Id = user.UserID;
}
else
myUser.Role = AdminRole.Normal;
// validate
// if (user == null || !BCryptNet.Verify(model.Password, user.PasswordHash))
// authentication successful so generate jwt token
var jwtToken = _jwtUtils.GenerateJwtToken(myUser);
statuscode = 1;
retval = new AuthenticateResponse(myUser, jwtToken);
}
return new ResultModel<AuthenticateResponse>()
{
Data = retval,
StatusCode = statuscode
};
}
public async Task<ResultModel<AuthenticateResponse>> LoginAD(AuthenticateRequest model)
{
User myUser = null;
AuthenticateResponse retval = null;
int statuscode = 0;
ADConfig adConfig = new ADConfig()
{
LDAPPassword = _appSettings.LDAPPassword,
LDAPPath = _appSettings.LDAPPath,
LDAPUser = _appSettings.LDAPUser,
ADTimeOutStr = _appSettings.ADTimeOut
};
myUser = await Task.Run(() => LoginADStaff(adConfig, model.Username, model.Password));
if (!string.IsNullOrEmpty(myUser.Username))
{
statuscode = 1;
// authentication successful so generate jwt token
var jwtToken = _jwtUtils.GenerateJwtToken(myUser);
retval = new AuthenticateResponse(myUser, jwtToken);
}
return new ResultModel<AuthenticateResponse>()
{
Data = retval,
StatusCode = statuscode
};
}
public async Task<ResultModel<List<User>>> GetAllUsers()
{
var retval = await _context.Users.ToListAsync();
return new ResultModel<List<User>>()
{
Data = retval,
StatusCode = 1
};
}
public async Task<ResultModel<User>> GetById(int id)
{
var user = await _context.Users.FindAsync(id);
if (user == null) throw new KeyNotFoundException("User not found");
return new ResultModel<User>()
{
Data = user,
StatusCode = 1
};
}
public async Task<ResultModel<User>> SearchApiStaff(string staffLinkNo)
{
User myUser = null;
int statuscode = 0;
string webAPIUrl = _appSettings.LoginWebAPI;
var loginInfo = await CommonAD.WebAPIUtil.SearchStaffAsync(webAPIUrl, "SearchStaff", staffLinkNo);
if (loginInfo != null && loginInfo.result)
{
myUser = new User();
myUser.Email = loginInfo.email;
myUser.FirstName = loginInfo.firstName;
myUser.LastName = loginInfo.lastName;
myUser.Username = loginInfo.stafflinkNo;
statuscode = 1;
}
return new ResultModel<User>()
{
Data = myUser,
StatusCode = statuscode
};
// return myUser;
}
public async Task<ResultModel<User>> SearchADStaff(string staffLinkNo)
{
int statuscode = 0;
ADConfig adConfig = new ADConfig()
{
LDAPPassword = _appSettings.LDAPPassword,
LDAPPath = _appSettings.LDAPPath,
LDAPUser = _appSettings.LDAPUser,
ADTimeOutStr = _appSettings.ADTimeOut
};
User myUser = await Task.Run(() => SearchADStaff(adConfig, staffLinkNo));
if (!string.IsNullOrEmpty(myUser.Username))
statuscode = 1;
return new ResultModel<User>()
{
Data = myUser,
StatusCode = statuscode
};
}
private User SearchADStaff(ADConfig adConfig, string username)
{
ADStaffLink staffLink = new ADStaffLink(adConfig);
MyADObject myADObj = staffLink.SearchStaff(username);
User user = new()
{
Username = myADObj.stafflinkNo,
Email = myADObj.email,
FirstName = myADObj.firstName,
LastName = myADObj.lastName
};
// myADObj.JobTitle;
return user;
// return null;
}
private User LoginADStaff(ADConfig adConfig, string username, string password)
{
ADStaffLink staffLink = new ADStaffLink(adConfig);
MyADObject myADObj = staffLink.CheckADCredentials(username, password);
User user = new()
{
Username = myADObj.stafflinkNo,
Email = myADObj.email,
FirstName = myADObj.firstName,
LastName = myADObj.lastName
};
// myADObj.JobTitle;
return user;
// return null;
}
public async Task<ResultModel<FileContent>> GetAllUserReport()
{
return new ResultModel<FileContent>
{
Data = null,
StatusCode = 1
};
}
}
}
+83 -57
View File
@@ -112,22 +112,70 @@ namespace CarParkValidationAPI.Repository
{
User myUser = null;
AuthenticateResponse retval = null;
string error = "user name is not in DB or user Name profile is not generic";
int statuscode = 0;
ADConfig adConfig = new ADConfig()
string webAPIUrl = _appSettings.LoginWebAPI;
//KCO, D204KCO
//now check the adminuser table
//ward clerk
try
{
LDAPPassword = _appSettings.LDAPPassword,
LDAPPath = _appSettings.LDAPPath,
LDAPUser = _appSettings.LDAPUser,
ADTimeOutStr = _appSettings.ADTimeOut
};
myUser = await Task.Run(() => LoginADStaff(adConfig,model.Username, model.Password));
if (!string.IsNullOrEmpty(myUser.Username))
{
statuscode = 1;
// authentication successful so generate jwt token
var jwtToken = _jwtUtils.GenerateJwtToken(myUser);
retval = new AuthenticateResponse(myUser, jwtToken);
var loginInfo = await CommonAD.WebAPIUtil.PostLoginADAsync(webAPIUrl, "CheckCredentials", model.Username, model.Password);
if (loginInfo != null && loginInfo.result)
{
myUser = new User();
myUser.Email = loginInfo.email;
myUser.FirstName = loginInfo.firstName;
myUser.LastName = loginInfo.lastName;
myUser.Username = loginInfo.stafflinkNo;
//myUser.Department = loginInfo.department;
//myUser.Position = loginInfo.jobTitle;
//myUser.ManagerEmail = loginInfo.managerEmail;
//myUser.Phone = loginInfo.phone;
//now check the adminuser table
var user = _context.AdminUsers.
SingleOrDefault(x => x.StafflinkNo == model.Username
&& true == x.Active);
if (user != null)
{
// myUser.Role = user.RoleType;
myUser.Id = user.UserID;
myUser.Role = user.RoleType;
//myUser.ValidationPointId = user.ValidationPointID ?? 0;
statuscode = 1;
}
else // allow
{
statuscode = 1;
myUser.Role = AdminRole.Normal;
myUser.Id = -1;
//error = "user name does not exist in adminUser";
}
// validate
// if (user == null || !BCryptNet.Verify(model.Password, user.PasswordHash))
// authentication successful so generate jwt token
if (statuscode == 1)
{
var jwtToken = _jwtUtils.GenerateJwtToken(myUser);
retval = new AuthenticateResponse(myUser, jwtToken);
error = "";
}
}
else
{
error = "user name or password is not correct";
}
}
catch (Exception ex)
{
retval = null;
error = ex.ToString();
statuscode = 0;
throw ex;
}
return new ResultModel<AuthenticateResponse>()
{
@@ -185,58 +233,36 @@ namespace CarParkValidationAPI.Repository
public async Task<ResultModel<User>> SearchADStaff(string staffLinkNo)
{
int statuscode = 0;
ADConfig adConfig = new ADConfig()
User myUser = null;
AuthenticateResponse retval = null;
string webAPIUrl = _appSettings.LoginWebAPI;
var loginInfo = await CommonAD.WebAPIUtil.SearchStaffAsync(webAPIUrl, "SearchStaff", staffLinkNo);
if (!string.IsNullOrEmpty(loginInfo.firstName))
{
LDAPPassword = _appSettings.LDAPPassword,
LDAPPath = _appSettings.LDAPPath,
LDAPUser = _appSettings.LDAPUser,
ADTimeOutStr = _appSettings.ADTimeOut
};
User myUser = await Task.Run(() => SearchADStaff(adConfig, staffLinkNo));
if (!string.IsNullOrEmpty(myUser.Username))
myUser = new();
myUser.FirstName = loginInfo.firstName;
myUser.LastName = loginInfo.lastName;
myUser.Username = staffLinkNo;
myUser.Email = loginInfo.email;
/*
myUser.Department = loginInfo.department;
myUser.Position = loginInfo.jobTitle;
myUser.Phone = loginInfo.phone;
myUser.ManagerEmail = loginInfo.managerEmail;
*/
statuscode = 1;
}
return new ResultModel<User>()
{
Data = myUser,
StatusCode = statuscode
};
}
private User SearchADStaff(ADConfig adConfig, string username)
{
ADStaffLink staffLink = new ADStaffLink(adConfig);
MyADObject myADObj = staffLink.SearchStaff(username);
User user = new()
{
Username = myADObj.stafflinkNo,
Email = myADObj.email,
FirstName = myADObj.firstName,
LastName = myADObj.lastName
};
// myADObj.JobTitle;
return user;
// return null;
}
private User LoginADStaff(ADConfig adConfig, string username, string password)
{
ADStaffLink staffLink = new ADStaffLink(adConfig);
MyADObject myADObj = staffLink.CheckADCredentials(username,password);
User user = new()
{
Username = myADObj.stafflinkNo,
Email = myADObj.email,
FirstName = myADObj.firstName,
LastName = myADObj.lastName
};
// myADObj.JobTitle;
return user;
// return null;
}
private string GetVPDesc(int? idn)
{
string result = "";