Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. IList<User> users;
  2. using (AppDbContext db = new AppDbContext())
  3. {
  4. users = db.Users.Where(x => x.Active == true).ToList();
  5. }
  6.  
  7. // do code to update UI
  8.  
  9. public interface IUserService : IDisposable
  10. {
  11. IList<applicationuser> GetUsers();
  12. IList<AllApplicationUser> GetActiveUsers();
  13. AllApplicationUser GetUserView(long id);
  14. applicationuser GetUser(long id);
  15. void CreateUser(applicationuser user);
  16. void UpdateUser(applicationuser user);
  17. void DeleteUser(long id);
  18. void Save();
  19. applicationuser Authenticate(string username, string password);
  20. }
  21.  
  22. class UserService : IUserService
  23. {
  24. private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
  25. private readonly OMEGAEntities _db;
  26. private bool _disposed = false;
  27.  
  28. public UserService()
  29. {
  30. _db = new OMEGAEntities();
  31. }
  32.  
  33. public void Dispose()
  34. {
  35. Dispose(true);
  36. GC.SuppressFinalize(this);
  37. }
  38.  
  39. protected virtual void Dispose(bool disposing)
  40. {
  41. if (!_disposed)
  42. {
  43. if (disposing)
  44. {
  45. _db.Dispose();
  46. }
  47. }
  48. _disposed = true;
  49. }
  50.  
  51. public IList<applicationuser> GetUsers()
  52. {
  53. // fetch only active users that do not have the role SuperAdmin
  54. return _db.applicationusers.Where(u => u.roleid != 1 && u.activeflag == true).ToList();
  55. }
  56.  
  57. public IList<AllApplicationUser> GetActiveUsers()
  58. {
  59. return _db.AllApplicationUsers.Where(u => u.activeflag == true && u.roleid != 1).ToList();
  60. }
  61.  
  62. public AllApplicationUser GetUserView(long id)
  63. {
  64. return _db.AllApplicationUsers.Single(x => x.id == id);
  65. }
  66.  
  67. public applicationuser GetUser(long id)
  68. {
  69. return _db.applicationusers.Find(id);
  70. }
  71.  
  72. public void CreateUser(applicationuser user)
  73. {
  74. _db.applicationusers.Add(user);
  75. }
  76.  
  77. public void UpdateUser(applicationuser user)
  78. {
  79. _db.Entry(user).State = EntityState.Modified;
  80. }
  81.  
  82. public void DeleteUser(long id)
  83. {
  84. var user = _db.applicationusers.Find(id);
  85. _db.applicationusers.Remove(user);
  86. }
  87.  
  88. public void Save()
  89. {
  90. _db.SaveChanges();
  91. }
  92.  
  93. public applicationuser Authenticate(string username, string password)
  94. {
  95. applicationuser user =
  96. _db.applicationusers.SingleOrDefault(u => u.loginid.ToLower().Equals(username.ToLower()));
  97.  
  98. if (user != null)
  99. {
  100. if (Common.Utils.DecryptData(user.password).Equals(password))
  101. {
  102. return user;
  103. }
  104. }
  105.  
  106. return null;
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement