DEV Community

Robertblazor
Robertblazor

Posted on

Blazor app give you some auto calculation

It looks like I missed the git operations in my last post, here I have it available

dotnet new gitignore
Enter fullscreen mode Exit fullscreen mode

Now, you can start from here to either move this project to GitHub or use Git to keep track of this project

Continue with the models file, we can define data access files as well.

namespace ThreadsLib.DataAccess
{
    public class DbConnection : IDbConnection
    {
        private readonly IConfiguration _config;
        private readonly IMongoDatabase _db;
        private string _connectionId = "MongoDB";
        public string DbName { get; private set; }
        public string MetricExternalCollectionName { get; private set; } = "Metric_External_ISO";
        public string MetricInternalCollectionName { get; private set; } = "Metric_Internal_ISO";
        public string MetricWireCollectionName { get; private set; } = "MetricWire";
        public string StandardExternalCollectionName { get; private set; } = "Standard_External_ANSI";
        public string StandardInternalCollectionName { get; private set; } = "Standard_Internal_ANSI";
        public string StandardWireCollectionName { get; private set; } = "StandardWire";
        public string UserCollectionName { get; private set; } = "User_Collection";
        public MongoClient Client { get; private set; }
        public IMongoCollection<MetricExternal> MetricExternalCollection { get; private set; }
        public IMongoCollection<MetricInternal> MetricInternalCollection { get; private set; }
        public IMongoCollection<MetricWire> MetricWireCollection { get; private set; }
        public IMongoCollection<StandardExternal> StandardExternalCollection { get; private set; }
        public IMongoCollection<StandardInternal> StandardInternalCollection { get; private set; }
        public IMongoCollection<StandardWire> StandardWireCollection { get; private set; }
        public IMongoCollection<UserModel> UserCollection { get; private set; }
        public DbConnection(IConfiguration config)
        {
            _config = config;
            Client = new MongoClient(_config.GetConnectionString(_connectionId));
            DbName = _config["DatabaseName"];
            _db = Client.GetDatabase(DbName);

            MetricExternalCollection = _db.GetCollection<MetricExternal>(MetricExternalCollectionName);
            MetricInternalCollection = _db.GetCollection<MetricInternal>(MetricInternalCollectionName);
            MetricWireCollection = _db.GetCollection<MetricWire>(MetricWireCollectionName);
            StandardExternalCollection = _db.GetCollection<StandardExternal>(StandardExternalCollectionName);
            StandardInternalCollection = _db.GetCollection<StandardInternal>(StandardInternalCollectionName);
            StandardWireCollection = _db.GetCollection<StandardWire>(StandardWireCollectionName);
            UserCollection = _db.GetCollection<UserModel>(UserCollectionName);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

namespace ThreadsLib.DataAccess
{
    public class MongoMetricExternalCollection : IMetricExternalCollection
    {
        private readonly IMongoCollection<MetricExternal> _metricExternals;
        private readonly IMemoryCache _cache;
        private const string cacheName = "MetricExternalData";
        public MongoMetricExternalCollection(IDbConnection db, IMemoryCache cache)
        {
            _cache = cache;
            _metricExternals = db.MetricExternalCollection;
        }
        public async Task<List<MetricExternal>> GetAllMetricExternalsAsync()
        {
            var output = _cache.Get<List<MetricExternal>>(cacheName);
            if (output == null)
            {
                var results = await _metricExternals.FindAsync(_ => true);
                output = results.ToList();
                _cache.Set(cacheName, output, TimeSpan.FromDays(1));
            }
            return output;
        }
        public Task CreateMetricExternalAsync(MetricExternal metricExternal)
        {
            return _metricExternals.InsertOneAsync(metricExternal);
        }
        public async Task<List<MetricExternal>> GetMetricExternalsByDesignition(string designition)
        {
            var results = await GetAllMetricExternalsAsync();
            return results.Where(me => me.CustomThreadDesignition == designition).ToList();
        }
        public async Task<MetricExternal> GetMetricExternalByInternalId(int id)
        {
            var results = await GetAllMetricExternalsAsync();
#pragma warning disable CS8603 // Possible null reference return.
            return results.FirstOrDefault(me => me.InternalId == id);
#pragma warning restore CS8603 // Possible null reference return.
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

We should be able to access the data based on those files, these are just our data service files.

Here is the GitHub Repo you can refer to:
GitHub repo of my project

Once these files are accomplished, we can continue on some more work on the database.

Top comments (0)