Keystore/app/Http/Controllers/StoresController.php
Helge-Mikael Nordgård a6a0a0a506 first commit
2025-02-05 03:05:01 +01:00

142 lines
5.1 KiB
PHP

<?php
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 StoresController extends Controller
{
public function getValueByKey($post, $key)
{
// Retrieve the post by the 'body' column (UUID)
$post = Post::where('body', $post)->first();
// If the post is not found, return a 404 response
if (!$post) {
return response()->json(['message' => 'Post not found'], Response::HTTP_NOT_FOUND);
}
// Retrieve the store record by post_id and key
$store = Store::where('post_id', $post->id)
->where('key', $key)
->first();
// If the store is not found, return a 404 response
if (!$store) {
return response()->json(['message' => 'Store with this key not found for this post'], Response::HTTP_NOT_FOUND);
}
// Return the value of the key
return response()->json(['value' => $store->values], Response::HTTP_OK);
}
public function storeKeyValue(Request $request, $post) {
// Validate the request body
$validator = Validator::make($request->all(), [
'key' => 'required|alpha_num', // Ensure the key is alphanumeric
'value' => 'required|json', // Ensure the value is valid JSON
]);
// If validation fails, return a 422 Unprocessable Entity response
if ($validator->fails()) {
return response()->json(['errors' => $validator->errors()], Response::HTTP_UNPROCESSABLE_ENTITY);
}
// Retrieve the post by the 'body' column (UUID)
$post = Post::where('body', $post)->first();
// If the post is not found, return a 404 response
if (!$post) {
return response()->json(['message' => 'Post not found'], Response::HTTP_NOT_FOUND);
}
// Check if the key already exists for this post
$existingStore = Store::where('post_id', $post->id)
->where('key', $request->key)
->first();
if ($existingStore) {
// Return a 409 Conflict response if the key already exists
return response()->json(['message' => 'Key already exists for this post'], Response::HTTP_CONFLICT);
}
// Create a new store record
$store = Store::create([
'post_id' => $post->id,
'key' => $request->key,
'values' => $request->value,
]);
// Return a 201 Created response with the created store record
return response()->json(['message' => 'Key and value stored successfully', 'store' => $store], Response::HTTP_CREATED);
}
public function updateKeyValue(Request $request, $post, $key) {
// Validate the request body
$validator = Validator::make($request->all(), [
'value' => 'required|json', // Ensure the value is valid JSON
]);
// If validation fails, return a 422 Unprocessable Entity response
if ($validator->fails()) {
return response()->json(['errors' => $validator->errors()], Response::HTTP_UNPROCESSABLE_ENTITY);
}
// Retrieve the post by the 'body' column (UUID)
$post = Post::where('body', $post)->first();
// If the post is not found, return a 404 response
if (!$post) {
return response()->json(['message' => 'Post not found'], Response::HTTP_NOT_FOUND);
}
// Retrieve the store record by post_id and key
$store = Store::where('post_id', $post->id)
->where('key', $key)
->first();
// If the key is not found for the given post, return a 404 response
if (!$store) {
return response()->json(['message' => 'Key not found for this post'], Response::HTTP_NOT_FOUND);
}
// Update the value for the key
$store->values = $request->value;
$store->save();
// Return a 200 OK response with the updated store record
return response()->json(['message' => 'Value updated successfully', 'store' => $store], Response::HTTP_OK);
}
public function deleteStoreByKey($post, $key) {
// Retrieve the post by the 'body' column (UUID)
$post = Post::where('body', $post)->first();
// If the post is not found, return a 404 response
if (!$post) {
return response()->json(['message' => 'Post not found'], Response::HTTP_NOT_FOUND);
}
// Retrieve the store record by post_id and key
$store = Store::where('post_id', $post->id)
->where('key', $key)
->first();
// If the store is not found, return a 404 response
if (!$store) {
return response()->json(['message' => 'Store with this key not found for this post'], Response::HTTP_NOT_FOUND);
}
// Delete the store record
$store->delete();
// Return a 200 OK response with a success message
return response()->json(['message' => 'Store deleted successfully'], Response::HTTP_OK);
}
}