Skip to content

Commit

Permalink
update feature add new product
Browse files Browse the repository at this point in the history
  • Loading branch information
thanhngan22 committed Dec 26, 2023
1 parent 1818108 commit fbc1219
Show file tree
Hide file tree
Showing 9 changed files with 213 additions and 77 deletions.
11 changes: 11 additions & 0 deletions BookShop2023/Data/Queries/reset_ident.sql
Original file line number Diff line number Diff line change
@@ -0,0 1,11 @@
USE BookShop2023
GO

DELETE FROM Product;
DBCC CHECKIDENT ('Product', RESEED, 0);

DELETE FROM Category;
DBCC CHECKIDENT ('Category', RESEED, 0);

SELECT * FROM Category;
SELECT * FROM Product;
10 changes: 1 addition & 9 deletions BookShop2023/Source/BookShop2023/DAO/CategoryDAO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 61,7 @@ public List<Category> getCategoryList()

resultList.Add(category);
}
// add all option for filter and display if has data
if (resultList.Count > 0)
{
resultList.Add(new Category()
{
ID = resultList[^1].ID 1,
Name = "All"
});
}


reader.Close();
return resultList;
Expand Down
2 changes: 1 addition & 1 deletion BookShop2023/Source/BookShop2023/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 31,7 @@
<StackPanel Grid.Row="1" Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
<!--Đường dẫn tương đối đến tệp hình ảnh logo-->
<Image RenderOptions.BitmapScalingMode="HighQuality"
Source="{Binding Source=Assets/Logo/book.png , Converter = {StaticResource absoluteConverter}}"
Source="{Binding Source=Assets/Logo/logo_edited.png , Converter = {StaticResource absoluteConverter}}"

Width="200" Height="100" />

Expand Down
28 changes: 22 additions & 6 deletions BookShop2023/Source/BookShop2023/Views/AddProductScreen.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 5,13 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:local="clr-namespace:ProjectMyShop.Views"
xmlns:local1="clr-namespace:ProjectMyShop.Converter"
mc:Ignorable="d"
Title="Thêm sản phẩm mới" Height="650" Width="800">
<Window.Resources>
<local1:CurrencyConverter x:Key="currencyConverter"/>
<local1:RelativeToAbsoluteConverter x:Key="absoluteConverter" />
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10*"/>
Expand All @@ -28,20 33,31 @@
<ComboBox Style="{Binding MaterialDesign}" BorderThickness="1" BorderBrush="MediumPurple" Name="categoryCombobox" Width="180" Height="35" SelectionChanged="categoryCombobox_SelectionChanged" Grid.Column="1" Grid.ColumnSpan="2">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Padding="5" Text="{Binding CatName}"/>
<TextBlock Padding="5" Text="{Binding Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
<ComboBoxItem IsSelected="True">
<TextBlock Padding="5" Text="Loại sản phẩm" />
</ComboBoxItem>

</ComboBox>

<Button x:Name="chooseImageButton" Background="Transparent" Click="chooseImageButton_Click" Width="300" Height="300" Grid.Row="1" Grid.Column="1" Grid.RowSpan="4">
<Image x:Name="avatar" RenderOptions.BitmapScalingMode="HighQuality" Source="{Binding ImagePath}" />
<Image x:Name="imagePath" RenderOptions.BitmapScalingMode="HighQuality" Source="{Binding ImagePath, Converter={StaticResource absoluteConverter}}" />
</Button>
<Button Name="selectImage"
Grid.Column="1"
Grid.Row="5"
Height="40"
Width="100"
Content="Chọn ảnh"
Background="Bisque"
Foreground="Black"
Click="chooseImageButton_Click"

/>


