Jelajahi Sumber

Добавьте файлы проекта.

matvejskripov 3 tahun lalu
induk
melakukan
48cf837532

+ 31 - 0
BookStore.sln

@@ -0,0 +1,31 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio Version 16
+VisualStudioVersion = 16.0.31829.152
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BookStore", "BookStore\BookStore.csproj", "{08C64792-D3DC-466E-A5DF-9DEF5F7DAB82}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BookStoreTest", "BookStoreTest\BookStoreTest.csproj", "{B278E543-F6EC-4B53-89ED-34487C0A406B}"
+EndProject
+Global
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
+		Debug|Any CPU = Debug|Any CPU
+		Release|Any CPU = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(ProjectConfigurationPlatforms) = postSolution
+		{08C64792-D3DC-466E-A5DF-9DEF5F7DAB82}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{08C64792-D3DC-466E-A5DF-9DEF5F7DAB82}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{08C64792-D3DC-466E-A5DF-9DEF5F7DAB82}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{08C64792-D3DC-466E-A5DF-9DEF5F7DAB82}.Release|Any CPU.Build.0 = Release|Any CPU
+		{B278E543-F6EC-4B53-89ED-34487C0A406B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{B278E543-F6EC-4B53-89ED-34487C0A406B}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{B278E543-F6EC-4B53-89ED-34487C0A406B}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{B278E543-F6EC-4B53-89ED-34487C0A406B}.Release|Any CPU.Build.0 = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(SolutionProperties) = preSolution
+		HideSolutionNode = FALSE
+	EndGlobalSection
+	GlobalSection(ExtensibilityGlobals) = postSolution
+		SolutionGuid = {E15AFE56-3729-4153-87F5-073821626643}
+	EndGlobalSection
+EndGlobal

+ 6 - 0
BookStore/App.config

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<configuration>
+	<connectionStrings>
+		<add name ="BookStore" connectionString="Server=DESKTOP-7CPP4TJ\SQLEXPRESS;Database=BookStore;Trusted_Connection=True"/>
+	</connectionStrings>
+</configuration>

+ 9 - 0
BookStore/App.xaml

@@ -0,0 +1,9 @@
+<Application x:Class="BookStore.App"
+             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+             xmlns:local="clr-namespace:BookStore"
+             StartupUri="MainWindow.xaml">
+    <Application.Resources>
+         
+    </Application.Resources>
+</Application>

+ 17 - 0
BookStore/App.xaml.cs

@@ -0,0 +1,17 @@
+using System;
+using System.Collections.Generic;
+using System.Configuration;
+using System.Data;
+using System.Linq;
+using System.Threading.Tasks;
+using System.Windows;
+
+namespace BookStore
+{
+    /// <summary>
+    /// Interaction logic for App.xaml
+    /// </summary>
+    public partial class App : Application
+    {
+    }
+}

+ 25 - 0
BookStore/AssemblyInfo.cs

@@ -0,0 +1,25 @@
+
+using Microsoft.EntityFrameworkCore;
+using System.Configuration;
+using System.Windows;
+
+[assembly: ThemeInfo(ResourceDictionaryLocation.None,
+    ResourceDictionaryLocation.SourceAssembly)]
+
+public class Book
+{
+    public int Id { get; set; }
+    public string Name { get; set; }
+    public decimal Price { get; set; }
+    public string Category { get; set; }
+    public string Author { get; set; }
+}
+
+public class BookStoreContext : DbContext
+{
+    public DbSet<Book> Books { get; set; }
+    public BookStoreContext() { }
+    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
+    { optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["BookStore"].ConnectionString); }
+}
+

+ 14 - 0
BookStore/BookStore.csproj

@@ -0,0 +1,14 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>WinExe</OutputType>
+    <TargetFramework>net5.0-windows</TargetFramework>
+    <UseWPF>true</UseWPF>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.0" />
+    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0" />
+  </ItemGroup>
+
+</Project>

+ 51 - 0
BookStore/MainWindow.xaml

