67 lines
2.4 KiB
C#
67 lines
2.4 KiB
C#
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; // Возвращаем старое имя, если пользователь закрыл окно
|
||
}
|
||
}
|
||
}
|