﻿using System.Linq;
using App.DataCore.Core;
using App.DataCore.Data;
using App.DataCore.Repository.Interfaces;

namespace App.DataCore.Repository
{
    public sealed class UsersRepository : BaseRepository<User>, IUsersRepository
    {
        public UsersRepository(IDatabaseContext database)
            : base(database)
        {
        }

        public User Get(int id)
        {
            return Database.Users.SingleOrDefault(u => u.Id == id);
        }

        public User GetByUsername(string username)
        {
            return Database.Users.SingleOrDefault(u => u.UserName == username);
        }

        public IQueryable<User> GetAllByRole(string roleName)
        {
            // TODO: test the method!

            var data = from users in Database.Users
                       where users.UserRoles.Single(r => r.Name == roleName) != null
                       select users;

            return data;
        }
    }
}
