Start of Datenbank

This commit is contained in:
Simon Lübeß 2023-10-10 16:21:00 +02:00
parent b9054b485a
commit d0d9cb8bb3
17 changed files with 345 additions and 9 deletions

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataAccess.DbAccess;
using DataAccess.Models;
namespace DataAccess.Data;
public class BildInfoData
{
private readonly ISqlDataAccess _db;
public BildInfoData(ISqlDataAccess db)
{
_db = db;
}
public async Task AddBildInfo(BildInfoModel bildInfo)
{
await _db.SaveData("dbo.spBildInfo_Insert", bildInfo);
}
}

View File

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dapper" Version="2.1.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.5" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,7 @@
namespace DataAccess.DbAccess;
public interface ISqlDataAccess
{
Task<IEnumerable<T>> LoadData<T, U>(string storedProcedure, U parameters, string connectionId = "Default");
Task SaveData<T>(string storedProcedure, T parameters, string connectionId = "Default");
}

View File

@ -0,0 +1,30 @@
using Microsoft.Extensions.Configuration;
using System.Data.SqlClient;
using System.Data;
using Dapper;
namespace DataAccess.DbAccess;
public class SqlDataAccess : ISqlDataAccess
{
private readonly IConfiguration _config;
public SqlDataAccess(IConfiguration config)
{
_config = config;
}
public async Task<IEnumerable<T>> LoadData<T, U>(string storedProcedure, U parameters, string connectionId = "Default")
{
using IDbConnection connection = new SqlConnection(_config.GetConnectionString(connectionId));
return await connection.QueryAsync<T>(storedProcedure, parameters, commandType: CommandType.StoredProcedure);
}
public async Task SaveData<T>(string storedProcedure, T parameters, string connectionId = "Default")
{
using IDbConnection connection = new SqlConnection(_config.GetConnectionString(connectionId));
await connection.ExecuteAsync(storedProcedure, parameters, commandType: CommandType.StoredProcedure);
}
}

View File

@ -0,0 +1,14 @@
namespace DataAccess.Models;
public class BildInfoModel
{
public int Id { get; set; }
public string BildPrompt { get; set; }
public string Wunsch { get; set; }
public string BildBeschreibung { get; set; }
public DateTime Datum { get; set; }
public string Dateiname { get; set; }
public string GPTModel { get; set; }
public string ImageModel { get; set; }
public int GroupIndex { get; set; }
}

View File

