UserService.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using GreenTree.Nachtragsmanagement.Core.Data;
  7. using GreenTree.Nachtragsmanagement.Core.Domain.User;
  8. namespace GreenTree.Nachtragsmanagement.Services.User
  9. {
  10. /// <summary>
  11. /// User service
  12. /// </summary>
  13. public class UserService : IUserService
  14. {
  15. #region Fields
  16. private readonly IRepository<Core.Domain.User.User> _userRepository;
  17. private readonly IRepository<Role> _roleRepository;
  18. private readonly IRepository<Function> _functionRepository;
  19. #endregion
  20. #region Ctor
  21. /// <summary>
  22. /// Initializes a new instance of the UserService class
  23. /// </summary>
  24. public UserService(
  25. IRepository<Core.Domain.User.User> userRepository,
  26. IRepository<Role> roleRepository,
  27. IRepository<Function> functionRepository)
  28. {
  29. _userRepository = userRepository;
  30. _roleRepository = roleRepository;
  31. _functionRepository = functionRepository;
  32. }
  33. #endregion
  34. #region User
  35. /// <summary>
  36. /// Gets all users
  37. /// </summary>
  38. public IList<Core.Domain.User.User> GetAllUsers()
  39. {
  40. return _userRepository.Table.ToList();
  41. }
  42. /// <summary>
  43. /// Gets a user by specified Id
  44. /// </summary>
  45. /// <param name="id">User identifier.</param>
  46. public Core.Domain.User.User GetUserById(int id)
  47. {
  48. return _userRepository.GetById(id);
  49. }
  50. /// <summary>
  51. /// Gets all users to the specified ids
  52. /// </summary>
  53. public IList<Core.Domain.User.User> GetUsersByIds(int[] ids)
  54. {
  55. return _userRepository.Table
  56. .Where(u => ids.Contains(u.Id))
  57. .ToList();
  58. }
  59. /// <summary>
  60. /// Gets a user by specified custom number
  61. /// </summary>
  62. /// <param name="customNumber">Customer number.</param>
  63. public Core.Domain.User.User GetUserByCustomNumber(string customNumber)
  64. {
  65. return _userRepository
  66. .Table.FirstOrDefault(u => u.CustomNumber == customNumber);
  67. }
  68. /// <summary>
  69. /// Insert a user
  70. /// </summary>
  71. /// <param name="user">User.</param>
  72. public void InsertUser(Core.Domain.User.User user)
  73. {
  74. _userRepository.Insert(user);
  75. }
  76. /// <summary>
  77. /// Update a user
  78. /// </summary>
  79. /// <param name="user">User.</param>
  80. public void UpdateUser(Core.Domain.User.User user)
  81. {
  82. _userRepository.Update(user);
  83. }
  84. /// <summary>
  85. /// Delete a user
  86. /// </summary>
  87. /// <param name="user">User.</param>
  88. public void DeleteUser(Core.Domain.User.User user)
  89. {
  90. _userRepository.Delete(user);
  91. }
  92. #endregion
  93. #region Role
  94. /// <summary>
  95. /// Gets all roles
  96. /// </summary>
  97. public IList<Role> GetAllRoles()
  98. {
  99. return _roleRepository.Table.ToList();
  100. }
  101. /// <summary>
  102. /// Gets a role by specified Id
  103. /// </summary>
  104. /// <param name="id">Role identifier.</param>
  105. public Role GetRoleById(int id)
  106. {
  107. return _roleRepository.GetById(id);
  108. }
  109. /// <summary>
  110. /// Gets all roles to the specified ids
  111. /// </summary>
  112. public IList<Role> GetRolesByIds(int[] ids)
  113. {
  114. return _roleRepository.Table
  115. .Where(r => ids.Contains(r.Id))
  116. .ToList();
  117. }
  118. /// <summary>
  119. /// Gets all users to the corresponding role
  120. /// </summary>
  121. /// <param name="roleId">Role id.</param>
  122. public IList<Core.Domain.User.User> GetUsersByRole(int roleId)
  123. {
  124. return _userRepository.Table
  125. .Where(u => u.Roles
  126. .Select(r => r.Id)
  127. .Contains(roleId))
  128. .ToList();
  129. }
  130. /// <summary>
  131. /// Insert a user
  132. /// </summary>
  133. /// <param name="role">Role.</param>
  134. public void InsertRole(Role role)
  135. {
  136. _roleRepository.Insert(role);
  137. }
  138. /// <summary>
  139. /// Update a role
  140. /// </summary>
  141. /// <param name="role">Role.</param>
  142. public void UpdateRole(Role role)
  143. {
  144. _roleRepository.Update(role);
  145. }
  146. /// <summary>
  147. /// Delete a role
  148. /// </summary>
  149. /// <param name="role">Role.</param>
  150. public void DeleteRole(Role role)
  151. {
  152. _roleRepository.Delete(role);
  153. }
  154. #endregion
  155. #region Function
  156. /// <summary>
  157. /// Gets all functions
  158. /// </summary>
  159. public IList<Function> GetAllFunctions()
  160. {
  161. return _functionRepository.Table.ToList();
  162. }
  163. /// <summary>
  164. /// Gets a function by specified Id
  165. /// </summary>
  166. /// <param name="id">Function identifier.</param>
  167. public Function GetFunctionById(int id)
  168. {
  169. return _functionRepository.GetById(id);
  170. }
  171. /// <summary>
  172. /// Gets all functions to the specified ids
  173. /// </summary>
  174. public IList<Function> GetFunctionsByIds(int[] ids)
  175. {
  176. return _functionRepository.Table
  177. .Where(f => ids.Contains(f.Id))
  178. .ToList();
  179. }
  180. /// <summary>
  181. /// Insert a function
  182. /// </summary>
  183. /// <param name="function">Function.</param>
  184. public void InsertFunction(Function function)
  185. {
  186. _functionRepository.Insert(function);
  187. }
  188. /// <summary>
  189. /// Update a function
  190. /// </summary>
  191. /// <param name="function">Function.</param>
  192. public void UpdateFunction(Function function)
  193. {
  194. _functionRepository.Update(function);
  195. }
  196. /// <summary>
  197. /// Delete a function
  198. /// </summary>
  199. /// <param name="function">Function.</param>
  200. public void DeleteFunction(Function function)
  201. {
  202. _functionRepository.Delete(function);
  203. }
  204. #endregion
  205. }
  206. }