ปรับปรุง phase 6 ให้ครบถ้วน
This commit is contained in:
parent
93570d38d8
commit
a196b8f9a8
14 changed files with 747 additions and 40 deletions
|
|
@ -314,6 +314,39 @@ public class TemplatesController(
|
|||
return NoContent();
|
||||
}
|
||||
|
||||
/// <summary>FR-12 — รายการเอกสารที่สร้างจาก template นี้ (เรียงตามกิจกรรมล่าสุด)
|
||||
/// ใช้ได้แม้ template ถูก deactivate (เอกสารเก่ายังต้องเข้าถึงได้ตาม policy FR-9)</summary>
|
||||
[HttpGet("{id:guid}/documents")]
|
||||
[ProducesResponseType(typeof(IReadOnlyList<TemplateDocumentListItem>), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult<IReadOnlyList<TemplateDocumentListItem>>> ListDocuments(Guid id, CancellationToken ct)
|
||||
{
|
||||
if (!await db.Templates.AnyAsync(t => t.Id == id, ct))
|
||||
return NotFound($"ไม่พบ template {id}");
|
||||
|
||||
// cast (int?) ใน Max จำเป็น — ทำให้ SQL MAX คืน NULL บนชุดว่างได้ (pattern เดียวกับ UploadVersion)
|
||||
// LatestActivity ต้อง compute ใน projection — EF จะยกเข้า SQL ORDER BY ให้
|
||||
var rows = await db.Documents
|
||||
.AsNoTracking()
|
||||
.Where(d => d.TemplateId == id)
|
||||
.Select(d => new
|
||||
{
|
||||
d.Id,
|
||||
d.FileName,
|
||||
d.CreatedAt,
|
||||
VersionCount = d.Versions.Count,
|
||||
LatestVersionNumber = d.Versions.Max(v => (int?)v.VersionNumber) ?? 0,
|
||||
LatestVersionAt = d.Versions.Max(v => (DateTimeOffset?)v.CreatedAt),
|
||||
LatestActivity = d.Versions.Max(v => (DateTimeOffset?)v.CreatedAt) ?? d.CreatedAt,
|
||||
})
|
||||
.OrderByDescending(x => x.LatestActivity)
|
||||
.ThenBy(x => x.FileName)
|
||||
.ToListAsync(ct);
|
||||
|
||||
return Ok(rows.Select(r =>
|
||||
new TemplateDocumentListItem(r.Id, r.FileName, r.CreatedAt, r.VersionCount, r.LatestVersionNumber, r.LatestVersionAt)).ToList());
|
||||
}
|
||||
|
||||
private static TemplateDetail ToDetail(Template template) => new(
|
||||
template.Id, template.Name, template.Description, template.IsActive,
|
||||
template.CreatedAt, template.UpdatedAt, FieldDtos(template));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue