Files
UP01TASK3/Labaratory/Labaratory/Services/AuthService.cs
2026-04-10 16:11:16 +05:00

67 lines
2.4 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 Labaratory.Models;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Labaratory.Services
{
public static class AuthService
{
public static void LogAttempt(string login, bool isSuccess)
{
using (var db = new Models.LaboratoryDBEntities())
{
db.LoggnHistories.Add(new Models.LoggnHistory
{
AttemptTime = DateTime.Now,
Login = login,
IsSuccess = isSuccess
});
db.SaveChanges();
}
}
public static string UpdatePhoto(string currentAvatarName)
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "Image files (*.png;*.jpg;*.jpeg)|*.png;*.jpg;*.jpeg";
if (openFile.ShowDialog() == true)
{
try
{
string fileName = Path.GetFileName(openFile.FileName);
// Формируем путь к папке Images в директории запущенного приложения
string imagesFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images");
string destPath = Path.Combine(imagesFolder, fileName);
// Создаем папку Images, если её еще не существует
if (!Directory.Exists(imagesFolder))
{
Directory.CreateDirectory(imagesFolder);
}
// Копируем файл, если его еще нет в целевой папке
if (!File.Exists(destPath))
{
File.Copy(openFile.FileName, destPath);
}
return fileName; // Возвращаем имя нового файла
}
catch (Exception ex)
{
MessageBox.Show($"Ошибка при загрузке фото: {ex.Message}");
return currentAvatarName;
}
}
return currentAvatarName; // Возвращаем старое имя, если пользователь закрыл окно
}
}
}