commit change all folder
This commit is contained in:
@@ -0,0 +1,206 @@
|
||||
//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 System;
|
||||
|
||||
namespace CarParkValidationAPI.Repository
|
||||
{
|
||||
public class AdminUserFakeRepository : IAdminUser
|
||||
{
|
||||
private DataContext _context;
|
||||
private string _currentUser = "";
|
||||
List<AdminUserViewDto> _resultList = new List<AdminUserViewDto>();
|
||||
private readonly AppSettings _appSettings;
|
||||
|
||||
|
||||
public AdminUserFakeRepository(
|
||||
DataContext context,
|
||||
|
||||
IOptions<AppSettings> appSettings)
|
||||
{
|
||||
_context = context;
|
||||
_appSettings = appSettings.Value;
|
||||
}
|
||||
|
||||
public void SetCurrentUser(string username)
|
||||
{
|
||||
_currentUser = username;
|
||||
}
|
||||
private AdminUserDto FillAdminUserDto(AdminUser model)
|
||||
{
|
||||
AdminUserDto dto = new AdminUserDto();
|
||||
dto.Id = model.UserID;
|
||||
dto.Name = model.Name;
|
||||
dto.RoleType = model.RoleType;
|
||||
dto.StafflinkNo = model.StafflinkNo;
|
||||
dto.Active = model.Active;
|
||||
dto.AddedBy = model.AddedBy;
|
||||
dto.AddedOn = model.AddedOn;
|
||||
return dto;
|
||||
}
|
||||
|
||||
private AdminUser FillAdminUser(AdminUserDto dto, AdminUser model)
|
||||
{
|
||||
model.UserID = dto.Id;
|
||||
model.Name = dto.Name;
|
||||
model.RoleType = dto.RoleType;
|
||||
model.StafflinkNo = dto.StafflinkNo;
|
||||
model.Active = dto.Active;
|
||||
model.AddedBy = dto.AddedBy;
|
||||
model.AddedOn = dto.AddedOn;
|
||||
return model;
|
||||
}
|
||||
public async Task<ResultModel<bool>> CheckAdminUserByStaffLinkNoAsync(string staffLinkNo)
|
||||
{
|
||||
var list = await _context.AdminUsers.Where(x => x.StafflinkNo == staffLinkNo).ToListAsync();
|
||||
return new ResultModel<bool>()
|
||||
{
|
||||
Data = false,
|
||||
StatusCode = 1
|
||||
};
|
||||
}
|
||||
public async Task<ResultModel<List<AdminUserViewDto>>> LoadAdminUserAsync(bool onlyActive)
|
||||
{
|
||||
/*
|
||||
List<AdminUserDto> resultList = new List<AdminUserDto>();
|
||||
try
|
||||
{
|
||||
var it = await _context.AdminUsers.AsQueryable().ToListAsync();
|
||||
|
||||
AdminUserDto dto;
|
||||
for (int i = 0; i < it.Count; i++)
|
||||
{
|
||||
dto = FillAdminUserDto(it[i]);
|
||||
resultList.Add(dto);
|
||||
}
|
||||
//var it = await Task.Run(() => _list);
|
||||
|
||||
return new ResultModel<List<AdminUserDto>>()
|
||||
{
|
||||
Data = resultList,
|
||||
StateCode = 1
|
||||
};
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
*/
|
||||
var list = await _context.AdminUsers.Where(x => x.Active == onlyActive).ToListAsync();
|
||||
for (int i =0; i < list.Count; i++)
|
||||
{
|
||||
AdminUserViewDto dto = new AdminUserViewDto();
|
||||
dto.Id = i;
|
||||
dto.Name = $"Name {i}";
|
||||
dto.RoleType = AdminRole.Admin.ToString();
|
||||
dto.StafflinkNo = $"{i+40000}";
|
||||
dto.Active = true;
|
||||
dto.AddedOn = DateTime.Today.AddDays(i);
|
||||
dto.AddedBy = "ddfed";
|
||||
_resultList.Add(dto);
|
||||
}
|
||||
return new ResultModel<List<AdminUserViewDto>>()
|
||||
{
|
||||
Data = _resultList,
|
||||
StatusCode = 1
|
||||
};
|
||||
}
|
||||
public async Task<ResultModel<AdminUserDto>> LoadAdminUserByIdAsync(int id)
|
||||
{
|
||||
AdminUserDto result = null;
|
||||
|
||||
try
|
||||
{
|
||||
if (id > 0)
|
||||
{
|
||||
AdminUser model = await _context.AdminUsers.FindAsync(id);
|
||||
result = FillAdminUserDto(model);
|
||||
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
|
||||
return new ResultModel<AdminUserDto>()
|
||||
{
|
||||
Data = result,
|
||||
StatusCode = 1
|
||||
};
|
||||
}
|
||||
public async Task<ResultModel<int>> SaveAdminUserAsync(AdminUserDto item)
|
||||
{
|
||||
int result = default(int);
|
||||
try
|
||||
{
|
||||
AdminUser model;
|
||||
if (item.Id > 0)
|
||||
{
|
||||
model = await _context.AdminUsers.FindAsync(item.Id);
|
||||
model = FillAdminUser(item, model);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
model = new AdminUser();
|
||||
model = FillAdminUser(item, model);
|
||||
_context.AdminUsers.Add(model);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
result = 1;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
result = -1;
|
||||
throw ex;
|
||||
}
|
||||
|
||||
return new ResultModel<int>()
|
||||
{
|
||||
Data = result,
|
||||
StatusCode = 1
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
public async Task<ResultModel<bool>> DeleteAdminUserAsync(int id)
|
||||
{
|
||||
bool result = true;
|
||||
|
||||
try
|
||||
{
|
||||
if (id > 0)
|
||||
{
|
||||
AdminUser model = await _context.AdminUsers.FindAsync(id);
|
||||
model.Active = false;
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
result = false;
|
||||
throw ex;
|
||||
}
|
||||
|
||||
return new ResultModel<bool>()
|
||||
{
|
||||
Data = result,
|
||||
StatusCode = 1
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,274 @@
|
||||
//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 System;
|
||||
|
||||
namespace CarParkValidationAPI.Repository
|
||||
{
|
||||
public class AdminUserRepository : IAdminUser
|
||||
{
|
||||
private DataContext _context;
|
||||
private string _currentUser = "";
|
||||
private readonly AppSettings _appSettings;
|
||||
private Dictionary<int, string> _ValidationPointDic = null;
|
||||
|
||||
|
||||
public AdminUserRepository(
|
||||
DataContext context,
|
||||
|
||||
IOptions<AppSettings> appSettings)
|
||||
{
|
||||
_context = context;
|
||||
_appSettings = appSettings.Value;
|
||||
}
|
||||
public void SetCurrentUser(string username)
|
||||
{
|
||||
_currentUser = username;
|
||||
}
|
||||
private string GetValidtionPointDesc(int id)
|
||||
{
|
||||
string result = "";
|
||||
if (_ValidationPointDic != null && _ValidationPointDic.ContainsKey(id))
|
||||
result = _ValidationPointDic[id];
|
||||
return result;
|
||||
}
|
||||
private AdminUserViewDto FillAdminUserViewDto(AdminUser model)
|
||||
{
|
||||
AdminUserViewDto dto = new AdminUserViewDto();
|
||||
dto.Id = model.UserID;
|
||||
dto.Name = model.Name;
|
||||
dto.RoleType = model.RoleType.ToString();
|
||||
dto.StafflinkNo = model.StafflinkNo;
|
||||
dto.Active = model.Active;
|
||||
dto.AddedBy = model.AddedBy;
|
||||
dto.AddedOn = model.AddedOn;
|
||||
dto.ValidationPoint = GetValidtionPointDesc(model.ValidationPointID??0);
|
||||
return dto;
|
||||
}
|
||||
private AdminUserDto FillAdminUserDto(AdminUser model)
|
||||
{
|
||||
AdminUserDto dto = new AdminUserDto();
|
||||
dto.Id = model.UserID;
|
||||
dto.Name = model.Name;
|
||||
dto.RoleType = model.RoleType;
|
||||
dto.StafflinkNo = model.StafflinkNo;
|
||||
dto.Active = model.Active;
|
||||
dto.AddedBy = model.AddedBy;
|
||||
dto.AddedOn = model.AddedOn;
|
||||
dto.ValidationPointId = model.ValidationPointID ?? 0;
|
||||
return dto;
|
||||
}
|
||||
|
||||
private AdminUser FillAdminUser(AdminUserDto dto, AdminUser model)
|
||||
{
|
||||
model.UserID = dto.Id;
|
||||
model.Name = dto.Name;
|
||||
model.RoleType = dto.RoleType;
|
||||
model.StafflinkNo = dto.StafflinkNo;
|
||||
model.Active = dto.Active;
|
||||
model.AddedBy = dto.AddedBy;
|
||||
model.AddedOn = dto.AddedOn.ToLocalTime();
|
||||
model.ValidationPointID = dto.ValidationPointId;
|
||||
return model;
|
||||
}
|
||||
|
||||
public async Task<ResultModel<List<AdminUserViewDto>>> LoadAdminUserAsync(bool onlyActive)
|
||||
{
|
||||
List<AdminUserViewDto> resultList = new List<AdminUserViewDto>();
|
||||
int statuscode = 0;
|
||||
string error = "";
|
||||
try
|
||||
{
|
||||
_ValidationPointDic = await _context.Validations.Where(x => x.Active == true).ToDictionaryAsync((x) => x.ValidationPointID, y => y.Description);
|
||||
|
||||
//var list = await _context.AdminUsers.Where(x => x.Active == onlyActive).ToListAsync();
|
||||
var list = await _context.AdminUsers.AsQueryable().ToListAsync();
|
||||
|
||||
AdminUserViewDto dto;
|
||||
AdminUser user;
|
||||
for (int i = 0; i < list.Count; i++)
|
||||
{
|
||||
user = list[i];
|
||||
dto = FillAdminUserViewDto(user);
|
||||
resultList.Add(dto);
|
||||
|
||||
}
|
||||
statuscode = 1;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
error = ex.ToString();
|
||||
statuscode = 0;
|
||||
throw ex;
|
||||
}
|
||||
|
||||
return new ResultModel<List<AdminUserViewDto>>()
|
||||
{
|
||||
Data = resultList,
|
||||
StatusCode = statuscode,
|
||||
Message = error
|
||||
};
|
||||
}
|
||||
public async Task<ResultModel<bool>> CheckAdminUserByStaffLinkNoAsync(string staffLinkNo)
|
||||
{
|
||||
bool result = false;
|
||||
int statuscode = 0;
|
||||
string error = "";
|
||||
try
|
||||
{
|
||||
if (!string.IsNullOrEmpty(staffLinkNo))
|
||||
{
|
||||
var list = await _context.AdminUsers.Where( x=> x.StafflinkNo == staffLinkNo).ToListAsync();
|
||||
if (list.Count > 0)
|
||||
{
|
||||
result = true;
|
||||
statuscode = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
error = ex.ToString();
|
||||
statuscode = 0;
|
||||
throw ex;
|
||||
}
|
||||
|
||||
return new ResultModel<bool>()
|
||||
{
|
||||
Data = result,
|
||||
StatusCode = statuscode,
|
||||
Message = error
|
||||
};
|
||||
}
|
||||
public async Task<ResultModel<AdminUserDto>> LoadAdminUserByIdAsync(int id)
|
||||
{
|
||||
AdminUserDto result = null;
|
||||
int statuscode = 0;
|
||||
string error = "";
|
||||
try
|
||||
{
|
||||
if (id > 0)
|
||||
{
|
||||
AdminUser model = await _context.AdminUsers.FindAsync(id);
|
||||
result = FillAdminUserDto(model);
|
||||
statuscode = 1;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
error = ex.ToString();
|
||||
statuscode = 0;
|
||||
throw ex;
|
||||
}
|
||||
|
||||
return new ResultModel<AdminUserDto>()
|
||||
{
|
||||
Data = result,
|
||||
StatusCode = statuscode,
|
||||
Message = error
|
||||
};
|
||||
}
|
||||
public async Task<ResultModel<int>> SaveAdminUserAsync(AdminUserDto item)
|
||||
{
|
||||
int result = default(int);
|
||||
int statuscode = 0;
|
||||
string error = "";
|
||||
try
|
||||
{
|
||||
AdminUser model;
|
||||
if (item.Id > 0)
|
||||
{
|
||||
model = await _context.AdminUsers.FindAsync(item.Id);
|
||||
model = FillAdminUser(item, model);
|
||||
model.LastModified = DateTime.Now;
|
||||
model.LastModifiedId = _currentUser;
|
||||
await _context.SaveChangesAsync();
|
||||
result = model.UserID;
|
||||
statuscode = 1;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
ResultModel<bool> found = await CheckAdminUserByStaffLinkNoAsync(item.StafflinkNo);
|
||||
|
||||
if (!found.Data)
|
||||
{
|
||||
model = new AdminUser();
|
||||
model = FillAdminUser(item, model);
|
||||
_context.AdminUsers.Add(model);
|
||||
await _context.SaveChangesAsync();
|
||||
result = model.UserID;
|
||||
statuscode = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
error = "staff link No '" + item.StafflinkNo + "' already in admin user";
|
||||
statuscode = 0;
|
||||
result = 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
result = -1;
|
||||
error = ex.ToString();
|
||||
statuscode = 0;
|
||||
throw ex;
|
||||
}
|
||||
|
||||
return new ResultModel<int>()
|
||||
{
|
||||
Data = result,
|
||||
StatusCode = statuscode,
|
||||
Message = error
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<ResultModel<bool>> DeleteAdminUserAsync(int id)
|
||||
{
|
||||
bool result = true;
|
||||
int statuscode = 0;
|
||||
string error = "";
|
||||
try
|
||||
{
|
||||
if (id > 0)
|
||||
{
|
||||
AdminUser model = await _context.AdminUsers.FindAsync(id);
|
||||
model.Active = false;
|
||||
model.LastModified = DateTime.Now;
|
||||
model.LastModifiedId = _currentUser;
|
||||
await _context.SaveChangesAsync();
|
||||
statuscode = 1;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
error = ex.ToString();
|
||||
statuscode = 0;
|
||||
result = false;
|
||||
throw ex;
|
||||
}
|
||||
|
||||
return new ResultModel<bool>()
|
||||
{
|
||||
Data = result,
|
||||
StatusCode = statuscode,
|
||||
Message = error
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,199 @@
|
||||
//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 System;
|
||||
using AutoMapper;
|
||||
|
||||
namespace CarParkValidationAPI.Repository
|
||||
{
|
||||
public class ConcessionTypeRepository : IConcessionType
|
||||
{
|
||||
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;
|
||||
}
|
||||
public void SetCurrentUser(string username)
|
||||
{
|
||||
_currentUser = username;
|
||||
}
|
||||
private ConcessionTypeDto FillConcessionTypeDto(ConcessionType model)
|
||||
{
|
||||
ConcessionTypeDto dto = new ConcessionTypeDto();
|
||||
dto.Active = model.Active;
|
||||
dto.Description = model.ConcessionDesc;
|
||||
dto.Display = model.ConcessionDisplay;
|
||||
dto.Id = model.ConcessionTypeID;
|
||||
return dto;
|
||||
}
|
||||
private ConcessionType FillConcessionType(ConcessionTypeDto dto, ConcessionType model)
|
||||
{
|
||||
model.Active = dto.Active;
|
||||
model.ConcessionDesc = dto.Description.Trim();
|
||||
model.ConcessionDisplay = dto.Display.Trim();
|
||||
model.ConcessionTypeID = dto.Id;
|
||||
return model;
|
||||
}
|
||||
public async Task<ResultModel<List<ConcessionTypeDto>>> LoadConcessionTypeAsync(bool onlyActive)
|
||||
{
|
||||
List<ConcessionTypeDto> result = null;
|
||||
int statuscode = 0;
|
||||
string error = "";
|
||||
try
|
||||
{
|
||||
/*
|
||||
result = await _context.ConcessionTypes.Select(
|
||||
x => new ConcessionTypeDto()
|
||||
{
|
||||
Id = x.ConcessionTypeID,
|
||||
Description = x.ConcessionDesc,
|
||||
Display = x.ConcessionDisplay,
|
||||
Active = x.Active
|
||||
}).ToListAsync();
|
||||
*/
|
||||
var olist = await _context.ConcessionTypes.ToListAsync();
|
||||
result = _mapper.Map<List<ConcessionTypeDto>>(olist);
|
||||
statuscode = 1;
|
||||
result.Sort((x, y) => x.Description.CompareTo(y.Description));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
result = null;
|
||||
error = ex.ToString();
|
||||
statuscode = 0;
|
||||
throw ex;
|
||||
}
|
||||
|
||||
return new ResultModel<List<ConcessionTypeDto>>()
|
||||
{
|
||||
Data = result,
|
||||
StatusCode = statuscode,
|
||||
Message = error
|
||||
};
|
||||
}
|
||||
public async Task<ResultModel<ConcessionTypeDto>> LoadConcessionTypeByIdAsync(int id)
|
||||
{
|
||||
ConcessionType result = null;
|
||||
int statuscode = 0;
|
||||
string error = "";
|
||||
ConcessionTypeDto dto;
|
||||
try
|
||||
{
|
||||
result = await _context.ConcessionTypes.FindAsync(id);
|
||||
dto = _mapper.Map<ConcessionTypeDto>(result);
|
||||
//dto = FillConcessionTypeDto(result);
|
||||
//result = _list.Find(x => x.Id == id);
|
||||
//var dto = await Task.Run(() => result);
|
||||
statuscode = 1;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
error = ex.ToString();
|
||||
statuscode = 0;
|
||||
throw ex;
|
||||
}
|
||||
return new ResultModel<ConcessionTypeDto>()
|
||||
{
|
||||
Data = dto,
|
||||
StatusCode = statuscode,
|
||||
Message = error
|
||||
};
|
||||
}
|
||||
public async Task<ResultModel<int>> SaveConcessionTypeAsync(ConcessionTypeDto item)
|
||||
{
|
||||
int result = default(int);
|
||||
int statuscode = 0;
|
||||
string error = "";
|
||||
try
|
||||
{
|
||||
ConcessionType model;
|
||||
if (item.Id > 0)
|
||||
{
|
||||
model = await _context.ConcessionTypes.FindAsync(item.Id);
|
||||
model = FillConcessionType(item, model);
|
||||
model.LastModified = DateTime.Now;
|
||||
model.LastModifiedId = _currentUser;
|
||||
await _context.SaveChangesAsync();
|
||||
result = item.Id;
|
||||
}
|
||||
else //new record
|
||||
{
|
||||
model = new ConcessionType();
|
||||
model = FillConcessionType(item, model);
|
||||
model.LastModified = DateTime.Now;
|
||||
model.LastModifiedId = _currentUser;
|
||||
_context.ConcessionTypes.Add(model);
|
||||
var id = await _context.SaveChangesAsync();
|
||||
result = model.ConcessionTypeID;
|
||||
|
||||
}
|
||||
statuscode = 1;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
error = ex.ToString();
|
||||
statuscode = 0;
|
||||
throw ex;
|
||||
}
|
||||
//var dto = await Task.Run(() => result);
|
||||
return new ResultModel<int>()
|
||||
{
|
||||
Data = result,
|
||||
StatusCode = statuscode,
|
||||
Message = error
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<ResultModel<bool>> DeleteConcessionTypeAsync(int id)
|
||||
{
|
||||
bool result = true;
|
||||
int statuscode = 0;
|
||||
string error = "";
|
||||
try
|
||||
{
|
||||
ConcessionType model;
|
||||
if (id > 0)
|
||||
{
|
||||
model = await _context.ConcessionTypes.FindAsync(id);
|
||||
model.Active = false;
|
||||
//_context.ConcessionTypes.Remove(model);
|
||||
await _context.SaveChangesAsync();
|
||||
statuscode = 1;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
error = ex.ToString();
|
||||
statuscode = 0;
|
||||
throw;
|
||||
}
|
||||
//var dto = await Task.Run(() => result);
|
||||
return new ResultModel<bool>()
|
||||
{
|
||||
Data = result,
|
||||
StatusCode = statuscode,
|
||||
Message = error
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
using CarParkValidationAPI.Datas;
|
||||
using CarParkValidationAPI.Models;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using SpreadsheetLight;
|
||||
using CarParkValidationAPI.Entities;
|
||||
using System.IO;
|
||||
|
||||
namespace CarParkValidationAPI.Repository
|
||||
{
|
||||
public class ConcessionValidationReportRepository
|
||||
{
|
||||
private readonly DataContext _context;
|
||||
private readonly IOptions<AppSettings> _appSettings;
|
||||
const string DateFormat = "dd-MM-yyyy HH:mm";
|
||||
const string dateFormat = "dd-MM-yyyyy";
|
||||
public ConcessionValidationReportRepository(
|
||||
DataContext context, IOptions<AppSettings> appSettings)
|
||||
{
|
||||
_context = context;
|
||||
_appSettings = appSettings;
|
||||
}
|
||||
|
||||
public async Task<ResultModel<FileContent>> GetConcessionValidationReport(DateTime fromDate, DateTime toDate)
|
||||
{
|
||||
//excel SLDocument sl = new SLDocument();
|
||||
int statusCode = 1;
|
||||
ConcessionValidationExtDto item;
|
||||
int col = 1;
|
||||
int row = 1;
|
||||
SLDocument sl = new SLDocument();
|
||||
|
||||
|
||||
//first sheetName
|
||||
sl.RenameWorksheet(SLDocument.DefaultFirstSheetName, "ConcessionValidation");
|
||||
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 ConssionValidation Report");
|
||||
sl.SetCellValue(row, 3, "Date Range: " + fromDate.ToString(dateFormat) + " - " + toDate.ToString(dateFormat) );
|
||||
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++, "Date");
|
||||
sl.SetCellValue(row, col++, "Concession Card Number");
|
||||
sl.SetCellValue(row, col++, "Concession Holder Name");
|
||||
sl.SetCellValue(row, col++, "Expiry date checked");
|
||||
sl.SetCellValue(row, col++, "ConcessionType");
|
||||
sl.SetCellValue(row, col++, "Validation Piont");
|
||||
// sl.SetCellValue(row, col++, "StaffLink No");
|
||||
sl.SetCellValue(row, col++, "Enter by");
|
||||
// sl.SetCellValue(row, col++, "Staff Last name");
|
||||
// sl.SetCellValue(row, col++, "Staff Email");
|
||||
sl.FreezePanes(row, col - 1); //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, 18);
|
||||
sl.SetColumnWidth(3, 25);
|
||||
sl.SetColumnWidth(4, 20);
|
||||
sl.SetColumnWidth(5, 50);
|
||||
sl.SetColumnWidth(6, 40);
|
||||
sl.SetColumnWidth(7, 40);
|
||||
// sl.SetColumnWidth(8, 20);
|
||||
// sl.SetColumnWidth(9, 20);
|
||||
// sl.SetColumnWidth(10, 20);
|
||||
int startRowFrom = startTitleRow+1;
|
||||
var list = await _context.SelectConcessionValidationByDate(true, fromDate.ToLocalTime(), toDate.ToLocalTime());
|
||||
for (int i = 0; i < list.Count; i++)
|
||||
{
|
||||
col = 1;
|
||||
item = list[i];
|
||||
row = i + startRowFrom;
|
||||
sl.SetCellValue(row, col++, item.ValidateDate);
|
||||
sl.SetCellValue(row, col++, item.ConcessionCard);
|
||||
sl.SetCellValue(row, col++, item.ConcessionHolder);
|
||||
sl.SetCellValue(row, col++, item.ConcessionExpiry ? "Yes": "No");
|
||||
sl.SetCellValue(row, col++, item.ConcessionType);
|
||||
sl.SetCellValue(row, col++, item.ValidationPoint);
|
||||
// sl.SetCellValue(row, col++, item.StafflinkNo);
|
||||
sl.SetCellValue(row, col++, item.StaffFirstName + " " + item.StaffLastName);
|
||||
// sl.SetCellValue(row, col++, item.StaffLastName);
|
||||
// sl.SetCellValue(row, col++, item.StaffEmail);
|
||||
}
|
||||
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 = "ConcessionValidation" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm")+ ".xlsx",
|
||||
|
||||
};
|
||||
|
||||
return new ResultModel<FileContent>
|
||||
{
|
||||
Data = result,
|
||||
StatusCode = statusCode
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,371 @@
|
||||
//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 System;
|
||||
|
||||
namespace CarParkValidationAPI.Repository
|
||||
{
|
||||
public class ConcessionValidationRepository : IConcessionValidation
|
||||
{
|
||||
private DataContext _context;
|
||||
private string _currentUser = "";
|
||||
private readonly AppSettings _appSettings;
|
||||
|
||||
public ConcessionValidationRepository(
|
||||
DataContext context,
|
||||
IOptions<AppSettings> appSettings)
|
||||
{
|
||||
_context = context;
|
||||
_appSettings = appSettings.Value;
|
||||
}
|
||||
|
||||
public void SetCurrentUser(string username)
|
||||
{
|
||||
_currentUser = username;
|
||||
}
|
||||
private ConcessionValidationDto FillConcessionValidationDto(ConcessionValidation model)
|
||||
{
|
||||
ConcessionValidationDto dto = new ConcessionValidationDto();
|
||||
dto.Active = model.Active;
|
||||
dto.ConcessionCard = model.ConcessionCard;
|
||||
dto.ConcessionExpiry = model.ConcessionExpiry;
|
||||
dto.ConcessionHolder = model.ConcessionHolder;
|
||||
dto.ConcessionTypeId = model.ConcessionTypeID;
|
||||
dto.Id = model.ValidationID;
|
||||
dto.StafflinkNo = model.StafflinkNo;
|
||||
dto.ValidateDate = model.ValidateDate;
|
||||
dto.ValidationPointId = model.ValidationPointID;
|
||||
dto.StaffFirstName = model.StaffFirstName;
|
||||
dto.StaffLastName = model.StaffLastName;
|
||||
dto.StaffEmail = model.StaffEmail;
|
||||
return dto;
|
||||
}
|
||||
|
||||
private ConcessionValidationExtDto FillConcessionValidationExtDto(ConcessionValidation model)
|
||||
{
|
||||
ConcessionValidationExtDto dto = new ConcessionValidationExtDto();
|
||||
dto.Active = model.Active;
|
||||
dto.ConcessionCard = model.ConcessionCard;
|
||||
dto.ConcessionExpiry = model.ConcessionExpiry;
|
||||
dto.ConcessionHolder = model.ConcessionHolder;
|
||||
dto.ConcessionTypeId = model.ConcessionTypeID;
|
||||
dto.Id = model.ValidationID;
|
||||
dto.StaffFirstName = model.StaffFirstName;
|
||||
dto.StaffLastName = model.StaffLastName;
|
||||
dto.StaffEmail = model.StaffEmail;
|
||||
dto.ValidateDate = model.ValidateDate;
|
||||
dto.ValidationPointId = model.ValidationPointID;
|
||||
return dto;
|
||||
}
|
||||
|
||||
private ConcessionValidation FillConcessionValidation(ConcessionValidationDto dto, ConcessionValidation model)
|
||||
{
|
||||
model.Active = dto.Active;
|
||||
model.ConcessionCard = dto.ConcessionCard.Trim();
|
||||
model.ConcessionExpiry = dto.ConcessionExpiry;
|
||||
model.ConcessionHolder = dto.ConcessionHolder.Trim();
|
||||
model.ConcessionTypeID = dto.ConcessionTypeId;
|
||||
model.ValidationID = dto.Id;
|
||||
model.StafflinkNo = dto.StafflinkNo;
|
||||
model.StaffFirstName = dto.StaffFirstName.Trim();
|
||||
model.StaffLastName = dto.StaffLastName.Trim();
|
||||
model.StaffEmail = dto.StaffEmail.Trim();
|
||||
model.ValidateDate = dto.ValidateDate.ToLocalTime();
|
||||
model.ValidationPointID = dto.ValidationPointId;
|
||||
return model;
|
||||
}
|
||||
public async Task<ResultModel<List<ConcessionValidationExtDto>>> LoadConcessionValidationAsync(bool onlyActive)
|
||||
{
|
||||
int statuscode = 0;
|
||||
string error = "";
|
||||
List<ConcessionValidationExtDto> result = new List<ConcessionValidationExtDto>();
|
||||
|
||||
ConcessionValidationExtDto dto;
|
||||
ConcessionValidation model;
|
||||
try
|
||||
{
|
||||
List<ConcessionValidation> clist = await _context.ConcessionValidations.Where(x => x.Active == true).ToListAsync();
|
||||
for (int i = 0; i < clist.Count; i++)
|
||||
{
|
||||
model = clist[i];
|
||||
dto = FillConcessionValidationExtDto(model);
|
||||
result.Add(dto);
|
||||
}
|
||||
statuscode = 1;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
statuscode = 0;
|
||||
error = ex.ToString();
|
||||
throw;
|
||||
}
|
||||
|
||||
return new ResultModel<List<ConcessionValidationExtDto>>()
|
||||
{
|
||||
Data = result,
|
||||
StatusCode = statuscode,
|
||||
Message = error
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<ResultModel<List<ConcessionValidationExtDto>>> LoadConcessionValidationStaffLinkNoAsync(string staffLinkNo, DateTime fromDate, DateTime toDate)
|
||||
{
|
||||
List<ConcessionValidationExtDto> result = new List<ConcessionValidationExtDto>();
|
||||
// ConcessionValidationExtDto dto;
|
||||
// ConcessionValidation model;
|
||||
int statuscode = 0;
|
||||
string error = "";
|
||||
try
|
||||
{
|
||||
//List<ConcessionValidation> clist = await _context.ConcessionValidations.Where(x =>
|
||||
// x.Active == true
|
||||
// && x.StafflinkNo == staffLinkNo
|
||||
// && x.ValidateDate.Date == currentDate.Date
|
||||
//).ToListAsync();
|
||||
result = await _context.SelectConcessionValidationByStaffId(true, staffLinkNo, fromDate, toDate);
|
||||
/*
|
||||
//var clist = await _context.SPSelectValidation(true, staffLinkNo, currentDate);
|
||||
for (int i = 0; i < clist.Count; i++)
|
||||
{
|
||||
model = clist[i];
|
||||
dto = FillConcessionValidationExtDto(model);
|
||||
result.Add(dto);
|
||||
}
|
||||
*/
|
||||
statuscode = 1;
|
||||
result.Sort((x, y) => x.ValidateDate.CompareTo(y.ValidateDate));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
statuscode = 0;
|
||||
error = ex.ToString();
|
||||
throw;
|
||||
}
|
||||
|
||||
return new ResultModel<List<ConcessionValidationExtDto>>()
|
||||
{
|
||||
Data = result,
|
||||
StatusCode = statuscode,
|
||||
Message = error
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<ResultModel<int>> CheckConcessionValidationStaffLinkNoAsync(string staffLinkNo, string concessionCard, DateTime currentDate, int id)
|
||||
{
|
||||
int statuscode = 1;
|
||||
int result = 1;
|
||||
int number = id < 1 ? 0 : 1; // if insert check > 0 . if modify > 1
|
||||
string message = "warning you already add concession card on this date!";
|
||||
try
|
||||
{
|
||||
var anyRecord = await _context.SelectCheckConcessionValidationAsync(currentDate.ToLocalTime().Date, concessionCard.Trim(), staffLinkNo.Trim());
|
||||
|
||||
if (anyRecord.Count > number)
|
||||
{
|
||||
result = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = 1; //test
|
||||
message = "test OK just one time";
|
||||
}
|
||||
statuscode = 1;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
message = "Error " + ex.ToString();
|
||||
statuscode = 0;
|
||||
result = 0;
|
||||
}
|
||||
|
||||
return new ResultModel<int>()
|
||||
{
|
||||
Data = result,
|
||||
StatusCode = statuscode,
|
||||
Message = message,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public async Task<ResultModel<int>> CheckConcessionValidationStaffLinkNoAsync_old(string staffLinkNo, string concessionCard, DateTime currentDate)
|
||||
{
|
||||
int statuscode = 1;
|
||||
int result = 1;
|
||||
string concard = concessionCard.Trim();
|
||||
string message = "warning you already add concession card on this date!";
|
||||
try
|
||||
{
|
||||
var anyRecord = await _context.ConcessionValidations.Where(x =>
|
||||
x.StafflinkNo == staffLinkNo
|
||||
&& x.ConcessionCard == concard
|
||||
&& x.Active == true
|
||||
&& x.ValidateDate.Date == currentDate.ToLocalTime().Date
|
||||
).ToListAsync();
|
||||
if (anyRecord.Count > 0)
|
||||
{
|
||||
result = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = 1; //test
|
||||
message = "test OK just one time";
|
||||
}
|
||||
statuscode = 1;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
message = "Error " + ex.ToString();
|
||||
statuscode = 0;
|
||||
result = 0;
|
||||
}
|
||||
|
||||
return new ResultModel<int>()
|
||||
{
|
||||
Data = result,
|
||||
StatusCode = statuscode,
|
||||
Message = message,
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<ResultModel<ConcessionValidationDto>> LoadConcessionValidationByIdAsync(int id)
|
||||
{
|
||||
ConcessionValidationDto result = null;
|
||||
int statuscode = 0;
|
||||
string error = "";
|
||||
try
|
||||
{
|
||||
if (id > 0)
|
||||
{
|
||||
ConcessionValidation model = await _context.ConcessionValidations.FindAsync(id);
|
||||
result = FillConcessionValidationDto(model);
|
||||
statuscode = 1;
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
statuscode = 0;
|
||||
error = ex.ToString();
|
||||
throw;
|
||||
}
|
||||
|
||||
return new ResultModel<ConcessionValidationDto>()
|
||||
{
|
||||
Data = result,
|
||||
StatusCode = statuscode,
|
||||
Message = error
|
||||
};
|
||||
}
|
||||
public async Task<ResultModel<int>> SaveConcessionValidationAsync(ConcessionValidationDto item)
|
||||
{
|
||||
int result = default(int);
|
||||
int statuscode = 0;
|
||||
string error = "";
|
||||
//_currentUser = item.StafflinkNo;
|
||||
try
|
||||
{
|
||||
ConcessionValidation model;
|
||||
if (item.Id > 0)
|
||||
{
|
||||
model = await _context.ConcessionValidations.FindAsync(item.Id);
|
||||
model = FillConcessionValidation(item, model);
|
||||
model.LastModified = DateTime.Now;
|
||||
model.LastModifiedId = _currentUser;
|
||||
result = await _context.SaveChangesAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
model = new ConcessionValidation();
|
||||
model = FillConcessionValidation(item, model);
|
||||
model.LastModified = DateTime.Now;
|
||||
model.LastModifiedId = _currentUser;
|
||||
model.CreatedDate = DateTime.Now;
|
||||
model.CreatedId = _currentUser;
|
||||
_context.ConcessionValidations.Add(model);
|
||||
result = await _context.SaveChangesAsync();
|
||||
result = model.ValidationID;
|
||||
}
|
||||
statuscode = 1;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
statuscode = 0;
|
||||
error = ex.ToString();
|
||||
throw;
|
||||
}
|
||||
|
||||
|
||||
return new ResultModel<int>()
|
||||
{
|
||||
Data = result,
|
||||
StatusCode = statuscode,
|
||||
Message = error
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
public async Task<ResultModel<bool>> DeleteConcessionValidationAsync(int id)
|
||||
{
|
||||
bool result = true;
|
||||
int statuscode = 0;
|
||||
string error = "";
|
||||
try
|
||||
{
|
||||
if (id > 0)
|
||||
{
|
||||
ConcessionValidation model = await _context.ConcessionValidations.FindAsync(id);
|
||||
model.Active = false;
|
||||
model.LastModified = DateTime.Now;
|
||||
model.LastModifiedId = _currentUser;
|
||||
//_context.ConcessionValidations.Remove(model);
|
||||
await _context.SaveChangesAsync();
|
||||
statuscode = 1;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
result = false;
|
||||
statuscode = 0;
|
||||
error = ex.ToString();
|
||||
throw;
|
||||
}
|
||||
|
||||
|
||||
return new ResultModel<bool>()
|
||||
{
|
||||
Data = result,
|
||||
StatusCode = statuscode,
|
||||
Message = error
|
||||
};
|
||||
}
|
||||
public async Task<ResultModel<FileContent>> GetReport(DateTime fromDate, DateTime toDate)
|
||||
{
|
||||
int statusCode = 1;
|
||||
string fileContext = "filecontain as string this is file contain in string format";
|
||||
//await this._concessionValidationReport.GetConcessionValidationReport(DateTime.Today.AddDays(-30), DateTime.Today.AddDays(30));
|
||||
var result = new FileContent
|
||||
{
|
||||
Content = Convert.FromBase64String(fileContext),
|
||||
FileName = "excel extension"
|
||||
};
|
||||
var task = await Task.Run(() => 1);
|
||||
return new ResultModel<FileContent>
|
||||
{
|
||||
Data = result,
|
||||
StatusCode = statusCode
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
//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 System;
|
||||
|
||||
namespace CarParkValidationAPI.Repository
|
||||
{
|
||||
|
||||
public class LookupCodeRepository: ILookupCode
|
||||
{
|
||||
private DataContext _context;
|
||||
|
||||
private readonly AppSettings _appSettings;
|
||||
|
||||
public LookupCodeRepository(
|
||||
DataContext context,
|
||||
|
||||
IOptions<AppSettings> appSettings)
|
||||
{
|
||||
_context = context;
|
||||
_appSettings = appSettings.Value;
|
||||
}
|
||||
public async Task<ResultModel<List<LookupCodeDto>>> LoadConcessionTypeAsync(bool onlyActive)
|
||||
{
|
||||
List<LookupCodeDto> result = null;
|
||||
int statuscode = 0;
|
||||
string error = "";
|
||||
try
|
||||
{
|
||||
result = await _context.ConcessionTypes.Where(x => x.Active == onlyActive).Select(
|
||||
x => new LookupCodeDto()
|
||||
{
|
||||
Id = x.ConcessionTypeID,
|
||||
Description = x.ConcessionDesc,
|
||||
|
||||
}).ToListAsync();
|
||||
statuscode = 1;
|
||||
result.Sort((x,y) => x.Description.CompareTo(y.Description));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
statuscode = 0;
|
||||
error = ex.ToString();
|
||||
throw;
|
||||
}
|
||||
return new ResultModel<List<LookupCodeDto>>()
|
||||
{
|
||||
Data = result,
|
||||
StatusCode = statuscode,
|
||||
Message = error
|
||||
};
|
||||
}
|
||||
public async Task<ResultModel<List<LookupCodeDto>>> LoadValidationPointAsync(bool onlyActive)
|
||||
{
|
||||
List<LookupCodeDto> result = null;
|
||||
int statuscode = 0;
|
||||
string error = "";
|
||||
try
|
||||
{
|
||||
|
||||
result = await _context.Validations.Where(x => x.Active == onlyActive).Select(
|
||||
x => new LookupCodeDto()
|
||||
{
|
||||
Id = x.ValidationPointID,
|
||||
Description = x.Description
|
||||
}).ToListAsync();
|
||||
result.Sort((x, y) => x.Description.CompareTo(y.Description));
|
||||
statuscode = 1;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
statuscode = 0;
|
||||
error = ex.ToString();
|
||||
throw;
|
||||
}
|
||||
return new ResultModel<List<LookupCodeDto>>()
|
||||
{
|
||||
Data = result,
|
||||
StatusCode = statuscode,
|
||||
Message = error
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,253 @@
|
||||
//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;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,229 @@
|
||||
//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;
|
||||
|
||||
namespace CarParkValidationAPI.Repository
|
||||
{
|
||||
public class UserRepository : IUserService
|
||||
{
|
||||
private DataContext _context;
|
||||
private IJwtUtils _jwtUtils;
|
||||
private readonly AppSettings _appSettings;
|
||||
|
||||
public UserRepository(
|
||||
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 myUser = null;
|
||||
AuthenticateResponse retval = null;
|
||||
int statuscode = 0;
|
||||
string webAPIUrl = _appSettings.LoginWebAPI;
|
||||
|
||||
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;
|
||||
//now check the adminuser table
|
||||
var user = _context.AdminUsers.
|
||||
SingleOrDefault(x => x.StafflinkNo == model.Username
|
||||
&& x.Active == true);
|
||||
if (user != null)
|
||||
{
|
||||
myUser.Role = user.RoleType;
|
||||
myUser.Id = user.UserID;
|
||||
myUser.ValidationPointId = user.ValidationPointID??0;
|
||||
statuscode = 1;
|
||||
}
|
||||
else //not allow
|
||||
{
|
||||
statuscode = 0;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,195 @@
|
||||
using CarParkValidationAPI.Datas;
|
||||
using CarParkValidationAPI.Entities;
|
||||
using CarParkValidationAPI.Interface;
|
||||
using CarParkValidationAPI.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CarParkValidationAPI.Repository
|
||||
{
|
||||
public class ValidationPointFakeRepository : IValidationPoint
|
||||
{
|
||||
private DataContext _context;
|
||||
|
||||
private readonly AppSettings _appSettings;
|
||||
private string _currentUser = "";
|
||||
private List<ValidationPointDto> _list = new List<ValidationPointDto>();
|
||||
|
||||
public ValidationPointFakeRepository(
|
||||
DataContext context,
|
||||
|
||||
IOptions<AppSettings> appSettings)
|
||||
{
|
||||
_context = context;
|
||||
_appSettings = appSettings.Value;
|
||||
}
|
||||
public void SetCurrentUser(string username)
|
||||
{
|
||||
_currentUser = username;
|
||||
}
|
||||
private ValidationPointDto FillValidationDto(ValidationPoint item)
|
||||
{
|
||||
ValidationPointDto dto = new ValidationPointDto();
|
||||
dto.Active = item.Active;
|
||||
dto.Description = item.Description;
|
||||
dto.Display = item.DisplayName;
|
||||
dto.Id = item.ValidationPointID;
|
||||
return dto;
|
||||
}
|
||||
|
||||
private ValidationPoint FillValidationModel(ValidationPointDto item, ValidationPoint model) {
|
||||
|
||||
model.Active = item.Active;
|
||||
model.Description = item.Description;
|
||||
model.DisplayName = item.Display;
|
||||
model.ValidationPointID = item.Id;
|
||||
return model;
|
||||
}
|
||||
|
||||
public async Task<ResultModel<List<ValidationPointDto>>> LoadValidationPointAsync(bool onlyActive)
|
||||
{
|
||||
try
|
||||
{
|
||||
/*
|
||||
var it = await _context.Validations.AsQueryable().ToListAsync();
|
||||
*/
|
||||
|
||||
ValidationPointDto dto;
|
||||
ValidationPoint val;
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
val = new ValidationPoint()
|
||||
{
|
||||
ValidationPointID = i,
|
||||
Description = $"Validation {i}",
|
||||
DisplayName = $"Display {i}",
|
||||
Active = true,
|
||||
|
||||
|
||||
};
|
||||
|
||||
dto = FillValidationDto(val);
|
||||
_list.Add(dto);
|
||||
}
|
||||
//var it = await Task.Run(() => _list);
|
||||
|
||||
return new ResultModel<List<ValidationPointDto>>()
|
||||
{
|
||||
Data = _list,
|
||||
StatusCode = 1
|
||||
};
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
|
||||
}
|
||||
public async Task<ResultModel<ValidationPointDto>> LoadValidationPointByIdAsync(int id)
|
||||
{
|
||||
ValidationPoint result = null;
|
||||
try
|
||||
{
|
||||
ValidationPointDto dto;
|
||||
result = await _context.Validations.FindAsync(id);
|
||||
dto = FillValidationDto(result);
|
||||
//result = _list.Find(x => x.Id == id);
|
||||
//var dto = await Task.Run(() => result);
|
||||
return new ResultModel<ValidationPointDto>()
|
||||
{
|
||||
Data = dto,
|
||||
StatusCode = 1
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
public async Task<ResultModel<int>> SaveValidationPointAsync(ValidationPointDto item)
|
||||
{
|
||||
int result = default(int);
|
||||
try
|
||||
{
|
||||
ValidationPoint model;
|
||||
if (item.Id > 0)
|
||||
{
|
||||
model = await _context.Validations.FindAsync(item.Id);
|
||||
model = FillValidationModel(item, model);
|
||||
await _context.SaveChangesAsync();
|
||||
result = item.Id;
|
||||
}
|
||||
else //new record
|
||||
{
|
||||
model = new ValidationPoint();
|
||||
model = FillValidationModel(item, model);
|
||||
_context.Validations.Add(model);
|
||||
var id = await _context.SaveChangesAsync();
|
||||
result = id;
|
||||
item.Id = id;
|
||||
}
|
||||
|
||||
//var dto = await Task.Run(() => result);
|
||||
return new ResultModel<int>()
|
||||
{
|
||||
Data = result,
|
||||
StatusCode = 1
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
/*
|
||||
ConcessionTypeDA dataAccess = new ConcessionTypeDA();
|
||||
try
|
||||
{
|
||||
result = await dataAccess.InsertConcessionTypeAsync(item);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
}
|
||||
|
||||
public async Task<ResultModel<bool>> DeleteValidationPointAsync(int id)
|
||||
{
|
||||
bool result = true;
|
||||
try
|
||||
{
|
||||
|
||||
|
||||
ValidationPoint model;
|
||||
if (id > 0)
|
||||
{
|
||||
model = await _context.Validations.FindAsync(id);
|
||||
model.Active = false;
|
||||
//_context.Validations.Remove(model);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
//var dto = await Task.Run(() => result);
|
||||
return new ResultModel<bool>()
|
||||
{
|
||||
Data = result,
|
||||
StatusCode = 1
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
using CarParkValidationAPI.Datas;
|
||||
using CarParkValidationAPI.Entities;
|
||||
using CarParkValidationAPI.Interface;
|
||||
using CarParkValidationAPI.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CarParkValidationAPI.Repository
|
||||
{
|
||||
public class ValidationPointRepository : IValidationPoint
|
||||
{
|
||||
private DataContext _context;
|
||||
|
||||
private readonly AppSettings _appSettings;
|
||||
private string _currentUser = "";
|
||||
private List<ValidationPointDto> _list = new List<ValidationPointDto>();
|
||||
|
||||
public ValidationPointRepository(
|
||||
DataContext context,
|
||||
|
||||
IOptions<AppSettings> appSettings)
|
||||
{
|
||||
_context = context;
|
||||
_appSettings = appSettings.Value;
|
||||
}
|
||||
public void SetCurrentUser(string username)
|
||||
{
|
||||
_currentUser = username;
|
||||
}
|
||||
private ValidationPointDto FillValidationDto(ValidationPoint item)
|
||||
{
|
||||
ValidationPointDto dto = new ValidationPointDto();
|
||||
dto.Active = item.Active;
|
||||
dto.Description = item.Description;
|
||||
dto.Display = item.DisplayName;
|
||||
dto.Id = item.ValidationPointID;
|
||||
return dto;
|
||||
}
|
||||
|
||||
private ValidationPoint FillValidationModel(ValidationPointDto item, ValidationPoint model) {
|
||||
|
||||
model.Active = item.Active;
|
||||
model.Description = item.Description.Trim();
|
||||
model.DisplayName = item.Display.Trim();
|
||||
model.ValidationPointID = item.Id;
|
||||
return model;
|
||||
}
|
||||
|
||||
public async Task<ResultModel<List<ValidationPointDto>>> LoadValidationPointAsync(bool onlyActive)
|
||||
{
|
||||
int statuscode = 0;
|
||||
string error = "";
|
||||
try
|
||||
{
|
||||
var it = await _context.Validations.ToListAsync();
|
||||
|
||||
ValidationPointDto dto;
|
||||
for (int i = 0; i < it.Count; i++)
|
||||
{
|
||||
dto = FillValidationDto(it[i]);
|
||||
_list.Add(dto);
|
||||
}
|
||||
_list.Sort((x, y) => x.Description.CompareTo(y.Description));
|
||||
statuscode = 1;
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
error = ex.ToString();
|
||||
statuscode = 0;
|
||||
throw ex;
|
||||
}
|
||||
return new ResultModel<List<ValidationPointDto>>()
|
||||
{
|
||||
Data = _list,
|
||||
StatusCode = statuscode,
|
||||
Message = error
|
||||
};
|
||||
}
|
||||
public async Task<ResultModel<ValidationPointDto>> LoadValidationPointByIdAsync(int id)
|
||||
{
|
||||
ValidationPoint result = null;
|
||||
int statuscode = 0;
|
||||
string error = "";
|
||||
ValidationPointDto dto = null;
|
||||
try
|
||||
{
|
||||
result = await _context.Validations.FindAsync(id);
|
||||
dto = FillValidationDto(result);
|
||||
//result = _list.Find(x => x.Id == id);
|
||||
//var dto = await Task.Run(() => result);
|
||||
statuscode = 1;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
error = ex.ToString();
|
||||
statuscode = 0;
|
||||
}
|
||||
return new ResultModel<ValidationPointDto>()
|
||||
{
|
||||
Data = dto,
|
||||
StatusCode = statuscode,
|
||||
Message = error
|
||||
};
|
||||
}
|
||||
public async Task<ResultModel<int>> SaveValidationPointAsync(ValidationPointDto item)
|
||||
{
|
||||
int result = default(int);
|
||||
int statuscode = 0;
|
||||
string error = "";
|
||||
try
|
||||
{
|
||||
ValidationPoint model;
|
||||
if (item.Id > 0)
|
||||
{
|
||||
model = await _context.Validations.FindAsync(item.Id);
|
||||
model = FillValidationModel(item, model);
|
||||
model.LastModified = DateTime.Now;
|
||||
model.LastModifiedId = _currentUser;
|
||||
await _context.SaveChangesAsync();
|
||||
result = item.Id;
|
||||
}
|
||||
else //new record
|
||||
{
|
||||
model = new ValidationPoint();
|
||||
model.LastModified = DateTime.Now;
|
||||
model.LastModifiedId = _currentUser;
|
||||
model = FillValidationModel(item, model);
|
||||
_context.Validations.Add(model);
|
||||
var id = await _context.SaveChangesAsync();
|
||||
result = model.ValidationPointID;
|
||||
|
||||
}
|
||||
statuscode = 1;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
error = ex.ToString();
|
||||
statuscode = 0;
|
||||
}
|
||||
//var dto = await Task.Run(() => result);
|
||||
return new ResultModel<int>()
|
||||
{
|
||||
Data = result,
|
||||
StatusCode = statuscode,
|
||||
Message = error
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<ResultModel<bool>> DeleteValidationPointAsync(int id)
|
||||
{
|
||||
bool result = true;
|
||||
int statuscode = 0;
|
||||
string error = "";
|
||||
try
|
||||
{
|
||||
ValidationPoint model;
|
||||
if (id > 0)
|
||||
{
|
||||
model = await _context.Validations.FindAsync(id);
|
||||
model.Active = false;
|
||||
//_context.Validations.Remove(model);
|
||||
await _context.SaveChangesAsync();
|
||||
statuscode = 1;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
error = ex.ToString();
|
||||
statuscode = 0;
|
||||
throw;
|
||||
}
|
||||
//var dto = await Task.Run(() => result);
|
||||
return new ResultModel<bool>()
|
||||
{
|
||||
Data = result,
|
||||
StatusCode = statuscode,
|
||||
Message = error
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user