ef0b83a
use models::issue::issue_content_history;
use sea_orm::{ColumnTrait, EntityTrait, PaginatorTrait, QueryFilter, QueryOrder, QuerySelect};
use serde::{Deserialize, Serialize};
use space::services::dto::UserDto;
use time::OffsetDateTime;
use utoipa::{IntoParams, ToSchema};
use uuid::Uuid;
use crate::errors::IssueError;
use crate::services::service::IssueService;
use crate::share::lookup;
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct ContentHistoryDto {
pub id: Uuid,
pub issue_id: Uuid,
pub poster: Option<UserDto>,
pub comment_id: Option<Uuid>,
pub content_before: Option<String>,
pub content_after: Option<String>,
pub edited_at: OffsetDateTime,
pub created_at: OffsetDateTime,
}
pub type ContentHistoryPage = export::pagination::Page<ContentHistoryDto>;
#[derive(Debug, Clone, Deserialize, IntoParams, ToSchema)]
pub struct ListContentHistoryQuery {
pub comment_id: Option<Uuid>,
pub limit: Option<u64>,
pub offset: Option<u64>,
}
#[derive(Debug, Clone, Deserialize, IntoParams, ToSchema)]
pub struct ContentHistoryPath {
pub space_name: String,
pub board_id: Uuid,
pub issue_id: String,
}
impl IssueService {
pub async fn list_content_history(
&self,
caller: i64,
space_name: &str,
board_id: Uuid,
issue_id: Uuid,
q: ListContentHistoryQuery,
) -> Result<ContentHistoryPage, IssueError> {
lookup::require_issue_access(self, caller, space_name, board_id, issue_id).await?;
let (limit, offset) = self.paginate(q.limit, q.offset);
let mut select = issue_content_history::Entity::find()
.filter(issue_content_history::Column::IssueId.eq(issue_id));
if let Some(cid) = q.comment_id {
select = select.filter(issue_content_history::Column::CommentId.eq(Some(cid)));
}
select = select.order_by_desc(issue_content_history::Column::EditedAt);
let total = select.clone().count(self.db().read()).await? as i64;
let rows = select
.limit(limit)
.offset(offset)
.all(self.db().read())
.await?;
let poster_ids: Vec<i64> = rows.iter().map(|h| h.poster_id).collect();
let user_map = self.load_user_briefs(&poster_ids).await?;
let items = rows
.into_iter()
.map(|h| {
let poster = user_map.get(&h.poster_id).cloned();
ContentHistoryDto {
id: h.id,
issue_id: h.issue_id,
poster,
comment_id: h.comment_id,
content_before: h.content_before,
content_after: h.content_after,
edited_at: h.edited_at,
created_at: h.created_at,
}
})
.collect();
Ok(ContentHistoryPage {
items,
total,
limit,
offset,
})
}
}