Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MP-8148 [BS-8103] NOX - Database configuration for "DateTime" #470

Merged
merged 26 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .nox/design/allnoxtypes.entity.nox.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ attributes:
type: currencyCode3
isRequired: false

# - name: DateTimeField
# description: DatetimeField Nox Type
# type: dateTime
# isRequired: false
- name: DateTimeField
description: DatetimeField Nox Type
type: dateTime
isRequired: false

- name: FormulaField
description: Formula Nox Type
Expand Down
30 changes: 12 additions & 18 deletions schemas/domainCommand.json
Original file line number Diff line number Diff line change
Expand Up @@ -1672,17 +1672,15 @@
"properties": {
"minValue": {
"type": [
"string",
"object",
"null"
],
"format": "date-time"
]
},
"maxValue": {
"type": [
"string",
"object",
"null"
],
"format": "date-time"
]
},
"allowFutureOnly": {
"type": [
Expand Down Expand Up @@ -5613,17 +5611,15 @@
"properties": {
"minValue": {
"type": [
"string",
"object",
"null"
],
"format": "date-time"
]
},
"maxValue": {
"type": [
"string",
"object",
"null"
],
"format": "date-time"
]
},
"allowFutureOnly": {
"type": [
Expand Down Expand Up @@ -9303,17 +9299,15 @@
"properties": {
"minValue": {
"type": [
"string",
"object",
"null"
],
"format": "date-time"
]
},
"maxValue": {
"type": [
"string",
"object",
"null"
],
"format": "date-time"
]
},
"allowFutureOnly": {
"type": [
Expand Down
30 changes: 12 additions & 18 deletions schemas/dto.json
Original file line number Diff line number Diff line change
Expand Up @@ -1645,17 +1645,15 @@
"properties": {
"minValue": {
"type": [
"string",
"object",
"null"
],
"format": "date-time"
]
},
"maxValue": {
"type": [
"string",
"object",
"null"
],
"format": "date-time"
]
},
"allowFutureOnly": {
"type": [
Expand Down Expand Up @@ -5573,17 +5571,15 @@
"properties": {
"minValue": {
"type": [
"string",
"object",
"null"
],
"format": "date-time"
]
},
"maxValue": {
"type": [
"string",
"object",
"null"
],
"format": "date-time"
]
},
"allowFutureOnly": {
"type": [
Expand Down Expand Up @@ -9107,17 +9103,15 @@
"properties": {
"minValue": {
"type": [
"string",
"object",
"null"
],
"format": "date-time"
]
},
"maxValue": {
"type": [
"string",
"object",
"null"
],
"format": "date-time"
]
},
"allowFutureOnly": {
"type": [
Expand Down
10 changes: 4 additions & 6 deletions schemas/type.json
Original file line number Diff line number Diff line change
Expand Up @@ -1179,17 +1179,15 @@
"properties": {
"minValue": {
"type": [
"string",
"object",
"null"
],
"format": "date-time"
]
},
"maxValue": {
"type": [
"string",
"object",
"null"
],
"format": "date-time"
]
},
"allowFutureOnly": {
"type": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Nox.EntityFramework.Postgres;

public class PostgresMarkdownDatabaseConfiguration : MarkdownDatabaseConfigurator
public class PostgresMarkdownDatabaseConfiguration : MarkdownDatabaseConfigurator, IPostgresNoxTypeDatabaseConfigurator
{
public override bool IsDefault => false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Nox.EntityFramework.Postgres;

public class PostgresTextDatabaseConfiguration : TextDatabaseConfigurator
public class PostgresTextDatabaseConfiguration : TextDatabaseConfigurator, IPostgresNoxTypeDatabaseConfigurator
{
public override bool IsDefault => false;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;


namespace Nox.EntityFramework.Postgres.Types.DateTime;

public class PostgresDateTimeConverter : ValueConverter<Nox.Types.DateTime, System.DateTime>
{
public PostgresDateTimeConverter()
: base(
dateTime => dateTime.Value.UtcDateTime,
dateTimeValue => Nox.Types.DateTime.FromDatabase(new DateTimeOffset(dateTimeValue, TimeSpan.Zero)))
{ }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Nox.Solution;
using Nox.Types;
using Nox.Types.EntityFramework.Types;

namespace Nox.EntityFramework.Postgres.Types.DateTime;

public class PostgresDateTimeDatabaseConfiguration : DateTimeDatabaseConfigurator, IPostgresNoxTypeDatabaseConfigurator
{
public override bool IsDefault => false;

public override void ConfigureEntityProperty(
NoxSolutionCodeGeneratorState noxSolutionCodeGeneratorState,
EntityTypeBuilder builder,
NoxSimpleTypeDefinition property,
Entity entity,
bool isKey)
{
builder
.Property(property.Name)
.IsRequired(property.IsRequired)
.HasConversion<PostgresDateTimeConverter>();
}
}
2 changes: 1 addition & 1 deletion src/Nox.Types.Abstractions/Enums/NoxType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public enum NoxType : uint
[SimpleType(typeof(short))]
Year = 3709785124,

[SimpleType(typeof(DateTime))]
[SimpleType(typeof(DateTimeOffset))]
DateTime = 2998644573,

[SimpleType(typeof(TimeSpan))]
Expand Down
10 changes: 6 additions & 4 deletions src/Nox.Types.Abstractions/TypeOptions/DateTimeTypeOptions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@

using System;

namespace Nox.Types;
public class DateTimeTypeOptions : INoxTypeOptions
{
public static readonly System.DateTime DefaultMinValue = System.DateTime.MinValue;
public static readonly System.DateTime DefaultMaxValue = System.DateTime.MaxValue;
public System.DateTime MinValue { get; set; } = DefaultMinValue;
public System.DateTime MaxValue { get; set; } = DefaultMaxValue;
public static readonly DateTimeOffset DefaultMinValue = DateTimeOffset.MinValue;
public static readonly DateTimeOffset DefaultMaxValue = DateTimeOffset.MaxValue;
public DateTimeOffset MinValue { get; set; } = DefaultMinValue;
public DateTimeOffset MaxValue { get; set; } = DefaultMaxValue;
public bool AllowFutureOnly { get; set; } = false;
}

Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

namespace Nox.Types.EntityFramework.Types;

public class DateTimeConverter : ValueConverter<DateTime, System.DateTime>
public class DateTimeConverter : ValueConverter<DateTime, DateTimeOffset>
{
public DateTimeConverter() : base(dt => dt.Value, dtValue => DateTime.From(dtValue)) { }

}
public DateTimeConverter()
: base(
d => d.Value,
d => DateTime.FromDatabase(d))
{ }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Nox.Solution;
using Nox.Types.EntityFramework.Abstractions;

namespace Nox.Types.EntityFramework.Types;
public class DateTimeDatabaseConfigurator : INoxTypeDatabaseConfigurator
{
public NoxType ForNoxType => NoxType.DateTime;

public virtual bool IsDefault => true;

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

public string GetKeyPropertyName(NoxSimpleTypeDefinition key) => key.Name;
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Nox.Types.EntityFramework.Types;

public class DateTimeRangeConverter : ValueConverter<DateTimeOffset, System.DateTime>
public class DateTimeOffsetConverter : ValueConverter<DateTimeOffset, System.DateTime>
{
public DateTimeRangeConverter()
public DateTimeOffsetConverter()
: base(
d => d.UtcDateTime,
d => new DateTimeOffset(d, TimeSpan.Zero))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public void ConfigureEntityProperty(
.OwnsOne(typeof(DateTimeRange), property.Name, dtr =>
{
dtr.Property(nameof(DateTimeRange.Start))
.HasConversion<DateTimeRangeConverter>();
.HasConversion<DateTimeOffsetConverter>();

dtr.Property(nameof(DateTimeRange.End))
.HasConversion<DateTimeRangeConverter>();
.HasConversion<DateTimeOffsetConverter>();

dtr.Property(nameof(DateTimeRange.StartTimeZoneOffset))
.UsePropertyAccessMode(PropertyAccessMode.Property);
Expand Down
Loading