I'm working on the laravel pattern, I will have questions in a few places.
I am doing a simple insert operation
PostController
public function store(StorePostRequest $request): JsonResponse
{
return $this->postService->store($request);
}
PostService
public function store(StorePostRequest $request): JsonResponse
{
DB::beginTransaction();
try {
$post = new Post();
$post->uuid = Str::uuid();
$post->title = $request->input('title');
$post->description = $request->input('description');
$post->status_id = $request->input('status_id');
$post->created_by_user_id = Auth::id();
$this->postRepository->store($post);
DB::commit();
return response()->json(['status' => true, __('service.the_operation_was_successful')]);
} catch (Exception $exception) {
DB::rollBack();
return response()->json(['status' => false, __('service.error_occurred_during_operation')], 500);
}
}
PostRepository
public function store(Post $post): Post
{
$post->save();
return $post;
}
My main question is: Should I move the try cacth block to the controller side? To make the service side look cleaner. If I move, how can I do DB::beginTransaction() transactions, can you help and share your ideas?
Aucun commentaire:
Enregistrer un commentaire