@@ -0,0 +1,51 @@
+<Window x:Class="BookStore.MainWindow"
+        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+        xmlns:local="clr-namespace:BookStore"
+        mc:Ignorable="d"
+        Title="MainWindow" Height="450" Width="800">
+    <Grid>
+        <Grid.ColumnDefinitions>
+            <ColumnDefinition/>
+            <ColumnDefinition/>
+        </Grid.ColumnDefinitions>
+        <Grid.RowDefinitions>
+            <RowDefinition Height="50"/>
+            <RowDefinition/>
+        </Grid.RowDefinitions>
+        <DataGrid Grid.Row="1" SelectionChanged="BooksGrid_SelectionChanged" Name="BooksGrid" AutoGenerateColumns="False" IsReadOnly="True" ColumnWidth="*" x:FieldModifier="public">
+            <DataGrid.Columns>
+                <DataGridTextColumn Header="Наименование" Binding="{Binding Name}"></DataGridTextColumn>
+                <DataGridTextColumn Header="Цена" Binding="{Binding Price}"></DataGridTextColumn>
+                <DataGridTextColumn Header="Категория" Binding="{Binding Category}"></DataGridTextColumn>
+                <DataGridTextColumn Header="Автор" Binding="{Binding Author}"></DataGridTextColumn>
+            </DataGrid.Columns>
+        </DataGrid>
+        <Button Height="30" Width="150" Name="BtnDelete" Click="BtnDelete_Click" HorizontalAlignment="Left" Content="Удалить"></Button>
+        <Grid Grid.Column="1" Grid.Row="1">
+            <Grid.ColumnDefinitions>
+                <ColumnDefinition/>
+                <ColumnDefinition/>
+            </Grid.ColumnDefinitions>
+            <Grid.RowDefinitions>
+                <RowDefinition/>
+                <RowDefinition/>
+                <RowDefinition/>
+                <RowDefinition/>
+                <RowDefinition/>
+            </Grid.RowDefinitions>
+            <TextBlock Text="Наименование" HorizontalAlignment="Right" VerticalAlignment="Center"></TextBlock>
+            <TextBlock Text="Цена" HorizontalAlignment="Right" VerticalAlignment="Center" Grid.Row="1"></TextBlock>
+            <TextBlock Text="Категория" HorizontalAlignment="Right" VerticalAlignment="Center" Grid.Row="2"></TextBlock>
+            <TextBlock Text="Автор" HorizontalAlignment="Right" VerticalAlignment="Center" Grid.Row="3"></TextBlock>
+            <TextBox Grid.Column="1" VerticalAlignment="Center" Name="TbName" x:FieldModifier="public"></TextBox>
+            <TextBox Grid.Column="1" VerticalAlignment="Center" Name="TbPrice" Grid.Row="1" x:FieldModifier="public"></TextBox>
+            <TextBox Grid.Column="1" VerticalAlignment="Center" Name="TbCategory" Grid.Row="2" x:FieldModifier="public"></TextBox>
+            <TextBox Grid.Column="1" VerticalAlignment="Center" Name="TbAuthor" Grid.Row="3" x:FieldModifier="public"></TextBox>
+            <Button Grid.Row="4" Name="BtnInsert" Click="BtnInsert_Click" Height="30" Width="150" Content="Добавить"></Button>
+            <Button Grid.Column="1" Grid.Row="4" Name="BtnUpdate" Click="BtnUpdate_Click" Height="30" Width="150" Content="Обновить"></Button>
+        </Grid>
+    </Grid>
+</Window>

+ 64 - 0
BookStore/MainWindow.xaml.cs

@@ -0,0 +1,64 @@
+using System.Linq;
+using System.Windows;
+using System.Windows.Controls;
+
+namespace BookStore
+{
+    public partial class MainWindow : Window
+    {
+        BookStoreContext _context = new BookStoreContext();
+
+        public MainWindow() { InitializeComponent(); Load(); }
+
+        public void Load() { BooksGrid.ItemsSource = _context.Books.ToList(); }
+
+        private void BtnInsert_Click(object sender, RoutedEventArgs e) { BIC(); }
+        public void BIC()
+        {
+            if (!decimal.TryParse(TbPrice.Text, out decimal price)) { return; }
+            Book book = new Book()
+            {
+                Name = TbName.Text,
+                Price = price,
+                Author = TbAuthor.Text,
+                Category = TbCategory.Text
+            };
+            _context.Books.Add(book); _context.SaveChanges(); Load();
+        }
+
+        private void BtnUpdate_Click(object sender, RoutedEventArgs e) { BUC(); }
+        public void BUC()
+        {
+            if (BooksGrid.SelectedItem is Book selectedBook)
+            {
+                if (!decimal.TryParse(TbPrice.Text, out decimal price)) { return; }
+                selectedBook.Name = TbName.Text;
+                selectedBook.Price = price;
+                selectedBook.Author = TbAuthor.Text;
+                selectedBook.Category = TbCategory.Text;
+                _context.SaveChanges(); Load();
+            }
+        }
+
+        private void BtnDelete_Click(object sender, RoutedEventArgs e) { BDC(); }
+        public void BDC()
+        {
+            if (BooksGrid.SelectedItem is Book selectedBook)
+            { _context.Books.Remove(selectedBook); _context.SaveChanges(); TbClear(); Load(); }
+        }
+        public void TbClear()
+        { TbName.Text = ""; TbPrice.Text = ""; TbAuthor.Text = ""; TbCategory.Text = ""; }
+
+        private void BooksGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) { BGSC(); }
+        public void BGSC()
+        {
+            if (BooksGrid.SelectedItem is Book selectedBook)
+            {
+                TbName.Text = selectedBook.Name;
+                TbPrice.Text = selectedBook.Price.ToString();
+                TbAuthor.Text = selectedBook.Author;
+                TbCategory.Text = selectedBook.Category;
+            }
+        }
+    }
+}

