Skip to content

Commit

Permalink
AllProducts -> image to show
Browse files Browse the repository at this point in the history
  • Loading branch information
Ciframa committed Jul 9, 2024
1 parent 44c0a96 commit a19b5b6
Show file tree
Hide file tree
Showing 9 changed files with 445 additions and 200 deletions.
97 changes: 59 additions & 38 deletions Fitmo_backend/app/Http/Controllers/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,44 @@ class ProductController extends Controller
*
* @return \Illuminate\Http\Response
*/
public function index()
public function index(Request $request)
{
$products = Product::select(
'prices.*',
'product_states.*',
'categories.name as category_name',
'colors.*',
'prices.*',
'product_states.*',
'categories.name as category_name',
'colors.*',
'products.*',
'product_categories.category_id as category_id'
)->with('categories')
->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')
)->with(['categories', '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')
->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')
->orderBy('products.created_at', 'desc')
// ->paginated(20)
->get();
->where('products.parent_id', 0)
->paginate(20, ['*'], 'page', $request->page ?? 1);


$products = $this->formatProducts($products);
$products = $this->formatProducts($products, true);
return $products;
}

Expand All @@ -53,7 +70,7 @@ public function getSingleProduct($product_url_name)
->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')
->join('categories', 'categories.id', '=', 'product_categories.category_id')
->join('map_table', 'product_categories.category_id', '=', 'map_table.category_id')
->leftJoin('colors', 'products.color_id', '=', 'colors.id')
->orderBy('products.created_at', "desc")
Expand Down Expand Up @@ -111,12 +128,16 @@ public function getSearchedItems($query)

public function getAllProducts()
{
return Product::select('colors.*', 'prices.*', 'product_states.*', 'products.*')
$products = Product::select('colors.*', 'prices.*', 'product_states.*', 'products.*')
->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);
}
return $products;
}

public function setUrlNames()
Expand All @@ -134,28 +155,28 @@ public function getMainProducts()
return Product::select('categories.*', 'products.*')->join('product_categories', 'products.id', '=', 'product_categories.product_id')
->join('categories', 'categories.id', '=', 'product_categories.category_id')->orderBy("products.name")->where("products.parent_id", 0)->with('categories')->get();
}
public function formatProducts($products)
public function formatProducts($products, $shouldWorkWithChildren = false)
{
$newProducts = [];
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->isActive = 0;
if ($product["parent_id"] === 0) {
$product->isActive = 1;
$temp[] = $product;

foreach ($products as $tempProduct) {
if ($product["id"] === $tempProduct["parent_id"]) {
$temp[] = $tempProduct;
}
}
$newProducts[] = $temp;
}
}
$temp = [];
$product->isActive = 1; // Activate the product
$temp[] = $product; // Add the product to the array
// Format parent product
$product->image_urls = explode(',', $product->image_urls);
$product->categoryPath = str_replace([","], "", Str::ascii(Str::kebab(strtolower(($product["category_name"])))));

// Check if the product has children and format them
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->isActive = 0; // Activate the product
$temp[] = $child;
}
}
$newProducts[] = $temp;
}

return $newProducts;
}
Expand Down Expand Up @@ -216,7 +237,7 @@ public function getCategoryProducts($categoryName)
->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')
->join('product_categories', 'products.id', '=', 'product_categories.product_id')
->join('categories', 'categories.id', '=', 'product_categories.category_id')
->join('categories', 'categories.id', '=', 'product_categories.category_id')
->join('product_states', 'products.id', '=', 'product_states.product_id')
->leftJoin('colors', 'products.color_id', '=', 'colors.id')
->whereIn('category_id', $categoriesToSearch)
Expand Down Expand Up @@ -331,7 +352,7 @@ public function store(Request $request)
$newProduct->name = $request->name;
$newProduct->description = $request->subName;
$newProduct->variant = $request->variant;

$newProduct->parent_id = $request->parent["id"];
$newProduct->url_name = str_replace(" ", "-", (strtolower(str_replace([","], "", Str::ascii($request["name"])))));
if ($request->colors["colorName"]) {
Expand All @@ -347,13 +368,13 @@ public function store(Request $request)
$newProductCategories->category_id = $category["categoryId"];
$newProductCategories->save();
}

}
else{
foreach ($request->parent["categories"] as $category) {
$newProductCategories = new ProductCategory();
$newProductCategories->product_id = $newProduct->id;
$newProductCategories->category_id = $category["id"];
$newProductCategories->category_id = $category["id"];
$newProductCategories->save();
}
}
Expand Down
6 changes: 5 additions & 1 deletion Fitmo_backend/app/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class Product extends Model
{
use HasFactory;

public function orders()
{
return $this->belongsToMany(Order::class)->withTimestamps();
Expand All @@ -24,4 +24,8 @@ public function color()
{
return $this->belongsTo(Color::class);
}
public function children()
{
return $this->hasMany(Product::class, 'parent_id');
}
}
88 changes: 88 additions & 0 deletions Fitmo_frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Fitmo_frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"vue3-quill": "^0.3.1",
"vue3-snackbar": "^2.2.0",
"vuedraggable": "^4.1.0",
"vuestic-ui": "^1.9.12",
"vuex": "^4.1.0",
"yup": "^1.3.2"
},
Expand Down
1 change: 0 additions & 1 deletion Fitmo_frontend/src/components/Product.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"
alt=""
/>

<img
v-if="product.image_urls[1] && product.color_id === null"
:src="
Expand Down
Loading

0 comments on commit a19b5b6

Please sign in to comment.