//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 readonly AppSettings _appSettings; public AdminUserRepository( DataContext context, IOptions appSettings) { _context = context; _appSettings = appSettings.Value; } 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(); int statuscode = 0; string error = ""; try { //var list = await _context.AdminUsers.Where(x => x.Active == onlyActive).ToListAsync(); var list = await _context.AdminUsers.AsQueryable().ToListAsync(); AdminUserDto dto; AdminUser user; for (int i = 0; i < list.Count; i++) { user = list[i]; dto = FillAdminUserDto(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> 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); await _context.SaveChangesAsync(); } else { model = new AdminUser(); model = FillAdminUser(item, model); _context.AdminUsers.Add(model); await _context.SaveChangesAsync(); } result = 1; statuscode = 1; } 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; 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 }; } } }