Skip to content

Commit fee30b6

Browse files
authored
Move CurrentSessionPollingStationId from domain to api::investigation (#2880)
1 parent 9ff3132 commit fee30b6

File tree

2 files changed

+36
-38
lines changed

2 files changed

+36
-38
lines changed

backend/src/api/investigation.rs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
use axum::{Json, extract::State, http::StatusCode};
1+
use axum::{
2+
Json,
3+
extract::{FromRef, FromRequestParts, Path, State},
4+
http::{StatusCode, request::Parts},
5+
};
26
use axum_extra::response::Attachment;
37
use chrono::Datelike;
48
use sqlx::{SqliteConnection, SqlitePool};
@@ -16,9 +20,8 @@ use crate::{
1620
data_entry::PollingStationResults,
1721
election::ElectionWithPoliticalGroups,
1822
investigation::{
19-
CurrentSessionPollingStationId, PollingStationInvestigation,
20-
PollingStationInvestigationConcludeRequest, PollingStationInvestigationCreateRequest,
21-
PollingStationInvestigationUpdateRequest,
23+
PollingStationInvestigation, PollingStationInvestigationConcludeRequest,
24+
PollingStationInvestigationCreateRequest, PollingStationInvestigationUpdateRequest,
2225
},
2326
models::{ModelNa14_2Bijlage1Input, ToPdfFileModel},
2427
polling_station::{PollingStation, PollingStationId},
@@ -105,6 +108,33 @@ pub async fn delete_investigation_for_polling_station(
105108
Ok(())
106109
}
107110

111+
pub struct CurrentSessionPollingStationId(pub PollingStationId);
112+
113+
impl<S> FromRequestParts<S> for CurrentSessionPollingStationId
114+
where
115+
SqlitePool: FromRef<S>,
116+
S: Send + Sync,
117+
{
118+
type Rejection = APIError;
119+
120+
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
121+
let path_extractor = Path::<PollingStationId>::from_request_parts(parts, state).await;
122+
let pool = SqlitePool::from_ref(state);
123+
let mut conn = pool.acquire().await?;
124+
125+
if let Ok(Path(id)) = path_extractor
126+
&& polling_station_repo::get(&mut conn, id).await.is_ok()
127+
{
128+
return Ok(CurrentSessionPollingStationId(id));
129+
}
130+
131+
Err(APIError::NotFound(
132+
"Polling station not found for the current committee session".to_string(),
133+
ErrorReference::EntryNotFound,
134+
))
135+
}
136+
}
137+
108138
/// Create an investigation for a polling station
109139
#[utoipa::path(
110140
post,

backend/src/domain/investigation.rs

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
use axum::{
22
Json,
3-
extract::{FromRef, FromRequestParts, Path},
4-
http::request::Parts,
53
response::{IntoResponse, Response},
64
};
75
use serde::{Deserialize, Serialize};
8-
use sqlx::{FromRow, SqlitePool};
6+
use sqlx::FromRow;
97
use utoipa::ToSchema;
108

11-
use crate::{
12-
APIError, domain::polling_station::PollingStationId, error::ErrorReference,
13-
repository::polling_station_repo,
14-
};
9+
use crate::domain::polling_station::PollingStationId;
1510

1611
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, ToSchema, FromRow)]
1712
#[serde(deny_unknown_fields)]
@@ -59,30 +54,3 @@ pub struct PollingStationInvestigationUpdateRequest {
5954
#[schema(nullable = false)]
6055
pub accept_data_entry_deletion: Option<bool>,
6156
}
62-
63-
pub struct CurrentSessionPollingStationId(pub PollingStationId);
64-
65-
impl<S> FromRequestParts<S> for CurrentSessionPollingStationId
66-
where
67-
SqlitePool: FromRef<S>,
68-
S: Send + Sync,
69-
{
70-
type Rejection = APIError;
71-
72-
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
73-
let path_extractor = Path::<PollingStationId>::from_request_parts(parts, state).await;
74-
let pool = SqlitePool::from_ref(state);
75-
let mut conn = pool.acquire().await?;
76-
77-
if let Ok(Path(id)) = path_extractor
78-
&& polling_station_repo::get(&mut conn, id).await.is_ok()
79-
{
80-
return Ok(CurrentSessionPollingStationId(id));
81-
}
82-
83-
Err(APIError::NotFound(
84-
"Polling station not found for the current committee session".to_string(),
85-
ErrorReference::EntryNotFound,
86-
))
87-
}
88-
}

0 commit comments

Comments
 (0)