//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 _resultList = new List(); private readonly AppSettings _appSettings; public AdminUserFakeRepository( DataContext context, IOptions 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>> LoadAdminUserAsync(bool onlyActive) { /* List resultList = new List(); 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>() { Data = resultList, StateCode = 1 }; } catch (Exception ex) { throw ex; } */ for (int i =0; i < 10; 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>() { Data = _resultList, StatusCode = 1 }; } public async Task> 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() { Data = result, StatusCode = 1 }; } public async Task> 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() { Data = result, StatusCode = 1 }; } public async Task> 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() { Data = result, StatusCode = 1 }; } } }