Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace App\Http\Api\MedicationHistory\Actions;

use App\Http\Api\Common\Responder\ApiErrorResponder;
use App\Http\Api\Common\Responder\EmptyResponder;
use App\Http\Controllers\Controller;
use App\Services\MedicationHistoryService;
use Domain\MedicationHistory\MedicationHistoryId;
use OpenApi\Attributes\Delete;
use OpenApi\Attributes\JsonContent;
use OpenApi\Attributes\Parameter;
use OpenApi\Attributes\Response;
use OpenApi\Attributes\Schema;

#[Delete(
path: '/api/medication_histories/{id}',
summary: '服薬履歴を削除する',
tags: ['MedicationHistory'],
parameters: [
new Parameter(
name: 'id',
in: 'path',
required: true,
schema: new Schema(
type: 'integer'
)
)
],
responses: [
new Response(
response: '200',
description: 'success',
content: new JsonContent(
ref: '#/components/schemas/base_responder'
)
),
new Response(
response: '404',
description: 'not found',
content: new JsonContent(
ref: '#/components/schemas/api_error_responder'
)
),
]
)]
class DeleteMedicationHistoryAction extends Controller
{
public function __construct(private MedicationHistoryService $medicationHistoryService)
{
parent::__construct();
}

public function __invoke(int $id): EmptyResponder|ApiErrorResponder
{
$result = $this->medicationHistoryService->delete(new MedicationHistoryId($id));

if ($result->isFailed()) {
return new ApiErrorResponder($result->getError());
}

return new EmptyResponder();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ public function __invoke(int $id, UpdateMedicationHistoryRequest $request): Upda
{
$existing = $this->medicationHistoryRepository->get(new MedicationHistoryId($id));

$medicationDate = $request->getMedicationDate() ?? $existing->getMedicationDate();

$medicationHistory = new MedicationHistory(
$existing->getId(),
$existing->getUserId(),
$existing->getDrugId(),
$request->getAmount(),
$existing->getMedicationDate(),
$medicationDate,
$request->getNote(),
$existing->getCreatedAt(),
UpdatedAt::now(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Http\Api\Shered\Request\ApiRequest;
use Domain\MedicationHistory\Amount;
use Domain\MedicationHistory\MedicationDate;
use Domain\MedicationHistory\MedicationNote;
use OpenApi\Attributes\Property;
use OpenApi\Attributes\Schema;
Expand All @@ -15,6 +16,7 @@
properties: [
new Property(property: 'amount', type: 'number', format: 'double'),
new Property(property: 'note', type: 'string', nullable: true),
new Property(property: 'medication_date', type: 'string', format: 'date-time', nullable: true),
]
)]
class UpdateMedicationHistoryRequest extends ApiRequest
Expand All @@ -29,6 +31,7 @@ public function rules(): array
return [
'amount' => 'required|numeric',
'note' => 'nullable|string',
'medication_date' => 'nullable|date',
];
}

Expand All @@ -41,4 +44,13 @@ public function getNote(): MedicationNote
{
return new MedicationNote($this->input('note'));
}

public function getMedicationDate(): ?MedicationDate
{
$value = $this->input('medication_date');
if (is_null($value)) {
return null;
}
return new MedicationDate($value);
}
}
15 changes: 15 additions & 0 deletions app/Services/MedicationHistoryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,21 @@ public function updateMedicationHistory(MedicationHistory $medicationHistory): M
return $this->medicationHistoryDomainService->update($medicationHistory);
}

/**
* @param MedicationHistoryId $id
* @return ServiceResult<null>
*/
public function delete(MedicationHistoryId $id): ServiceResult
{
try {
$this->medicationHistoryDomainService->delete($id);

return ServiceResult::success(null);
} catch (NotFoundException) {
return ServiceResult::fail(ServiceError::NotFound);
}
}

/**
* @param DrugId $drugId
* @param UserId $userId
Expand Down
2 changes: 2 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Http\Api\Drug\Actions\GetDrugListAction;
use App\Http\Api\Drug\Actions\UpdateDrugAction;
use App\Http\Api\MedicationHistory\Actions\CreateMedicationHistoryAction;
use App\Http\Api\MedicationHistory\Actions\DeleteMedicationHistoryAction;
use App\Http\Api\MedicationHistory\Actions\GetMedicationHistoryAction;
use App\Http\Api\MedicationHistory\Actions\GetMedicationHistoryListAction;
use App\Http\Api\MedicationHistory\Actions\UpdateMedicationHistoryAction;
Expand Down Expand Up @@ -42,5 +43,6 @@
Route::post('/', CreateMedicationHistoryAction::class)->name('api.medication_histories.create');
Route::get('/{id}', GetMedicationHistoryAction::class)->name('api.medication_histories.detail');
Route::put('/{id}', UpdateMedicationHistoryAction::class)->name('api.medication_histories.update');
Route::delete('/{id}', DeleteMedicationHistoryAction::class)->name('api.medication_histories.delete');
});
});
44 changes: 44 additions & 0 deletions storage/api-docs/api-docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,45 @@
"description": "Unauthorized"
}
}
},
"delete": {
"tags": [
"MedicationHistory"
],
"summary": "服薬履歴を削除する",
"operationId": "1ad17a3f19538c0535c42eb5fd6295cd",
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"schema": {
"type": "integer"
}
}
],
"responses": {
"200": {
"description": "success",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/base_responder"
}
}
}
},
"404": {
"description": "not found",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/api_error_responder"
}
}
}
}
}
}
}
},
Expand Down Expand Up @@ -725,6 +764,11 @@
"note": {
"type": "string",
"nullable": true
},
"medication_date": {
"type": "string",
"format": "date-time",
"nullable": true
}
},
"type": "object"
Expand Down
Loading