//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 _ValidationPointDic = null; public AdminUserRepository( DataContext context, IOptions 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>> LoadAdminUserAsync(bool onlyActive) { List resultList = new List(); 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>() { Data = resultList, StatusCode = statuscode, Message = error }; } public async Task> 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() { Data = result, StatusCode = statuscode, Message = error }; } public async Task> 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() { Data = result, StatusCode = statuscode, Message = error }; } public async Task> 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 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() { Data = result, StatusCode = statuscode, Message = error }; } public async Task> 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() { Data = result, StatusCode = statuscode, Message = error }; } } }