1- using System ;
2- using System . Linq ;
1+ using System . Linq ;
32using System . Net ;
4- using System . Text . RegularExpressions ;
53using System . Threading . Tasks ;
6- using Google . Apis . Sheets . v4 ;
4+ using HwProj . APIGateway . API . ExportServices ;
75using HwProj . APIGateway . API . Models ;
6+ using HwProj . APIGateway . API . TableGenerators ;
87using HwProj . AuthService . Client ;
8+ using HwProj . CoursesService . Client ;
99using HwProj . Models . Result ;
1010using HwProj . SolutionsService . Client ;
1111using Microsoft . AspNetCore . Mvc ;
@@ -17,58 +17,103 @@ namespace HwProj.APIGateway.API.Controllers
1717 public class StatisticsController : AggregationController
1818 {
1919 private readonly ISolutionsServiceClient _solutionClient ;
20- private readonly SheetsService _sheetsService ;
20+ private readonly ICoursesServiceClient _coursesClient ;
21+ private readonly GoogleService _googleService ;
2122
22- public StatisticsController ( ISolutionsServiceClient solutionClient , IAuthServiceClient authServiceClient ,
23- SheetsService sheetsService ) :
24- base ( authServiceClient )
23+ public StatisticsController (
24+ ISolutionsServiceClient solutionClient ,
25+ ICoursesServiceClient coursesServiceClient ,
26+ IAuthServiceClient authServiceClient ,
27+ GoogleService googleService )
28+ : base ( authServiceClient )
2529 {
2630 _solutionClient = solutionClient ;
27- _sheetsService = sheetsService ;
31+ _coursesClient = coursesServiceClient ;
32+ _googleService = googleService ;
2833 }
2934
3035 [ HttpGet ( "{courseId}" ) ]
3136 [ ProducesResponseType ( typeof ( StatisticsCourseMatesModel [ ] ) , ( int ) HttpStatusCode . OK ) ]
3237 public async Task < IActionResult > GetCourseStatistics ( long courseId )
38+ {
39+ var result = await GetStatistics ( courseId ) ;
40+ if ( result == null )
41+ {
42+ return Forbid ( ) ;
43+ }
44+
45+ return Ok ( result ) ;
46+ }
47+
48+ private async Task < IOrderedEnumerable < StatisticsCourseMatesModel > ? > GetStatistics ( long courseId )
3349 {
3450 var statistics = await _solutionClient . GetCourseStatistics ( courseId , UserId ) ;
35- if ( statistics == null ) return Forbid ( ) ;
51+ if ( statistics == null ) return null ;
3652
3753 var studentIds = statistics . Select ( t => t . StudentId ) . ToArray ( ) ;
3854 var students = await AuthServiceClient . GetAccountsData ( studentIds ) ;
3955
40- var result = statistics . Zip ( students , ( stats , student ) => new StatisticsCourseMatesModel
41- {
42- Id = student . UserId ,
43- Name = student . Name ,
44- Surname = student . Surname ,
45- Homeworks = stats . Homeworks
46- } ) . OrderBy ( t => t . Surname ) . ThenBy ( t => t . Name ) ;
56+ var result
57+ = statistics . Zip ( students , ( stats , student )
58+ => new StatisticsCourseMatesModel
59+ {
60+ Id = student . UserId ,
61+ Name = student . Name ,
62+ Surname = student . Surname ,
63+ Homeworks = stats . Homeworks
64+ } ) . OrderBy ( t => t . Surname ) . ThenBy ( t => t . Name ) ;
4765
48- return Ok ( result ) ;
66+ return result ;
4967 }
5068
51- public class SheetUrl
69+ /// <summary>
70+ /// Implements file download.
71+ /// </summary>
72+ /// <param name="courseId">The course Id the report is based on.</param>
73+ /// <param name="userId">Id of the user requesting the report.</param>
74+ /// <param name="sheetName">Name of the sheet on which the report will be generated.</param>
75+ /// <returns>File download process.</returns>
76+ [ HttpGet ( "getFile" ) ]
77+ public async Task < IActionResult > GetFile ( long courseId , string userId , string sheetName )
5278 {
53- public string Url { get ; set ; }
79+ var course = await _coursesClient . GetCourseById ( courseId , userId ) ;
80+ var statistics = await GetStatistics ( courseId ) ;
81+ if ( statistics == null || course == null ) return Forbid ( ) ;
82+
83+ var statisticStream =
84+ await ExcelGenerator . Generate ( statistics . ToList ( ) , course , sheetName ) . GetAsByteArrayAsync ( ) ;
85+ return new FileContentResult ( statisticStream , "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ) ;
5486 }
5587
56- [ HttpPost ( "getSheetTitles" ) ]
57- public async Task < Result < string [ ] > > GetSheetTitles ( [ FromBody ] SheetUrl sheetUrl )
88+ [ HttpGet ( "getSheetTitles" ) ]
89+ public async Task < Result < string [ ] > > GetSheetTitles ( string sheetUrl )
90+ => await _googleService . GetSheetTitles ( sheetUrl ) ;
91+
92+ [ HttpPost ( "processLink" ) ]
93+ public Result ProcessLink ( string ? sheetUrl )
5894 {
59- var match = Regex . Match ( sheetUrl . Url , "https://docs\\ .google\\ .com/spreadsheets/d/(?<id>.+)/" ) ;
60- if ( ! match . Success ) return Result < string [ ] > . Failed ( "Некорректная ссылка на страницу Google Docs" ) ;
95+ if ( sheetUrl == null ) return Result . Failed ( "Некорректная ссылка" ) ;
96+ if ( GoogleService . ParseLink ( sheetUrl ) . Succeeded ) return Result . Success ( ) ;
97+ return Result . Failed ( "Некорректная ссылка" ) ;
98+ }
6199
62- var spreadsheetId = match . Groups [ "id" ] . Value ;
63- try
64- {
65- var sheet = await _sheetsService . Spreadsheets . Get ( spreadsheetId ) . ExecuteAsync ( ) ;
66- return Result < string [ ] > . Success ( sheet . Sheets . Select ( t => t . Properties . Title ) . ToArray ( ) ) ;
67- }
68- catch ( Exception ex )
69- {
70- return Result < string [ ] > . Failed ( $ "Ошибка при обращении к Google Docs: { ex . Message } ") ;
71- }
100+ /// <summary>
101+ /// Implements sending a report to the Google Sheets.
102+ /// </summary>
103+ /// <param name="courseId">The course Id the report is based on.</param>
104+ /// <param name="userId">Id of the user requesting the report.</param>
105+ /// <param name="sheetUrl">Sheet Url parameter, required to make requests to the Google Sheets.</param>
106+ /// <param name="sheetName">Sheet Name parameter, required to make requests to the Google Sheets.</param>
107+ /// <returns>Operation status.</returns>
108+ [ HttpGet ( "exportToSheet" ) ]
109+ public async Task < Result > ExportToGoogleSheets (
110+ long courseId , string userId , string sheetUrl , string sheetName )
111+ {
112+ var course = await _coursesClient . GetCourseById ( courseId , userId ) ;
113+ var statistics = await GetStatistics ( courseId ) ;
114+ if ( course == null || statistics == null ) return Result . Failed ( "Ошибка при получении статистики" ) ;
115+ var result = await _googleService . Export ( course , statistics , sheetUrl , sheetName ) ;
116+ return result ;
72117 }
73118 }
74119}
0 commit comments