<commit_before>using System;
using MongoDB.Bson;
using MongoDB.Driver;
using Api.Models;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Api.Data
{
    public class ProductContext: IProductContext
    {
        private IMongoDatabase _mongoDb;
        private IMongoCollection<Product> Products {get; set;}
        private IMongoCollection<ProductHistory> ProductsHistory {get; set;}

        public ProductContext()
        {
            InitializeMongoDatabase();
            Products = _mongoDb.GetCollection<Product>("products");
            ProductsHistory = _mongoDb.GetCollection<ProductHistory>("productsHistory");
        }

        private void InitializeMongoDatabase()
        {
            try
            {
                var client = new MongoClient("mongodb://localhost:27017");
                _mongoDb = client.GetDatabase("local");
            }
            catch (Exception ex) 
            { 
                Console.WriteLine(ex.Message);
                _mongoDb = null; 
            }
        }

        public async Task AddProduct(Product product)
        {
            await Products.InsertOneAsync(product);            
        }

        public async Task Update(Product product)
        {
            //Update
            //await Products.InsertOneAsync(product);            
        }

        public async Task UpdateProductHistory(ProductHistory product)
        {
            //Update
            //await ProductsHistory.InsertOneAsync(product);            
        }

        public async Task AddProductHistory(ProductHistory productHistory)
        {            
            await ProductsHistory.InsertOneAsync(productHistory);
        }

        public async Task<IEnumerable<Product>> GetAll()
        {
            var dataProducts = Products.Find(_ => true);
            if(!dataProducts.Any())
                return null;                       

            return await dataProducts.ToListAsync();
        }

        public async Task<IEnumerable<ProductHistory>> GetAllWithHistory()
        {
            var productWithHistories = await Products
                .Aggregate()
                .Lookup<Product, ProductHistory, ProductWithHistories>(
                    ProductsHistory,
                    r => r.Id,
                    h => h.Id,
                    j => j.ProductsHistory
                ).ToListAsync();      
            
            return productWithHistories.SelectMany(p => p.ProductsHistory);
        }

        public async Task<Product> FindByName(string name)<commit_msg>Implementado update de produto e historico de produto<commit_after>using System;
using MongoDB.Bson;
using MongoDB.Driver;
using Api.Models;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Api.Data
{
    public class ProductContext: IProductContext
    {
        private IMongoDatabase _mongoDb;
        private IMongoCollection<Product> Products {get; set;}
        private IMongoCollection<ProductHistory> ProductsHistory {get; set;}

        public ProductContext()
        {
            InitializeMongoDatabase();
            Products = _mongoDb.GetCollection<Product>("products");
            ProductsHistory = _mongoDb.GetCollection<ProductHistory>("productsHistory");
        }

        private void InitializeMongoDatabase()
        {
            try
            {
                var client = new MongoClient("mongodb://localhost:27017");
                _mongoDb = client.GetDatabase("local");
            }
            catch (Exception ex) 
            { 
                Console.WriteLine(ex.Message);
                _mongoDb = null; 
            }
        }

        public async Task AddProduct(Product product)
        {
            await Products.InsertOneAsync(product);            
        }

        public async Task Update(Product product)
        {            
            await Products.ReplaceOneAsync(p => p.Id == product.Id, product);            
        }

        public async Task UpdateProductHistory(ProductHistory productHistory)
        {
            await ProductsHistory.ReplaceOneAsync(h => h.Id == productHistory.Id, productHistory);            
        }

        public async Task AddProductHistory(ProductHistory productHistory)
        {            
            await ProductsHistory.InsertOneAsync(productHistory);
        }

        public async Task<IEnumerable<Product>> GetAll()
        {
            var dataProducts = Products.Find(_ => true);
            if(!dataProducts.Any())
                return null;                       

            return await dataProducts.ToListAsync();
        }

        public async Task<IEnumerable<ProductHistory>> GetAllWithHistory()
        {
            var productWithHistories = await Products
                .Aggregate()
                .Lookup<Product, ProductHistory, ProductWithHistories>(
                    ProductsHistory,
                    r => r.Id,
                    h => h.Id,
                    j => j.ProductsHistory
                ).ToListAsync();      
            
            return productWithHistories.SelectMany(p => p.ProductsHistory);
        }

        public async Task<Product> FindByName(string name)
{'fraction_non_alphanumeric': 0.07942238267148015, 'fraction_numerical': 0.0019000570017100513, 'mean_word_length': 1.6747967479674797, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 35, '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': '1753395', 'n_tokens_mistral': 1254, 'n_tokens_neox': 1203, 'n_words': 266}