@ -5,6 +5,10 @@ VisualStudioVersion = 17.6.33829.357
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KIKunstKirstenKlöckner", "KIKunstKirstenKlöckner\KIKunstKirstenKlöckner.csproj", "{0085541E-50D4-42A5-9BFD-6CE402FB8B26}"
EndProject
Project("{00D1A9C2-B5F0-4AF3-8072-F6C62B433612}") = "KiKunstDatenbank", "KiKunstDatenbank\KiKunstDatenbank.sqlproj", "{A19CD19A-FE5B-4D4E-896B-DCC43B45F734}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataAccess", "DataAccess\DataAccess.csproj", "{0880FD07-236B-42C1-9CA3-2A6F695A623C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -15,6 +19,16 @@ Global
{0085541E-50D4-42A5-9BFD-6CE402FB8B26}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0085541E-50D4-42A5-9BFD-6CE402FB8B26}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0085541E-50D4-42A5-9BFD-6CE402FB8B26}.Release|Any CPU.Build.0 = Release|Any CPU
{A19CD19A-FE5B-4D4E-896B-DCC43B45F734}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A19CD19A-FE5B-4D4E-896B-DCC43B45F734}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A19CD19A-FE5B-4D4E-896B-DCC43B45F734}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{A19CD19A-FE5B-4D4E-896B-DCC43B45F734}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A19CD19A-FE5B-4D4E-896B-DCC43B45F734}.Release|Any CPU.Build.0 = Release|Any CPU
{A19CD19A-FE5B-4D4E-896B-DCC43B45F734}.Release|Any CPU.Deploy.0 = Release|Any CPU
{0880FD07-236B-42C1-9CA3-2A6F695A623C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0880FD07-236B-42C1-9CA3-2A6F695A623C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0880FD07-236B-42C1-9CA3-2A6F695A623C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0880FD07-236B-42C1-9CA3-2A6F695A623C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,12 @@
{
"version": 1,
"isRoot": true,
"tools": {
"dotnet-ef": {
"version": "7.0.9",
"commands": [
"dotnet-ef"
]
}
}
}

View File

@ -20,4 +20,8 @@
<PackageReference Include="SixLabors.ImageSharp" Version="3.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DataAccess\DataAccess.csproj" />
</ItemGroup>
</Project>

View File

@ -4,11 +4,15 @@
@using OpenAI_API.Chat
@using OpenAI_API.Models
@using System.Diagnostics
@using DataAccess.Data
@using DataAccess.Models
@inject IConfiguration Config
@inject TooltipService TooltipService
@inject DialogService DialogService
@inject BildInfoData BildInfoData;
<PageTitle>AiArt</PageTitle>
<section class="about_section layout_padding">
@ -244,7 +248,7 @@
private string _inferenceApiKey = "";
private string _openAiApiKey = "";
protected override Task OnInitializedAsync()
protected override async Task OnInitializedAsync()
{
_inferenceApiKey = Config.GetValue<string>("API:HF_Inference");
_openAiApiKey = Config.GetValue<string>("API:OpenAI");
@ -258,7 +262,31 @@
_client.DefaultRequestHeaders.Clear();
_client.DefaultRequestHeaders.Add("Authorization", $"Bearer {_inferenceApiKey}");
return base.OnInitializedAsync();
BildInfoModel bildInfo = new()
{
BildBeschreibung = "Test",
BildPrompt = "Tost",
Dateiname = "Task",
Datum = DateTime.Now,
GPTModel = "Geht dich nichts an",
ImageModel = "Jup",
Wunsch = request
};
try
{
await BildInfoData.AddBildInfo(bildInfo);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
await base.OnInitializedAsync();
}
/// <summary>
@ -372,7 +400,9 @@
using Image image = await Image.LoadAsync(imageStream);
string imgUrl = $"generated_images/{DateTime.Now:dd_MM_yyyy_hh_mm_s_fffffff}.jpg";
DateTime imageDate = DateTime.Now;
string imgUrl = $"generated_images/{imageDate:dd_MM_yyyy_hh_mm_s_fffffff}.jpg";
string mapPath = $"./wwwroot/{imgUrl}";
await image.SaveAsJpegAsync(mapPath);
@ -383,6 +413,19 @@
string newLine = $"{imgUrl}: {request}, {desc}\n";
await File.AppendAllTextAsync(infoTextsPath, newLine);
BildInfoModel bildInfo = new()
{
BildBeschreibung = desc,
BildPrompt = prompt,
Dateiname = imgUrl,
Datum = imageDate,
GPTModel = "Geht dich nichts an",
ImageModel = "Jup",
Wunsch = request
};
await BildInfoData.AddBildInfo(bildInfo);
return imgUrl;
}
else

View File

@ -1,3 +1,5 @@
using DataAccess.Data;
using DataAccess.DbAccess;
using Radzen;
var builder = WebApplication.CreateBuilder(args);
@ -8,6 +10,8 @@ builder.Services.AddServerSideBlazor();
builder.Services.AddScoped<TooltipService>();
builder.Services.AddScoped<DialogService>();
builder.Services.AddScoped<NotificationService>();
builder.Services.AddSingleton<ISqlDataAccess, SqlDataAccess>();
builder.Services.AddSingleton<BildInfoData>();
var app = builder.Build();

View File

@ -5,5 +5,8 @@
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"Default": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=KiKunstDatenbank;Integrated Security=True;Connect Timeout=60;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
}
}

View File

