Skip to content

Commit 8e876aa

Browse files
committed
wip
1 parent b47a24d commit 8e876aa

3 files changed

Lines changed: 50 additions & 4 deletions

File tree

HwProj.APIGateway/HwProj.APIGateway.API/Controllers/StatisticsController.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
using System.Linq;
1+
using System;
2+
using System.Linq;
23
using System.Net;
4+
using System.Text.RegularExpressions;
35
using System.Threading.Tasks;
6+
using Google.Apis.Sheets.v4;
47
using HwProj.APIGateway.API.Models;
58
using HwProj.AuthService.Client;
69
using HwProj.SolutionsService.Client;
@@ -13,11 +16,14 @@ namespace HwProj.APIGateway.API.Controllers
1316
public class StatisticsController : AggregationController
1417
{
1518
private readonly ISolutionsServiceClient _solutionClient;
19+
private readonly SheetsService _sheetsService;
1620

17-
public StatisticsController(ISolutionsServiceClient solutionClient, IAuthServiceClient authServiceClient) :
21+
public StatisticsController(ISolutionsServiceClient solutionClient, IAuthServiceClient authServiceClient,
22+
SheetsService sheetsService) :
1823
base(authServiceClient)
1924
{
2025
_solutionClient = solutionClient;
26+
_sheetsService = sheetsService;
2127
}
2228

2329
[HttpGet("{courseId}")]
@@ -40,5 +46,21 @@ public async Task<IActionResult> GetCourseStatistics(long courseId)
4046

4147
return Ok(result);
4248
}
49+
50+
public class SheetUrl
51+
{
52+
public string Url { get; set; }
53+
}
54+
55+
[HttpPost("getSheetTitles")]
56+
public async Task<string[]> GetSheetTitles([FromBody] SheetUrl sheetUrl)
57+
{
58+
var match = Regex.Match(sheetUrl.Url, "https://docs\\.google\\.com/spreadsheets/d/(?<id>.+)/");
59+
if (!match.Success) return Array.Empty<string>();
60+
61+
var spreadsheetId = match.Groups["id"].Value;
62+
var sheet = await _sheetsService.Spreadsheets.Get(spreadsheetId).ExecuteAsync();
63+
return sheet.Sheets.Select(t => t.Properties.Title).ToArray();
64+
}
4365
}
4466
}

HwProj.APIGateway/HwProj.APIGateway.API/HwProj.APIGateway.API.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<ItemGroup>
1313
<PackageReference Include="Microsoft.AspNetCore.All" />
1414
<PackageReference Include="Microsoft.AspNetCore.App" />
15+
<PackageReference Include="Google.Apis.Sheets.v4" Version="1.57.0.2637" />
1516
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.3" />
1617
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.13" />
1718
</ItemGroup>
@@ -23,4 +24,10 @@
2324
<ProjectReference Include="..\..\HwProj.NotificationsService\HwProj.NotificationsService.Client\HwProj.NotificationsService.Client.csproj" />
2425
<ProjectReference Include="..\..\HwProj.SolutionsService\HwProj.SolutionsService.Client\HwProj.SolutionsService.Client.csproj" />
2526
</ItemGroup>
27+
28+
<ItemGroup>
29+
<Content Update="googlesheets_credentials.json">
30+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
31+
</Content>
32+
</ItemGroup>
2633
</Project>

HwProj.APIGateway/HwProj.APIGateway.API/Startup.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
using HwProj.AuthService.Client;
1+
using System.IO;
2+
using Google.Apis.Auth.OAuth2;
3+
using Google.Apis.Services;
4+
using Google.Apis.Sheets.v4;
5+
using HwProj.AuthService.Client;
26
using HwProj.CoursesService.Client;
37
using HwProj.NotificationsService.Client;
48
using HwProj.SolutionsService.Client;
@@ -26,7 +30,7 @@ public void ConfigureServices(IServiceCollection services)
2630
services.ConfigureHwProjServices("API Gateway");
2731

2832
const string authenticationProviderKey = "GatewayKey";
29-
33+
3034
services.AddAuthentication()
3135
.AddJwtBearer(authenticationProviderKey, x =>
3236
{
@@ -44,6 +48,7 @@ public void ConfigureServices(IServiceCollection services)
4448

4549
services.AddHttpClient();
4650
services.AddHttpContextAccessor();
51+
services.AddScoped(_ => ConfigureGoogleSheets());
4752

4853
services.AddAuthServiceClient();
4954
services.AddCoursesServiceClient();
@@ -55,5 +60,17 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
5560
{
5661
app.ConfigureHwProj(env, "API Gateway");
5762
}
63+
64+
private static SheetsService ConfigureGoogleSheets()
65+
{
66+
using var stream = new FileStream("googlesheets_credentials.json", FileMode.Open, FileAccess.ReadWrite);
67+
var credential = GoogleCredential.FromStream(stream).CreateScoped(SheetsService.Scope.Spreadsheets);
68+
69+
return new SheetsService(new BaseClientService.Initializer
70+
{
71+
HttpClientInitializer = credential,
72+
ApplicationName = "HwProjSheets"
73+
});
74+
}
5875
}
5976
}

0 commit comments

Comments
 (0)