<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ArgonautCore.Lw;
using Microsoft.EntityFrameworkCore;
using SoraBot.Data.Dtos;
using SoraBot.Data.Models.SoraDb;
using SoraBot.Data.Repositories.Interfaces;

namespace SoraBot.Data.Repositories
{
    public class WaifuRequestRepository : IWaifuRequestRepository
    {
        private readonly ITransactor<SoraContext> _soraTransactor;

        public WaifuRequestRepository(ITransactor<SoraContext> soraTransactor)
        {
            _soraTransactor = soraTransactor;
        }

        public async Task<Option<List<WaifuRequest>>> GetUserWaifuRequests(ulong userId)
            => await _soraTransactor.DoAsync<Option<List<WaifuRequest>>>(async context =>
            {
                var requests = await context.WaifuRequests
                    .Where(x => x.UserId == userId)
                    .ToListAsync()
                    .ConfigureAwait(false);

                if (requests.Count == 0)
                    return Option.None<List<WaifuRequest>>();

                return requests;
            }).ConfigureAwait(false);

        public async Task<bool> UserHasNotificationOn(ulong userId)
            => await _soraTransactor.DoAsync(async context =>
                    await context.UserNotifiedOnRequestProcesses.FindAsync(userId) != null)
                .ConfigureAwait(false);

        public async Task ActivateUserNotification(ulong userId)
            => await _soraTransactor.DoInTransactionAsync(async context =>
            {
                var notif = await context.UserNotifiedOnRequestProcesses.FindAsync(userId).ConfigureAwait(false);
                if (notif != null) return; // Already set so we ignore
                var setNotif = new UserNotifiedOnRequestProcess(userId);
                context.UserNotifiedOnRequestProcesses.Add(setNotif);
                await context.SaveChangesAsync().ConfigureAwait(false);
            });

        public async Task RemoveUserNotification(ulong userId)
            => await _soraTransactor.DoInTransactionAsync(async context =>
            {
                var notif = await context.UserNotifiedOnRequestProcesses.FindAsync(userId).ConfigureAwait(false);
                if (notif == null) return; // Already not existant so we ignore
                context.UserNotifiedOnRequestProcesses.Remove(notif);
                await context.SaveChangesAsync().ConfigureAwait(false);
            });

        public async Task<Option<List<WaifuRequest>>> AllActiveRequests()
            => await _soraTransactor.DoAsync<Option<List<WaifuRequest>>>(async context =>
            {
                var reqs = await context.WaifuRequests
                    .Where(x => x.RequestState == RequestState.Pending)
                    .ToListAsync()
                    .ConfigureAwait(false);
                return reqs.Count == 0 ? Option.None<List<WaifuRequest>>() : reqs;
            }).ConfigureAwait(false);

        public async Task<Option<List<WaifuRequest>>> AllRequests()
            => await _soraTransactor.DoAsync<Option<List<WaifuRequest>>>(async context =>
            {
                var reqs = await context.WaifuRequests
                    .ToListAsync()
                    .ConfigureAwait(false);
                return reqs.Count == 0 ? Option.None<List<WaifuRequest>>() : reqs;
            }).ConfigureAwait(false);

        public async Task AddWaifuRequest(WaifuRequestAddDto waifuRequestAddDto)
            => await _soraTransactor.DoInTransactionAsync(async context =>
            {
                WaifuRequest req = new WaifuRequest()
                {
                    Name = waifuRequestAddDto.Name,
                    ImageUrl = waifuRequestAddDto.ImageUrl,
                    Rarity = waifuRequestAddDto.Rarity,
                    RequestState = RequestState.Pending,
                    RequestTime = DateTime.UtcNow,
                    UserId = ulong.Parse(waifuRequestAddDto.UserId),
                };
                context.WaifuRequests.Add(req);
                await context.SaveChangesAsync().ConfigureAwait(false);
            }).ConfigureAwait(false);

        public async Task EditWaifuRequest(WaifuRequestEditDto waifuRequestAddDto)
            => await _soraTransactor.DoInTransactionAsync(async context =>
            {
                var req = await context.WaifuRequests.FindAsync(waifuRequestAddDto.RequestId).ConfigureAwait(false);
                if (req == null) return; // no op if it doesn't exist

                req.Name = waifuRequestAddDto.Name;
                req.ImageUrl = waifuRequestAddDto.ImageUrl;
                req.Rarity = waifuRequestAddDto.Rarity;

                await context.SaveChangesAsync().ConfigureAwait(false);
            }).ConfigureAwait(false);

        public async Task<bool> WaifuExists(string waifuName)
            => await _soraTransactor.DoAsync(async context =>
                    await context.Waifus.FirstOrDefaultAsync(x =>
                        x.Name.Equals(waifuName, StringComparison.OrdinalIgnoreCase)) != null)
                .ConfigureAwait(false);

        public async Task<bool> WaifuExists(int id)
            => await _soraTransactor.DoAsync(async context =>
                    await context.Waifus.FindAsync(id) != null)
                .ConfigureAwait(false);

        public async Task<int> UserRequestCountLast24Hours(ulong userId)
            => await _soraTransactor.DoAsync<int>(async context =>
                {
                    var dt = DateTime.UtcNow.Subtract(TimeSpan.FromHours(24));
                    return await context.WaifuRequests.CountAsync(x => x.UserId == userId && x.RequestTime > dt)
                        .ConfigureAwait(false);
                })
                .ConfigureAwait(false);
    }
}<commit_msg>#116 - Make sure only pending is counted<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ArgonautCore.Lw;
using Microsoft.EntityFrameworkCore;
using SoraBot.Data.Dtos;
using SoraBot.Data.Models.SoraDb;
using SoraBot.Data.Repositories.Interfaces;