<TextBox BorderThickness="1" BorderBrush="Purple" Padding="5" Height="50" Text="{Binding Name}" x:Name="NameBox" Grid.Column="2" Grid.Row="1" Margin="10" materialDesign:HintAssist.Hint="Tên sản phẩm" Style="{StaticResource MaterialDesignFloatingHintTextBox}" />
<TextBox BorderThickness="1" BorderBrush="Purple" Padding="5" Height="50" Text="{Binding Author}" x:Name="authorBox" Grid.Column="2" Grid.Row="2" Margin="10" materialDesign:HintAssist.Hint="Tác giả" Style="{StaticResource MaterialDesignFloatingHintTextBox}"/>
<TextBox BorderThickness="1" BorderBrush="Purple" Padding="5" Height="50" Text="{Binding Author}" x:Name="publishedYearBox" Grid.Column="2" Grid.Row="3" Margin="10" materialDesign:HintAssist.Hint="Năm xuất bản" Style="{StaticResource MaterialDesignFloatingHintTextBox}"/>
<TextBox BorderThickness="1" BorderBrush="Purple" Padding="5" Height="50" Text="{Binding PublishedYear}" x:Name="publishedYearBox" Grid.Column="2" Grid.Row="3" Margin="10" materialDesign:HintAssist.Hint="Năm xuất bản" Style="{StaticResource MaterialDesignFloatingHintTextBox}"/>
<TextBox BorderThickness="1" BorderBrush="Purple" Padding="5" Height="100" MaxHeight="140" Text="{Binding Description}" x:Name="descriptionBox" Grid.Column="2" Grid.Row="4" Grid.RowSpan="2" materialDesign:HintAssist.Hint="Mô tả thêm ..." Style="{StaticResource MaterialDesignFloatingHintTextBox}" Margin="10"/>
<Grid Grid.Row="6" Grid.Column="1" Grid.ColumnSpan="2">
<Grid.ColumnDefinitions>
Expand Down
120 changes: 96 additions & 24 deletions BookShop2023/Source/BookShop2023/Views/AddProductScreen.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 14,8 @@
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using Microsoft.IdentityModel.Tokens;

