feat: meal post ingredients
This commit is contained in:
@@ -13,6 +13,7 @@ use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class MealPostController extends Controller
|
||||
{
|
||||
@@ -66,12 +67,26 @@ class MealPostController extends Controller
|
||||
|
||||
public function store(MealPostsRequest $request): JsonResponse
|
||||
{
|
||||
$mealPost = MealPosts::create([
|
||||
...$this->getValidatedDataWithImageUrl($request),
|
||||
'user_id' => $request->user()->getKey(),
|
||||
]);
|
||||
$data = $this->getValidatedDataWithImageUrl($request);
|
||||
$ingredients = $data['ingredients'] ?? [];
|
||||
|
||||
unset($data['ingredients']);
|
||||
|
||||
$mealPost = DB::transaction(function () use ($data, $ingredients, $request): MealPosts {
|
||||
$mealPost = MealPosts::create([
|
||||
...$data,
|
||||
'user_id' => $request->user()->getKey(),
|
||||
]);
|
||||
|
||||
if ($ingredients !== []) {
|
||||
$mealPost->ingredients()->createMany($ingredients);
|
||||
}
|
||||
|
||||
return $mealPost;
|
||||
});
|
||||
|
||||
$mealPost->setRelation('user', $request->user());
|
||||
$mealPost->loadMissing('ingredients');
|
||||
|
||||
return (new MealPostsResource($mealPost))
|
||||
->response()
|
||||
@@ -82,16 +97,36 @@ class MealPostController extends Controller
|
||||
{
|
||||
$this->abortIfNotOwner($request, $mealPost);
|
||||
|
||||
return new MealPostsResource($mealPost->loadMissing('user:id,name,avatar_url'));
|
||||
return new MealPostsResource($mealPost->loadMissing('user:id,name,avatar_url', 'ingredients'));
|
||||
}
|
||||
|
||||
public function update(MealPostsRequest $request, MealPosts $mealPost): MealPostsResource
|
||||
{
|
||||
$this->abortIfNotOwner($request, $mealPost);
|
||||
|
||||
$mealPost->update($this->getValidatedDataWithImageUrl($request));
|
||||
$data = $this->getValidatedDataWithImageUrl($request);
|
||||
$shouldSyncIngredients = array_key_exists('ingredients', $data);
|
||||
$ingredients = $data['ingredients'] ?? [];
|
||||
|
||||
return new MealPostsResource($mealPost->refresh()->loadMissing('user:id,name,avatar_url'));
|
||||
unset($data['ingredients']);
|
||||
|
||||
$mealPost = DB::transaction(function () use ($data, $ingredients, $mealPost, $shouldSyncIngredients): MealPosts {
|
||||
if ($data !== []) {
|
||||
$mealPost->update($data);
|
||||
}
|
||||
|
||||
if ($shouldSyncIngredients) {
|
||||
$mealPost->ingredients()->delete();
|
||||
|
||||
if ($ingredients !== []) {
|
||||
$mealPost->ingredients()->createMany($ingredients);
|
||||
}
|
||||
}
|
||||
|
||||
return $mealPost->refresh();
|
||||
});
|
||||
|
||||
return new MealPostsResource($mealPost->loadMissing('user:id,name,avatar_url', 'ingredients'));
|
||||
}
|
||||
|
||||
public function destroy(Request $request, MealPosts $mealPost): Response
|
||||
|
||||
Reference in New Issue
Block a user