Почти работает

This commit is contained in:
bekodeg 2024-11-25 20:48:20 +04:00
parent a474bd8bbb
commit e66e721e98
8 changed files with 141 additions and 59 deletions

View File

@ -21,7 +21,7 @@ namespace Lab3.Extensions
return services;
}
public static IServiceCollection AddForms(
public static IServiceCollection AddLab3Forms(
this IServiceCollection services)
{
services.AddScoped<MainForm>();

View File

@ -34,7 +34,7 @@ namespace Lab3
services.AddMapping();
services.AddForms();
services.AddLab3Forms();
});
}
}

View File

@ -0,0 +1,29 @@
using Lab4.Forms;
using Lab4.Implementations;
using Lab4.Interfaces;
using Microsoft.Extensions.DependencyInjection;
namespace Lab4.Extensions
{
public static class DiExtensions
{
public static IServiceCollection AddLab4Forms(
this IServiceCollection services)
{
services.AddScoped<FormMain>();
services.AddScoped<PluginsConvention>();
return services;
}
public static IServiceCollection AddScopes(
this IServiceCollection services)
{
services.AddScoped<Func<Type, IPluginsConvention>>(sp
=> (type) => (sp.GetRequiredService(type) as IPluginsConvention)!);
return services;
}
}
}

View File