+ 6 - 0
BookStoreTest/App.config

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<configuration>
+	<connectionStrings>
+		<add name ="BookStore" connectionString="Server=DESKTOP-7CPP4TJ\SQLEXPRESS;Database=BookStore;Trusted_Connection=True"/>
+	</connectionStrings>
+</configuration>

+ 64 - 0
BookStoreTest/BookStoreTest.cs

@@ -0,0 +1,64 @@
+using System;
+using Xunit;
+using BookStore;
+
+namespace BookStoreTest
+{    
+    public class BST
+    {
+        public static MainWindow w = new MainWindow();
+        public static string[] tb = { "Name", "0", "Author", "Category" };        
+
+        public static void Text(string i)
+        {
+            w.TbName.Text = tb[0] + i;
+            w.TbPrice.Text = tb[1];
+            w.TbAuthor.Text = tb[2] + i;
+            w.TbCategory.Text = tb[3] + i;
+        }
+    }
+
+    public class BookStoreTest
+    {
+        [StaFact]
+        public void Test1_Insert()
+        {
+            int c = BST.w.BooksGrid.Items.Count;
+            BST.Text(""); BST.w.BIC(); // Äîáàâëåíèå ñòðîêè
+
+            Assert.Equal(c + 1, BST.w.BooksGrid.Items.Count); // Ïðîâåðêà äîáàâëåíèÿ ñòðîêè
+
+            BST.w.TbClear(); BST.w.BooksGrid.SelectedIndex = BST.w.BooksGrid.Items.Count - 1; // Âûáîð äîáàâëåííîé ñòðîêè
+
+            Assert.Equal(BST.tb[0], BST.w.TbName.Text);           // Ïðîâåðêè êîððåêòíîñòè äîáàâëåíèÿ
+            Assert.Equal(BST.tb[1], BST.w.TbPrice.Text);
+            Assert.Equal(BST.tb[2], BST.w.TbAuthor.Text);
+            Assert.Equal(BST.tb[3], BST.w.TbCategory.Text);
+        }
+
+        [StaFact]
+        public void Test2_Update()
+        {
+            int c = BST.w.BooksGrid.Items.Count;
+            BST.w.BooksGrid.SelectedIndex = BST.w.BooksGrid.Items.Count - 1;
+            BST.Text("2");
+
+            BST.w.BUC(); BST.w.TbClear(); BST.w.BGSC();
+
+            Assert.Equal(c, BST.w.BooksGrid.Items.Count);   // Ïðîâåðêà îòñóòñòâèÿ íîâûõ ñòðîê
+            Assert.Equal(BST.tb[0] + "2", BST.w.TbName.Text);   // Ïðîâåðêè êîððåêòíîñòè îáíîâëåíèÿ
+            Assert.Equal(BST.tb[1], BST.w.TbPrice.Text);
+            Assert.Equal(BST.tb[2] + "2", BST.w.TbAuthor.Text);
+            Assert.Equal(BST.tb[3] + "2", BST.w.TbCategory.Text);
+        }
+
+        [StaFact]
+        public void Test3_Delete()
+        {
+            int c = BST.w.BooksGrid.Items.Count;
+            BST.w.BooksGrid.SelectedIndex = BST.w.BooksGrid.Items.Count - 1; BST.w.BDC();
+
+            Assert.Equal(c - 1, BST.w.BooksGrid.Items.Count); // Ïðîâåðêà óäàëåíèÿ ñòðîêè            
+        }
+    }
+}

+ 29 - 0
BookStoreTest/BookStoreTest.csproj

@@ -0,0 +1,29 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <TargetFramework>net5.0-windows</TargetFramework>
+
+    <IsPackable>false</IsPackable>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.0" />
+    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0" />
+    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
+    <PackageReference Include="xunit" Version="2.4.1" />
+    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
+      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
+      <PrivateAssets>all</PrivateAssets>
+    </PackageReference>
+    <PackageReference Include="coverlet.collector" Version="3.0.2">
+      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
+      <PrivateAssets>all</PrivateAssets>
+    </PackageReference>
+    <PackageReference Include="Xunit.StaFact" Version="1.1.5-alpha" />
+  </ItemGroup>
+
+  <ItemGroup>
+    <ProjectReference Include="..\BookStore\BookStore.csproj" />
+  </ItemGroup>
+
+</Project>