Skip to content

Commit

Permalink
NOX-169 AutoNumber Fields can not be set on the Create and Update Dto (
Browse files Browse the repository at this point in the history
…#1102)

* Removing autonumber from factory and create/update dtos

* Updating factory test

* Added Create flag for the nox types to mark entities that are not user created
  • Loading branch information
emir-sehovic1 authored Nov 14, 2023
1 parent de3e58a commit 5f80924
Show file tree
Hide file tree
Showing 27 changed files with 492 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,6 @@ public abstract class BookingCreateDtoBase : IEntityDto<DomainNamespace.Booking>
/// </summary>
public virtual System.DateTimeOffset? CancelledDateTime { get; set; }
/// <summary>
/// Booking's status
/// <remarks>Optional</remarks>
/// </summary>
public virtual System.String? Status { get; set; }
/// <summary>
/// Booking's related vat number
/// <remarks>Optional</remarks>
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ public abstract class CashStockOrderCreateDtoBase : IEntityDto<DomainNamespace.C
/// <remarks>Optional</remarks>
/// </summary>
public virtual System.DateTimeOffset? DeliveryDateTime { get; set; }
/// <summary>
/// Order status
/// <remarks>Optional</remarks>
/// </summary>
public virtual System.String? Status { get; set; }

/// <summary>
/// CashStockOrder for ExactlyOne VendingMachines
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ public abstract class CountryCreateDtoBase : IEntityDto<DomainNamespace.Country>
/// </summary>
public virtual System.String? FirstLanguageCode { get; set; }
/// <summary>
/// The Formula
/// <remarks>Optional</remarks>
/// </summary>
public virtual System.String? ShortDescription { get; set; }
/// <summary>
/// Country's iso number id
/// <remarks>Optional</remarks>
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ public abstract class StoreLicenseCreateDtoBase : IEntityDto<DomainNamespace.Sto
[Required(ErrorMessage = "Issuer is required")]

public virtual System.String Issuer { get; set; } = default!;
/// <summary>
/// License external id
/// <remarks>Required</remarks>
/// </summary>
[Required(ErrorMessage = "ExternalId is required")]

public virtual System.Int64 ExternalId { get; set; } = default!;

/// <summary>
/// StoreLicense Store that this license related to ExactlyOne Stores
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ public partial class StoreLicenseUpdateDto : IEntityDto<DomainNamespace.StoreLic
[Required(ErrorMessage = "Issuer is required")]

public System.String Issuer { get; set; } = default!;
/// <summary>
/// License external id
/// <remarks>Required.</remarks>
/// </summary>
[Required(ErrorMessage = "ExternalId is required")]

public System.Int64 ExternalId { get; set; } = default!;

/// <summary>
/// StoreLicense Store that this license related to ExactlyOne Stores
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ public abstract class WorkplaceCreateDtoBase : IEntityDto<DomainNamespace.Workpl
/// <remarks>Optional</remarks>
/// </summary>
public virtual System.String? Description { get; set; }
/// <summary>
/// The Formula
/// <remarks>Optional</remarks>
/// </summary>
public virtual System.String? Greeting { get; set; }

/// <summary>
/// Workplace Workplace country ZeroOrOne Countries
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ private ClientApi.Domain.StoreLicense ToEntity(StoreLicenseCreateDto createDto)
private void UpdateEntityInternal(StoreLicenseEntity entity, StoreLicenseUpdateDto updateDto, Nox.Types.CultureCode cultureCode)
{
entity.Issuer = ClientApi.Domain.StoreLicenseMetadata.CreateIssuer(updateDto.Issuer.NonNullValue<System.String>());
entity.ExternalId = ClientApi.Domain.StoreLicenseMetadata.CreateExternalId(updateDto.ExternalId.NonNullValue<System.Int64>());
}

private void PartialUpdateEntityInternal(StoreLicenseEntity entity, Dictionary<string, dynamic> updatedProperties, Nox.Types.CultureCode cultureCode)
Expand All @@ -74,17 +73,6 @@ private void PartialUpdateEntityInternal(StoreLicenseEntity entity, Dictionary<s
entity.Issuer = ClientApi.Domain.StoreLicenseMetadata.CreateIssuer(IssuerUpdateValue);
}
}

if (updatedProperties.TryGetValue("ExternalId", out var ExternalIdUpdateValue))
{
if (ExternalIdUpdateValue == null)
{
throw new ArgumentException("Attribute 'ExternalId' can't be null");
}
{
entity.ExternalId = ClientApi.Domain.StoreLicenseMetadata.CreateExternalId(ExternalIdUpdateValue);
}
}
}

