From ca229fbd89475bc348d521b5f02876d3ad9ca376 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20=C5=A0taleker?= Date: Fri, 2 Aug 2024 11:00:59 +0200 Subject: [PATCH] nove verzije --- InfosysPublisher/App.xaml.cs | 33 +- InfosysPublisher/Classes/Encryption.cs | 27 ++ InfosysPublisher/Classes/User.cs | 16 + InfosysPublisher/InfosysPublisher.csproj | 3 +- InfosysPublisher/WinLogin.xaml | 46 +++ InfosysPublisher/WinLogin.xaml.cs | 100 ++++++ InfosysPublisher/WinMain.xaml | 4 +- InfosysPublisher/WinMain.xaml.cs | 419 ++++++++++++++--------- 8 files changed, 491 insertions(+), 157 deletions(-) create mode 100644 InfosysPublisher/Classes/User.cs create mode 100644 InfosysPublisher/WinLogin.xaml create mode 100644 InfosysPublisher/WinLogin.xaml.cs diff --git a/InfosysPublisher/App.xaml.cs b/InfosysPublisher/App.xaml.cs index c958877..8285c7b 100644 --- a/InfosysPublisher/App.xaml.cs +++ b/InfosysPublisher/App.xaml.cs @@ -1,4 +1,5 @@ -using System; +using InfosysPublisher.Classes; +using System; using System.Collections.Generic; using System.Configuration; using System.Data; @@ -14,5 +15,35 @@ namespace InfosysPublisher /// public partial class App : Application { + internal static string SqlConnectionString = @"Server=192.168.111.99\INFOSYS;Database=IsStoritve;User Id=infosys;Password=is2005is;MultipleActiveResultSets=True;Encrypt=false"; + + internal static Settings.Application? _application; + internal static User User; + + protected override void OnStartup(StartupEventArgs e) + { + // On start stuff here + //base.OnStartup(e); + _application = WinSettings.GetSettings(); + + if (_application == null) + { + OpenSettings(); + return; + } + + var win = new WinLogin(); + win.ShowDialog(); + } + + internal static void OpenSettings() + { + var win = new WinSettings(); + win.ShowDialog(); + if (win.DialogResult != null && (bool)win.DialogResult) + { + _application = WinSettings.GetSettings(); + } + } } } diff --git a/InfosysPublisher/Classes/Encryption.cs b/InfosysPublisher/Classes/Encryption.cs index 133b0b6..cf1f6c2 100644 --- a/InfosysPublisher/Classes/Encryption.cs +++ b/InfosysPublisher/Classes/Encryption.cs @@ -102,5 +102,32 @@ namespace ResevalnaScanner.Classes return Encoding.UTF8.GetString(plainTextBytes, 0, byteCount).Trim(); } + + public const int SALT_SIZE = 24; // size in bytes + public const int HASH_SIZE = 256; // size in bytes + public const int ITERATIONS = 20000; // number of pbkdf2 iterations + + public static (byte[] Hash, byte[] Salt) CreatePasswordHash(string iText, byte[]? iSalt = null) + { + byte[] salt; + if (iSalt == null) + { + salt = new byte[SALT_SIZE]; + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(salt); + } + else + { + salt = iSalt; + } + + var pbkdf2 = new Rfc2898DeriveBytes(iText, salt, ITERATIONS, HashAlgorithmName.SHA256); + return (pbkdf2.GetBytes(HASH_SIZE), salt); + } + + public static bool CompareByteArrays(byte[] iArray, byte[] iArrayToCompare) + { + return iArray.Length == iArrayToCompare.Length && !iArray.Where((t, i) => t != iArrayToCompare[i]).ToList().Any(); + } } } diff --git a/InfosysPublisher/Classes/User.cs b/InfosysPublisher/Classes/User.cs new file mode 100644 index 0000000..7c1c66c --- /dev/null +++ b/InfosysPublisher/Classes/User.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace InfosysPublisher.Classes +{ + internal class User + { + public int Id { get; set; } + public string Name { get; set; } + public string Surname { get; set; } + public string Username { get; set; } + } +} diff --git a/InfosysPublisher/InfosysPublisher.csproj b/InfosysPublisher/InfosysPublisher.csproj index 3521c28..7a9111d 100644 --- a/InfosysPublisher/InfosysPublisher.csproj +++ b/InfosysPublisher/InfosysPublisher.csproj @@ -2,7 +2,7 @@ WinExe - net6.0-windows + net8.0-windows7.0 enable true 1.0.1.0 @@ -14,6 +14,7 @@ + diff --git a/InfosysPublisher/WinLogin.xaml b/InfosysPublisher/WinLogin.xaml new file mode 100644 index 0000000..b06874f --- /dev/null +++ b/InfosysPublisher/WinLogin.xaml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + Infosys-Publisher + + + Username: + + + + + + + Password: + + + + + + + + diff --git a/InfosysPublisher/WinLogin.xaml.cs b/InfosysPublisher/WinLogin.xaml.cs new file mode 100644 index 0000000..c0ed3fe --- /dev/null +++ b/InfosysPublisher/WinLogin.xaml.cs @@ -0,0 +1,100 @@ +using Renci.SshNet; +using ResevalnaScanner.Classes; +using System; +using System.Collections.Generic; +using System.Data; +using System.Threading.Tasks; +using System.Windows; +using InfosysPublisher.Classes; +using Microsoft.Data.SqlClient; + +namespace InfosysPublisher +{ + /// + /// Interaction logic for WinLogin.xaml + /// + public partial class WinLogin : Window + { + public WinLogin() + { + InitializeComponent(); + + } + + private async void Login_OnClick(object sender, RoutedEventArgs e) + { + var succeeded = false; + var isLockedOut = false; + + var username = tbUsername.Text; + var password = tbPassword.Password; + User user = null; + + await Task.Run(() => + { + using var sqlConnection = new SqlConnection(App.SqlConnectionString); + sqlConnection.Open(); + + var sqlCommandString = @" +SELECT ID_Uporabnik, + UporabniskoIme, + Ime, + Priimek, + GesloBytes, + SaltBytes, + Aktiven +FROM Uporabnik +WHERE UporabniskoIme = @uporabniskoIme + AND GesloBytes IS NOT NULL + AND SaltBytes IS NOT NULL" + ; + + using var sqlCommand = new SqlCommand(sqlCommandString, sqlConnection); + sqlCommand.Parameters.AddWithValue("uporabniskoIme", username); + + using var sqlDataReader = sqlCommand.ExecuteReader(); + if (sqlDataReader.Read()) + { + if (sqlDataReader.GetString("UporabniskoIme") == username + && Encryption.CompareByteArrays(Encryption.CreatePasswordHash(password, (byte[])sqlDataReader.GetValue("SaltBytes")).Hash, (byte[])sqlDataReader.GetValue("GesloBytes"))) + { + if (sqlDataReader.GetBoolean("Aktiven")) + { + user = new User + { + Id = sqlDataReader.GetInt32(sqlDataReader.GetOrdinal("ID_Uporabnik")), + Name = sqlDataReader.GetString(sqlDataReader.GetOrdinal("Ime")), + Surname = sqlDataReader.GetString(sqlDataReader.GetOrdinal("Priimek")), + Username = sqlDataReader.GetString(sqlDataReader.GetOrdinal("UporabniskoIme")), + }; + succeeded = true; + } + else + { + isLockedOut = true; + } + } + } + else + { + //zato da ne takoj konca + Encryption.CompareByteArrays(Encryption.CreatePasswordHash(password).Hash, new byte[4] { 0, 1, 0, 1 }); + } + }); + tbPassword.Password = ""; + + if (!succeeded) + { + MessageBox.Show("Wrong login data!", "Login", MessageBoxButton.OK, MessageBoxImage.Stop); + return; + } + + App.User = user; + this.Hide(); + var win = new WinMain(); + win.ShowDialog(); + this.Show(); + this.Close(); + } + } +} diff --git a/InfosysPublisher/WinMain.xaml b/InfosysPublisher/WinMain.xaml index f493fb8..141d454 100644 --- a/InfosysPublisher/WinMain.xaml +++ b/InfosysPublisher/WinMain.xaml @@ -1,4 +1,4 @@ - - Pripravi samo zip + Pripravi samo zip diff --git a/InfosysPublisher/WinMain.xaml.cs b/InfosysPublisher/WinMain.xaml.cs index ce8d710..af22d8e 100644 --- a/InfosysPublisher/WinMain.xaml.cs +++ b/InfosysPublisher/WinMain.xaml.cs @@ -21,6 +21,7 @@ using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Xml; +using Microsoft.Data.SqlClient; using Path = System.IO.Path; namespace InfosysPublisher @@ -28,7 +29,7 @@ namespace InfosysPublisher /// /// Interaction logic for MainWindow.xaml /// - public partial class MainWindow : Window + public partial class WinMain : Window { #region Classes @@ -39,25 +40,24 @@ namespace InfosysPublisher } #endregion - private Settings.Application? _application; - - + private string _projectPath; private List _projects; private Project? _selectedProject = null; private string _selectedProjectSubPath = ""; private string _selectedProjectPublishLocation = ""; private string _selectedProjectVersion = ""; + private string _selectedProjectGuid = ""; private const string SftpArchivePath = "Archive"; - public MainWindow() + public WinMain() { InitializeComponent(); var version = typeof(App).Assembly.GetName().Version; this.Title += " " + version; - _application = WinSettings.GetSettings(); + App._application = WinSettings.GetSettings(); TbProjectsPath.LostFocus += TbProjectsPath_LostFocus; CbProjects.SelectionChanged += CbProjects_SelectionChanged; @@ -66,20 +66,14 @@ namespace InfosysPublisher BtnSettings.Click += BtnSettings_Click; Closing += MainWindow_Closing; - - if (_application == null) - { - OpenSettings(); - return; - } - TbProjectsPath.Text = _application.LastFolder; + TbProjectsPath.Text = App._application.LastFolder; LoadProjects(); } private void BtnSettings_Click(object sender, RoutedEventArgs e) { - OpenSettings(); + App.OpenSettings(); } private void MainWindow_Closing(object? sender, System.ComponentModel.CancelEventArgs e) @@ -91,15 +85,7 @@ namespace InfosysPublisher WinSettings.SaveSettings(settings); } - private void OpenSettings() - { - var win = new WinSettings(); - win.ShowDialog(); - if (win.DialogResult != null && (bool)win.DialogResult) - { - _application = WinSettings.GetSettings(); - } - } + private void CbProjects_SelectionChanged(object sender, SelectionChangedEventArgs e) { @@ -162,43 +148,105 @@ namespace InfosysPublisher .Where(x => x.Name is "App.xaml.cs" or "Program.cs") .ToList(); + if (projectFileInfos.Count <= 0) + { + projectFileInfos = new DirectoryInfo(projectSubPath) + .GetDirectories("Properties") + .SelectMany(x => x.GetFiles("AssemblyInfo.cs")) + .ToList(); + }; + if (projectFileInfos.Count <= 0) return; var csLines = File.ReadLines(projectFileInfos[0].FullName).ToList(); + var tmpSelectedProjectVersion = ""; + var tmpSelectedProjectSubPath = ""; + var tmpSelectedProjectPublishLocation = ""; + var tmpSelectedProjectGuid = ""; + csLines.Where(csLine => csLine.StartsWith("[assembly: AssemblyVersion(")) .ToList() .ForEach(csLine => { - _selectedProjectVersion = csLine.Replace("[assembly: AssemblyVersion(\"", "").Replace("\")]", ""); - _selectedProjectSubPath = projectSubPath; + tmpSelectedProjectVersion = csLine.Replace("[assembly: AssemblyVersion(\"", "").Replace("\")]", ""); + tmpSelectedProjectSubPath = projectSubPath; - System.Diagnostics.Debug.WriteLine(_selectedProjectVersion); + System.Diagnostics.Debug.WriteLine(tmpSelectedProjectVersion); }); - if (_selectedProjectSubPath != "") + if (tmpSelectedProjectSubPath != "") { csLines.Where(csLine => csLine.StartsWith("//Publish location:")) .ToList() .ForEach(csLine => { - _selectedProjectPublishLocation = csLine.Replace("//Publish location:", ""); + tmpSelectedProjectPublishLocation = csLine.Replace("//Publish location:", ""); - System.Diagnostics.Debug.WriteLine(_selectedProjectPublishLocation); + System.Diagnostics.Debug.WriteLine(tmpSelectedProjectPublishLocation); + }); + + //Guid za paket vnos dela, tiste ko ga imajo v program cd + csLines.Where(csLine => csLine.Contains("public static string GuidAplikacije")) + .ToList() + .ForEach(csLine => + { + tmpSelectedProjectGuid = csLine.Split('"')[1]; + + System.Diagnostics.Debug.WriteLine(tmpSelectedProjectGuid); }); } + + if (tmpSelectedProjectVersion == "" + || tmpSelectedProjectSubPath == "" + || tmpSelectedProjectPublishLocation == "") return; + + //ce se ni guidja pogledam v nlog + if (tmpSelectedProjectGuid == "") + { + var projectNlogConfig= new DirectoryInfo(projectSubPath) + .GetFiles("NLog.config") + .ToList(); + + if (projectFileInfos.Count <= 0) return; + try + { + var xmlDocument = new XmlDocument(); + xmlDocument.Load(projectNlogConfig[0].FullName); + + var node = xmlDocument + .GetElementsByTagName("variable") + .Cast() + .FirstOrDefault(x => x.Attributes != null + && x.Attributes.Cast().Any(y => y.Name == "name" && y.Value == "AplikacijaGuid")); + + if (node != null) + tmpSelectedProjectGuid = node.Attributes["value"].Value; + } + catch (Exception exception) + { + MessageBox.Show(exception.ToString()); + } + } + if (tmpSelectedProjectGuid == "") return; + + _selectedProjectVersion = tmpSelectedProjectVersion; + _selectedProjectSubPath = tmpSelectedProjectSubPath; + _selectedProjectPublishLocation = tmpSelectedProjectPublishLocation; + _selectedProjectGuid = tmpSelectedProjectGuid; + }); - if (_selectedProjectVersion == "" || _selectedProjectSubPath == "" || _selectedProjectPublishLocation == "") + if (_selectedProjectVersion == "" || _selectedProjectSubPath == "" || _selectedProjectPublishLocation == "" || _selectedProjectGuid == "") return; - LblProjectInfo.Content = $"Verzija: {_selectedProjectVersion} Pot: {_selectedProjectPublishLocation}"; + LblProjectInfo.Content = $"Verzija: {_selectedProjectVersion} Pot: {_selectedProjectPublishLocation} Guid: {_selectedProjectGuid}"; BtnPublish.IsEnabled = true; } private async void BtnPublish_Click(object sender, RoutedEventArgs e) { - if (_selectedProject == null || _application == null) + if (_selectedProject == null || App._application == null) return; var releaseFolder = Path.Combine(_selectedProjectSubPath, @"bin\Release"); @@ -208,6 +256,9 @@ namespace InfosysPublisher return; } + TbOutput.Text = $"Publish start {DateTime.Now:dd.MM.yyyy HH:mm:ss}"; + + TbOutput.Text += $"\nClean {DateTime.Now:dd.MM.yyyy HH:mm:ss}"; GridProgress.Visibility = Visibility.Visible; LblLoading.Content = $"Čiščenje mape {releaseFolder}"; await Task.Run(() => @@ -225,6 +276,7 @@ namespace InfosysPublisher }); LblLoading.Content = $"Rebuild {_selectedProject.Path}"; + TbOutput.Text += $"\nRebuild {_selectedProject.Path} {DateTime.Now:dd.MM.yyyy HH:mm:ss}"; var output = ""; await Task.Run(() => { @@ -241,17 +293,18 @@ namespace InfosysPublisher //cmd.StandardInput.WriteLine("echo Oscar"); cmd.StandardInput.Flush(); cmd.StandardInput.Close(); - cmd.WaitForExit(_application.BuildSeconds * 1000); + cmd.WaitForExit(App._application.BuildSeconds * 1000); output = cmd.StandardOutput.ReadToEnd(); Debug.WriteLine("\n\n\n\n"); Debug.WriteLine(output); //System.Diagnostics.Process.Start("CMD.exe", ""); }); - TbOutput.Text = output; + TbOutput.Text += "\n" + output; var zipDirectory = new DirectoryInfo(releaseFolder).Parent?.FullName ?? ""; var zipPath = Path.Combine(zipDirectory, "Package.zip"); LblLoading.Content = $"Zip {zipPath}"; + TbOutput.Text += $"\nZip {zipPath} {DateTime.Now:dd.MM.yyyy HH:mm:ss}"; if (File.Exists(zipPath)) File.Delete(zipPath); @@ -260,129 +313,189 @@ namespace InfosysPublisher ZipFile.CreateFromDirectory(releaseFolder, zipPath, CompressionLevel.Optimal, false); }); - if (ChbPripraviSamoZip.IsChecked != null && (bool) ChbPripraviSamoZip.IsChecked) - { - Process.Start("explorer.exe", zipDirectory); - } - else - { - //InfosysUpdate - //v&H6c$wTbTkgSgdWvL*8k$st3#z5X - LblLoading.Content = $"Upload to SFTP"; - var error = ""; - await Task.Run(() => - { - try - { - using var sftpClient = new SftpClient(_application.SftpServerAddress, _application.SftpPort, - _application.SftpUsername, _application.SftpPassword); - sftpClient.Connect(); + UploadSftp(zipDirectory, zipPath); - if (!sftpClient.Exists(_selectedProjectPublishLocation)) - sftpClient.CreateDirectory(_selectedProjectPublishLocation); - - if (!sftpClient.Exists(SftpArchivePath)) - sftpClient.CreateDirectory(SftpArchivePath); - - var files = sftpClient.ListDirectory(_selectedProjectPublishLocation).ToList(); - var xmlVersion = ""; - //arhiv - foreach (var file in files) - { - if (!file.Name.ToLower().EndsWith(".xml")) - continue; - - var tmpXmlFile = Path.GetTempFileName(); - - using (var tmpFile = File.OpenWrite(tmpXmlFile)) - { - sftpClient.DownloadFile(file.FullName, tmpFile); - } - - try - { - var xmlContent = File.ReadAllText(tmpXmlFile); - if (!string.IsNullOrEmpty(xmlContent)) - { - //Oddaljena verzije - var xmlDocument = new XmlDocument(); - xmlDocument.LoadXml(xmlContent); - var xmlNode = xmlDocument.SelectSingleNode("/Update"); - xmlVersion = xmlNode["Version"].InnerText; - } - } - catch (Exception exception) - { - error += "\n\n"; - error += exception.ToString(); - } - - File.Delete(tmpXmlFile); - } - - if (xmlVersion != "") - { - var projectArchive = SftpArchivePath + "/" + _selectedProjectPublishLocation; - if (!sftpClient.Exists(projectArchive)) - sftpClient.CreateDirectory(projectArchive); - - var archiveFolderWithoutIndex = projectArchive + "/" + xmlVersion.Replace(".", "_"); - var archiveFolder = archiveFolderWithoutIndex; - var index = 1; - while (sftpClient.Exists(archiveFolder)) - { - archiveFolder = archiveFolderWithoutIndex + "_" + index; - index++; - } - - sftpClient.CreateDirectory(archiveFolder); - - foreach (var file in files) - { - if (!file.IsRegularFile) - continue; - - sftpClient.Get(file.FullName).MoveTo(archiveFolder + "/" + file.Name); - - } - } - - //Upload - var tmpXmlFileUpload = Path.GetTempFileName(); - string xml = $@" - - {_selectedProjectVersion} -"; - File.WriteAllText(tmpXmlFileUpload, xml); - using (var fileStream = new FileStream(tmpXmlFileUpload, FileMode.Open)) - { - sftpClient.UploadFile(fileStream, _selectedProjectPublishLocation + "/" + "Update.xml", - true); - } - - using (var fileStream = new FileStream(zipPath, FileMode.Open)) - { - sftpClient.UploadFile(fileStream, _selectedProjectPublishLocation + "/" + "Package.zip", - true); - } - - File.Delete(tmpXmlFileUpload); - sftpClient.Disconnect(); - } - catch (Exception ex) - { - error += "\n\n"; - error += ex.ToString(); - } - }); - if (error != "") - { - MessageBox.Show(error); - } - } + TbOutput.Text += $"\nVersion {_selectedProjectVersion} {DateTime.Now:dd.MM.yyyy HH:mm:ss}"; + WriteVersion(); GridProgress.Visibility = Visibility.Hidden; LblLoading.Content = ""; + + TbOutput.Text += $"\nEnd {DateTime.Now:dd.MM.yyyy HH:mm:ss}"; + } + + private async void UploadSftp(string zipDirectory, string zipPath) + { + if (ChbCreateOnlyZip.IsChecked != null && (bool)ChbCreateOnlyZip.IsChecked) + { + Process.Start("explorer.exe", zipDirectory); + return; + } + + //InfosysUpdate + //v&H6c$wTbTkgSgdWvL*8k$st3#z5X + LblLoading.Content = $"Upload to SFTP"; + TbOutput.Text += $"\nUpload to SFTP {DateTime.Now:dd.MM.yyyy HH:mm:ss}"; + var error = ""; + await Task.Run(() => + { + try + { + using var sftpClient = new SftpClient(App._application.SftpServerAddress, App._application.SftpPort, + App._application.SftpUsername, App._application.SftpPassword); + sftpClient.Connect(); + + if (!sftpClient.Exists(_selectedProjectPublishLocation)) + sftpClient.CreateDirectory(_selectedProjectPublishLocation); + + if (!sftpClient.Exists(SftpArchivePath)) + sftpClient.CreateDirectory(SftpArchivePath); + + var files = sftpClient.ListDirectory(_selectedProjectPublishLocation).ToList(); + var xmlVersion = ""; + //arhiv + foreach (var file in files) + { + if (!file.Name.ToLower().EndsWith(".xml")) + continue; + + var tmpXmlFile = Path.GetTempFileName(); + + using (var tmpFile = File.OpenWrite(tmpXmlFile)) + { + sftpClient.DownloadFile(file.FullName, tmpFile); + } + + try + { + var xmlContent = File.ReadAllText(tmpXmlFile); + if (!string.IsNullOrEmpty(xmlContent)) + { + //Oddaljena verzije + var xmlDocument = new XmlDocument(); + xmlDocument.LoadXml(xmlContent); + var xmlNode = xmlDocument.SelectSingleNode("/Update"); + xmlVersion = xmlNode["Version"].InnerText; + } + } + catch (Exception exception) + { + error += "\n\n"; + error += exception.ToString(); + } + + File.Delete(tmpXmlFile); + } + + if (xmlVersion != "") + { + var projectArchive = SftpArchivePath + "/" + _selectedProjectPublishLocation; + if (!sftpClient.Exists(projectArchive)) + sftpClient.CreateDirectory(projectArchive); + + var archiveFolderWithoutIndex = projectArchive + "/" + xmlVersion.Replace(".", "_"); + var archiveFolder = archiveFolderWithoutIndex; + var index = 1; + while (sftpClient.Exists(archiveFolder)) + { + archiveFolder = archiveFolderWithoutIndex + "_" + index; + index++; + } + + sftpClient.CreateDirectory(archiveFolder); + + foreach (var file in files) + { + if (!file.IsRegularFile) + continue; + + sftpClient.Get(file.FullName).MoveTo(archiveFolder + "/" + file.Name); + + } + } + + //Upload + var tmpXmlFileUpload = Path.GetTempFileName(); + string xml = $@" + + {_selectedProjectVersion} +"; + File.WriteAllText(tmpXmlFileUpload, xml); + using (var fileStream = new FileStream(tmpXmlFileUpload, FileMode.Open)) + { + sftpClient.UploadFile(fileStream, _selectedProjectPublishLocation + "/" + "Update.xml", + true); + } + + using (var fileStream = new FileStream(zipPath, FileMode.Open)) + { + sftpClient.UploadFile(fileStream, _selectedProjectPublishLocation + "/" + "Package.zip", + true); + } + + var sftpDirVersion = _selectedProjectPublishLocation + "/" + _selectedProjectVersion; + + if (!sftpClient.Exists(sftpDirVersion)) + sftpClient.CreateDirectory(sftpDirVersion); + + using (var fileStream = new FileStream(zipPath, FileMode.Open)) + { + sftpClient.UploadFile(fileStream, sftpDirVersion + "/" + "Package.zip", + true); + } + + File.Delete(tmpXmlFileUpload); + sftpClient.Disconnect(); + } + catch (Exception ex) + { + error += "\n\n"; + error += ex.ToString(); + } + }); + if (error != "") + { + MessageBox.Show(error); + return; + } + + + } + + private void WriteVersion() + { + using var sqlConnection = new SqlConnection(App.SqlConnectionString); + sqlConnection.Open(); + + var sqlCommandString = @" +DECLARE @idVerzija INT = NULL + +SELECT TOP 1 @idVerzija = ID_Verzija +FROM AplikacijaVerzija +WHERE GUID_Aplikacija_FK = @guid + AND Verzija = @verzija + +IF @idVerzija IS NULL + BEGIN + CREATE TABLE #tmpId( + ID INT + ); + + INSERT INTO AplikacijaVerzija (GUID_Aplikacija_FK, Verzija) + OUTPUT inserted.ID_Verzija INTO #tmpId + VALUES (@guid, @verzija) + + SELECT TOP 1 @idVerzija = Id FROM #tmpId + END + +INSERT INTO AplikacijaVerzijaRevizija (ID_AplikacijaVerzija_FK, ID_Uporabnik_FK) +VALUES (@idVerzija, @idUporabnik);"; + using var sqlCommand = new SqlCommand(sqlCommandString, sqlConnection); + sqlCommand.Parameters.AddWithValue("guid", _selectedProjectGuid); + sqlCommand.Parameters.AddWithValue("verzija", _selectedProjectVersion); + sqlCommand.Parameters.AddWithValue("idUporabnik", App.User.Id); + + sqlCommand.ExecuteNonQuery(); } } }