@ -8,13 +8,13 @@ namespace Lab4.Forms
private readonly Dictionary<string, IPluginsConvention> _plugins;
private string _selectedPlugin;
private readonly Func<string, IPluginsConvention> _pluginsConventionFunc;
private readonly Func<Type, IPluginsConvention> _getPluginObjectFunc;
public FormMain(Func<string, IPluginsConvention> pluginsConventionFunc)
public FormMain(Func<Type, IPluginsConvention> getPluginObjectFunc)
{
InitializeComponent();
_pluginsConventionFunc = pluginsConventionFunc;
_getPluginObjectFunc = getPluginObjectFunc;
_plugins = LoadPlugins();
_selectedPlugin = string.Empty;
@ -24,20 +24,30 @@ namespace Lab4.Forms
{
Dictionary<string, IPluginsConvention> result = [];
List<string> keys = ["Успеваемость"];
result = keys.Select(k => KeyValuePair.Create(k, _pluginsConventionFunc(k))).ToDictionary();
foreach (string key in keys)
{
var item = new ToolStripMenuItem(key);
item.Click += (s, e) => _selectedPlugin = key;
var plurinType = typeof(IPluginsConvention);
ControlsStripMenuItem.Container!.Add(item);
foreach (var type in AppDomain.CurrentDomain
.GetAssemblies()
.SelectMany(x => x.GetTypes())
.Where(x => plurinType.IsAssignableFrom(x) && x != plurinType))
{
var plugin = _getPluginObjectFunc(type);
string key = plugin.PluginName;
result[key] = plugin;
var item = new ToolStripMenuItem(key);
item.Click += (s, e) =>
{
_selectedPlugin = key;
panelControl.Controls.Clear();
panelControl.Controls.Add(_plugins[key].GetControl);
};
ControlsStripMenuItem.DropDownItems!.Add(item);
}
// TODO При выборе пункта меню получать UserControl и заполнять элемент panelControl этим контролом на всю площадь
// Пример: panelControl.Controls.Clear();
// panelControl.Controls.Add(ctrl);
return [];
return result;
}
private void FormMain_KeyDown(object sender, KeyEventArgs e)
@ -136,8 +146,19 @@ namespace Lab4.Forms
private void CreateSimpleDoc()
{
// TODO узнать где сохранять
if (_plugins[_selectedPlugin].CreateSimpleDocument(new PluginsConventionSaveDocument()))
using var saveFileDialog = new SaveFileDialog
{
Filter = "pdf|*.pdf"
};
if (saveFileDialog.ShowDialog() != DialogResult.OK)
{
return;
}
if (_plugins[_selectedPlugin].CreateSimpleDocument(new PluginsConventionSaveDocument()
{
FileName = saveFileDialog.FileName,
}))
{
_ = MessageBox.Show("Документ сохранен",
"Создание документа",
@ -151,9 +172,21 @@ namespace Lab4.Forms
}
private void CreateTableDoc()
{
// TODO узнать где сохранять
using var saveFileDialog = new SaveFileDialog
{
Filter = "xlsx|*.xlsx"
};
if (saveFileDialog.ShowDialog() != DialogResult.OK)
{
return;
}
if (_plugins[_selectedPlugin].CreateTableDocument(new
PluginsConventionSaveDocument()))
PluginsConventionSaveDocument()
{
FileName = saveFileDialog.FileName,
}))
{
_ = MessageBox.Show("Документ сохранен",
"Создание документа",
@ -167,8 +200,20 @@ namespace Lab4.Forms
}
private void CreateChartDoc()
{
// TODO узнать где сохранять
if (_plugins[_selectedPlugin].CreateChartDocument(new PluginsConventionSaveDocument()))
using var saveFileDialog = new SaveFileDialog
{
Filter = "docx|*.docx"
};
if (saveFileDialog.ShowDialog() != DialogResult.OK)
{
return;
}
if (_plugins[_selectedPlugin].CreateChartDocument(new PluginsConventionSaveDocument()
{
FileName= saveFileDialog.FileName,
}))
{
_ = MessageBox.Show("Документ сохранен",
"Создание документа",
@ -180,7 +225,6 @@ namespace Lab4.Forms
_ = MessageBox.Show("Ошибка при создании документа", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ThesaurusToolStripMenuItem_Click(object sender, EventArgs e) => ShowThesaurus();
private void AddElementToolStripMenuItem_Click(object sender, EventArgs e) => AddNewElement();

View File

@ -39,7 +39,7 @@ namespace Lab4.Implementations
_simpleDocumentCreator = new();
}
public string PluginName => throw new NotImplementedException();
public string PluginName => "Успеваемость";
public UserControl GetControl => _control;
@ -61,16 +61,6 @@ namespace Lab4.Implementations
{
try
{
using var saveFileDialog = new SaveFileDialog
{
Filter = "docx|*.docx"
};
if (saveFileDialog.ShowDialog() != DialogResult.OK)
{
return false;
}
var data = _studentRepository.GetAsync().Result
.GroupBy(s => s.EducationForm)
.Select(s => new DataLine(
@ -82,7 +72,7 @@ namespace Lab4.Implementations
_chartCreator.AddDiagram(
new(
saveFileDialog.FileName,
saveDocument.FileName,
"Поступления",
"Диаграмма",
@ -103,16 +93,6 @@ namespace Lab4.Implementations
{
try
{
using var saveFileDialog = new SaveFileDialog
{
Filter = "pdf|*.pdf"
};
if (saveFileDialog.ShowDialog() != DialogResult.OK)
{
return false;
}
var values = (_studentRepository.GetAsync()).Result
.Select(s => s.StudentSessions
.OrderBy(x => x.Number)
@ -132,7 +112,7 @@ namespace Lab4.Implementations
_simpleDocumentCreator.SaveToPdf(new()
{
FilePath = saveFileDialog.FileName,
FilePath = saveDocument.FileName,
Title = "Сессии",
Tables = [tables]
});
@ -149,18 +129,9 @@ namespace Lab4.Implementations
{
try
{
using var saveFileDialog = new SaveFileDialog
{
Filter = "xlsx|*.xlsx"
};
if (saveFileDialog.ShowDialog() != DialogResult.OK)
{
return false;
}
_tableCreator.SaveToExcel<StudentDTO>(new()
{
FilePath = saveFileDialog.FileName,
FilePath = saveDocument.FileName,
Title = "Студенты",
Headers =
[

View File

@ -12,4 +12,10 @@
<ProjectReference Include="..\Lab3\Lab3.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@ -1,4 +1,10 @@
using Lab3.Extensions;
using Lab3.Forms;
using Lab4.Extensions;
using Lab4.Forms;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Lab4
{
@ -13,7 +19,28 @@ namespace Lab4
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new FormMain());
var app = CreateHostBuilder().Build();
Application.Run(app.Services.GetRequiredService<FormMain>());
}
static IHostBuilder CreateHostBuilder()
{
return Host.CreateDefaultBuilder()
.ConfigureAppConfiguration(c
=> c.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true))
.ConfigureServices((context, services) => {
services.ConfigureDAL(context.Configuration);
services.AddMapping();
services.AddLab3Forms();
services.AddLab4Forms();
services.AddScopes();
});
}
}
}

View File

@ -0,0 +1,5 @@
{
"ConnectionStrings": {
"COPDataBase": "Host=localhost;Username=postgres;Password=postgres;Database=COP"
}
}