Gallery verwendet Datenbank

- Menü: Fixed typo in Gallerie
- Datenbanken-Funktionen dokumentiert
This commit is contained in:
Simon Lübeß 2023-10-10 18:56:05 +02:00
parent 1b28481b5d
commit e6a2db0753
9 changed files with 94 additions and 63 deletions

View File

@ -1,14 +1,11 @@
using System; using DataAccess.DbAccess;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using DataAccess.DbAccess;
using DataAccess.Models; using DataAccess.Models;
namespace DataAccess.Data; namespace DataAccess.Data;
/// <summary>
/// Ermöglicht den Zugriff auf die BildInfo Datenbank.
/// </summary>
public class BildInfoData public class BildInfoData
{ {
private readonly ISqlDataAccess _db; private readonly ISqlDataAccess _db;
@ -18,18 +15,34 @@ public class BildInfoData
_db = db; _db = db;
} }
/// <summary>
/// Fügt die gegebene BildInfo zur Datenbank hinzu und aktualisiert das <see cref="BildInfoModel.Id"/>-Feld mit dem entsprechenden Wert.
/// </summary>
/// <param name="bildInfo">Die BildInfo, die zur Datenbank hinzugefügt werden soll.</param>
public async Task AddBildInfoAsync(BildInfoModel bildInfo) public async Task AddBildInfoAsync(BildInfoModel bildInfo)
{ {
var id = await _db.LoadData<int, BildInfoModel>("dbo.spBildInfo_Insert", bildInfo); var id = await _db.LoadData<int, BildInfoModel>("dbo.spBildInfo_Insert", bildInfo);
bildInfo.Id = id.Single(); bildInfo.Id = id.Single();
} }
public async Task UpdateBildInfoDateinameAsync(BildInfoModel bildInfo) /// <summary>
/// Aktualisiert das Dateiname-Feld der übergebenen BildInfo in der Datenbank.
/// </summary>
/// <param name="bildInfo">Die BildInfo deren Dateiname aktualisiert werden soll.</param>
public Task UpdateBildInfoDateinameAsync(BildInfoModel bildInfo)
{ {
await _db.SaveData("dbo.spBildInfo_UpdateFileName", return _db.SaveData("dbo.spBildInfo_UpdateFileName",
new { new {
Id = bildInfo.Id, Id = bildInfo.Id,
Dateiname = bildInfo.Dateiname, Dateiname = bildInfo.Dateiname,
}); });
} }
/// <summary>
/// Gibt alle Bild Infos der Datenbank zurück.
/// </summary>
public Task<IEnumerable<BildInfoModel>> GetAllBildInfosAsync()
{
return _db.LoadData<BildInfoModel, dynamic>("dbo.spBildInfo_GetAll", new { });
}
} }

View File

@ -22,4 +22,15 @@ public class WunschInfoData
var id = await _db.LoadData<int, WunschInfoModel>("dbo.spWunschInfo_Insert", wunschInfo); var id = await _db.LoadData<int, WunschInfoModel>("dbo.spWunschInfo_Insert", wunschInfo);
wunschInfo.Id = id.Single(); wunschInfo.Id = id.Single();
} }
/// <summary>
/// Gibt die WunschInfo mit der gegebenen Id zurück.
/// </summary>
/// <param name="wunschId">Die Id der zu ladenen WunschInfo</param>
/// <returns>Die WunschInfo mit der gegebenen Id</returns>
public async Task<WunschInfoModel> GetWunschInfoAsync(int wunschId)
{
var wunschInfo = await _db.LoadData<WunschInfoModel, dynamic>("dbo.spWunschInfo_Get", new { Id = wunschId });
return wunschInfo.Single();
}
} }

View File

