<commit_before>﻿using AtomicChessPuzzles.Models;
using MongoDB.Driver;
using System;
using System.Linq;

namespace AtomicChessPuzzles.DbRepositories
{
    public class PuzzleRepository : IPuzzleRepository
    {
        MongoSettings settings;
        IMongoCollection<Puzzle> puzzleCollection;
        Random rnd;

        public PuzzleRepository()
        {
            settings = new MongoSettings();
            rnd = new Random();
            GetCollection();
        }

        private void GetCollection()
        {
            MongoClient client = new MongoClient();
            puzzleCollection = client.GetDatabase(settings.Database).GetCollection<Puzzle>(settings.PuzzleCollectionName);
        }

        public bool Add(Puzzle puzzle)
        {
            var found = puzzleCollection.FindSync<Puzzle>(new ExpressionFilterDefinition<Puzzle>(x => x.ID == puzzle.ID));
            if (found != null && found.Any()) return false;
            try
            {
                puzzleCollection.InsertOne(puzzle);
            }
            catch (Exception e) when (e is MongoWriteException || e is MongoBulkWriteException)
            {
                return false;
            }
            return true;
        }

        public Puzzle Get(string id)
        {
            var found = puzzleCollection.FindSync<Puzzle>(new ExpressionFilterDefinition<Puzzle>(x => string.Compare(x.ID, id, true) == 0));
            if (found == null) return null;
            return found.FirstOrDefault();
        }

        public Puzzle GetOneRandomly()
        {
            ExpressionFilterDefinition<Puzzle> filter = new ExpressionFilterDefinition<Puzzle>(x => true);
            long count = puzzleCollection.Count(filter);
            if (count < 1) return null;
            return puzzleCollection.Find(filter).FirstOrDefault();
        }

        public DeleteResult Remove(string id)
        {
            return puzzleCollection.DeleteOne(x => string.Compare(x.ID, id, true) == 0);
        }

        public DeleteResult RemoveAllBy(string author)
        {
            return puzzleCollection.DeleteMany(new ExpressionFilterDefinition<Puzzle>(x => string.Compare(x.Author, author, true) == 0));
        }
    }
}<commit_msg>Update PuzzleRepository's methods<commit_after>﻿using AtomicChessPuzzles.Models;
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Linq;

namespace AtomicChessPuzzles.DbRepositories
{
    public class PuzzleRepository : IPuzzleRepository
    {
        MongoSettings settings;
        IMongoCollection<Puzzle> puzzleCollection;
        Random rnd;

        public PuzzleRepository()
        {
            settings = new MongoSettings();
            rnd = new Random();
            GetCollection();
        }

        private void GetCollection()
        {
            MongoClient client = new MongoClient();
            puzzleCollection = client.GetDatabase(settings.Database).GetCollection<Puzzle>(settings.PuzzleCollectionName);
        }

        public bool Add(Puzzle puzzle)
        {
            var found = puzzleCollection.Find(new BsonDocument("_id", new BsonString(puzzle.ID)));
            if (found != null && found.Any()) return false;
            try
            {
                puzzleCollection.InsertOne(puzzle);
            }
            catch (Exception e) when (e is MongoWriteException || e is MongoBulkWriteException)
            {
                return false;
            }
            return true;
        }

        public Puzzle Get(string id)
        {
            var found = puzzleCollection.Find(new BsonDocument("_id", new BsonString(id)));
            if (found == null) return null;
            return found.FirstOrDefault();
        }

        public Puzzle GetOneRandomly()
        {
            long count = puzzleCollection.Count(new BsonDocument());
            if (count < 1) return null;
            return puzzleCollection.Find(new BsonDocument()).FirstOrDefault();
        }

        public DeleteResult Remove(string id)
        {
            return puzzleCollection.DeleteOne(new BsonDocument("_id", new BsonString(id)));
        }

        public DeleteResult RemoveAllBy(string author)
        {
            return puzzleCollection.DeleteMany(new BsonDocument("author", new BsonString(author)));
        }
    }
}
{'fraction_non_alphanumeric': 0.09046624913013222, 'fraction_numerical': 0.001159823706796567, 'mean_word_length': 2.0690391459074733, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 19, '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': '4624412', 'n_tokens_mistral': 1130, 'n_tokens_neox': 1064, 'n_words': 252}