@ -9,5 +9,8 @@
"API": {
"OpenAI": "<put OpenAI Key here>",
"HF_Inference": "<put Hugging Face inference API Key here>"
},
"ConnectionStrings": {
"Default": "<put Connection String here>"
}
}

View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<Operations Version="1.0" xmlns="http://schemas.microsoft.com/sqlserver/dac/Serialization/2012/02">
<Operation Name="Rename Refactor" Key="c957123b-8f1b-4753-96eb-c2ea2811027a" ChangeDateTime="10/10/2023 12:47:37">
<Property Name="ElementName" Value="[dbo].[BildInfo].[Prompt]" />
<Property Name="ElementType" Value="SqlSimpleColumn" />
<Property Name="ParentElementName" Value="[dbo].[BildInfo]" />
<Property Name="ParentElementType" Value="SqlTable" />
<Property Name="NewName" Value="BildPrompt" />
</Operation>
<Operation Name="Rename Refactor" Key="9e69babc-e66c-496f-960a-0c9ad936d763" ChangeDateTime="10/10/2023 12:47:42">
<Property Name="ElementName" Value="[dbo].[BildInfo].[Beschreibung]" />
<Property Name="ElementType" Value="SqlSimpleColumn" />
<Property Name="ParentElementName" Value="[dbo].[BildInfo]" />
<Property Name="ParentElementType" Value="SqlTable" />
<Property Name="NewName" Value="BildBeschreibung" />
</Operation>
<Operation Name="Rename Refactor" Key="1dc11132-1619-4a0d-b261-0d56392a3d51" ChangeDateTime="10/10/2023 13:39:00">
<Property Name="ElementName" Value="[dbo].[BildInfo].[Index]" />
<Property Name="ElementType" Value="SqlSimpleColumn" />
<Property Name="ParentElementName" Value="[dbo].[BildInfo]" />
<Property Name="ParentElementType" Value="SqlTable" />
<Property Name="NewName" Value="GroupIndex" />
</Operation>
</Operations>

View File