@ -1,7 +1,26 @@
namespace DataAccess.DbAccess; namespace DataAccess.DbAccess;
/// <summary>
/// Bietet lesenden und schreibenden Zugriff auf eine Datenbank.
/// </summary>
public interface ISqlDataAccess public interface ISqlDataAccess
{ {
Task<IEnumerable<T>> LoadData<T, U>(string storedProcedure, U parameters, string connectionId = "Default"); /// <summary>
Task SaveData<T>(string storedProcedure, T parameters, string connectionId = "Default"); /// Führt die angegebene Stored Procedure aus. Diese Prozedur nimmt Parameter des Typs <see cref="TParameter"/> engegen und gibt ein Enumerable des Typs <see cref="TResult"/> zurück.
/// </summary>
/// <typeparam name="TResult">Der Typ der Parameter</typeparam>
/// <typeparam name="TParameter">Der Typ der Rückgabewerte</typeparam>
/// <param name="storedProcedure">Der Name der Prozedur</param>
/// <param name="parameters">Die Parameter für die Prozedur.</param>
/// <param name="connectionId">Die optionale Id des zu verwendenen Connection Strings.</param>
Task<IEnumerable<TResult>> LoadData<TResult, TParameter>(string storedProcedure, TParameter parameters, string connectionId = "Default");
/// <summary>
/// Führt die angegebene Stored Procedure aus. Diese Prozedur nimmt Parameter des Typs <see cref="TParameter"/> engegen und gibt nichts zurück.
/// </summary>
/// <typeparam name="TParameter">Der Typ der Parameter</typeparam>
/// <param name="storedProcedure">Der Name der Prozedur</param>
/// <param name="parameters">Die Parameter für die Prozedur.</param>
/// <param name="connectionId">Die optionale Id des zu verwendenen Connection Strings.</param>
Task SaveData<TParameter>(string storedProcedure, TParameter parameters, string connectionId = "Default");
} }

View File

