Added post store routine
This commit is contained in:
parent
563673f654
commit
814df6cdca
|
|
@ -2,10 +2,12 @@
|
|||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
|
||||
use App\Models\Post;
|
||||
use App\Models\Store;
|
||||
|
||||
class PostsController extends Controller
|
||||
{
|
||||
|
|
@ -71,4 +73,37 @@ class PostsController extends Controller
|
|||
// Return a 200 OK response with a success message
|
||||
return response()->json(['message' => 'Post deleted successfully'], Response::HTTP_OK);
|
||||
}
|
||||
|
||||
public function createOrUpdatePostStore(Request $request) {
|
||||
// Validate the input
|
||||
$validator = Validator::make($request->all(), [
|
||||
'body' => 'required|uuid', // Ensure body is a valid UUID
|
||||
'key' => 'required|string|max:255', // Ensure key is a string
|
||||
'value' => 'required|json', // Ensure value is a valid JSON string
|
||||
]);
|
||||
|
||||
// If validation fails, return a 422 response with errors
|
||||
if ($validator->fails()) {
|
||||
return response()->json(['errors' => $validator->errors()], Response::HTTP_UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
|
||||
// Retrieve or create the post
|
||||
$post = Post::firstOrCreate(
|
||||
['body' => $request->body], // Search by body (UUID)
|
||||
['body' => $request->body] // Create data if not exists
|
||||
);
|
||||
|
||||
// Retrieve or create the store
|
||||
$store = Store::updateOrCreate(
|
||||
['post_id' => $post->id, 'key' => $request->key], // Search by post_id and key
|
||||
['values' => $request->value] // Create or update data
|
||||
);
|
||||
|
||||
// Return a success response
|
||||
return response()->json([
|
||||
'message' => 'Post and store processed successfully',
|
||||
'post' => $post,
|
||||
'store' => $store,
|
||||
], Response::HTTP_OK);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ Route::get('/user', function (Request $request) {
|
|||
Route::middleware(['api_key'])->group(function () {
|
||||
Route::get('/get/{post}/fields', [PostsController::class, 'getFields'])->name('api.get.fields');
|
||||
Route::post('/store/post', [PostsController::class, 'storePost'])->name('api.store.post');
|
||||
Route::post('/post/store', [PostsController::class, 'createOrUpdatePostStore'])->name('api.post.store');
|
||||
Route::delete('/delete/post/{post}', [PostsController::class, 'deletePost'])->name('api.delete.post');
|
||||
|
||||
Route::get('/post/{post}/store/{key}', [StoresController::class, 'getValueByKey'])->name('api.get.value.by.key');
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user