@ -0,0 +1,76 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<Name>KiKunstDatenbank</Name>
<SchemaVersion>2.0</SchemaVersion>
<ProjectVersion>4.1</ProjectVersion>
<ProjectGuid>{a19cd19a-fe5b-4d4e-896b-dcc43b45f734}</ProjectGuid>
<DSP>Microsoft.Data.Tools.Schema.Sql.Sql150DatabaseSchemaProvider</DSP>
<OutputType>Database</OutputType>
<RootPath>
</RootPath>
<RootNamespace>KiKunstDatenbank</RootNamespace>
<AssemblyName>KiKunstDatenbank</AssemblyName>
<ModelCollation>1033, CI</ModelCollation>
<DefaultFileStructure>BySchemaAndSchemaType</DefaultFileStructure>
<DeployToDatabase>True</DeployToDatabase>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<TargetLanguage>CS</TargetLanguage>
<AppDesignerFolder>Properties</AppDesignerFolder>
<SqlServerVerification>False</SqlServerVerification>
<IncludeCompositeObjects>True</IncludeCompositeObjects>
<TargetDatabaseSet>True</TargetDatabaseSet>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<OutputPath>bin\Release\</OutputPath>
<BuildScriptName>$(MSBuildProjectName).sql</BuildScriptName>
<TreatWarningsAsErrors>False</TreatWarningsAsErrors>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<DefineDebug>false</DefineDebug>
<DefineTrace>true</DefineTrace>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<OutputPath>bin\Debug\</OutputPath>
<BuildScriptName>$(MSBuildProjectName).sql</BuildScriptName>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<DefineDebug>true</DefineDebug>
<DefineTrace>true</DefineTrace>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">11.0</VisualStudioVersion>
<!-- Default to the v11.0 targets path if the targets file for the current VS version is not found -->
<SSDTExists Condition="Exists('$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets')">True</SSDTExists>
<VisualStudioVersion Condition="'$(SSDTExists)' == ''">11.0</VisualStudioVersion>
</PropertyGroup>
<Import Condition="'$(SQLDBExtensionsRefPath)' != ''" Project="$(SQLDBExtensionsRefPath)\Microsoft.Data.Tools.Schema.SqlTasks.targets" />
<Import Condition="'$(SQLDBExtensionsRefPath)' == ''" Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets" />
<ItemGroup>
<Folder Include="Properties" />
<Folder Include="dpo" />
<Folder Include="dpo\Tables" />
<Folder Include="dpo\StoredProcedures" />
</ItemGroup>
<ItemGroup>
<Build Include="dpo\Tables\BildInfo.sql" />
<Build Include="dpo\StoredProcedures\spBildInfo_Insert.sql" />
</ItemGroup>
<ItemGroup>
<RefactorLog Include="KiKunstDatenbank.refactorlog" />
</ItemGroup>
<ItemGroup>
<PostDeploy Include="Script.PostDeployment.sql" />
</ItemGroup>
<ItemGroup>
<None Include="KiKunstDatenbank.publish.xml" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,19 @@
/*
Vorlage für ein Skript nach der Bereitstellung
--------------------------------------------------------------------------------------
Diese Datei enthält SQL-Anweisungen, die an das Buildskript angefügt werden.
Schließen Sie mit der SQLCMD-Syntax eine Datei in das Skript nach der Bereitstellung ein.
Beispiel: :r .\myfile.sql
Verwenden Sie die SQLCMD-Syntax, um auf eine Variable im Skript nach der Bereitstellung zu verweisen.
Beispiel: :setvar TableName MyTable
SELECT * FROM [$(TableName)]
--------------------------------------------------------------------------------------
*/
--IF NOT EXISTS (SELECT 1 FROM [dbo].[User])
--BEGIN
-- INSERT INTO [dbo].[User] (FirstName, LastName)
-- VALUES ('Simon', 'Lübeß'),
-- ('Peter', 'Enis'),
-- ('John', 'Smith'),
-- ('Mary', 'Jones')
--END

View File

@ -0,0 +1,22 @@
CREATE PROCEDURE [dbo].[spBildInfo_Insert]
@Id INT,
@BildPrompt NVARCHAR(MAX) ,
@Wunsch NVARCHAR(1024) ,
@BildBeschreibung NVARCHAR(MAX) ,
@Datum DATETIME2 ,
@Dateiname NCHAR(256) ,
@GPTModel NCHAR(32) ,
@ImageModel NCHAR(32) ,
@GroupIndex INT
AS
BEGIN
INSERT INTO [dbo].[BildInfo] (BildPrompt, Wunsch, BildBeschreibung, Datum, Dateiname, GPTModel, ImageModel, GroupIndex)
VALUES (@BildPrompt,
@Wunsch,
@BildBeschreibung,
@Datum,
@Dateiname,
@GPTModel,
@ImageModel,
@GroupIndex);
END

View File

@ -0,0 +1,22 @@
CREATE TABLE [dbo].[BildInfo]
(
[Id] INT NOT NULL PRIMARY KEY IDENTITY,
[BildPrompt] NVARCHAR(MAX) NOT NULL,
[Wunsch] NVARCHAR(1024) NOT NULL,
[BildBeschreibung] NVARCHAR(MAX) NOT NULL,
[Datum] DATETIME2 NOT NULL,
[Dateiname] NCHAR(256) NOT NULL,
[GPTModel] NCHAR(32) NOT NULL,
[ImageModel] NCHAR(32) NOT NULL,
[GroupIndex] INT NOT NULL
)
GO
EXEC sp_addextendedproperty @name = N'MS_Description',
@value = N'Index der Gruppe, in der dieses Bild generiert wurde und so.',
@level0type = N'SCHEMA',
@level0name = N'dbo',
@level1type = N'TABLE',
@level1name = N'BildInfo',
@level2type = N'COLUMN',
@level2name = 'GroupIndex'