@@ -4,7 +4,6 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Webshop.Models;



@@ -16,151 +15,151 @@ class DALProduct : DALGeneral
{

List<string> count = new List<string>();
public List<Product> SearchProduct(Product product)
{
string sql =
"SELECT "+
"prod.ProductName, " +
"category.Category, " +
"size.Size, " +
"color.Color, " +
"brand.Brand, " +
"prod.Description, " +
"prod.PricePerUnit, " +
"prod.UnitsInStock, " +
"prod.PictureID " +
"from tblProduct AS prod WHERE ";
//public List<Product> SearchProduct(Product product)
//{
// string sql =
// "SELECT "+
// "prod.ProductName, " +
// "category.Category, " +
// "size.Size, " +
// "color.Color, " +
// "brand.Brand, " +
// "prod.Description, " +
// "prod.PricePerUnit, " +
// "prod.UnitsInStock, " +
// "prod.PictureID " +
// "from tblProduct AS prod WHERE ";

if (product.productID != null)
{
if (count.Count > 0)
sql += "'AND ";
sql += $"ProductID = {product.productID} ";
count.Add(sql);
}
if (product.name != null)
{
if (count.Count > 0)
sql += "AND ";
sql += $"ProductName = {product.name} ";
count.Add(sql);
}
if (product.category != null)
{
if (count.Count > 0)
sql += "AND ";
sql += $"CategoryID = {product.category} ";
count.Add(sql);
}
if (product.size != null)
{
if (count.Count > 0)
sql += "AND ";
sql += $"SizeID = {product.size} ";
count.Add(sql);
}
if (product.Color != null)
{
if (count.Count > 0)
sql += "AND ";
sql += $"ColorID = {product.Color} ";
count.Add(sql);
}
if (product.brand != null)
{
if (count.Count > 0)
sql += "AND ";
sql += $"BrandID = {product.brand} ";
count.Add(sql);
}
if (product.description != null)
{
if (count.Count > 0)
sql += "AND ";
sql += $"Description = {product.description} ";
count.Add(sql);
}
if (product.ppu != null)
{
if (count.Count > 0)
sql += "AND ";
sql += $"PricePerUnit = {product.ppu} ";
count.Add(sql);
}
if (product.unitsInStock != null)
{
if (count.Count > 0)
sql += "AND ";
sql += $"UnitsInStock = {product.unitsInStock} ";
count.Add(sql);
}
if (product.picture != null)
{
if (count.Count > 0)
sql += "AND ";
sql += $"PictureID = {product.picture} ";
}
// if (product.productID != null)
// {
// if (count.Count > 0)
// sql += "'AND ";
// sql += $"ProductID = {product.productID} ";
// count.Add(sql);
// }
// if (product.name != null)
// {
// if (count.Count > 0)
// sql += "AND ";
// sql += $"ProductName = {product.name} ";
// count.Add(sql);
// }
// if (product.category != null)
// {
// if (count.Count > 0)
// sql += "AND ";
// sql += $"CategoryID = {product.category} ";
// count.Add(sql);
// }
// if (product.size != null)
// {
// if (count.Count > 0)
// sql += "AND ";
// sql += $"SizeID = {product.size} ";
// count.Add(sql);
// }
// if (product.Color != null)
// {
// if (count.Count > 0)
// sql += "AND ";
// sql += $"ColorID = {product.Color} ";
// count.Add(sql);
// }
// if (product.brand != null)
// {
// if (count.Count > 0)
// sql += "AND ";
// sql += $"BrandID = {product.brand} ";
// count.Add(sql);
// }
// if (product.description != null)
// {
// if (count.Count > 0)
// sql += "AND ";
// sql += $"Description = {product.description} ";
// count.Add(sql);
// }
// if (product.ppu != null)
// {
// if (count.Count > 0)
// sql += "AND ";
// sql += $"PricePerUnit = {product.ppu} ";
// count.Add(sql);
// }
// if (product.unitsInStock != null)
// {
// if (count.Count > 0)
// sql += "AND ";
// sql += $"UnitsInStock = {product.unitsInStock} ";
// count.Add(sql);
// }
// if (product.picture != null)
// {
// if (count.Count > 0)
// sql += "AND ";
// sql += $"PictureID = {product.picture} ";
// }

sql += "INNER JOIN tblCategory AS category ON prod.CategoryID = category.CategoryID " +
"INNER JOIN tblColor AS color ON color.ColorID = prod.ColorID " +
"INNER JOIN tblSize AS size ON size.SizeID = prod.SizeID " +
"INNER JOIN tblBrand AS brand ON brand.BrandID = prod.BrandID";
// sql += "INNER JOIN tblCategory AS category ON prod.CategoryID = category.CategoryID " +
// "INNER JOIN tblColor AS color ON color.ColorID = prod.ColorID " +
// "INNER JOIN tblSize AS size ON size.SizeID = prod.SizeID " +
// "INNER JOIN tblBrand AS brand ON brand.BrandID = prod.BrandID";

List<Product> products = new List<Product>();
var myCommand = new SqlCommand(sql, connection);
// List<Product> products = new List<Product>();
// var myCommand = new SqlCommand(sql, connection);

using (var datareader = myCommand.ExecuteReader())
{
while (datareader.Read())
{
Product item = new Product();
item.productID = Convert.ToInt32(datareader["ProductID"]);
item.name = $"{datareader["ProductName"]}";
item.category = $"{datareader["Category"]}";
item.size = $"{datareader["Size"]}";
item.Color = $"{datareader["Color"]}";
item.brand = $"{datareader["Brand"]}";
item.description = $"{datareader["Description"]}";
item.ppu = Convert.ToDecimal(datareader["PricePerUnit"]);
item.unitsInStock = Convert.ToInt32(datareader["UnitsInStock"]);
item.picture = $"{datareader["PictureID"]}";
products.Add(item);
}
}
return products;
}
public void UpdateProduct(Product product)
{
// using (var datareader = myCommand.ExecuteReader())
// {
// while (datareader.Read())
// {
// Product item = new Product();
// item.productID = Convert.ToInt32(datareader["ProductID"]);
// item.name = $"{datareader["ProductName"]}";
// item.category = $"{datareader["Category"]}";
// item.size = $"{datareader["Size"]}";
// item.Color = $"{datareader["Color"]}";
// item.brand = $"{datareader["Brand"]}";
// item.description = $"{datareader["Description"]}";
// item.ppu = Convert.ToDecimal(datareader["PricePerUnit"]);
// item.unitsInStock = Convert.ToInt32(datareader["UnitsInStock"]);
// item.picture = $"{datareader["PictureID"]}";
// products.Add(item);
// }
// }
// return products;
//}
//public void UpdateProduct(Product product)
//{

}
public void DeleteProduct(Product product)
{
string sql = "DELETE FROM " + "tblProduct" + " WHERE " + "ProductID" + " = '" + product.productID + "'";
using (connection)
{
connection.Open();
using (SqlCommand command = new SqlCommand(sql, connection))
{
command.ExecuteNonQuery();
}
connection.Close();
}
}
public void AddProduct(Product product)
{
string sql = "INSERT into tblProduct (ProductID,ProductName,Category,Size,Color,Brand,Description,PricePerUnit,UnitsInStock,PictureID) " +
" VALUES ('" + product.productID + "', '" + product.name + "', '" + product.category + "', '" + product.size + "', '" + product.brand + "', '" + product.description + "', '" + product.ppu
+ "', '" + product.unitsInStock + "', '" + product.picture+"');";
//}
//public void DeleteProduct(Product product)
//{
// string sql = "DELETE FROM " + "tblProduct" + " WHERE " + "ProductID" + " = '" + product.productID + "'";
// using (connection)
// {
// connection.Open();
// using (SqlCommand command = new SqlCommand(sql, connection))
// {
// command.ExecuteNonQuery();
// }
// connection.Close();
// }
//}
//public void AddProduct(Product product)
//{
// string sql = "INSERT into tblProduct (ProductID,ProductName,Category,Size,Color,Brand,Description,PricePerUnit,UnitsInStock,PictureID) " +
// " VALUES ('" + product.productID + "', '" + product.name + "', '" + product.category + "', '" + product.size + "', '" + product.brand + "', '" + product.description + "', '" + product.ppu
// + "', '" + product.unitsInStock + "', '" + product.picture+"');";

using (connection)
{
connection.Open();
using (SqlCommand command = new SqlCommand(sql,connection))
{
command.ExecuteNonQuery();
}
connection.Close();
}
}
// using (connection)
// {
// connection.Open();
// using (SqlCommand command = new SqlCommand(sql,connection))
// {
// command.ExecuteNonQuery();
// }
// connection.Close();
// }
//}
}
}
@@ -4,141 +4,140 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Webshop.Models;

