Skip to content

Commit

Permalink
Merge pull request #463 from NoxOrg/feature/time-configuration
Browse files Browse the repository at this point in the history
MP-8359 - Database configuration for "Time"
  • Loading branch information
rochar authored Aug 8, 2023
2 parents e7d550f + 964fe12 commit 6960747
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/Nox.Types.EntityFramework/Types/Time/TimeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ public class TimeConverter : ValueConverter<Time, System.DateTime>
/// <summary>
/// Initializes a new instance of the <see cref="TimeConverter"/> class.
/// </summary>
public TimeConverter() : base(time => time.Value.ToDateTime(), convertFromProviderExpression: timeValue => Time.From(timeValue.Ticks, null)) { }
public TimeConverter() : base(time => time.Value.ToDateTime(), convertFromProviderExpression: timeValue => Time.FromDatabase(timeValue)) { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Nox.Solution;
using Nox.Types.EntityFramework.Abstractions;
namespace Nox.Types.EntityFramework.Types;

/// <summary>
/// The time type database configuration.
/// </summary>
public class TimeDatabaseConfiguration : INoxTypeDatabaseConfigurator
{
/// <inheritdoc/>
public NoxType ForNoxType => NoxType.Time;

/// <inheritdoc/>
public bool IsDefault => true;

/// <inheritdoc/>
public void ConfigureEntityProperty(NoxSolutionCodeGeneratorState noxSolutionCodeGeneratorState, EntityTypeBuilder builder, NoxSimpleTypeDefinition property, Entity entity, bool isKey)
{
builder
.Property(property.Name)
.IsRequired(property.IsRequired)
.HasConversion<TimeConverter>();
}

/// <inheritdoc/>
public string GetKeyPropertyName(NoxSimpleTypeDefinition key) => key.Name;
}
2 changes: 1 addition & 1 deletion src/Nox.Types/Types/DateTimeDuration/DateTimeDuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public sealed class DateTimeDuration : ValueObject<long, DateTimeDuration>
private DateTimeDurationTypeOptions _dateTimeDurationTypeOptions = new();
private TimeSpan _duration;

/// <inheritdoc/>
/// <inheritdocs />
public new static DateTimeDuration FromDatabase(long value)
{
return new DateTimeDuration
Expand Down
18 changes: 10 additions & 8 deletions src/Nox.Types/Types/Time/Time.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ namespace Nox.Types;
/// </summary>
public sealed class Time : ValueObject<TimeOnly, Time> // This should be TimeOnly
{

private TimeTypeOptions _timeTypeOptions = new();

/// <summary>
/// Creates the Time object.
/// </summary>
/// <param name="hours">The hours.</param>
/// <param name="minutes">The minutes.</param>
/// <param name="timeTypeOptions">Time options.</param>
/// <returns>A Time.</returns>
public static Time From(int hours, int minutes, TimeTypeOptions? timeTypeOptions = null) => From(hours, minutes, 0, 0, timeTypeOptions);

Expand All @@ -25,6 +25,7 @@ public sealed class Time : ValueObject<TimeOnly, Time> // This should be TimeOnl
/// <param name="hours">The hours.</param>
/// <param name="minutes">The minutes.</param>
/// <param name="seconds">The seconds.</param>
/// <param name="timeTypeOptions">Time options.</param>
/// <returns>A Time.</returns>
public static Time From(int hours, int minutes, int seconds, TimeTypeOptions? timeTypeOptions = null) => From(hours, minutes, seconds, 0, timeTypeOptions);

Expand All @@ -35,6 +36,7 @@ public sealed class Time : ValueObject<TimeOnly, Time> // This should be TimeOnl
/// <param name="minutes">The minutes.</param>
/// <param name="seconds">The seconds.</param>
/// <param name="milliseconds">The milliseconds.</param>
/// <param name="timeTypeOptions">Time options.</param>
/// <returns>A Time.</returns>
public static Time From(int hours, int minutes, int seconds, int milliseconds, TimeTypeOptions? timeTypeOptions = null)
{
Expand All @@ -48,11 +50,11 @@ public static Time From(int hours, int minutes, int seconds, int milliseconds, T
/// Creates the Time object from ticks.
/// </summary>
/// <param name="ticks">The ticks.</param>
/// <param name="timeTypeOptions">Time options.</param>
/// <returns>A Time.</returns>
public static Time From(long ticks, TimeTypeOptions? timeTypeOptions = null)
{
return From(new TimeOnly(ticks), timeTypeOptions);

}

/// <summary>
Expand All @@ -73,9 +75,11 @@ public static Time From(TimeOnly time, TimeTypeOptions? timeTypeOptions)
}

return newObject;

}

/// <inheritdoc cref="ValueObject{T,TValueObject}.FromDatabase"/>
public static Time FromDatabase(System.DateTime value) => From(value.Ticks);

/// <summary>
/// Validates the contents of the Value object.
/// </summary>
Expand Down Expand Up @@ -107,8 +111,7 @@ internal override ValidationResult Validate()
/// <returns>
/// A <see cref="System.String" /> that represents this instance.
/// </returns>
public string ToString(string format)
=> ToString(format, CultureInfo.InvariantCulture);
public string ToString(string format) => ToString(format, CultureInfo.InvariantCulture);

/// <summary>
/// Returns the string in the provided culture and the long time format for the provided culture.
Expand All @@ -117,8 +120,7 @@ public string ToString(string format)
/// <returns>
/// A <see cref="System.String" /> that represents this instance.
/// </returns>
public string ToString(IFormatProvider formatProvider)
=> ToString("T", formatProvider);
public string ToString(IFormatProvider formatProvider) => ToString("T", formatProvider);

/// <summary>
/// Returns the string in the provided culture and format.
Expand All @@ -128,5 +130,5 @@ public string ToString(IFormatProvider formatProvider)
/// <returns>
/// A <see cref="System.String" /> that represents this instance.
/// </returns>
public string ToString(string format, IFormatProvider formatProvider)=> Value.ToString(format, formatProvider);
public string ToString(string format, IFormatProvider formatProvider) => Value.ToString(format, formatProvider);
}
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,9 @@ domain:
- name: DateTimeDurationTestField
type: dateTimeDuration

- name: TimeTestField
type: time

- name: VatNumberTestField
type: vatNumber

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ public partial class TestEntityForTypes : AuditableEntityBase
/// </summary>
public Nox.Types.DateTimeDuration? DateTimeDurationTestField { get; set; } = null!;

/// <summary>
/// (Optional).
/// </summary>
public Time? TimeTestField { get; set; } = null!;

/// <summary>
/// (Optional).
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public void GeneratedEntity_Postgres_CanSaveAndReadFields_AllTypes()
// dateTimeSchedule
// html
// json
// time

// TODO: commented types

Expand Down Expand Up @@ -68,6 +67,7 @@ public void GeneratedEntity_Postgres_CanSaveAndReadFields_AllTypes()
var dateTimeDurationInHours = 30.5;
var color = new byte[] { 1, 2, 3, 4 };
var date = new DateOnly(2023, 7, 14);
var time = new System.TimeOnly(11152500000);
var percentage = 0.5f;
var fileName = "MyFile";
var fileSizeInBytes = 1000000UL;
Expand Down Expand Up @@ -141,6 +141,7 @@ public void GeneratedEntity_Postgres_CanSaveAndReadFields_AllTypes()
MonthTestField = Month.From(month),
CurrencyNumberTestField = CurrencyNumber.From(currencyNumber),
DateTimeDurationTestField = DateTimeDuration.FromHours(dateTimeDurationInHours),
TimeTestField = Time.From(time.Ticks),
JsonTestField = Json.From(addressJsonPretty),
BooleanTestField = Types.Boolean.From(boolean),
EmailTestField = Email.From(email),
Expand Down Expand Up @@ -200,6 +201,7 @@ public void GeneratedEntity_Postgres_CanSaveAndReadFields_AllTypes()
testEntity.MonthTestField!.Value.Should().Be(month);
testEntity.CurrencyNumberTestField!.Value.Should().Be(currencyNumber);
testEntity.DateTimeDurationTestField!.TotalHours.Should().Be(dateTimeDurationInHours);
testEntity.TimeTestField!.ToString("hh:mm").Should().Be(time.ToString("hh:mm"));
testEntity.JsonTestField!.Value.Should().Be(addressJsonMinified);
testEntity.JsonTestField!.ToString(string.Empty).Should().Be(addressJsonPretty);
testEntity.JsonTestField!.ToString("p").Should().Be(addressJsonPretty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ public void GeneratedEntity_SqlServer_CanSaveAndReadFields_AllTypes()
// dateTimeSchedule
// html
// json
// time
// translatedText
// markdown
// jwtToken

// TODO: commented types

Expand Down Expand Up @@ -75,6 +71,7 @@ public void GeneratedEntity_SqlServer_CanSaveAndReadFields_AllTypes()
var vatNumberCountryCode2 = CountryCode2.From("FR");
var color = new byte[] { 1, 2, 3, 4 };
var date = new DateOnly(2023, 7, 14);
var time = new System.TimeOnly(11152500000);
var fileName = "MyFile";
var fileSizeInBytes = 1000000UL;
var fileUrl = "https://example.com/myfile.pdf";
Expand Down Expand Up @@ -150,6 +147,7 @@ public void GeneratedEntity_SqlServer_CanSaveAndReadFields_AllTypes()
DayOfWeekTestField = DayOfWeek.From(1),
MonthTestField = Month.From(month),
DateTimeDurationTestField = DateTimeDuration.FromHours(dateTimeDurationInHours),
TimeTestField = Time.From(time.Ticks),
CurrencyNumberTestField = CurrencyNumber.From(currencyNumber),
JsonTestField = Json.From(addressJsonPretty),
YearTestField = Year.From(year),
Expand Down Expand Up @@ -212,6 +210,7 @@ public void GeneratedEntity_SqlServer_CanSaveAndReadFields_AllTypes()
testEntity.DayOfWeekTestField!.Value.Should().Be(dayOfWeek);
testEntity.MonthTestField!.Value.Should().Be(month);
testEntity.DateTimeDurationTestField!.TotalHours.Should().Be(dateTimeDurationInHours);
testEntity.TimeTestField!.ToString("hh:mm").Should().Be(time.ToString("hh:mm"));
testEntity.CurrencyNumberTestField!.Value.Should().Be(currencyNumber);
testEntity.JsonTestField!.Value.Should().Be(addressJsonMinified);
testEntity.JsonTestField!.ToString(string.Empty).Should().Be(addressJsonPretty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public void GeneratedEntity_Sqlite_CanSaveAndReadFields_AllTypes()
// dateTimeSchedule
// html
// json
// time

// TODO: commented types

Expand Down Expand Up @@ -71,6 +70,7 @@ public void GeneratedEntity_Sqlite_CanSaveAndReadFields_AllTypes()
var vatNumberCountryCode2 = CountryCode2.From("FR");
var color = new byte[] { 1, 2, 3, 4 };
var date = new DateOnly(2023, 7, 14);
var time = new System.TimeOnly(11152500000);
var fileName = "MyFile";
var fileSizeInBytes = 1000000UL;
var fileUrl = "https://example.com/myfile.pdf";
Expand Down Expand Up @@ -146,6 +146,7 @@ public void GeneratedEntity_Sqlite_CanSaveAndReadFields_AllTypes()
DayOfWeekTestField = DayOfWeek.From(1),
MonthTestField = Month.From(month),
DateTimeDurationTestField = DateTimeDuration.FromHours(dateTimeDurationInHours),
TimeTestField = Time.From(time.Ticks),
CurrencyNumberTestField = CurrencyNumber.From(970),
JsonTestField = Json.From(addressJsonPretty),
YearTestField = Year.From(year),
Expand Down Expand Up @@ -209,6 +210,7 @@ public void GeneratedEntity_Sqlite_CanSaveAndReadFields_AllTypes()
testEntity.DayOfWeekTestField!.Value.Should().Be(dayOfWeek);
testEntity.MonthTestField!.Value.Should().Be(month);
testEntity.DateTimeDurationTestField!.TotalHours.Should().Be(dateTimeDurationInHours);
testEntity.TimeTestField!.ToString("hh:mm").Should().Be(time.ToString("hh:mm"));
testEntity.CurrencyNumberTestField!.Value.Should().Be(970);
testEntity.JsonTestField!.Value.Should().Be(addressJsonMinified);
testEntity.JsonTestField!.ToString(string.Empty).Should().Be(addressJsonPretty);
Expand Down
12 changes: 5 additions & 7 deletions tests/Nox.Types.Tests/Types/Time/TimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace Nox.Types.Tests.Types;
/// <summary>
/// The time tests.
/// </summary>

public class TimeTests
{
[Fact]
Expand Down Expand Up @@ -124,7 +123,7 @@ void Test()
int minute = 22;
int seconds = 35;
int milliseconds = 947;
var exception = Assert.Throws<ArgumentOutOfRangeException>(() => _ =
Assert.Throws<ArgumentOutOfRangeException>(() => _ =
Time.From(hour, minute, seconds, milliseconds)
);
}
Expand Down Expand Up @@ -154,8 +153,8 @@ public void From_WithInvalidMaxTicks_ThrowsArgumentOutOfRangeException()
void Test()
{
long ticks = 11111525000000;
var exception = Assert.Throws<ArgumentOutOfRangeException>(() => _ =
Time.From(ticks)
Assert.Throws<ArgumentOutOfRangeException>(() => _ =
Time.From(ticks)
);
}

Expand All @@ -168,10 +167,9 @@ public void From_WithNegativeTicks_ThrowsArgumentOutOfRangeException()
void Test()
{
long ticks = -11111525000000;
var exception = Assert.Throws<ArgumentOutOfRangeException>(() => _ =
Time.From(ticks)
Assert.Throws<ArgumentOutOfRangeException>(() => _ =
Time.From(ticks)
);

}

TestUtility.RunInCulture(Test, "en-GB");
Expand Down

0 comments on commit 6960747

Please sign in to comment.