Skip to content

Commit

Permalink
updated: manage categories | add, update, remove categories, view in …
Browse files Browse the repository at this point in the history
…datagrid
  • Loading branch information
thanhngan22 committed Dec 26, 2023
1 parent 9eedae6 commit b3af073
Show file tree
Hide file tree
Showing 11 changed files with 370 additions and 228 deletions.
5 changes: 5 additions & 0 deletions BookShop2023/Source/BookShop2023/BUS/CategoryBUS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,10 @@ public void updateCategory(int ID, Category category)
_categoryDAO.updateCategory(ID, category);

}

public int getTotalProductsOfCat(int ID)
{
return _categoryDAO.getTotalProductsOfCat(ID);
}
}
}
31 changes: 30 additions & 1 deletion BookShop2023/Source/BookShop2023/DAO/CategoryDAO.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ProjectMyShop.DTO;
using Microsoft.Graph;
using ProjectMyShop.DTO;
using ProjectMyShop.Helpers;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -163,5 +164,33 @@ public void removeCategory(int ID)
System.Diagnostics.Debug.WriteLine($"Deleted {ID} Fail: " + ex.Message);
}
}

public int getTotalProductsOfCat(int ID)
{
var sql = "";

sql = "select count(*) from Product where CatID = @ID";

SqlCommand sqlCommand = new SqlCommand(sql, DB.Instance.Connection);

sqlCommand.Parameters.AddWithValue("@ID", ID);


try
{
// Sử dụng ExecuteScalar để nhận giá trị đếm
int totalProducts = (int)sqlCommand.ExecuteScalar();
System.Diagnostics.Debug.WriteLine($"Total: {totalProducts} OK");
return totalProducts;


}
catch (Exception ex)
{

System.Diagnostics.Debug.WriteLine($" Fail: " + ex.Message);
return 0;
}
}
}
}
6 changes: 5 additions & 1 deletion BookShop2023/Source/BookShop2023/DAO/ProductDAO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -468,13 +468,16 @@ public void setSortingCriteriaQuery(String query)
}
#endregion

#region load all products
public List<Product> loadAllProducts () {
List<Product> list = new List<Product>();

var sql = @"
select *, count(*) over() as Total from Product
where (LOWER(CONVERT(VARCHAR(100), Name)) LIKE LOWER(CONVERT(VARCHAR(100), @Keyword))
OR LOWER(CONVERT(NVARCHAR(100), Author)) LIKE LOWER(CONVERT(NVARCHAR(100), @Keyword)))
OR LOWER(CONVERT(NVARCHAR(100), Author)) LIKE LOWER(CONVERT(NVARCHAR(100), @Keyword))
OR CAST(PublishedYear AS NVARCHAR(10)) LIKE @Keyword
)
"
+ (_hasCategoryFilter ? " AND CatID = @CatID " : " ")
+ (_hasPublishedYearFilter ? " AND PublishedYear = @PublishedYear " : " ")
Expand Down Expand Up @@ -537,5 +540,6 @@ OR LOWER(CONVERT(NVARCHAR(100), Author)) LIKE LOWER(CONVERT(NVARCHAR(100), @Keyw
}
return list;
}
#endregion
}
}
29 changes: 20 additions & 9 deletions BookShop2023/Source/BookShop2023/Views/AddCategoryScreen.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
xmlns:local="clr-namespace:ProjectMyShop.Views"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
mc:Ignorable="d"
Title="AddCategoryScreen" Height="600" Width="800">
Title="Thêm thể loại mới" Height="350" Width="600"
WindowStartupLocation="CenterScreen"
>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10*"/>
Expand All @@ -17,19 +19,28 @@
<Grid.RowDefinitions>
<RowDefinition Height="10*"/>
<RowDefinition Height="20*"/>
<RowDefinition Height="10*"/>
</Grid.RowDefinitions>


</Grid.RowDefinitions>

<TextBox BorderThickness="1" BorderBrush="Purple" Padding="5" Height="50" Text="{Binding Name}" x:Name="categoryNameBox" Grid.Row="0"
materialDesign:HintAssist.Hint="Tên thể loại:" Style="{StaticResource MaterialDesignFloatingHintTextBox}" Grid.Column="1" />

<TextBox BorderThickness="1" BorderBrush="Purple" Padding="5" Height="140" Text="{Binding Description}" x:Name="categoryDescriptionBox" Grid.Row="1"
materialDesign:HintAssist.Hint="Mô tả:" Style="{StaticResource MaterialDesignFloatingHintTextBox}" Grid.Column="1" />

<Button x:Name="chooseImageButton" Background="Transparent" Click="chooseImageButton_Click" Width="180" Height="180" Grid.Row="0" Grid.Column="1" Grid.RowSpan="1">
<Image x:Name="avatar" RenderOptions.BitmapScalingMode="HighQuality" Source="{Binding Avatar}" />
</Button>
<TextBox BorderThickness="1" BorderBrush="Purple" Padding="5" Height="50" Text="{Binding CatName}" x:Name="categoryNameBox" Grid.Row="1" Margin="80,31,80,211" materialDesign:HintAssist.Hint="Category name" Style="{StaticResource MaterialDesignFloatingHintTextBox}" Grid.Column="1" />



