276 lines
6.5 KiB
C#
276 lines
6.5 KiB
C#
//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.Id;
|
|
dto.FirstName = model.FirstName;
|
|
dto.RoleType = (int) model.RoleType;
|
|
dto.StafflinkNo = model.StafflinkNo;
|
|
dto.Active = model.Active;
|
|
dto.LastName = model.LastName;
|
|
dto.AddedOn = Helper.DateToStr(model.AddedOn);
|
|
dto.ValidationPoint = GetValidtionPointDesc(model.ValidationPointId??0);
|
|
dto.ValidationPointId = model.ValidationPointId ?? 0;
|
|
return dto;
|
|
}
|
|
private AdminUserDto FillAdminUserDto(AdminUser model)
|
|
{
|
|
AdminUserDto dto = new AdminUserDto();
|
|
dto.Id = model.Id;
|
|
dto.FirstName = model.FirstName;
|
|
dto.RoleType = model.RoleType;
|
|
dto.StafflinkNo = model.StafflinkNo;
|
|
dto.Active = model.Active;
|
|
dto.LastName = model.LastName;
|
|
dto.AddedOn = Helper.DateToStr(model.AddedOn);
|
|
dto.ValidationPointId = model.ValidationPointId ?? 0;
|
|
return dto;
|
|
}
|
|
|
|
private AdminUser FillAdminUser(AdminUserDto dto, AdminUser model)
|
|
{
|
|
if (dto.Id > 0)
|
|
model.Id = dto.Id;
|
|
model.FirstName = dto.FirstName;
|
|
model.RoleType = (int) dto.RoleType;
|
|
model.StafflinkNo = dto.StafflinkNo;
|
|
model.Active = dto.Active ?? false;
|
|
model.LastName = dto.LastName;
|
|
model.AddedOn = Helper.StrToDate(dto.AddedOn);
|
|
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.Id, 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 = -1;
|
|
|
|
}
|
|
|
|
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 = -1;
|
|
|
|
}
|
|
|
|
return new ResultModel<bool>()
|
|
{
|
|
Data = result,
|
|
StatusCode = statuscode,
|
|
Message = error
|
|
};
|
|
}
|
|
public async Task<ResultModel<AdminUserDto>> LoadAdminUserByIdAsync(int id)
|
|
{
|
|
AdminUserDto result = null;
|
|
int statuscode = -1;
|
|
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 = -1;
|
|
|
|
}
|
|
|
|
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.Id;
|
|
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.Id;
|
|
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 = -1;
|
|
|
|
}
|
|
|
|
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 = -1;
|
|
result = false;
|
|
|
|
}
|
|
|
|
return new ResultModel<bool>()
|
|
{
|
|
Data = result,
|
|
StatusCode = statuscode,
|
|
Message = error
|
|
};
|
|
}
|
|
|
|
|
|
}
|
|
} |