11using System . Text . Json ;
22using Bugget . Entities . BO . Search ;
33using Bugget . Entities . DbModels ;
4+ using Bugget . Entities . DbModels . Bug ;
5+ using Bugget . Entities . DbModels . Comment ;
46using Bugget . Entities . DbModels . Report ;
57using Bugget . Entities . DTO . Report ;
68using Dapper ;
79
810namespace Bugget . DA . Postgres ;
911
10- public sealed class ReportsDbClient : PostgresClient
12+ public sealed class ReportsDbClient : PostgresClient
1113{
1214 /// <summary>
1315 /// Получает отчет по ID.
@@ -28,15 +30,39 @@ public sealed class ReportsDbClient: PostgresClient
2830 /// </summary>
2931 public async Task < ReportDbModel ? > GetReportAsync ( int reportId , string ? organizationId )
3032 {
31- await using var connection = await DataSource . OpenConnectionAsync ( ) ;
32- var jsonResult = await connection . ExecuteScalarAsync < string > (
33- "SELECT public.get_report_v2(@report_id, @organization_id);" ,
34- new { report_id = reportId , organization_id = organizationId }
35- ) ;
36-
37- return jsonResult != null ? Deserialize < ReportDbModel > ( jsonResult ) : null ;
33+ await using var conn = await DataSource . OpenConnectionAsync ( ) ;
34+ // Открываем транзакцию для курсоров
35+ await using var multi = await conn . QueryMultipleAsync ( @"
36+ SELECT * FROM public.get_report_v2(@reportId, @organizationId);
37+ SELECT * FROM public.list_bugs(@reportId);
38+ SELECT * FROM public.list_participants(@reportId);
39+ SELECT * FROM public.list_comments(@reportId);
40+ SELECT * FROM public.list_attachments(@reportId);
41+ " , new { reportId , organizationId } ) ;
42+
43+ // проверка доступа к репорту
44+ var report = await multi . ReadSingleOrDefaultAsync < ReportDbModel > ( ) ;
45+ if ( report == null ) return null ;
46+
47+ // 2. Дочерние сущности
48+ report . Bugs = ( await multi . ReadAsync < BugDbModel > ( ) ) . ToArray ( ) ;
49+ report . ParticipantsUserIds = ( await multi . ReadAsync < string > ( ) ) . ToArray ( ) ;
50+ var comments = ( await multi . ReadAsync < CommentDbModel > ( ) ) . ToArray ( ) ;
51+ var attachments = ( await multi . ReadAsync < AttachmentDbModel > ( ) ) . ToArray ( ) ;
52+
53+ // 3. Группируем по багам
54+ var commentsByBug = comments . GroupBy ( c => c . BugId ) . ToDictionary ( g => g . Key , g => g . ToArray ( ) ) ;
55+ var attachmentsByBug = attachments . GroupBy ( a => a . BugId ) . ToDictionary ( g => g . Key , g => g . ToArray ( ) ) ;
56+
57+ foreach ( var bug in report . Bugs )
58+ {
59+ bug . Comments = commentsByBug . TryGetValue ( bug . Id , out var c ) ? c : [ ] ;
60+ bug . Attachments = attachmentsByBug . TryGetValue ( bug . Id , out var a ) ? a : [ ] ;
61+ }
62+
63+ return report ;
3864 }
39-
65+
4066 public async Task < ReportObsoleteDbModel [ ] > ListReportsAsync ( string userId )
4167 {
4268 await using var connection = await DataSource . OpenConnectionAsync ( ) ;
@@ -75,7 +101,7 @@ public async Task<ReportSummaryDbModel> CreateReportAsync(string userId, string?
75101 /// <summary>
76102 /// Обновляет краткую информацию об отчете и возвращает его краткую структуру.
77103 /// </summary>
78- public async Task < ReportPatchDbModel > PatchReportAsync ( int reportId , string userId , string ? organizationId , ReportPatchDto dto )
104+ public async Task < ReportPatchResultDbModel > PatchReportAsync ( int reportId , string userId , string ? organizationId , ReportPatchDto dto )
79105 {
80106 await using var connection = await DataSource . OpenConnectionAsync ( ) ;
81107
@@ -92,7 +118,7 @@ public async Task<ReportPatchDbModel> PatchReportAsync(int reportId, string user
92118 }
93119 ) ;
94120
95- return Deserialize < ReportPatchDbModel > ( jsonResult ! ) ! ;
121+ return Deserialize < ReportPatchResultDbModel > ( jsonResult ! ) ! ;
96122 }
97123
98124
@@ -123,11 +149,11 @@ public async Task<ReportPatchDbModel> PatchReportAsync(int reportId, string user
123149 ? Deserialize < ReportObsoleteDbModel > ( jsonResult )
124150 : null ;
125151 }
126-
152+
127153 public async Task < ReportObsoleteDbModel ? > UpdateReportAsync ( ReportUpdateDbModel reportDbModel )
128154 {
129155 await using var connection = await DataSource . OpenConnectionAsync ( ) ;
130-
156+
131157 var jsonResult = await connection . ExecuteScalarAsync < string > (
132158 "SELECT public.update_report(@report_id, @participants,@title, @status, @responsible_user_id);" ,
133159 new
@@ -144,7 +170,7 @@ public async Task<ReportPatchDbModel> PatchReportAsync(int reportId, string user
144170 ? Deserialize < ReportObsoleteDbModel > ( jsonResult )
145171 : null ;
146172 }
147-
173+
148174 public async Task < SearchReportsDbModel > SearchReportsAsync ( SearchReports search )
149175 {
150176 await using var connection = await DataSource . OpenConnectionAsync ( ) ;
@@ -165,6 +191,16 @@ public async Task<SearchReportsDbModel> SearchReportsAsync(SearchReports search)
165191
166192 return Deserialize < SearchReportsDbModel > ( jsonResult ) ;
167193 }
168-
194+
195+ public async Task ChangeStatusAsync ( int reportId , int newStatus )
196+ {
197+ await using var connection = await DataSource . OpenConnectionAsync ( ) ;
198+
199+ await connection . ExecuteAsync (
200+ "SELECT public.change_status(@report_id, @new_status);" ,
201+ new { report_id = reportId , new_status = newStatus }
202+ ) ;
203+ }
204+
169205 private T ? Deserialize < T > ( string json ) => JsonSerializer . Deserialize < T > ( json , JsonSerializerOptions ) ;
170206}
0 commit comments