You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IRestaurantUserRepository.cs 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using Microsoft.EntityFrameworkCore;
  2. using ProRestaurant.Classes;
  3. using ProRestaurant.DBContexts;
  4. using ProRestaurant.Models.Accounts;
  5. using ProRestaurant.Models.Restaurants;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Security.Cryptography.Xml;
  9. namespace ProRestaurant.Repository.Restaurants
  10. {
  11. public interface IRestaurantUserRepository
  12. {
  13. List<User> GetUsers(int RestaurantId);
  14. RestaurantUser GetRestaurantUser(int UserId);
  15. void InsertUser(RestaurantUser User);
  16. void UpdateUser(RestaurantUser User);
  17. void RemoveUser(int id);
  18. void Save();
  19. }
  20. public class RestaurantUserRepository : IRestaurantUserRepository
  21. {
  22. private readonly DBContext dBContext;
  23. public RestaurantUserRepository(DBContext db)
  24. {
  25. dBContext = db;
  26. }
  27. public RestaurantUser GetRestaurantUser(int UserId)
  28. {
  29. var restUser = dBContext.RestaurantUsers.Include("User").Where(r => r.UserId == UserId).FirstOrDefault();
  30. if (restUser == null)
  31. {
  32. restUser = new RestaurantUser
  33. {
  34. User = new User()
  35. {
  36. Id = 0,
  37. EmailAddress = "",
  38. FirstName = "",
  39. Surname = "",
  40. Cellphone = ""
  41. }
  42. };
  43. }
  44. return restUser;
  45. }
  46. public List<User> GetUsers(int RestaurantId)
  47. {
  48. var users = dBContext.RestaurantUsers.Include("User").Where(r => r.RestaurantId == RestaurantId).Select(r => r.User).ToList();
  49. return users;
  50. }
  51. public void InsertUser(RestaurantUser User)
  52. {
  53. dBContext.Add(User);
  54. string password = CommonFunctions.GenerateRandomPassword();
  55. User.User.Password = CommonFunctions.GetHashSHA256(password + "≡∆≤≥√∞ProVision");
  56. User.User.ChangePassword = true;
  57. User.User.SystemRole = SystemRole.RestaurantUser;
  58. //TODO: Email generated password to the email address
  59. Save();
  60. }
  61. public void RemoveUser(int id)
  62. {
  63. var user = dBContext.Users.Where(u => u.Id == id).FirstOrDefault();
  64. var rUser = dBContext.RestaurantUsers.Where(r => r.UserId == user.Id).FirstOrDefault();
  65. dBContext.Users.Remove(user);
  66. dBContext.RestaurantUsers.Remove(rUser);
  67. Save();
  68. }
  69. public void Save()
  70. {
  71. dBContext.SaveChanges();
  72. }
  73. public void UpdateUser(RestaurantUser User)
  74. {
  75. dBContext.Entry(User.User).State = EntityState.Modified;
  76. Save();
  77. }
  78. }
  79. }