147 lines
5.8 KiB
C#
147 lines
5.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using System.Linq;
|
|
using Datamodels.BusinessModels;
|
|
using Datamodels.Lookups;
|
|
using Repositories.Interfaces;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
namespace Repositories
|
|
{
|
|
public class FwMariaLookupRepo: IFwMariaLookupRepo
|
|
{
|
|
|
|
private readonly FwDbContext _dbContext = new FwDbContext();
|
|
private readonly ILogger<FwMariaLookupRepo> _logger;
|
|
private readonly IConfiguration _configuration;
|
|
|
|
public FwMariaLookupRepo(ILogger<FwMariaLookupRepo> logger, IConfiguration configuration)
|
|
{
|
|
_logger = logger;
|
|
_configuration = configuration;
|
|
try
|
|
{
|
|
if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development")
|
|
{
|
|
_dbContext._connectionString = "SERVER=127.0.0.1;DATABASE=testdb;PORT=3306;USER=root;PASSWORD=example";
|
|
}
|
|
else
|
|
{
|
|
string usedDb = configuration.GetSection("GeneralSettings").GetSection("DbToUse").Get<string>();
|
|
string connectionString = configuration.GetConnectionString(usedDb);
|
|
_dbContext._connectionString = connectionString;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Database Connection Configuration not valid");
|
|
}
|
|
}
|
|
|
|
public async Task<DataResult<List<LookupCategory>>> GetAllLookups()
|
|
{
|
|
var result = await Task.Run(() =>
|
|
{
|
|
try
|
|
{
|
|
var newList = _dbContext.LookupCategories.Include(vals => vals.LookupValues).OrderBy(o => o.CategoryName).ToList();
|
|
return new DataResult<List<LookupCategory>> { Data = newList };
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Error reading Lookup Data");
|
|
return new DataResult<List<LookupCategory>>
|
|
{
|
|
UserMessage = "Daten für Lookups konnten nicht gelesen werden.",
|
|
ServerMessage = ex.Message,
|
|
GeneralResult = GeneralResults.DatabaseError,
|
|
StackTrace = ex.StackTrace
|
|
};
|
|
}
|
|
});
|
|
return result;
|
|
}
|
|
|
|
public async Task<DataResult<LookupCategory>> GetLookupCategoryById(long id)
|
|
{
|
|
var result = await Task.Run(() =>
|
|
{
|
|
try
|
|
{
|
|
var cat = _dbContext.LookupCategories.Where(x => x.Id == id).Include(vals => vals.LookupValues).FirstOrDefault();
|
|
return new DataResult<LookupCategory> { Data = cat };
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, $"Error reading Lookup Category with Id: {id}");
|
|
return new DataResult<LookupCategory>
|
|
{
|
|
UserMessage = $"Daten für Lookup Kategorie mit Id {id} konnten nicht gelesen werden.",
|
|
ServerMessage = ex.Message,
|
|
GeneralResult = GeneralResults.DatabaseError,
|
|
StackTrace = ex.StackTrace
|
|
};
|
|
}
|
|
});
|
|
return result;
|
|
}
|
|
|
|
public async Task<DataResult<List<LookupCategory>>> GetLookupCategoryByName(string categoryName)
|
|
{
|
|
var result = await Task.Run(() =>
|
|
{
|
|
try
|
|
{
|
|
var cat = _dbContext.LookupCategories.Where(x => x.CategoryName.ToUpper().Contains(categoryName.ToUpper())).Include(vals => vals.LookupValues).ToList();
|
|
return new DataResult<List<LookupCategory>> { Data = cat };
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, $"Error reading Lookup Category with Name: {categoryName}");
|
|
return new DataResult<List<LookupCategory>>
|
|
{
|
|
UserMessage = $"Daten für Lookup Kategorie mit Namen {categoryName} konnten nicht gelesen werden.",
|
|
ServerMessage = ex.Message,
|
|
GeneralResult = GeneralResults.DatabaseError,
|
|
StackTrace = ex.StackTrace
|
|
};
|
|
}
|
|
});
|
|
return result;
|
|
}
|
|
|
|
public async Task<DataResult<List<LookupCategory>>> GetActiveLookupCategories()
|
|
{
|
|
var result = await Task.Run(() =>
|
|
{
|
|
try
|
|
{
|
|
var catList = _dbContext.LookupCategories.Where(x => x.IsActive).Include(vals => vals.LookupValues).OrderBy(o => o.CategoryName).ToList();
|
|
return new DataResult<List<LookupCategory>> { Data = catList };
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Error reading active Lookup Data");
|
|
return new DataResult<List<LookupCategory>>
|
|
{
|
|
UserMessage = "Daten für aktive Lookups konnten nicht gelesen werden.",
|
|
ServerMessage = ex.Message,
|
|
GeneralResult = GeneralResults.DatabaseError,
|
|
StackTrace = ex.StackTrace
|
|
};
|
|
}
|
|
});
|
|
return result;
|
|
}
|
|
|
|
public void DeleteAllCategories()
|
|
{
|
|
_dbContext.Database.ExecuteSqlRaw("delete from LookupValues");
|
|
_dbContext.Database.ExecuteSqlRaw("delete from LookupCategories");
|
|
|
|
}
|
|
}
|
|
} |