-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1818108
commit fbc1219
Showing
9 changed files
with
213 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
{ | ||
|
@@ -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) | ||
|
@@ -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); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.