Skip to content

Commit 845e926

Browse files
committed
V14 Integrations (Semrush)
- Add embedded resources to get semrush datasources and columns
1 parent 58ea356 commit 845e926

File tree

9 files changed

+1372
-45
lines changed

9 files changed

+1372
-45
lines changed

src/Umbraco.Cms.Integrations.SEO.Semrush/Api/Management/Controllers/GetColumnsController.cs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.AspNetCore.Http;
44
using Microsoft.AspNetCore.Mvc;
55
using Microsoft.Extensions.Options;
6+
using System.Reflection;
67
using System.Text.Json;
78
using Umbraco.Cms.Integrations.SEO.Semrush.Configuration;
89
using Umbraco.Cms.Integrations.SEO.Semrush.Models.Dtos;
@@ -29,30 +30,36 @@ public GetColumnsController(
2930
[ProducesResponseType(typeof(IEnumerable<ColumnDto>), StatusCodes.Status200OK)]
3031
public IActionResult GetColumns()
3132
{
32-
string semrushColumnsPath = "semrushColumns.json";
33+
string semrushColumnsPath = $"{Constants.EmbeddedResourceNamespace}.semrushColumns.json";
34+
var assembly = Assembly.GetExecutingAssembly();
3335

3436
_lock.EnterReadLock();
3537

3638
try
3739
{
38-
if (!System.IO.File.Exists(semrushColumnsPath))
40+
using (Stream stream = assembly.GetManifestResourceStream(semrushColumnsPath))
3941
{
40-
var fs = System.IO.File.Create(semrushColumnsPath);
41-
fs.Close();
42-
43-
return Ok(Enumerable.Empty<ColumnDto>());
44-
}
45-
46-
var content = System.IO.File.ReadAllText(semrushColumnsPath);
47-
var deserializeContent = JsonSerializer.Deserialize<IEnumerable<ColumnDto>>(content).Select(p =>
48-
new ColumnDto
42+
if (stream != null)
4943
{
50-
Name = p.Name,
51-
Value = p.Value,
52-
Description = p.Description
53-
});
44+
using (StreamReader reader = new StreamReader(stream))
45+
{
46+
string result = reader.ReadToEnd();
47+
var deserializeContent = JsonSerializer.Deserialize<IEnumerable<ColumnDto>>(result).Select(p =>
48+
new ColumnDto
49+
{
50+
Name = p.Name,
51+
Value = p.Value,
52+
Description = p.Description
53+
});
5454

55-
return Ok(deserializeContent);
55+
return Ok(deserializeContent);
56+
}
57+
}
58+
else
59+
{
60+
return Ok(Enumerable.Empty<ColumnDto>());
61+
}
62+
}
5663

5764
}
5865
catch (FileNotFoundException ex)

src/Umbraco.Cms.Integrations.SEO.Semrush/Api/Management/Controllers/GetDataSourcesController.cs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.AspNetCore.Http;
44
using Microsoft.AspNetCore.Mvc;
55
using Microsoft.Extensions.Options;
6+
using System.Reflection;
67
using System.Text.Json;
78
using Umbraco.Cms.Integrations.SEO.Semrush.Configuration;
89
using Umbraco.Cms.Integrations.SEO.Semrush.Models.Dtos;
@@ -28,33 +29,39 @@ public GetDataSourcesController(IOptions<SemrushSettings> options,
2829
[ProducesResponseType(typeof(DataSourceDto), StatusCodes.Status200OK)]
2930
public IActionResult GetDataSources()
3031
{
31-
string semrushDataSourcesPath = "semrushDataSources.json";
32+
string semrushDataSourcesPath = $"{Constants.EmbeddedResourceNamespace}.semrushDataSources.json";
33+
var assembly = Assembly.GetExecutingAssembly();
3234

3335
_lock.EnterReadLock();
3436
try
3537
{
36-
if (!System.IO.File.Exists(Path.Combine(Directory.GetCurrentDirectory(), semrushDataSourcesPath)))
38+
using (Stream stream = assembly.GetManifestResourceStream(semrushDataSourcesPath))
3739
{
38-
var fs = System.IO.File.Create(semrushDataSourcesPath);
39-
fs.Close();
40-
41-
return Ok(new DataSourceDto());
42-
}
43-
44-
var content = System.IO.File.ReadAllText(semrushDataSourcesPath);
45-
var dataSourceDto = new DataSourceDto
46-
{
47-
Items = JsonSerializer.Deserialize<List<DataSourceItemDto>>(content).Select(p =>
48-
new DataSourceItemDto
40+
if (stream != null)
41+
{
42+
using (StreamReader reader = new StreamReader(stream))
4943
{
50-
Code = p.Code,
51-
Region = p.Region,
52-
ResearchTypes = p.ResearchTypes,
53-
GoogleSearchDomain = p.GoogleSearchDomain
54-
})
55-
};
44+
string result = reader.ReadToEnd();
45+
var dataSourceDto = new DataSourceDto
46+
{
47+
Items = JsonSerializer.Deserialize<List<DataSourceItemDto>>(result).Select(p =>
48+
new DataSourceItemDto
49+
{
50+
Code = p.Code,
51+
Region = p.Region,
52+
ResearchTypes = p.ResearchTypes,
53+
GoogleSearchDomain = p.GoogleSearchDomain
54+
})
55+
};
5656

57-
return Ok(dataSourceDto);
57+
return Ok(dataSourceDto);
58+
}
59+
}
60+
else
61+
{
62+
return Ok(new DataSourceDto());
63+
}
64+
}
5865
}
5966
catch (FileNotFoundException ex)
6067
{

src/Umbraco.Cms.Integrations.SEO.Semrush/Constants.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public class Constants
1313

1414
public const string BadRefreshToken = "BAD_REFRESH_TOKEN";
1515

16+
public const string EmbeddedResourceNamespace = "Umbraco.Cms.Integrations.SEO.Semrush.EmbeddedResources";
17+
1618
public static class Configuration
1719
{
1820
public const string Settings = "Umbraco:Cms:Integrations:SEO:Semrush:Settings";

0 commit comments

Comments
 (0)