namespace DAL
{
class DALUser : DALGeneral
{
List<string> count = new List<string>();

public List<User> GetAllUserBySearch(User user)
{
string sql =
"SELECT " +
"user.UserID" +
"user.Firstname" +
"user.Lastname" +
"user.Username" +
"user.Password" +
"user.StreetAdress" +
"zipcode.ZipcodeID" +
"city.CityID" +
"customergroup.CustomergroupID" +
"user.Admin" +
"FROM tblUser AS user WHERE ";
//public List<User> GetAllUserBySearch(User user)
//{
// string sql =
// "SELECT " +
// "user.UserID" +
// "user.Firstname" +
// "user.Lastname" +
// "user.Username" +
// "user.Password" +
// "user.StreetAdress" +
// "zipcode.ZipcodeID" +
// "city.CityID" +
// "customergroup.CustomergroupID" +
// "user.Admin" +
// "FROM tblUser AS user WHERE ";

if (user.userID != null)
{
if (count.Count > 0)
sql += "AND";
sql += $"UserID = {user.userID}";
}
if (user.firstname != null)
{
if(count.Count > 0 )
sql += "AND";
sql += $"FirstName = {user.firstname}";
// if (user.userID != null)
// {
// if (count.Count > 0)
// sql += "AND";
// sql += $"UserID = {user.userID}";
// }
// if (user.firstname != null)
// {
// if(count.Count > 0 )
// sql += "AND";
// sql += $"FirstName = {user.firstname}";

}
if (user.lastname != null)
{
if (count.Count > 0)
sql += "AND";
sql += $"LastName = {user.lastname}";
}
if (user.username != null)
{
if (count.Count > 0)
sql += "AND";
sql += $"Username = {user.username}";
}
if (user.streetAdress != null)
{
if (count.Count > 0)
sql += "AND";
sql += $"StreetAdress = {user.streetAdress}";
}
if (user.zipcodeID != null)
{
if (count.Count > 0)
sql += "AND";
sql += $"ZipcodeID = {user.zipcodeID}";
}
if (user.cityID != null)
{
if (count.Count > 0)
sql += "AND";
sql += $"CityID = {user.cityID}";
}
if (user.customergroupID != null)
{
if (count.Count > 0)
sql += "AND";
sql += $"CustomergroupID = {user.customergroupID}";
}
// }
// if (user.lastname != null)
// {
// if (count.Count > 0)
// sql += "AND";
// sql += $"LastName = {user.lastname}";
// }
// if (user.username != null)
// {
// if (count.Count > 0)
// sql += "AND";
// sql += $"Username = {user.username}";
// }
// if (user.streetAdress != null)
// {
// if (count.Count > 0)
// sql += "AND";
// sql += $"StreetAdress = {user.streetAdress}";
// }
// if (user.zipcodeID != null)
// {
// if (count.Count > 0)
// sql += "AND";
// sql += $"ZipcodeID = {user.zipcodeID}";
// }
// if (user.cityID != null)
// {
// if (count.Count > 0)
// sql += "AND";
// sql += $"CityID = {user.cityID}";
// }
// if (user.customergroupID != null)
// {
// if (count.Count > 0)
// sql += "AND";
// sql += $"CustomergroupID = {user.customergroupID}";
// }

sql += "INNER JOIN tblZipcode AS zipcode ON user.ZipcodeID = zipcode.ZipcodeID"
+ "INNER JOIN tblCity AS city ON user.CityID = city.CityID"
+ "INNER JOIN tblCustomerGroup AS customergroup ON user.CustomergroupID = customergroup.CustomergroupID";
// sql += "INNER JOIN tblZipcode AS zipcode ON user.ZipcodeID = zipcode.ZipcodeID"
// + "INNER JOIN tblCity AS city ON user.CityID = city.CityID"
// + "INNER JOIN tblCustomerGroup AS customergroup ON user.CustomergroupID = customergroup.CustomergroupID";

List<User> users = new List<User>();
var myCommand = new SqlCommand(sql, connection);
// List<User> users = new List<User>();
// var myCommand = new SqlCommand(sql, connection);

using (var datareader = myCommand.ExecuteReader())
{
while (datareader.Read())
{
User item = new User();
user.userID = Convert.ToInt32(datareader["UserID"]);
user.firstname = $"{datareader["FirstName"]}";
user.lastname = $"{datareader["LastName"]}";
user.username = $"{datareader["Username"]}";
user.streetAdress = $"{datareader["StreetAdress"]}";
user.zipcodeID = Convert.ToInt32(datareader["ZipcodeID"]);
user.cityID = Convert.ToInt32(datareader["CityID"]);
user.customergroupID = Convert.ToInt32(datareader["CustomergroupID"]);
user.admin = Convert.ToBoolean(datareader["Admin"]);
users.Add(item);
}
}
return users;
}
// using (var datareader = myCommand.ExecuteReader())
// {
// while (datareader.Read())
// {
// User item = new User();
// user.userID = Convert.ToInt32(datareader["UserID"]);
// user.firstname = $"{datareader["FirstName"]}";
// user.lastname = $"{datareader["LastName"]}";
// user.username = $"{datareader["Username"]}";
// user.streetAdress = $"{datareader["StreetAdress"]}";
// user.zipcodeID = Convert.ToInt32(datareader["ZipcodeID"]);
// user.cityID = Convert.ToInt32(datareader["CityID"]);
// user.customergroupID = Convert.ToInt32(datareader["CustomergroupID"]);
// user.admin = Convert.ToBoolean(datareader["Admin"]);
// users.Add(item);
// }
// }
// return users;
//}

public void UpdateUser (int user)
{
SqlCommand cmdUpdate = new SqlCommand("UPDATE tblUser SET FirstName = @newFirstName, LastName = @newLastName, Username = @newUsername, Password = @newPsw, StreetAdress = @newStreetAddress WHERE UserID = @UserID", connection);
//public void UpdateUser (int user)
//{
// SqlCommand cmdUpdate = new SqlCommand("UPDATE tblUser SET FirstName = @newFirstName, LastName = @newLastName, Username = @newUsername, Password = @newPsw, StreetAdress = @newStreetAddress WHERE UserID = @UserID", connection);

cmdUpdate.Parameters.AddWithValue("@UserID", user);
// cmdUpdate.Parameters.AddWithValue("@UserID", user);


}
//}

public void InsertUser(User user)
{
connection.Open();
SqlCommand insertUser = new SqlCommand("INSERT INTO tblUser (FirstName, LastName, Username, Password, StreetAdress, ZipcodeID, CityID, CustomergroupID) VALUES(@newFirstName, @newLastName, @newUsername, @newPsw, @newStreetAddress, @newZipcodeID, @newCityID, @newCustomergroupID");
insertUser.Parameters.AddWithValue("@newFirstName", user.firstname);
insertUser.Parameters.AddWithValue("@newLastName", user.lastname);
insertUser.Parameters.AddWithValue("@newUsername", user.username);
insertUser.Parameters.AddWithValue("@newPsw", user.password);
insertUser.Parameters.AddWithValue("@newStreetAddress", user.streetAdress);
insertUser.Parameters.AddWithValue("@newZipcodeID", user.zipcodeID);
insertUser.Parameters.AddWithValue("@newCityID", user.cityID);
insertUser.Parameters.AddWithValue("@newCustomergroupID", user.customergroupID);
insertUser.ExecuteNonQuery();
connection.Close();
//public void InsertUser(User user)
//{
// connection.Open();
// SqlCommand insertUser = new SqlCommand("INSERT INTO tblUser (FirstName, LastName, Username, Password, StreetAdress, ZipcodeID, CityID, CustomergroupID) VALUES(@newFirstName, @newLastName, @newUsername, @newPsw, @newStreetAddress, @newZipcodeID, @newCityID, @newCustomergroupID");
// insertUser.Parameters.AddWithValue("@newFirstName", user.firstname);
// insertUser.Parameters.AddWithValue("@newLastName", user.lastname);
// insertUser.Parameters.AddWithValue("@newUsername", user.username);
// insertUser.Parameters.AddWithValue("@newPsw", user.password);
// insertUser.Parameters.AddWithValue("@newStreetAddress", user.streetAdress);
// insertUser.Parameters.AddWithValue("@newZipcodeID", user.zipcodeID);
// insertUser.Parameters.AddWithValue("@newCityID", user.cityID);
// insertUser.Parameters.AddWithValue("@newCustomergroupID", user.customergroupID);
// insertUser.ExecuteNonQuery();
// connection.Close();


}
//}

public void DeleteUser(User user)
{
connection.Open();
SqlCommand deleteUser = new SqlCommand("DELETE FROM tblUser WHERE UserID = @UserID", connection);
deleteUser.Parameters.AddWithValue("@UserID", user);
deleteUser.ExecuteNonQuery();
}
//public void DeleteUser(User user)
//{
// connection.Open();
// SqlCommand deleteUser = new SqlCommand("DELETE FROM tblUser WHERE UserID = @UserID", connection);
// deleteUser.Parameters.AddWithValue("@UserID", user);
// deleteUser.ExecuteNonQuery();
//}

}
}
@@ -6,6 +6,7 @@
using Webshop;
using DAL;
using BLL;
using BLL.Models;
using System.Data.SqlClient;