namespace SoraBot.Data.Repositories
{
    public class WaifuRequestRepository : IWaifuRequestRepository
    {
        private readonly ITransactor<SoraContext> _soraTransactor;

        public WaifuRequestRepository(ITransactor<SoraContext> soraTransactor)
        {
            _soraTransactor = soraTransactor;
        }

        public async Task<Option<List<WaifuRequest>>> GetUserWaifuRequests(ulong userId)
            => await _soraTransactor.DoAsync<Option<List<WaifuRequest>>>(async context =>
            {
                var requests = await context.WaifuRequests
                    .Where(x => x.UserId == userId)
                    .ToListAsync()
                    .ConfigureAwait(false);

                if (requests.Count == 0)
                    return Option.None<List<WaifuRequest>>();

                return requests;
            }).ConfigureAwait(false);

        public async Task<bool> UserHasNotificationOn(ulong userId)
            => await _soraTransactor.DoAsync(async context =>
                    await context.UserNotifiedOnRequestProcesses.FindAsync(userId) != null)
                .ConfigureAwait(false);

        public async Task ActivateUserNotification(ulong userId)
            => await _soraTransactor.DoInTransactionAsync(async context =>
            {
                var notif = await context.UserNotifiedOnRequestProcesses.FindAsync(userId).ConfigureAwait(false);
                if (notif != null) return; // Already set so we ignore
                var setNotif = new UserNotifiedOnRequestProcess(userId);
                context.UserNotifiedOnRequestProcesses.Add(setNotif);
                await context.SaveChangesAsync().ConfigureAwait(false);
            });

        public async Task RemoveUserNotification(ulong userId)
            => await _soraTransactor.DoInTransactionAsync(async context =>
            {
                var notif = await context.UserNotifiedOnRequestProcesses.FindAsync(userId).ConfigureAwait(false);
                if (notif == null) return; // Already not existant so we ignore
                context.UserNotifiedOnRequestProcesses.Remove(notif);
                await context.SaveChangesAsync().ConfigureAwait(false);
            });

        public async Task<Option<List<WaifuRequest>>> AllActiveRequests()
            => await _soraTransactor.DoAsync<Option<List<WaifuRequest>>>(async context =>
            {
                var reqs = await context.WaifuRequests
                    .Where(x => x.RequestState == RequestState.Pending)
                    .ToListAsync()
                    .ConfigureAwait(false);
                return reqs.Count == 0 ? Option.None<List<WaifuRequest>>() : reqs;
            }).ConfigureAwait(false);

        public async Task<Option<List<WaifuRequest>>> AllRequests()
            => await _soraTransactor.DoAsync<Option<List<WaifuRequest>>>(async context =>
            {
                var reqs = await context.WaifuRequests
                    .ToListAsync()
                    .ConfigureAwait(false);
                return reqs.Count == 0 ? Option.None<List<WaifuRequest>>() : reqs;
            }).ConfigureAwait(false);

        public async Task AddWaifuRequest(WaifuRequestAddDto waifuRequestAddDto)
            => await _soraTransactor.DoInTransactionAsync(async context =>
            {
                WaifuRequest req = new WaifuRequest()
                {
                    Name = waifuRequestAddDto.Name,
                    ImageUrl = waifuRequestAddDto.ImageUrl,
                    Rarity = waifuRequestAddDto.Rarity,
                    RequestState = RequestState.Pending,
                    RequestTime = DateTime.UtcNow,
                    UserId = ulong.Parse(waifuRequestAddDto.UserId),
                };
                context.WaifuRequests.Add(req);
                await context.SaveChangesAsync().ConfigureAwait(false);
            }).ConfigureAwait(false);

        public async Task EditWaifuRequest(WaifuRequestEditDto waifuRequestAddDto)
            => await _soraTransactor.DoInTransactionAsync(async context =>
            {
                var req = await context.WaifuRequests.FindAsync(waifuRequestAddDto.RequestId).ConfigureAwait(false);
                if (req == null) return; // no op if it doesn't exist

                req.Name = waifuRequestAddDto.Name;
                req.ImageUrl = waifuRequestAddDto.ImageUrl;
                req.Rarity = waifuRequestAddDto.Rarity;

                await context.SaveChangesAsync().ConfigureAwait(false);
            }).ConfigureAwait(false);

        public async Task<bool> WaifuExists(string waifuName)
            => await _soraTransactor.DoAsync(async context =>
                    await context.Waifus.FirstOrDefaultAsync(x =>
                        x.Name.Equals(waifuName, StringComparison.OrdinalIgnoreCase)) != null)
                .ConfigureAwait(false);

        public async Task<bool> WaifuExists(int id)
            => await _soraTransactor.DoAsync(async context =>
                    await context.Waifus.FindAsync(id) != null)
                .ConfigureAwait(false);

        public async Task<int> UserRequestCountLast24Hours(ulong userId)
            => await _soraTransactor.DoAsync<int>(async context =>
                {
                    var dt = DateTime.UtcNow.Subtract(TimeSpan.FromHours(24));
                    return await context.WaifuRequests.CountAsync(x => x.UserId == userId && x.RequestTime > dt && x.RequestState == RequestState.Pending)
                        .ConfigureAwait(false);
                })
                .ConfigureAwait(false);
    }
}
{'fraction_non_alphanumeric': 0.08838747985410128, 'fraction_numerical': 0.0014420222241072185, 'mean_word_length': 2.07752545027408, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 119, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1085292', 'n_tokens_mistral': 3237, 'n_tokens_neox': 3120, 'n_words': 546}