Files
carparkvalidation/API/Repository/AdminUserFakeRepository.cs
2026-04-22 08:48:04 +10:00

206 lines
4.7 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 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
};
}
}
}