Skip to content

Commit

Permalink
seo, categories, tags, etc added
Browse files Browse the repository at this point in the history
  • Loading branch information
maqndon authored and Marcel Caraballo committed Sep 11, 2024
1 parent f138e6c commit 0c2880b
Showing 1 changed file with 63 additions and 5 deletions.
68 changes: 63 additions & 5 deletions app/Http/Controllers/Api/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,29 @@ public function store(Request $request)
'related_parametric' => 'string',
'downloads' => 'numeric',
'created_at' => 'required|date',
'printing_materials' => 'array',
'printing_materials.*.id' => 'numeric',
'print_settings' => 'array',
'print_settings.*.id' => 'numeric',
'bill_of_materials' => 'array',
'bill_of_materials.*.qty' => 'integer',
'bill_of_materials.*.item' => 'string|max:255',
'files' => 'array',
'files.*.files_names' => 'array',
'files.*.files_names.*' => 'string',
'files.*.metadata' => 'string|max:255',
'images' => 'array',
'images.*.images_names' => 'array',
'images.*.images_names.*' => 'string',
'images.*.metadata' => 'string|max:255',
'seos' => 'array',
'seos.*.title' => 'string|unique:seos,title',
'seos.*.meta_description.*' => 'string',
'seos.*.meta_keywords' => 'array',
'categories' => 'array',
'categories.*.category_id' => 'numeric',
'tags' => 'array',
'tags.*.tag_id' => 'numeric'
]);

try {
Expand All @@ -84,31 +100,73 @@ public function store(Request $request)
'downloads' => $request->input('downloads'),
'created_at' => $request->input('created_at'),
]);


// add printing materials
if ($request->printing_materials) {
foreach ($request->input('print_settings') as $print_setting) {
$product->print_settings()->attach($print_setting);
}
}

// add print settings
if ($request->printing_materials) {
foreach ($request->input('printing_materials') as $printing_material) {
$product->printing_materials()->attach($printing_material);
}
}

// add bill of materials
if ($request->bill_of_materials) {
foreach ($request->input('bill_of_materials') as $bom) {
$product->bill_of_materials()->create($bom);
}
}

// add product files
if ($request->input('files')) {
foreach ($request->input('files') as $fileData) {
$product->files()->create($fileData);
}
}


// add product images
if ($request->input('images')) {
foreach ($request->input('images') as $image) {
$product->images()->create($image);
}
}

// add product seo
if ($request->input('seos')) {
foreach ($request->input('seos') as $seo) {
$product->seos()->create($seo);
}
}

// add product categories
if ($request->input('categories')) {
foreach ($request->input('categories') as $category) {
$product->categories()->sync($category);
}
}

// add product tags
if ($request->input('tags')) {
foreach ($request->input('categories') as $tag) {
$product->tags()->sync($tag);
}
}

return response()->json([
'message' => 'Product ' . $request->title . ' Created Successfully',
], 201);
} catch (\Throwable $th) {
#\Log::error('Error creating product: ' . $th->getMessage());
\Log::error('Error creating product: ' . $th->getMessage());

return response()->json([
'message' => 'Product could not be Created Successfully',
], 500);
}

}

/**
Expand Down

0 comments on commit 0c2880b

Please sign in to comment.