private static bool IsDefaultCultureCode(Nox.Types.CultureCode cultureCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public async Task Post_RefToCurrency_Succes()
//Arrange
var usdCurrency = await PostAsync<CurrencyCreateDto, CurrencyDto>(Endpoints.CurrenciesUrl, new CurrencyCreateDto { Id = "USD" });
var eurCurrency = await PostAsync<CurrencyCreateDto, CurrencyDto>(Endpoints.CurrenciesUrl, new CurrencyCreateDto { Id = "EUR" });
var store = await CreateStore();
var store = await CreateStoreAsync();
var storeLicenseReponse = await PostAsync<StoreLicenseCreateDto, StoreLicenseDto>(Endpoints.StoreLicensesUrl,
new StoreLicenseCreateDto {
Issuer = _fixture.Create<string>(),
Expand Down Expand Up @@ -72,8 +72,8 @@ public async Task Post_RefToCurrency_Succes()
public async Task Put_UpdateRelatedStore_Success()
{
//Arrange
var store1 = await CreateStore();
var store2 = await CreateStore();
var store1 = await CreateStoreAsync();
var store2 = await CreateStoreAsync();
var storeLicenseResponse = await PostAsync<StoreLicenseCreateDto, StoreLicenseDto>(Endpoints.StoreLicensesUrl,
new StoreLicenseCreateDto
{
Expand Down Expand Up @@ -102,7 +102,7 @@ await PutAsync<StoreLicenseUpdateDto, StoreLicenseDto>($"{Endpoints.StoreLicense
public async Task Put_UpdateRelatedStoreToEmpty_Fail()
{
//Arrange
var store1 = await CreateStore();
var store1 = await CreateStoreAsync();
var storeLicenseResponse = await PostAsync<StoreLicenseCreateDto, StoreLicenseDto>(Endpoints.StoreLicensesUrl,
new StoreLicenseCreateDto
{
Expand Down Expand Up @@ -140,8 +140,8 @@ public async Task GetById_WithRelationshipSet_ReturnsValidData()
{
var issuer = _fixture.Create<string>();
// Arrange
var store = await CreateStore();
var store2 = await CreateStore();
var store = await CreateStoreAsync();
var store2 = await CreateStoreAsync();

var createDto = new StoreLicenseCreateDto
{
Expand Down Expand Up @@ -169,7 +169,7 @@ public async Task GetById_WithRelationshipNotSet_ReturnsValidData()
{
var issuer = _fixture.Create<string>();
// Arrange
StoreDto? store = await CreateStore();
StoreDto? store = await CreateStoreAsync();

var createDto = new StoreLicenseCreateDto
{
Expand All @@ -195,8 +195,8 @@ public async Task GetById_WithRelationshipNotSet_ReturnsValidData()
public async Task WhenStoreLicenceCreated_ShouldGenerateAutoNumberExternalId()
{
//Arrange
var store1 = await CreateStore();
var store2 = await CreateStore();
var store1 = await CreateStoreAsync();
var store2 = await CreateStoreAsync();

// Act
var storeLicenseResponse = await PostAsync<StoreLicenseCreateDto, StoreLicenseDto>(Endpoints.StoreLicensesUrl,
Expand All @@ -215,13 +215,61 @@ public async Task WhenStoreLicenceCreated_ShouldGenerateAutoNumberExternalId()

//Assert
storeLicenseResponse.Should().NotBeNull();
storeLicenseResponse!.ExternalId.Should().BeGreaterOrEqualTo(30000);
storeLicenseResponse!.ExternalId.Should().Be(3000000);

storeLicenseResponse2.Should().NotBeNull();
storeLicenseResponse2!.ExternalId.Should().BeGreaterOrEqualTo(30000 + 10);
storeLicenseResponse2!.ExternalId.Should().Be(3000000 + 10);
}

private async Task<StoreDto?> CreateStore()
[Fact]
public async Task WhenStoreLicenceCreated_AutoNumberExternalIdIsSystemGenerated()
{
//Arrange
var store = await CreateStoreAsync();
var storeLicenseCreateResponse = await PostAsync<object, StoreLicenseDto>(Endpoints.StoreLicensesUrl,
new
{
Issuer = _fixture.Create<string>(),
StoreId = store!.Id,
ExternalId = 123456
});

// Act
//Assert
storeLicenseCreateResponse.Should().NotBeNull();
storeLicenseCreateResponse!.ExternalId.Should().Be(3000000);
}

[Fact]
public async Task WhenStoreLicenceUpdated_AutoNumberExternalIdIsNotUpdated()
{
//Arrange
var store = await CreateStoreAsync();
var storeLicenseCreateResponse = await PostAsync<object, StoreLicenseDto>(Endpoints.StoreLicensesUrl,
new
{
Issuer = _fixture.Create<string>(),
StoreId = store!.Id,
});
var externalId = storeLicenseCreateResponse!.ExternalId;

// Act
var headers = CreateEtagHeader(storeLicenseCreateResponse!.Etag);
var storeLicenseUpdateResponse = await PutAsync<object, StoreLicenseDto>($"{Endpoints.StoreLicensesUrl}/{storeLicenseCreateResponse!.Id}",
new
{
Issuer = _fixture.Create<string>(),
StoreId = store!.Id,
ExternalId = 123456
},
headers);

//Assert
storeLicenseUpdateResponse.Should().NotBeNull();
storeLicenseUpdateResponse!.ExternalId.Should().Be(externalId);
}

private async Task<StoreDto?> CreateStoreAsync()
{
var createDto = new StoreCreateDto
{
Expand Down
23 changes: 14 additions & 9 deletions src/Nox.Generator.Tasks/Common/ScribanScriptsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public static void AddFunctions(TemplateContext context, NoxSolution noxSolution
NoxType>(noxSolution.GetSingleTypeForKey));

var scriptObject3 = new ScriptObject();
scriptObject2.Import("SingleKeyPrimitiveTypeForEntity", new Func<string,
scriptObject3.Import("SingleKeyPrimitiveTypeForEntity", new Func<string,
string>(noxSolution.GetSingleKeyPrimitiveTypeForEntity));

var scriptObject4 = new ScriptObject();
scriptObject2.Import("SinglePrimitiveTypeForKey", new Func<NoxSimpleTypeDefinition,
scriptObject4.Import("SinglePrimitiveTypeForKey", new Func<NoxSimpleTypeDefinition,
string>(noxSolution.GetSinglePrimitiveTypeForKey));

var scriptObject5 = new ScriptObject();
Expand All @@ -37,19 +37,23 @@ public static void AddFunctions(TemplateContext context, NoxSolution noxSolution
bool>(type => type.IsUpdatableType()));

var scriptObject7 = new ScriptObject();
scriptObject7.Import("IsNoxTypeSimpleType", new Func<NoxType,
bool>(type => type.IsSimpleType()));
scriptObject7.Import("IsNoxTypeCreatable", new Func<NoxType,
bool>(type => type.IsCreatableType()));

var scriptObject8 = new ScriptObject();
scriptObject8.Import("IsValueType", new Func<string,
bool>(type => Type.GetType(type)?.IsValueType ?? false));
scriptObject8.Import("IsNoxTypeSimpleType", new Func<NoxType,
bool>(type => type.IsSimpleType()));

var scriptObject9 = new ScriptObject();
scriptObject9.Import("Pluralize", new Func<string,
scriptObject9.Import("IsValueType", new Func<string,
bool>(type => Type.GetType(type)?.IsValueType ?? false));

var scriptObject10 = new ScriptObject();
scriptObject10.Import("Pluralize", new Func<string,
string>(name => name.Pluralize()));

var scriptObject10 = new ScriptObject();
scriptObject10.Import("ToLowerFirstChar", new Func<string, string>(
var scriptObject11 = new ScriptObject();
scriptObject11.Import("ToLowerFirstChar", new Func<string, string>(
input => input.ToLowerFirstChar()));


Expand All @@ -63,5 +67,6 @@ public static void AddFunctions(TemplateContext context, NoxSolution noxSolution
context.PushGlobal(scriptObject8);
context.PushGlobal(scriptObject9);
context.PushGlobal(scriptObject10);
context.PushGlobal(scriptObject11);
}
}
3 changes: 3 additions & 0 deletions src/Nox.Generator/Application/Dto/EntityCreateDto.template.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public abstract class {{className}}Base : IEntityDto<DomainNamespace.{{entity.Na
{{- end }}

{{- for attribute in entity.Attributes }}
{{- if !IsNoxTypeCreatable attribute.Type -}}
{{ continue; -}}
{{- end }}
/// <summary>
/// {{attribute.Description | string.rstrip}}
/// <remarks>{{if attribute.IsRequired}}Required{{else}}Optional{{end}}</remarks>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public virtual void PartialUpdateEntity({{entity.Name}}Entity entity, Dictionary
private void UpdateEntityInternal({{entity.Name}}Entity entity, {{entity.Name}}UpdateDto updateDto, Nox.Types.CultureCode cultureCode)
{
{{- for attribute in entity.Attributes }}
{{- if !IsNoxTypeReadable attribute.Type || attribute.Type == "Formula" -}}
{{- if !IsNoxTypeReadable attribute.Type || !IsNoxTypeUpdatable attribute.Type -}}
{{ continue; }}
{{- end}}
{{ if attribute.IsLocalized }}if(IsDefaultCultureCode(cultureCode)) {{ end }}
Expand All @@ -139,7 +139,7 @@ private void UpdateEntityInternal({{entity.Name}}Entity entity, {{entity.Name}}U
private void PartialUpdateEntityInternal({{entity.Name}}Entity entity, Dictionary<string, dynamic> updatedProperties, Nox.Types.CultureCode cultureCode)
{
{{- for attribute in entity.Attributes }}
{{- if !IsNoxTypeReadable attribute.Type || attribute.Type == "Formula" -}}
{{- if !IsNoxTypeReadable attribute.Type || !IsNoxTypeUpdatable attribute.Type -}}
{{ continue; }}
{{- end}}

Expand Down
31 changes: 18 additions & 13 deletions src/Nox.Generator/Common/ScribanScriptsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,35 @@ public static void AddFunctions(TemplateContext context, NoxSolution noxSolution
bool>(type => type.IsUpdatableType()));

var scriptObject7 = new ScriptObject();
scriptObject7.Import("IsNoxTypeSimpleType", new Func<NoxType,
bool>(type => type.IsSimpleType()));
scriptObject7.Import("IsNoxTypeCreatable", new Func<NoxType,
bool>(type => type.IsCreatableType()));

var scriptObject8 = new ScriptObject();
scriptObject8.Import("IsValueType", new Func<string,
bool>(type => Type.GetType(type)?.IsValueType ?? false));
scriptObject8.Import("IsNoxTypeSimpleType", new Func<NoxType,
bool>(type => type.IsSimpleType()));

var scriptObject9 = new ScriptObject();
scriptObject9.Import("Pluralize", new Func<string,
scriptObject9.Import("IsValueType", new Func<string,
bool>(type => Type.GetType(type)?.IsValueType ?? false));

var scriptObject10 = new ScriptObject();
scriptObject10.Import("Pluralize", new Func<string,
string>(name => name.Pluralize()));

var scriptObject10 = new ScriptObject();
scriptObject10.Import("ToLowerFirstChar", new Func<string, string>(
var scriptObject11 = new ScriptObject();
scriptObject11.Import("ToLowerFirstChar", new Func<string, string>(
input => input.ToLowerFirstChar()));

var scriptObject11 = new ScriptObject();
scriptObject11.Import("GetEntityNameForLocalizedType", new Func<string, string>(
var scriptObject12 = new ScriptObject();
scriptObject12.Import("GetEntityNameForLocalizedType", new Func<string, string>(
entityName => NoxCodeGenConventions.GetEntityNameForLocalizedType(entityName)));

var scriptObject12 = new ScriptObject();
scriptObject12.Import("GetEntityDtoNameForLocalizedType", new Func<string, string>(
var scriptObject13 = new ScriptObject();
scriptObject13.Import("GetEntityDtoNameForLocalizedType", new Func<string, string>(
entityName => NoxCodeGenConventions.GetEntityDtoNameForLocalizedType(entityName)));

var scriptObject13 = new ScriptObject();
scriptObject13.Import("GetNavigationPropertyName", new Func<Entity, EntityRelationship, string>(
var scriptObject14 = new ScriptObject();
scriptObject14.Import("GetNavigationPropertyName", new Func<Entity, EntityRelationship, string>(
(entity, relationship) => entity.GetNavigationPropertyName(relationship)));

context.PushGlobal(scriptObject1);
Expand All @@ -80,5 +84,6 @@ public static void AddFunctions(TemplateContext context, NoxSolution noxSolution
context.PushGlobal(scriptObject11);
context.PushGlobal(scriptObject12);
context.PushGlobal(scriptObject13);
context.PushGlobal(scriptObject14);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ public class CompoundTypeAttribute : Attribute, IDtoGenerateControl
{
public virtual bool Read { get; set; }
public virtual bool Update { get; set; }
public virtual bool Create { get; set; }

public CompoundTypeAttribute(bool read = true, bool update = true)
public CompoundTypeAttribute(bool read = true, bool update = true, bool create = true)
{
Read = read;
Update = update;
Create = create;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ internal interface IDtoGenerateControl
{
public bool Read { get; set; }
public bool Update { get; set; }
public bool Create { get; set; }
}
Loading

0 comments on commit 5f80924

Please sign in to comment.