Skip to content

Commit e9132cd

Browse files
Merge pull request #44 from IowaComputerGurus/Feature/NewRelease
Roll-up of new functionality for 5.0.x release
2 parents f6fdb00 + c37d015 commit e9132cd

11 files changed

+226
-22
lines changed

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Install from NuGet
1818
```
1919
Install-Package ICG.AspNetCore.Utilities
2020
```
21+
2122
### Register Dependencies
2223

2324
Inside of of your project's Startus.cs within the RegisterServices method add this line of code.
@@ -26,7 +27,13 @@ Inside of of your project's Startus.cs within the RegisterServices method add th
2627
services.UseIcgAspNetCoreUtilities();
2728
```
2829

29-
### Included Features
30+
If you desire to use the included Taghelpers inside of your `_viewimports.cshtml` add
31+
32+
```
33+
@addTagHelper *, ICG.AspNetCore.Utilities
34+
```
35+
36+
### Included C# Objects
3037

3138
| Object | Purpose |
3239
| ---- | --- |
@@ -43,3 +50,23 @@ services.UseIcgAspNetCoreUtilities();
4350
Detailed information can be found in the XML Comment documentation for the objects, we are working to add to this document as well.
4451

4552
**Caution:** As expected the use of both ForceNonWwwRewriteRule and ForceWwwRewriteRule in the same installation will result in broken sites.
53+
54+
### Included Tag Helpers
55+
56+
#### HideCondition
57+
This tag helper will hide the content of the target element if the condition is true, an example.
58+
59+
```
60+
<div hide-condition="Model.Deleted">
61+
62+
</div>
63+
```
64+
65+
#### Show Condition
66+
This tag helper will show the content of the target element if the condition is true, an example.
67+
68+
```
69+
<div show-condition="Model.Published">
70+
71+
</div>
72+
```

src/AspNetCore.Utilities.Tests/AspNetCore.Utilities.Tests.csproj

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.0</TargetFramework>
5-
4+
<TargetFramework>net5.0</TargetFramework>
65
<IsPackable>false</IsPackable>
7-
86
<RootNamespace>ICG.AspNetCore.Utilities.Tests</RootNamespace>
9-
10-
<ApplicationIcon />
11-
127
<OutputType>Library</OutputType>
13-
14-
<StartupObject />
158
</PropertyGroup>
169

1710
<ItemGroup>

src/AspNetCore.Utilities.Tests/CurrentEnvironmentInfoServicesTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ namespace ICG.AspNetCore.Utilities.Tests
1010
public class CurrentEnvironmentInfoServicesTests
1111
{
1212
private readonly ICurrentEnvironmentInfoService _currentEnvironmentInfoService;
13-
private readonly Mock<IHostingEnvironment> _hostingEnvironmentMock;
13+
private readonly Mock<IWebHostEnvironment> _hostingEnvironmentMock;
1414
private readonly Mock<IConfiguration> _configurationMock;
1515
private readonly Mock<IDatabaseEnvironmentModelFactory> _databaseEnvironmentModelFactoryMock;
1616

1717
public CurrentEnvironmentInfoServicesTests()
1818
{
19-
_hostingEnvironmentMock = new Mock<IHostingEnvironment>();
19+
_hostingEnvironmentMock = new Mock<IWebHostEnvironment>();
2020
_configurationMock = new Mock<IConfiguration>();
2121
_databaseEnvironmentModelFactoryMock = new Mock<IDatabaseEnvironmentModelFactory>();
2222
_currentEnvironmentInfoService = new CurrentEnvironmentInfoService(_hostingEnvironmentMock.Object,

src/AspNetCore.Utilities.Tests/TimeProviderTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Runtime.InteropServices;
23
using ICG.AspNetCore.Utilities;
34
using Xunit;
45

@@ -109,5 +110,43 @@ public void SecondsSinceEpoch_ShouldReturnProperValue()
109110
//Assert
110111
Assert.Equal(expectedResult, actualResult);
111112
}
113+
114+
[Fact]
115+
public void ConvertTimeFromUtc_ShouldThrowException_WhenUnknownTimezoneTarget()
116+
{
117+
//Arrange
118+
var startDate = DateTime.Now;
119+
var targetTimezone = "Happy Place";
120+
121+
//Act.Assert
122+
Assert.Throws<TimeZoneNotFoundException>(() => _timeProvider.ConvertTimeFromUtc(targetTimezone, startDate));
123+
}
124+
125+
[Fact]
126+
public void ConvertTimeToUtc_ShouldThrowException_WhenUnknownTimezoneTarget()
127+
{
128+
//Arrange
129+
var startDate = DateTime.Now;
130+
var targetTimezone = "Happy Place";
131+
132+
//Act.Assert
133+
Assert.Throws<TimeZoneNotFoundException>(() => _timeProvider.ConvertTimeToUtc(targetTimezone, startDate));
134+
}
135+
136+
[Fact]
137+
public void ConvertTimes_ShouldSupportConversionBothDirections()
138+
{
139+
//Arrange
140+
var startDate = DateTime.UtcNow;
141+
var targetTimezone = Timezones.CentralStandardTime;
142+
143+
//Act
144+
var convertedLocal = _timeProvider.ConvertTimeFromUtc(targetTimezone, startDate);
145+
var convertedUtc = _timeProvider.ConvertTimeToUtc(targetTimezone, convertedLocal);
146+
147+
//Assert
148+
Assert.Equal(startDate, convertedUtc);
149+
150+
}
112151
}
113152
}

src/AspNetCore.Utilities/AspNetCore.Utilities.csproj

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,44 @@
33
<PropertyGroup>
44
<Version>0.0.0</Version>
55
</PropertyGroup>
6+
7+
<PropertyGroup>
8+
<TargetFramework>net5.0</TargetFramework>
9+
<RootNamespace>ICG.AspNetCore.Utilities</RootNamespace>
10+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
11+
<ApplicationIcon />
12+
<OutputType>Library</OutputType>
13+
<StartupObject />
14+
</PropertyGroup>
15+
616
<PropertyGroup>
717
<PackageId>ICG.AspNetCore.Utilities</PackageId>
818
<Title>AspNetCore Utilities</Title>
919
<Description>A collection of utilities designed to impove the unit-testability or speed development of AspNetCore development projects.</Description>
10-
<Copyright>Copyright 2019, IowaComputerGurus, Subject to the MIT License</Copyright>
20+
<Copyright>Copyright 2021, IowaComputerGurus, Subject to the MIT License</Copyright>
1121
<PackageProjectUrl>https://github.com/IowaComputerGurus/aspnetcore.utilities</PackageProjectUrl>
1222
<PackageTags>aspnetcore;utility;unit-testing</PackageTags>
1323
<RepositoryUrl>https://github.com/IowaComputerGurus/aspnetcore.utilities</RepositoryUrl>
1424
<Authors>MitchelSellers;IowaComputerGurus</Authors>
1525
<Owners>IowaComputerGurus</Owners>
1626
<PackageIconUrl>https://raw.githubusercontent.com/IowaComputerGurus/aspnetcore.utilities/master/icgAppIcon.png</PackageIconUrl>
1727
<IsPackable>True</IsPackable>
28+
<PublishRepositoryUrl>true</PublishRepositoryUrl>
29+
<IncludeSymbols>true</IncludeSymbols>
30+
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
31+
<EmbedUntrackedSources>true</EmbedUntrackedSources>
1832
</PropertyGroup>
19-
<PropertyGroup>
20-
<TargetFramework>netcoreapp3.0</TargetFramework>
21-
<RootNamespace>ICG.AspNetCore.Utilities</RootNamespace>
22-
<GenerateDocumentationFile>true</GenerateDocumentationFile>
23-
<ApplicationIcon />
24-
<OutputType>Library</OutputType>
25-
<StartupObject />
33+
34+
<PropertyGroup Condition="'$(TF_BUILD)' == 'true'">
35+
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
2636
</PropertyGroup>
37+
2738
<ItemGroup>
2839
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.0-preview7.19362.4" />
40+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0">
41+
<PrivateAssets>all</PrivateAssets>
42+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
43+
</PackageReference>
2944
</ItemGroup>
3045

3146
</Project>

src/AspNetCore.Utilities/CurrentEnvironmentInfoService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public interface ICurrentEnvironmentInfoService
2323
/// <inheritdoc />
2424
public class CurrentEnvironmentInfoService : ICurrentEnvironmentInfoService
2525
{
26-
private readonly IHostingEnvironment _hostingEnvironment;
26+
private readonly IWebHostEnvironment _hostingEnvironment;
2727
private readonly IConfiguration _configuration;
2828
private readonly IDatabaseEnvironmentModelFactory _databaseEnvironmentModelFactory;
2929

@@ -33,7 +33,7 @@ public class CurrentEnvironmentInfoService : ICurrentEnvironmentInfoService
3333
/// <param name="hostingEnvironment">The current hosting environment information</param>
3434
/// <param name="configuration">The current configuration</param>
3535
/// <param name="databaseEnvironmentModelFactory">Factory to create new objects</param>
36-
public CurrentEnvironmentInfoService(IHostingEnvironment hostingEnvironment, IConfiguration configuration, IDatabaseEnvironmentModelFactory databaseEnvironmentModelFactory)
36+
public CurrentEnvironmentInfoService(IWebHostEnvironment hostingEnvironment, IConfiguration configuration, IDatabaseEnvironmentModelFactory databaseEnvironmentModelFactory)
3737
{
3838
_hostingEnvironment = hostingEnvironment;
3939
_configuration = configuration;

src/AspNetCore.Utilities/DirectoryProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ IEnumerable<string> EnumerateFileSystemEntries(string path, string searchPattern
355355
DateTime GetLastWriteTimeUtc(string path);
356356

357357
/// <summary>
358-
/// Retrieves the names of the logical drives on this computer in the form "<drive letter>:\".
358+
/// Retrieves the names of the logical drives on this computer in the form "&lt;drive letter&gt;:\".
359359
/// </summary>
360360
/// <returns></returns>
361361
string[] GetLogicalDrives();
@@ -424,6 +424,7 @@ IEnumerable<string> EnumerateFileSystemEntries(string path, string searchPattern
424424
void SetLastWriteTimeUtc(string path, DateTime lastWriteTimeUtc);
425425
}
426426

427+
/// <inheritdoc />
427428
public class DirectoryProvider : IDirectoryProvider
428429
{
429430
/// <inheritdoc />
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Microsoft.AspNetCore.Razor.TagHelpers;
2+
3+
namespace ICG.AspNetCore.Utilities.TagHelpers
4+
{
5+
/// <summary>
6+
/// A tag helper that if passed a tre value will suppress rendering content
7+
/// </summary>
8+
[HtmlTargetElement(Attributes = nameof(HideCondition))]
9+
public class HideConditionTagHelper : TagHelper
10+
{
11+
/// <summary>
12+
/// The boolean indicator if the element should be hidden
13+
/// </summary>
14+
public bool HideCondition { get; set; }
15+
16+
/// <summary>
17+
/// Processes the tag
18+
/// </summary>
19+
/// <param name="context">The input context</param>
20+
/// <param name="output">The output content</param>
21+
public override void Process(TagHelperContext context, TagHelperOutput output)
22+
{
23+
if (HideCondition)
24+
output.SuppressOutput();
25+
}
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Microsoft.AspNetCore.Razor.TagHelpers;
2+
3+
namespace ICG.AspNetCore.Utilities.TagHelpers
4+
{
5+
/// <summary>
6+
/// A tag helper that if passed a false value will suppress rendering content
7+
/// </summary>
8+
[HtmlTargetElement(Attributes = nameof(ShowCondition))]
9+
public class ShowConditionTagHelper : TagHelper
10+
{
11+
/// <summary>
12+
/// The boolean indicator if the element should be shown
13+
/// </summary>
14+
public bool ShowCondition { get; set; }
15+
16+
/// <summary>
17+
/// Processes the tag
18+
/// </summary>
19+
/// <param name="context">The input context</param>
20+
/// <param name="output">The output content</param>
21+
public override void Process(TagHelperContext context, TagHelperOutput output)
22+
{
23+
if (!ShowCondition)
24+
output.SuppressOutput();
25+
}
26+
}
27+
}

src/AspNetCore.Utilities/TimeProvider.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,24 @@ bool TryParseExact(string s, string[] formats, IFormatProvider provider, DateTim
158158
/// The Total # of Seconds since Epoch, based on the difference between <see cref="DateTime.UtcNow"/> and 1/1/1970
159159
/// </returns>
160160
ulong UtcNowSecondsSinceEpoch();
161+
162+
/// <summary>
163+
/// Provides a method to convert a UTC time to a time in the targeted timezone
164+
/// </summary>
165+
/// <param name="destinationTimeZone">The Timezone Targeted such as "Central Standard Time"</param>
166+
/// <param name="utcDateTime">The UTC date to be converted</param>
167+
/// <returns>The passed date, adjusted for the target timezone.</returns>
168+
/// <exception cref="TimeZoneNotFoundException">Thrown if the requested timezone doesn't exist</exception>
169+
DateTime ConvertTimeFromUtc(string destinationTimeZone, DateTime utcDateTime);
170+
171+
/// <summary>
172+
/// Provides a method to convert time from a local timezone back to UTC
173+
/// </summary>
174+
/// <param name="currentTimeZone">The current timezone</param>
175+
/// <param name="localDateTime">The current time</param>
176+
/// <returns>The updated time</returns>
177+
/// <exception cref="TimeZoneNotFoundException">Thrown if the requested timezone doesn't exist</exception>
178+
DateTime ConvertTimeToUtc(string currentTimeZone, DateTime localDateTime);
161179
}
162180

163181
/// <summary>
@@ -213,5 +231,19 @@ public ulong UtcNowSecondsSinceEpoch()
213231
{
214232
return SecondsSinceEpoch(UtcNow);
215233
}
234+
235+
/// <inheritdoc />
236+
public DateTime ConvertTimeFromUtc(string destinationTimeZone, DateTime utcDateTime)
237+
{
238+
var targetTimeZone = TimeZoneInfo.FindSystemTimeZoneById(destinationTimeZone);
239+
return TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, targetTimeZone);
240+
}
241+
242+
/// <inheritdoc />
243+
public DateTime ConvertTimeToUtc(string currentTimeZone, DateTime localDateTime)
244+
{
245+
var sourceTimezone = TimeZoneInfo.FindSystemTimeZoneById(currentTimeZone);
246+
return TimeZoneInfo.ConvertTimeToUtc(localDateTime, sourceTimezone);
247+
}
216248
}
217249
}

0 commit comments

Comments
 (0)