using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using GreenTree.Nachtragsmanagement.Core.Data; using GreenTree.Nachtragsmanagement.Core.Domain.User; namespace GreenTree.Nachtragsmanagement.Services.User { /// /// User service /// public class UserService : IUserService { #region Fields private readonly IRepository _userRepository; private readonly IRepository _roleRepository; private readonly IRepository _functionRepository; #endregion #region Ctor /// /// Initializes a new instance of the UserService class /// public UserService( IRepository userRepository, IRepository roleRepository, IRepository functionRepository) { _userRepository = userRepository; _roleRepository = roleRepository; _functionRepository = functionRepository; } #endregion #region User /// /// Gets all users /// public IList GetAllUsers() { return _userRepository.Table.ToList(); } /// /// Gets a user by specified Id /// /// User identifier. public Core.Domain.User.User GetUserById(int id) { return _userRepository.GetById(id); } /// /// Gets all users to the specified ids /// public IList GetUsersByIds(int[] ids) { return _userRepository.Table .Where(u => ids.Contains(u.Id)) .ToList(); } /// /// Gets a user by specified custom number /// /// Customer number. public Core.Domain.User.User GetUserByCustomNumber(string customNumber) { return _userRepository .Table.FirstOrDefault(u => u.CustomNumber == customNumber); } /// /// Insert a user /// /// User. public void InsertUser(Core.Domain.User.User user) { _userRepository.Insert(user); } /// /// Update a user /// /// User. public void UpdateUser(Core.Domain.User.User user) { _userRepository.Update(user); } /// /// Delete a user /// /// User. public void DeleteUser(Core.Domain.User.User user) { _userRepository.Delete(user); } #endregion #region Role /// /// Gets all roles /// public IList GetAllRoles() { return _roleRepository.Table.ToList(); } /// /// Gets a role by specified Id /// /// Role identifier. public Role GetRoleById(int id) { return _roleRepository.GetById(id); } /// /// Gets all roles to the specified ids /// public IList GetRolesByIds(int[] ids) { return _roleRepository.Table .Where(r => ids.Contains(r.Id)) .ToList(); } /// /// Gets all users to the corresponding role /// /// Role id. public IList GetUsersByRole(int roleId) { return _userRepository.Table .Where(u => u.Roles .Select(r => r.Id) .Contains(roleId)) .ToList(); } /// /// Insert a user /// /// Role. public void InsertRole(Role role) { _roleRepository.Insert(role); } /// /// Update a role /// /// Role. public void UpdateRole(Role role) { _roleRepository.Update(role); } /// /// Delete a role /// /// Role. public void DeleteRole(Role role) { _roleRepository.Delete(role); } #endregion #region Function /// /// Gets all functions /// public IList GetAllFunctions() { return _functionRepository.Table.ToList(); } /// /// Gets a function by specified Id /// /// Function identifier. public Function GetFunctionById(int id) { return _functionRepository.GetById(id); } /// /// Gets a function by specified name /// /// Function name. public Function GetFunctionByName(string name) { return _functionRepository.Table .FirstOrDefault(f => f.Name == name); } /// /// Gets all functions to the specified ids /// public IList GetFunctionsByIds(int[] ids) { return _functionRepository.Table .Where(f => ids.Contains(f.Id)) .ToList(); } /// /// Insert a function /// /// Function. public void InsertFunction(Function function) { _functionRepository.Insert(function); } /// /// Update a function /// /// Function. public void UpdateFunction(Function function) { _functionRepository.Update(function); } /// /// Delete a function /// /// Function. public void DeleteFunction(Function function) { _functionRepository.Delete(function); } #endregion } }