Skip to content

Commit

Permalink
AllProducts fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Ciframa committed Jul 12, 2024
1 parent ed3db2b commit 429c7b8
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 141 deletions.
126 changes: 85 additions & 41 deletions Fitmo_backend/app/Http/Controllers/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,20 +127,47 @@ public function getSearchedItems($query)
return $this->formatProducts($matchedProducts);
}


public function getAllProducts()
{
$products = Product::select('colors.*', 'prices.*', 'product_states.*', 'products.*')
$products = Product::select(
// 'prices.*',
// 'product_states.*',
'categories.name as category_name',
'colors.*',
'products.*',
'product_categories.category_id as category_id'
)->with(['children' => function ($query) {
$query->select(
// 'prices.*',
// 'product_states.*',
'categories.name as category_name',
'colors.*',
'products.*',
// 'product_categories.category_id as category_id'
)
// ->leftJoin('prices', 'products.id', '=', 'prices.product_id')
// ->join('product_states', 'products.id', '=', 'product_states.product_id')
->join('product_categories', 'products.id', '=', 'product_categories.product_id')
->join('categories', 'categories.id', '=', 'product_categories.category_id')
->leftJoin('colors', 'products.color_id', '=', 'colors.id')
->selectRaw('(SELECT GROUP_CONCAT(image_path) FROM images WHERE images.product_id = products.id AND images.is_main = 1) AS image_urls')
->where('products.parent_id', '!=', 0);
}])
// ->leftJoin('prices', 'products.id', '=', 'prices.product_id')
// ->join('product_states', 'products.id', '=', 'product_states.product_id')
->join('product_categories', 'products.id', '=', 'product_categories.product_id')
->join('categories', 'categories.id', '=', 'product_categories.category_id')
->leftJoin('colors', 'products.color_id', '=', 'colors.id')
->selectRaw('(SELECT GROUP_CONCAT(image_path) FROM images WHERE images.product_id = products.id AND images.is_main = 1) AS image_urls')
->leftJoin('prices', 'products.id', '=', 'prices.product_id')
->leftJoin('colors', 'products.color_id', '=', 'colors.id')
->join('product_states', 'products.id', '=', 'product_states.product_id')
->get();
foreach ($products as $product) {
$product->image_urls = explode(',', $product->image_urls);
}
->orderBy('products.name', 'asc')
->where('products.parent_id', 0)->get();

$products = $this->formatOnlyPhotos($products);
return $products;
}


public function setUrlNames()
{
$products = Product::all();
Expand All @@ -158,39 +185,56 @@ public function getMainProducts()
}

public function formatProducts($products, $shouldWorkWithChildren = false)
{
foreach ($products as $product) {
$product->image_urls = explode(',', $product->image_urls);
$product->categoryPath = str_replace([","], "", Str::ascii(Str::kebab(strtolower(($product["category_name"])))));
}
$newProducts = [];
foreach ($products as $product) {
$temp = [];
$product->isMain = 0;
if ($product["parent_id"] === 0) {
$product->isMain = 1;
$temp[] = $product;

foreach ($products as $tempProduct) {
if ($product["id"] === $tempProduct["parent_id"]) {
$temp[] = $tempProduct;
}
{
foreach ($products as $product) {
$product->image_urls = explode(',', $product->image_urls);
$product->categoryPath = str_replace([","], "", Str::ascii(Str::kebab(strtolower(($product["category_name"])))));
}
$newProducts = [];
foreach ($products as $product) {
$temp = [];
$product->isMain = 0;
if ($product["parent_id"] === 0) {
$product->isMain = 1;
$temp[] = $product;

foreach ($products as $tempProduct) {
if ($product["id"] === $tempProduct["parent_id"]) {
$temp[] = $tempProduct;
}
if ($shouldWorkWithChildren && $product->children->isNotEmpty()) {
foreach ($product->children as $child) {
$child->image_urls = explode(',', $child->image_urls);
$child->categoryPath = str_replace([","], "", Str::ascii(Str::kebab(strtolower(($child["category_name"])))));
$child->isMain = 0; // Activate the product
$temp[] = $child;
}
}
$newProducts[] = $temp;
}
if ($shouldWorkWithChildren && $product->children->isNotEmpty()) {
foreach ($product->children as $child) {
$child->image_urls = explode(',', $child->image_urls);
$child->categoryPath = str_replace([","], "", Str::ascii(Str::kebab(strtolower(($child["category_name"])))));
$child->isMain = 0; // Activate the product
$temp[] = $child;
}
}
$newProducts[] = $temp;
}
}

return $newProducts;
}

public function formatOnlyPhotos($products)
{
foreach ($products as $product) {
$product->image_urls = explode(',', $product->image_urls);
$product->categoryPath = str_replace([","], "", Str::ascii(Str::kebab(strtolower(($product["category_name"])))));

return $newProducts;
if ($product->children->isNotEmpty()) {
foreach ($product->children as $child) {
$child->image_urls = explode(',', $child->image_urls);
$child->categoryPath = str_replace([","], "", Str::ascii(Str::kebab(strtolower(($child["category_name"])))));
}
}
}

return $products;
}

public function getProductById($id)
{

Expand Down Expand Up @@ -554,7 +598,7 @@ public function update(Request $request, $id)
$productOriginalVariant = $product->variant;
$product->name = $request->name;
$product->description = $request->description;
$product->category_id = $request->category_id;
// $product->category_id = $request->category_id;
$product->parent_id = $request->parent_id;

$product->variant = $request->variant;
Expand Down Expand Up @@ -637,11 +681,11 @@ public function update(Request $request, $id)

rename($folderPath, $path);
}
if ($request->parent["category_id"] != 0) {

$product->category_id = $request->parent["category_id"];
Product::where('parent_id', $product->id)->update(['category_id' => $request->parent["category_id"]]);
}
// if ($request->parent["category_id"] != 0) {
//
// $product->category_id = $request->parent["category_id"];
// Product::where('parent_id', $product->id)->update(['category_id' => $request->parent["category_id"]]);
// }
$this->setUrlNames();
return $product->save();
}
Expand Down
1 change: 0 additions & 1 deletion Fitmo_frontend/src/components/admin/AddProduct.vue
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,6 @@ export default {
},
})
.then((response) => {
console.log(response);
if (response.status == 200 || response.status == 201) {
this.$snackbar.add({
type: "success",
Expand Down
Loading

0 comments on commit 429c7b8

Please sign in to comment.