@ -5,6 +5,9 @@ using Dapper;
namespace DataAccess.DbAccess; namespace DataAccess.DbAccess;
/// <summary>
/// Bietet lesenden und schreibenden Zugriff auf eine Datenbank.
/// </summary>
public class SqlDataAccess : ISqlDataAccess public class SqlDataAccess : ISqlDataAccess
{ {
private readonly IConfiguration _config; private readonly IConfiguration _config;
@ -14,14 +17,14 @@ public class SqlDataAccess : ISqlDataAccess
_config = config; _config = config;
} }
public async Task<IEnumerable<T>> LoadData<T, U>(string storedProcedure, U parameters, string connectionId = "Default") public async Task<IEnumerable<TResult>> LoadData<TResult, TParameter>(string storedProcedure, TParameter parameters, string connectionId = "Default")
{ {
using IDbConnection connection = new SqlConnection(_config.GetConnectionString(connectionId)); using IDbConnection connection = new SqlConnection(_config.GetConnectionString(connectionId));
return await connection.QueryAsync<T>(storedProcedure, parameters, commandType: CommandType.StoredProcedure); return await connection.QueryAsync<TResult>(storedProcedure, parameters, commandType: CommandType.StoredProcedure);
} }
public async Task SaveData<T>(string storedProcedure, T parameters, string connectionId = "Default") public async Task SaveData<TParameter>(string storedProcedure, TParameter parameters, string connectionId = "Default")
{ {
using IDbConnection connection = new SqlConnection(_config.GetConnectionString(connectionId)); using IDbConnection connection = new SqlConnection(_config.GetConnectionString(connectionId));

View File

@ -1,5 +1,11 @@
@page "/gallery" @page "/gallery"
@using DataAccess.Data
@using DataAccess.Models
@inject BildInfoData BildInfoData;
@inject WunschInfoData WunschInfoData;
<style> <style>
.image-grid { .image-grid {
background-color: white; /* Hintergrund der RadzenDataList weiß machen */ background-color: white; /* Hintergrund der RadzenDataList weiß machen */
@ -57,12 +63,12 @@
</style> </style>
<div> <div>
<RadzenDataList WrapItems="@true" AllowPaging="true" Data="@imagePaths" PageSize="20" PagerHorizontalAlign="HorizontalAlign.Left" ShowPagingSummary="true" class="image-grid"> <RadzenDataList WrapItems="@true" AllowPaging="true" Data="@_bildInfos" PageSize="20" PagerHorizontalAlign="HorizontalAlign.Left" ShowPagingSummary="true" class="image-grid">
<Template Context="imagePath"> <Template Context="bildInfo">
<div class="image-grid-item"> <div class="image-grid-item">
<RadzenCard Style="width: 200px; height: 200px; padding: 0; border: none;"> <RadzenCard Style="width: 200px; height: 200px; padding: 0; border: none;">
<RadzenButton Click="@(args => ShowImageInfo(imagePath))"> <RadzenButton Click="@(args => ShowImageInfo(bildInfo))">
<RadzenImage Src="@imagePath" Style="width: 100%; height: 100%; object-fit: cover;" /> <RadzenImage Src="@bildInfo.Dateiname" Style="width: 100%; height: 100%; object-fit: cover;" />
</RadzenButton> </RadzenButton>
</RadzenCard> </RadzenCard>
</div> </div>
@ -83,63 +89,26 @@
</div> </div>
@code { @code {
List<string> imagePaths = new List<string>();
string selectedImage; string selectedImage;
string infoText = "Info Text"; string infoText = "Info Text";
string popupStyle = "display: none;"; string popupStyle = "display: none;";
IEnumerable<BildInfoModel>? _bildInfos;
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
await base.OnInitializedAsync(); await base.OnInitializedAsync();
// Get the physical path to the folder _bildInfos = await BildInfoData.GetAllBildInfosAsync();
string folderPath = Path.Combine(_environment.WebRootPath, "generated_images");
if (Directory.Exists(folderPath))
{
// Load the image file names from the folder
var imageFiles = Directory.GetFiles(folderPath, "*.jpg");
// Get the relative paths to the images
imagePaths = imageFiles.Select(file => file.Replace(_environment.WebRootPath, "").Replace("\\", "/")).ToList();
}
} }
private async void ShowImageInfo(string imagePath) private async void ShowImageInfo(BildInfoModel bildInfo)
{ {
selectedImage = imagePath; selectedImage = bildInfo.Dateiname;
popupStyle = "display: block;"; popupStyle = "display: block;";
infoText = await GetInfoTextForImageAsync(imagePath);
}
WunschInfoModel wunschInfo = await WunschInfoData.GetWunschInfoAsync(bildInfo.WunschId);
private async Task<string> GetInfoTextForImageAsync(string imagePath) infoText = wunschInfo.BildBeschreibung;
{
// Bestimme den Ordnerpfad, in dem sich die Bilder und die info_texts.txt Datei befinden
string folderPath = Path.Combine(_environment.WebRootPath, "generated_images");
// Bestimme den Pfad zur info_texts.txt Datei
string infoTextsFilePath = Path.Combine(folderPath, "info_texts.txt");
// Überprüfe, ob die Datei existiert
if (!File.Exists(infoTextsFilePath))
return $"Kein Infotext für {imagePath} gefunden.";
// Lies alle Zeilen der Datei
var lines = await File.ReadAllLinesAsync(infoTextsFilePath);
string adaptedImagePath = imagePath.Substring(1) + ":";
// Durchsuche jede Zeile nach dem gegebenen imagePath
foreach (var line in lines)
{
if (line.StartsWith(adaptedImagePath)) // Überprüft, ob die Zeile mit dem Dateinamen des Bildes beginnt
{
// Trenne den Dateinamen und den Infotext und gib den Infotext zurück
return line.Split(new[] { ':' }, 2).LastOrDefault()?.Trim();
}
}
return $"Kein Infotext für {imagePath} gefunden.";
} }
private void CloseImageInfo() private void CloseImageInfo()

View File

@ -36,7 +36,7 @@
<span class="nav-link">Über Kirsten Klöckner</span> <span class="nav-link">Über Kirsten Klöckner</span>
</NavLink> </NavLink>
<NavLink class="nav-item" href="gallery" Match="NavLinkMatch.All"> <NavLink class="nav-item" href="gallery" Match="NavLinkMatch.All">
<span class="nav-link">Galerier</span> <span class="nav-link">Galerie</span>
</NavLink> </NavLink>
</ul> </ul>
</div> </div>

View File

@ -66,6 +66,8 @@
<Build Include="dpo\Tables\WunschInfo.sql" /> <Build Include="dpo\Tables\WunschInfo.sql" />
<Build Include="dpo\StoredProcedures\spWunschInfo_Insert.sql" /> <Build Include="dpo\StoredProcedures\spWunschInfo_Insert.sql" />
<Build Include="dpo\StoredProcedures\spBildInfo_UpdateFileName.sql" /> <Build Include="dpo\StoredProcedures\spBildInfo_UpdateFileName.sql" />
<Build Include="dpo\StoredProcedures\spBildInfo_GetAll.sql" />
<Build Include="dpo\StoredProcedures\spWunschInfo_Get.sql" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<RefactorLog Include="KiKunstDatenbank.refactorlog" /> <RefactorLog Include="KiKunstDatenbank.refactorlog" />

View File

@ -0,0 +1,6 @@
CREATE PROCEDURE [dbo].[spBildInfo_GetAll]
AS
BEGIN
SELECT Id, Datum, Dateiname, ImageModel, WunschId
FROM [dbo].[BildInfo];
END

View File

@ -0,0 +1,8 @@
CREATE PROCEDURE [dbo].[spWunschInfo_Get]
@Id INT
AS
BEGIN
SELECT Id, Wunsch, BildPrompt, BildBeschreibung, Datum, GPTModel
FROM [dbo].[WunschInfo]
WHERE Id = @Id;
END