namespace ProjectMyShop.Views
{
Expand All @@ -23,21 25,42 @@ namespace ProjectMyShop.Views
public partial class AddProductScreen : Window
{
public Product newProduct { get; set; }
public int catIndex { get; set; } = -1;
ProductBUS _ProductBUS { get; set; }

public List<Category> catListCopy;

public AddProductScreen(List<Category> category)
public AddProductScreen(List<Category> categories)
{
InitializeComponent();
newProduct = new Product();
this.DataContext = newProduct;
categoryCombobox.ItemsSource = category;
newProduct.CatID = -1;

// Tạo một bản sao của danh sách để thực hiện xóa
catListCopy = new List<Category>(categories);

// if list cat has All opt, then remove, just for display in manage product
if (catListCopy[^1] != null && catListCopy[^1].Name.Equals("Tất cả"))
{
catListCopy.Remove(catListCopy[^1]);
}

// add option 'Chọn thể loại'
catListCopy.Add(new Category
{
ID = catListCopy[^1].ID,
Name = "----Chọn thể loại----"
});

categoryCombobox.ItemsSource = catListCopy;
categoryCombobox.SelectedIndex = catListCopy.Count - 1;

// display template image for product
newProduct.ImagePath = "Assets/Images/template.jpg";

}

private void categoryCombobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
catIndex = categoryCombobox.SelectedIndex;
newProduct.CatID = ((Category)categoryCombobox.SelectedItem).ID;
}

private void cancelButton_Click(object sender, RoutedEventArgs e)
Expand All @@ -47,35 70,84 @@ private void cancelButton_Click(object sender, RoutedEventArgs e)

private void addButton_Click(object sender, RoutedEventArgs e)
{
// Check validity

//Product Product = new Product()
//{
// Name = "Galaxy",
// Manufacturer = "Samsung",
// PurchasePrice = 500,
// SellingPrice = 700,
// Description = "[email protected]"
//};
if(catIndex < 0)
// Check validity:

// kiểm tra đã chọn thể loại chưa
if (newProduct.CatID == -1 || newProduct.CatID == catListCopy.Count - 1)
{
MessageBox.Show(this, "Invalid category");
MessageBox.Show($"Vui lòng chọn thể loại sản phẩm!", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
return;

}
else

// kiểm tra đã chọn ảnh sản phẩm chưa
if (newProduct.ImagePath.Equals("Assets/Images/template.jpg"))
{
DialogResult = true;
MessageBox.Show($"Vui lòng chọn ảnh minh họa sản phẩm!", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}

// kiểm tra đã thêm tên, tác giả, giá bán , năm xuất bản, số lượng, giá mua.. hay chưa
if (newProduct.Name.IsNullOrEmpty() || newProduct.Quantity <= 0 || newProduct.SellingPrice <= 0 || newProduct.PurchasePrice <= 0 )
{
MessageBox.Show($"Vui lòng cung cấp đầy đủ thông tin cần thiết cho sản phẩm!", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}

if (newProduct.Author.IsNullOrEmpty())
{
newProduct.Author = "Anonymous";
}


if (newProduct.PublishedYear <= 0 )
{
newProduct.PublishedYear = 2023;
}

DialogResult = true;


}

private void chooseImageButton_Click(object sender, RoutedEventArgs e)
{

var screen = new OpenFileDialog();
if(screen.ShowDialog() == true)
screen.Filter = "Image Files|*.jpg;*.jpeg;*.png;...";
if (screen.ShowDialog() == true)
{
//newProduct.Avatar = new BitmapImage(new Uri(screen.FileName, UriKind.Absolute));
//avatar.Source = newProduct.Avatar;
var fileName = screen.FileName;

var sourceInfo = new FileInfo(fileName);
var extension = sourceInfo.Extension;

// Lấy thư mục ảnh hiện hành
var exeFolder = AppDomain.CurrentDomain.BaseDirectory;
var imgSubFolder = exeFolder "Images";

// phát sinh id duy nhất toàn hệ thống
var newName = Guid.NewGuid() extension;
var destination = $"{imgSubFolder}\\{newName}";

System.IO.File.Copy(fileName, destination);

// cập nhật tên mới để lưu vào db, (Images/...jpg..)
string relativePath = "Images" @"\" newName;

newProduct.ImagePath = relativePath;

string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
string fullPath = System.IO.Path.Combine(baseDirectory, relativePath);


// Tạo một đối tượng BitmapImage
BitmapImage bitmapImage = new BitmapImage(new Uri(fullPath));

// Gán BitmapImage vào thuộc tính Source của đối tượng Image
imagePath.Source = bitmapImage;


//MessageBox.Show("Path: " relativePath);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 4,8 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:local="clr-namespace:ProjectMyShop.Views" xmlns:local1="clr-namespace:ProjectMyShop.Converter"
xmlns:local="clr-namespace:ProjectMyShop.Views"
xmlns:local1="clr-namespace:ProjectMyShop.Converter"
mc:Ignorable="d"
Title="Chỉnh sửa sản phẩm" Height="650" Width="800">
<Window.Resources>
Expand Down
16 changes: 13 additions & 3 deletions BookShop2023/Source/BookShop2023/Views/EditProductScreen.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 29,21 @@ public EditProductScreen(Product p, List<Category> _listCat)
InitializeComponent();
EditedProduct = (Product)p.Clone();
this.DataContext = EditedProduct;
categoryCombobox.ItemsSource = _listCat;

// Tạo một bản sao của danh sách để thực hiện xóa
var catListCopy = new List<Category>(_listCat);

// if list cat has All opt, then remove, just for display in manage product
if (catListCopy[^1] != null && catListCopy[^1].Name.Equals("Tất cả") )
{
catListCopy.Remove(catListCopy[^1]);
}

categoryCombobox.ItemsSource = catListCopy;
int catId = p.CatID;
for (int i = 0; i < _listCat.Count; i )
for (int i = 0; i < catListCopy.Count; i )
{
if (catId == _listCat[i].ID)
if (catId == catListCopy[i].ID)
{
categoryCombobox.SelectedIndex = i;
break;
Expand Down
46 changes: 27 additions & 19 deletions BookShop2023/Source/BookShop2023/Views/ManageProduct.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 72,9 @@
</Button>
<TextBox Name="searchTextBox"
TextChanged="searchTextBox_TextChanged"
Padding="10"
FontSize="15"
Padding="5"
Height="32"
FontSize="13"
Foreground="Black"
materialDesign:HintAssist.Hint="Tìm sản phẩm theo tên, tác giả..."
Grid.Column="1"
Expand Down Expand Up @@ -102,45 103,52 @@
<Grid.ColumnDefinitions>
<ColumnDefinition Width="4*"/>
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="10"/>
</Grid.ColumnDefinitions>


<!-- Filter combo box-->
<materialDesign:ColorZone Mode="Custom"
<materialDesign:Card
Margin="0, 0, 0, 0"
Height="{Binding ActualHeight, ElementName=searchContainer}"
Height="{Binding ActualHeight, ElementName=filterContainer}"
Grid.Column="2">



<materialDesign:Card Name="sortBoxContainer"
Height="45"
UniformCornerRadius="23"
Grid.Column="0" Margin="0,0,-110,0">
<materialDesign:Card Name="filterBoxContainer"
Height="36"
Grid.Column="0" Margin="5 0 5 0" >
<StackPanel Orientation="Horizontal" >
<TextBlock Text="From"

<TextBlock Text="Min:"
VerticalAlignment="Center" Foreground="Black" Margin="10,0"/>
<TextBox Width="45" Name="minPriceTextbox" Height="30" Padding="0 6 0 0" Text="0" Foreground="Black" HorizontalAlignment="Center"/>
<TextBlock Text="To"
<TextBox Width="50" Name="minPriceTextbox" Height="30" Padding="0 6 0 0"
TextAlignment="Center"
Text="0" Foreground="Black" HorizontalAlignment="Center"/>
<TextBlock Text="Max:"
VerticalAlignment="Center" Foreground="Black" Margin="10,0"/>
<TextBox Width="45" Name="maxPriceTextbox" Height="30" Padding="0 6 0 0" Text="0" Foreground="Black" HorizontalAlignment="Center"/>

<Button Name="FilterButton" Click="FilterButton_Click" Margin="5 0 0 0"
Style="{StaticResource MaterialDesignFloatingActionMiniDarkButton}">
<TextBox Width="50" Name="maxPriceTextbox" Height="30" Padding="0 6 0 0"
TextAlignment="Center"
Text="0" Foreground="Black" HorizontalAlignment="Center"/>

<Button Name="FilterButton" Click="FilterButton_Click"
Height="36" Width="36"
Margin="5 0 0 0"
Style="{StaticResource MaterialDesignFloatingActionMiniDarkButton}"
>
<Button.Content>
<materialDesign:PackIcon Kind="Filter"/>
<materialDesign:PackIcon Kind="Filter"/>
</Button.Content>
</Button>

</StackPanel>
</materialDesign:Card>
</materialDesign:ColorZone>
</materialDesign:Card>
</Grid>
<!--Category comboBox-->

<ComboBox Name="categoriesComboBox" Width="200" Height="30" HorizontalAlignment="Left"
<ComboBox Name="categoriesComboBox" Width="200" Height="36" HorizontalAlignment="Left"
Grid.Row="0" Grid.Column="1"
SelectionChanged="categoriesComboBox_SelectionChanged"
DropDownOpened="categoriesComboBox_DropDownOpened" DropDownClosed="categoriesComboBox_DropDownClosed"
Expand Down
Loading

0 comments on commit fbc1219

Please sign in to comment.