feat: implement find_all method in ChannelRepository and update related services and routes for public access
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
//! Channel routes
|
||||
//!
|
||||
//! CRUD for channels and broadcast/EPG endpoints.
|
||||
//!
|
||||
//! All routes require authentication (Bearer JWT).
|
||||
//! CRUD + schedule generation require authentication (Bearer JWT).
|
||||
//! Viewing endpoints (list, now, epg, stream) are intentionally public so the
|
||||
//! TV page works without login.
|
||||
|
||||
use axum::{
|
||||
Json, Router,
|
||||
@@ -49,9 +49,8 @@ pub fn router() -> Router<AppState> {
|
||||
|
||||
async fn list_channels(
|
||||
State(state): State<AppState>,
|
||||
CurrentUser(user): CurrentUser,
|
||||
) -> Result<impl IntoResponse, ApiError> {
|
||||
let channels = state.channel_service.find_by_owner(user.id).await?;
|
||||
let channels = state.channel_service.find_all().await?;
|
||||
let response: Vec<ChannelResponse> = channels.into_iter().map(Into::into).collect();
|
||||
Ok(Json(response))
|
||||
}
|
||||
@@ -173,11 +172,9 @@ async fn get_active_schedule(
|
||||
/// Returns 204 No Content when the channel is in a gap between blocks (no-signal).
|
||||
async fn get_current_broadcast(
|
||||
State(state): State<AppState>,
|
||||
CurrentUser(user): CurrentUser,
|
||||
Path(channel_id): Path<Uuid>,
|
||||
) -> Result<Response, ApiError> {
|
||||
let channel = state.channel_service.find_by_id(channel_id).await?;
|
||||
require_owner(&channel, user.id)?;
|
||||
let _channel = state.channel_service.find_by_id(channel_id).await?;
|
||||
|
||||
let now = Utc::now();
|
||||
let schedule = state
|
||||
@@ -209,12 +206,10 @@ struct EpgQuery {
|
||||
|
||||
async fn get_epg(
|
||||
State(state): State<AppState>,
|
||||
CurrentUser(user): CurrentUser,
|
||||
Path(channel_id): Path<Uuid>,
|
||||
Query(params): Query<EpgQuery>,
|
||||
) -> Result<impl IntoResponse, ApiError> {
|
||||
let channel = state.channel_service.find_by_id(channel_id).await?;
|
||||
require_owner(&channel, user.id)?;
|
||||
let _channel = state.channel_service.find_by_id(channel_id).await?;
|
||||
|
||||
let now = Utc::now();
|
||||
let from = parse_optional_dt(params.from, now)?;
|
||||
@@ -244,11 +239,9 @@ async fn get_epg(
|
||||
/// Returns 204 No Content when the channel is in a gap (no-signal).
|
||||
async fn get_stream(
|
||||
State(state): State<AppState>,
|
||||
CurrentUser(user): CurrentUser,
|
||||
Path(channel_id): Path<Uuid>,
|
||||
) -> Result<Response, ApiError> {
|
||||
let channel = state.channel_service.find_by_id(channel_id).await?;
|
||||
require_owner(&channel, user.id)?;
|
||||
let _channel = state.channel_service.find_by_id(channel_id).await?;
|
||||
|
||||
let now = Utc::now();
|
||||
let schedule = state
|
||||
|
||||
@@ -36,6 +36,7 @@ pub trait UserRepository: Send + Sync {
|
||||
pub trait ChannelRepository: Send + Sync {
|
||||
async fn find_by_id(&self, id: ChannelId) -> DomainResult<Option<Channel>>;
|
||||
async fn find_by_owner(&self, owner_id: UserId) -> DomainResult<Vec<Channel>>;
|
||||
async fn find_all(&self) -> DomainResult<Vec<Channel>>;
|
||||
/// Insert or update a channel.
|
||||
async fn save(&self, channel: &Channel) -> DomainResult<()>;
|
||||
async fn delete(&self, id: ChannelId) -> DomainResult<()>;
|
||||
|
||||
@@ -112,6 +112,10 @@ impl ChannelService {
|
||||
.ok_or(DomainError::ChannelNotFound(id))
|
||||
}
|
||||
|
||||
pub async fn find_all(&self) -> DomainResult<Vec<crate::entities::Channel>> {
|
||||
self.channel_repo.find_all().await
|
||||
}
|
||||
|
||||
pub async fn find_by_owner(
|
||||
&self,
|
||||
owner_id: crate::value_objects::UserId,
|
||||
|
||||
@@ -113,6 +113,16 @@ impl ChannelRepository for SqliteChannelRepository {
|
||||
rows.into_iter().map(Channel::try_from).collect()
|
||||
}
|
||||
|
||||
async fn find_all(&self) -> DomainResult<Vec<Channel>> {
|
||||
let sql = format!("SELECT {SELECT_COLS} FROM channels ORDER BY created_at ASC");
|
||||
let rows: Vec<ChannelRow> = sqlx::query_as(&sql)
|
||||
.fetch_all(&self.pool)
|
||||
.await
|
||||
.map_err(|e| DomainError::RepositoryError(e.to_string()))?;
|
||||
|
||||
rows.into_iter().map(Channel::try_from).collect()
|
||||
}
|
||||
|
||||
async fn save(&self, channel: &Channel) -> DomainResult<()> {
|
||||
let schedule_config = serde_json::to_string(&channel.schedule_config).map_err(|e| {
|
||||
DomainError::RepositoryError(format!("Failed to serialize schedule_config: {}", e))
|
||||
@@ -205,6 +215,16 @@ impl ChannelRepository for PostgresChannelRepository {
|
||||
rows.into_iter().map(Channel::try_from).collect()
|
||||
}
|
||||
|
||||
async fn find_all(&self) -> DomainResult<Vec<Channel>> {
|
||||
let sql = format!("SELECT {SELECT_COLS} FROM channels ORDER BY created_at ASC");
|
||||
let rows: Vec<ChannelRow> = sqlx::query_as(&sql)
|
||||
.fetch_all(&self.pool)
|
||||
.await
|
||||
.map_err(|e| DomainError::RepositoryError(e.to_string()))?;
|
||||
|
||||
rows.into_iter().map(Channel::try_from).collect()
|
||||
}
|
||||
|
||||
async fn save(&self, channel: &Channel) -> DomainResult<()> {
|
||||
let schedule_config = serde_json::to_string(&channel.schedule_config).map_err(|e| {
|
||||
DomainError::RepositoryError(format!("Failed to serialize schedule_config: {}", e))
|
||||
|
||||
Reference in New Issue
Block a user