Adding of archive items is mostly operational. Need to handle file upload

This commit is contained in:
Vincent Allen
2025-07-29 16:16:42 -04:00
parent 13c45e8459
commit 6475a28263
158 changed files with 2628 additions and 801 deletions

View File

@@ -1,62 +1,17 @@
using Microsoft.Extensions.Options;
using Npgsql;
using Dapper;
using OpenArchival.Core;
namespace OpenArchival.Database.Category;
public class Category
{
public int CategoryId { get; set; }
public required string CategoryName { get; set; }
public required string FieldSeparator { get; set; }
public required string[] FieldNames { get; set; }
public required string[] FieldDescriptions { get; set; }
}
public class CategoryFieldOption
{
public required int CategoryId { get; set; }
public required int FieldNumber { get; set; }
public required string Value { get; set; }
public required string Name { get; set; }
public string? Description { get; set; }
}
namespace OpenArchival.Database;
public class CategoryProvider : ICategoryProvider
{
public static string TableCreationQuery = """
DROP TABLE IF EXISTS Categories CASCADE;
CREATE TABLE IF NOT EXISTS Categories (
categoryid SERIAL PRIMARY KEY,
categoryname TEXT NOT NULL,
fieldseparator TEXT NOT NULL,
fieldnames TEXT [] NOT NULL,
fielddescriptions TEXT [] NOT NULL
);
CREATE TABLE IF NOT EXISTS CategoryFieldOptions (
categoryid INT NOT NULL,
fieldnumber INT NOT NULL,
value TEXT NOT NULL,
name TEXT NOT NULL,
FOREIGN KEY (categoryid) REFERENCES Categories(categoryid)
);
""";
private readonly PostgresConnectionOptions _options;
private readonly NpgsqlDataSource _dataSource;
public CategoryProvider(IOptions<PostgresConnectionOptions> options, NpgsqlDataSource databaseConnection)
public CategoryProvider(NpgsqlDataSource databaseConnection)
{
_options = options?.Value ?? throw new ArgumentNullException(nameof(options), "Postgres connection options cannot be null.");
_dataSource = databaseConnection ?? throw new ArgumentNullException(nameof(databaseConnection), "Database connection cannot be null.");
if (_options.Host == null || _options.Port <= 0 || _options.Database == null || _options.Username == null || _options.Password == null)
{
throw new ArgumentException("Postgres connection options are not properly configured.");
}
}
public async Task<Category?> GetCategoryAsync(string categoryName)
@@ -68,6 +23,17 @@ public class CategoryProvider : ICategoryProvider
return await connection.QueryFirstOrDefaultAsync<Category>(sql, new {CategoryName=categoryName});
}
public async Task<bool> RemoveCategory(string categoryName)
{
await using var connection = await _dataSource.OpenConnectionAsync();
var sql = @"DELETE FROM Categories WHERE categoryname = @CategoryName";
int rowsAffected = await connection.ExecuteAsync(sql, new { CategoryName = categoryName });
return rowsAffected > 0;
}
public async Task<int?> GetCategoryId(string categoryName)
{
await using var connection = await _dataSource.OpenConnectionAsync();
@@ -86,6 +52,24 @@ public class CategoryProvider : ICategoryProvider
return await connection.QueryAsync<Category>(sql);
}
public async Task<IEnumerable<Category>> SearchCategories(string searchQuery)
{
await using var connection = await _dataSource.OpenConnectionAsync();
var sql = @"SELECT * FROM Categories WHERE POSITION(LOWER(@SearchQuery) in LOWER(categoryname)) > 0";
return await connection.QueryAsync<Category>(sql, new {SearchQuery=searchQuery});
}
public async Task<IEnumerable<Category>> TopCategories(int numResults)
{
await using var connection = await _dataSource.OpenConnectionAsync();
var sql = $@"SELECT * FROM Categories ORDER BY categoryname ASC LIMIT {numResults}";
return await connection.QueryAsync<Category>(sql);
}
public async Task<int> InsertCategoryAsync(Category category)
{
await using var connection = await _dataSource.OpenConnectionAsync();
@@ -99,8 +83,6 @@ public class CategoryProvider : ICategoryProvider
{
await using var connection = await _dataSource.OpenConnectionAsync();
// 1. Add commas between each SET assignment.
// 2. Use a distinct parameter (e.g., @OldCategoryName) in the WHERE clause.
const string sql = @"
UPDATE Categories
SET
@@ -110,16 +92,13 @@ public class CategoryProvider : ICategoryProvider
fielddescriptions = @FieldDescriptions
WHERE categoryname = @OldCategoryName;";
// 3. Create a parameter object that includes the value for the WHERE clause.
var parameters = new
{
// These parameters come from the 'category' object for the SET clause
category.CategoryName,
category.FieldSeparator,
category.FieldNames,
category.FieldDescriptions,
// This parameter comes from the method argument for the WHERE clause
OldCategoryName = oldCategoryName
};

View File

@@ -1,4 +1,6 @@
namespace OpenArchival.Database.Category;
using OpenArchival.Core;
namespace OpenArchival.Database;
public interface ICategoryProvider
{
@@ -6,6 +8,10 @@ public interface ICategoryProvider
public Task<int?> GetCategoryId(string categoryName);
public Task<IEnumerable<Category>> TopCategories(int numResults);
public Task<IEnumerable<Category>> SearchCategories(string searchQuery);
public Task<int> InsertCategoryAsync(Category category);
public Task<int> UpdateCategoryAsync(string categoryName, Category category);