1
0
Files
OAiP-Presnyakov_Ilya-Labora…/Views/MainWindow.xaml.cs
Debug_pro fb13e5a20a upload
2026-06-07 18:19:53 +03:00

174 lines
5.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Windows;
using System.Windows.Threading;
using Microsoft.EntityFrameworkCore;
using SimpleWiki.Data;
using SimpleWiki.Models;
using SimpleWiki.Services;
namespace SimpleWiki.Views;
public partial class MainWindow : Window
{
private readonly User _currentUser;
private bool _isArticleBrowserReady;
private string _pendingArticleHtml = string.Empty;
public MainWindow(User currentUser)
{
InitializeComponent();
_currentUser = currentUser;
_pendingArticleHtml = MarkdownService.ToHtmlDocument("Simple Wiki", "## Загрузка...");
Loaded += MainWindow_Loaded;
ContentRendered += MainWindow_ContentRendered;
}
private void MainWindow_ContentRendered(object? sender, EventArgs e)
{
_isArticleBrowserReady = true;
Dispatcher.BeginInvoke(RenderArticleIfReady, DispatcherPriority.ApplicationIdle);
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
FullNameTextBlock.Text = _currentUser.FullName;
EmailTextBlock.Text = _currentUser.Email;
RoleTextBlock.Text = _currentUser.Role == Roles.Admin ? "Роль: администратор" : "Роль: пользователь";
CreatedAtTextBlock.Text = $"Дата регистрации: {_currentUser.CreatedAt.ToLocalTime():dd.MM.yyyy HH:mm}";
var isAdmin = _currentUser.Role == Roles.Admin;
AdminToolbar.Visibility = isAdmin ? Visibility.Visible : Visibility.Collapsed;
LoadArticles();
}
private void LoadArticles(string? query = null)
{
using var db = new AppDbContext();
var items = db.Articles
.Include(x => x.Author)
.OrderByDescending(x => x.UpdatedAt ?? x.CreatedAt)
.AsQueryable();
if (!string.IsNullOrWhiteSpace(query))
{
var term = query.Trim().ToLower();
items = items.Where(x => x.Title.ToLower().Contains(term) || x.Summary.ToLower().Contains(term));
}
var articles = items.ToList();
ArticlesListBox.ItemsSource = articles;
if (articles.Count > 0)
{
ArticlesListBox.SelectedIndex = 0;
}
else
{
ArticleTitleTextBlock.Text = "Статей пока нет";
ArticleSummaryTextBlock.Text = "Бебебе";
ArticleMetaTextBlock.Text = string.Empty;
_pendingArticleHtml = MarkdownService.ToHtmlDocument("Empty", "## Пусто\n\nПока нечего читать.");
RenderArticleIfReady();
}
}
private void ArticlesListBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (ArticlesListBox.SelectedItem is not Article selectedArticle)
{
return;
}
using var db = new AppDbContext();
var article = db.Articles
.Include(x => x.Author)
.FirstOrDefault(x => x.Id == selectedArticle.Id);
if (article is null)
{
return;
}
ArticleTitleTextBlock.Text = article.Title;
ArticleSummaryTextBlock.Text = article.Summary;
ArticleMetaTextBlock.Text = $"Автор: {article.Author?.FullName} | Создано: {article.CreatedAt.ToLocalTime():dd.MM.yyyy HH:mm}" +
(article.UpdatedAt.HasValue ? $" | Обновлено: {article.UpdatedAt.Value.ToLocalTime():dd.MM.yyyy HH:mm}" : string.Empty);
_pendingArticleHtml = MarkdownService.ToHtmlDocument(article.Title, article.MarkdownContent);
RenderArticleIfReady();
}
private void RenderArticleIfReady()
{
if (!_isArticleBrowserReady)
{
return;
}
ArticleBrowser.NavigateToString(_pendingArticleHtml);
}
private void SearchTextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
LoadArticles(SearchTextBox.Text);
}
private void AddArticleButton_Click(object sender, RoutedEventArgs e)
{
var editor = new ArticleEditorWindow(_currentUser);
if (editor.ShowDialog() == true)
{
LoadArticles(SearchTextBox.Text);
}
}
private void EditArticleButton_Click(object sender, RoutedEventArgs e)
{
if (ArticlesListBox.SelectedItem is not Article article)
{
MessageBox.Show("Сначала выберите статью.", "Внимание", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
var editor = new ArticleEditorWindow(_currentUser, article.Id);
if (editor.ShowDialog() == true)
{
LoadArticles(SearchTextBox.Text);
}
}
private void DeleteArticleButton_Click(object sender, RoutedEventArgs e)
{
if (ArticlesListBox.SelectedItem is not Article article)
{
MessageBox.Show("Сначала выберите статью.", "Внимание", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
var result = MessageBox.Show($"Удалить статью '{article.Title}'?", "Подтверждение", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result != MessageBoxResult.Yes)
{
return;
}
using var db = new AppDbContext();
var entity = db.Articles.FirstOrDefault(x => x.Id == article.Id);
if (entity is null)
{
return;
}
db.Articles.Remove(entity);
db.SaveChanges();
LoadArticles(SearchTextBox.Text);
}
private void LogoutButton_Click(object sender, RoutedEventArgs e)
{
SessionService.CurrentUser = null;
new LoginWindow().Show();
Close();
}
}