namespace Databasetestoutput
@@ -14,43 +15,15 @@ class Program
{
static void Main(string[] args)
{
var connection = new SqlConnection();
connection.ConnectionString = @"Data source=217.210.151.153,1433; Network Library=DBMSSOCN; Initial Catalog=Webbshop; User ID = guest; Password=temppass22;";
connection.Open();
string sql =
"SELECT " +
"prod.ProductName, " +
"category.Category, " +
"size.Size, " +
"color.Color, " +
"brand.Brand, " +
"prod.Description, " +
"prod.PricePerUnit, " +
"prod.UnitsInStock, " +
"prod.PictureID " +
"from tblProduct AS prod ";
var tempbll = new BLLProduct();
var product = new Product();
product.ppu = 10;
List<Product> products = tempbll.SearchProduct(product);

sql += "INNER JOIN tblCategory AS category ON prod.CategoryID = category.CategoryID " +
"INNER JOIN tblColor AS color ON color.ColorID = prod.ColorID " +
"INNER JOIN tblSize AS size ON size.SizeID = prod.SizeID " +
"INNER JOIN tblBrand AS brand ON brand.BrandID = prod.BrandID";

List<string> tempname = new List<string>();
var myCommand = new SqlCommand(sql, connection);
using (var datareader = myCommand.ExecuteReader())
{
while (datareader.Read())
{
tempname.Add($"{datareader["ProductName"]}, {datareader["Category"]},{datareader["Size"]},{datareader["Color"]},{datareader["Brand"]},{ datareader["Description"]},{datareader["PricePerUnit"]},{datareader["UnitsInStock"]},{datareader["PictureID"]}");
}
}

foreach (var item in tempname)
foreach (var item in products)
{
Console.WriteLine(item);
Console.WriteLine(item.ppu);
}
connection.Close();
Console.ReadKey();
}
}
}