use element update route

This commit is contained in:
Yisroel Baum 2026-06-02 16:33:18 +03:00
parent 9cb968afd8
commit 1d9e2cff0a
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
4 changed files with 26 additions and 15 deletions

View file

@ -55,7 +55,7 @@ class ElementController
], 200); ], 200);
} }
public function update(?int $id, Request $request): JsonResponse public function update(Request $request): JsonResponse
{ {
$iconImage = $this->uploadedFileInput($request, 'iconImage'); $iconImage = $this->uploadedFileInput($request, 'iconImage');
$pdf = $this->uploadedFileInput($request, 'pdf'); $pdf = $this->uploadedFileInput($request, 'pdf');
@ -63,7 +63,7 @@ class ElementController
try { try {
$element = $this->updateElement->execute( $element = $this->updateElement->execute(
new UpdateElementRequest( new UpdateElementRequest(
id: $id, id: $this->intInput($request, 'elementId'),
title: $this->stringInput($request, 'title'), title: $this->stringInput($request, 'title'),
description: $this->stringInput($request, 'description'), description: $this->stringInput($request, 'description'),
iconImageUrl: $this->stringInput( iconImageUrl: $this->stringInput(
@ -98,6 +98,20 @@ class ElementController
], 200); ], 200);
} }
private function intInput(Request $request, string $key): ?int
{
$value = $request->input($key);
if (is_int($value)) {
return $value;
}
if (is_string($value) && ctype_digit($value)) {
return (int) $value;
}
return null;
}
private function stringInput(Request $request, string $key): ?string private function stringInput(Request $request, string $key): ?string
{ {
$value = $request->input($key); $value = $request->input($key);

View file

@ -12,5 +12,5 @@ Route::get('/me', [AuthController::class, 'me'])
->middleware(AuthMiddleware::class); ->middleware(AuthMiddleware::class);
Route::get('/sets', [SetController::class, 'index']); Route::get('/sets', [SetController::class, 'index']);
Route::get('/elements/{id}', [ElementController::class, 'show']); Route::get('/elements/{id}', [ElementController::class, 'show']);
Route::post('/elements/{id}', [ElementController::class, 'update']) Route::post('/element/update', [ElementController::class, 'update'])
->middleware(AuthMiddleware::class); ->middleware(AuthMiddleware::class);

View file

@ -179,7 +179,7 @@ class ElementControllerTest extends TestCase
null, null,
true, true,
), ),
]); ], ['REQUEST_METHOD' => 'POST']);
$response = $this->controller->update($request); $response = $this->controller->update($request);
@ -220,7 +220,7 @@ class ElementControllerTest extends TestCase
null, null,
true, true,
), ),
]); ], ['REQUEST_METHOD' => 'POST']);
$response = $this->controller->update($request); $response = $this->controller->update($request);

View file

@ -44,6 +44,7 @@ interface ErrorResponse {
} }
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL as string const API_BASE_URL = import.meta.env.VITE_API_BASE_URL as string
const ELEMENT_UPDATE_URL = `${API_BASE_URL}/api/element/update`
export const useElementsStore = defineStore('elements', () => { export const useElementsStore = defineStore('elements', () => {
const element = ref<Element | null>(null) const element = ref<Element | null>(null)
@ -109,13 +110,11 @@ export const useElementsStore = defineStore('elements', () => {
isSaving.value = true isSaving.value = true
try { try {
const encodedElementId = encodeURIComponent(elementId) const response = await fetch(ELEMENT_UPDATE_URL, {
const elementUrl = `${API_BASE_URL}/api/elements/${encodedElementId}`
const response = await fetch(elementUrl, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
credentials: 'include', credentials: 'include',
body: JSON.stringify(input), body: JSON.stringify({ elementId, ...input }),
}) })
return await handleElementResponse(response, failureMessage) return await handleElementResponse(response, failureMessage)
@ -132,11 +131,10 @@ export const useElementsStore = defineStore('elements', () => {
isUploadingIconImage.value = true isUploadingIconImage.value = true
try { try {
const encodedElementId = encodeURIComponent(elementId)
const elementUrl = `${API_BASE_URL}/api/elements/${encodedElementId}`
const formData = new FormData() const formData = new FormData()
formData.append('elementId', elementId)
formData.append('iconImage', file) formData.append('iconImage', file)
const response = await fetch(elementUrl, { const response = await fetch(ELEMENT_UPDATE_URL, {
method: 'POST', method: 'POST',
headers: { Accept: 'application/json' }, headers: { Accept: 'application/json' },
credentials: 'include', credentials: 'include',
@ -157,11 +155,10 @@ export const useElementsStore = defineStore('elements', () => {
isUploadingPdf.value = true isUploadingPdf.value = true
try { try {
const encodedElementId = encodeURIComponent(elementId)
const elementUrl = `${API_BASE_URL}/api/elements/${encodedElementId}`
const formData = new FormData() const formData = new FormData()
formData.append('elementId', elementId)
formData.append('pdf', file) formData.append('pdf', file)
const response = await fetch(elementUrl, { const response = await fetch(ELEMENT_UPDATE_URL, {
method: 'POST', method: 'POST',
headers: { Accept: 'application/json' }, headers: { Accept: 'application/json' },
credentials: 'include', credentials: 'include',