<StackPanel Height="40" Grid.Column="1" Grid.Row="6" Grid.ColumnSpan="2" Orientation="Horizontal" HorizontalAlignment="Center"/>
<Button x:Name="cancelButton" Click="cancelButton_Click" Content="Cancel" Grid.Column="1" Margin="80,134,279,126" Grid.Row="1"/>
<Button x:Name="addButton" Click="addButton_Click" Content="Add category" Margin="268,134,80,126" Grid.Column="1" Grid.Row="1"/>
<StackPanel Height="40" Grid.Column="1" Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Center">
<Button x:Name="cancelButton" Click="cancelButton_Click" Content="Cancel"
Width="100" Margin="0 0 100 0"
/>
<Button x:Name="addButton" Click="addButton_Click"
Width="100"
Content="Add " />
</StackPanel>

</Grid>
</Window>
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,6 @@ public AddCategoryScreen()
public int catIndex { get; set; } = -1;
CategoryBUS _categoryBUS { get; set; }

private void chooseImageButton_Click(object sender, RoutedEventArgs e)
{
var screen = new OpenFileDialog();
if (screen.ShowDialog() == true)
{
//newCategory.ImagePath = new BitmapImage(new Uri(screen.FileName, UriKind.Absolute));
//avatar.Source = newCategory.ImagePath;
}
}

private void cancelButton_Click(object sender, RoutedEventArgs e)
{
Expand Down
27 changes: 18 additions & 9 deletions BookShop2023/Source/BookShop2023/Views/EditCategoryScreen.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
xmlns:local="clr-namespace:ProjectMyShop.Views"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
mc:Ignorable="d"
Title="Edit Category" Height="600" Width="800">
Title="Chỉnh sửa thể loại" Height="350" Width="600">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10*"/>
Expand All @@ -17,19 +17,28 @@
<Grid.RowDefinitions>
<RowDefinition Height="10*"/>
<RowDefinition Height="20*"/>
<RowDefinition Height="10*"/>
</Grid.RowDefinitions>


</Grid.RowDefinitions>

<TextBox BorderThickness="1" BorderBrush="Purple" Padding="5" Height="50" Text="{Binding Name}" x:Name="categoryNameBox" Grid.Row="0"
materialDesign:HintAssist.Hint="Tên thể loại:" Style="{StaticResource MaterialDesignFloatingHintTextBox}" Grid.Column="1" />

<TextBox BorderThickness="1" BorderBrush="Purple" Padding="5" Height="140" Text="{Binding Description}" x:Name="categoryDescriptionBox" Grid.Row="1"
materialDesign:HintAssist.Hint="Mô tả:" Style="{StaticResource MaterialDesignFloatingHintTextBox}" Grid.Column="1" />



<Button x:Name="chooseImageButton" Background="Transparent" Click="ImageButton_Click" Width="180" Height="180" Grid.Row="0" Grid.Column="1" Grid.RowSpan="1">
<Image x:Name="avatar" RenderOptions.BitmapScalingMode="HighQuality" Source="{Binding Avatar}" />
</Button>
<TextBox BorderThickness="1" BorderBrush="Purple" Padding="5" Height="50" Text="{Binding CatName}" x:Name="categoryNameBox" Grid.Row="1" Margin="80,31,80,211" materialDesign:HintAssist.Hint="Category name" Style="{StaticResource MaterialDesignFloatingHintTextBox}" Grid.Column="1" />

<StackPanel Height="40" Grid.Column="1" Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Center">
<Button x:Name="cancelButton" Click="cancelButton_Click" Content="Cancel"
Width="100" Margin="0 0 100 0"
/>
<Button x:Name="addButton" Click="updateButton_Click"
Width="100"
Content="Update " />
</StackPanel>

<StackPanel Height="40" Grid.Column="1" Grid.Row="6" Grid.ColumnSpan="2" Orientation="Horizontal" HorizontalAlignment="Center"/>
<Button x:Name="cancelButton" Click="cancelButton_Click" Content="Cancel" Grid.Column="1" Margin="80,134,279,126" Grid.Row="1"/>
<Button x:Name="addButton" Click="editButton_Click" Content="Edit category" Margin="268,134,80,126" Grid.Column="1" Grid.Row="1"/>
</Grid>
</Window>
14 changes: 2 additions & 12 deletions BookShop2023/Source/BookShop2023/Views/EditCategoryScreen.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public EditCategoryScreen(Category p)
public Category EditedCategory { get; set; }


private void editButton_Click(object sender, RoutedEventArgs e)
private void updateButton_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
}
Expand All @@ -40,17 +40,7 @@ private void cancelButton_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
}
public static readonly List<string> ImageExtensions = new List<string> { ".JPG", ".JPE", ".BMP", ".GIF", ".PNG" };

private void ImageButton_Click(object sender, RoutedEventArgs e)
{
var screen = new OpenFileDialog();
screen.Filter = "Image Files|*.jpg;*.jpeg;*.png;...";
if (screen.ShowDialog() == true)
{
//EditedCategory.Avatar = new BitmapImage(new Uri(screen.FileName, UriKind.Absolute));
//avatar.Source = EditedCategory.Avatar;
}
}

}
}
Loading

0 comments on commit b3af073

Please sign in to comment.