﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BaseStation.Domain.Abstract;
using BaseStation.Domain.Entities;
using Microsoft.EntityFrameworkCore;

namespace BaseStation.Data
{
    public class EfAppTokenRepository : EfRepository<AppToken>, IAppTokenRepository
    {
        public EfAppTokenRepository(Context context, IChangeTracker tracker) : base(context, tracker)
        {
        }

        public AppToken FindByToken(string token)
        {
            var appToken = Context.AppTokens.Include(x => x.AppUser).FirstOrDefault(x => x.Token == token);
            return appToken ?? AppToken.Null;
        }

        public async Task<AppToken> FindByTokenAsync(string token)
        {
            var appToken = await Context.AppTokens.FirstOrDefaultAsync(x => x.Token == token);
            return appToken ?? AppToken.Null;
        }

        public IList<AppToken> GetByUser(int userId)
        {
            return Context.AppTokens.Where(x => x.AppUserId == userId).ToList();
        }
    }
}