Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete recipe Impl #57

Merged
merged 3 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 0 additions & 1 deletion cdk/lib/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import MealPlannerStage from './application-stage';
import {
CodePipeline,
CodePipelineSource,
ManualApprovalStep,
ShellStep,
} from 'aws-cdk-lib/pipelines';
import path = require('path');
Expand Down
1 change: 1 addition & 0 deletions lambda/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ pub mod services;
pub trait Repository<T>: Send + Sync {
fn find_by_id(&self, id: Uuid) -> impl Future<Output = Result<T, StatusCode>> + Send;
fn save(&self, item: &T) -> impl Future<Output = Result<(), StatusCode>> + Send;
fn delete(&self, id: Uuid) -> impl Future<Output = Result<(), StatusCode>> + Send;
}
17 changes: 16 additions & 1 deletion lambda/src/recipe/repository.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use aws_config::SdkConfig;
use aws_sdk_dynamodb::operation::get_item::GetItemError;
use aws_sdk_dynamodb::operation::{delete_item::DeleteItemError, get_item::GetItemError};
use aws_sdk_dynamodb::types::AttributeValue;
use aws_sdk_dynamodb::Client;
use axum::http::StatusCode;
Expand Down Expand Up @@ -60,4 +60,19 @@ impl Repository<Recipe> for DynamoDbRecipe {

Ok(())
}

async fn delete(&self, id: Uuid) -> Result<(), StatusCode> {
self.client
.delete_item()
.table_name(&self.table_name)
.key("id", AttributeValue::S(id.as_hyphenated().to_string()))
.send()
.await
.map_err(|sdk_err| match sdk_err.into_service_error() {
DeleteItemError::ResourceNotFoundException(_) => StatusCode::NOT_FOUND,
_ => StatusCode::INTERNAL_SERVER_ERROR,
})?;

Ok(())
}
}
3 changes: 2 additions & 1 deletion lambda/src/services/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use aws_config::BehaviorVersion;
use axum::routing::{get, post};
use axum::routing::{delete, get, post};
use axum::Router;
use tracing::{info, instrument};

Expand All @@ -26,5 +26,6 @@ pub async fn recipes() -> Router {
Router::new()
.route("/:id", get(recipes::read_one::<DynamoDbRecipe>))
.route("/", post(recipes::create::<DynamoDbRecipe>))
.route("/:id", delete(recipes::delete::<DynamoDbRecipe>))
.with_state(recipe_context)
}
12 changes: 12 additions & 0 deletions lambda/src/services/recipes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,15 @@ where

Ok(Json(recipe))
}

pub(super) async fn delete<T>(
State(state): State<ApplicationContext<T>>,
Path(id): Path<Uuid>,
) -> Result<StatusCode, StatusCode>
where
T: Repository<Recipe>
{
state.repo.delete(id).await?;

Ok(StatusCode::NO_CONTENT)
}
53 changes: 34 additions & 19 deletions playwright/tests/recipes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,50 @@ import { test, expect } from '@playwright/test';
const data = {
name: 'Playwright Recipe',
};
const NIL_UUID = '00000000-0000-0000-0000-000000000000';

let recipeUuid: string;

test.beforeAll('Create Recipe', async ({ request }) => {
const response = await request.post('./recipes', { data });
test.describe('Happy Path', () => {
test.beforeAll('Create Recipe', async ({ request }) => {
const response = await request.post('./recipes', { data });

expect(response.ok()).toBeTruthy();
expect(response.ok()).toBeTruthy();

const responseBody = await response.json();
expect(responseBody).toEqual({
// eslint-disable-next-line max-len
id: expect.stringMatching(/^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/),
...data,
const responseBody = await response.json();
expect(responseBody).toEqual({
// eslint-disable-next-line max-len
id: expect.stringMatching(/^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/),
...data,
});
recipeUuid = responseBody.id;
});

test('Read Recipe', async ({ request }) => {
const response = await request.get(`./recipes/${recipeUuid}`);

expect(response.ok()).toBeTruthy();
expect(await response.json()).toEqual({
id: recipeUuid,
...data,
});
});
recipeUuid = responseBody.id;
});

test('Read Recipe', async ({ request }) => {
const response = await request.get(`./recipes/${recipeUuid}`);
test.afterAll('Delete Recipe', async ({ request }) => {
const response = await request.delete(`./recipes/${recipeUuid}`);

expect(response.ok()).toBeTruthy();
expect(await response.json()).toEqual({
id: recipeUuid,
...data,
expect(response.status()).toEqual(204);
});
});

test.afterAll('Delete Recipe', async ({ request }) => {
const response = await request.delete(`./recipes/${recipeUuid}`);
test('Read Non-existent Recipe', async ({ request }) => {
const response = await request.get(`./recipes/${NIL_UUID}`);

expect(response.status()).toEqual(404);
});

test('Delete Non-existent Recipe', async ({ request }) => {
const response = await request.delete(`./recipes/${NIL_UUID}`);

expect(response.ok()).toBeTruthy();
expect(response.status()).toEqual(404);
});
Loading