Skip to content

Commit

Permalink
Fix relationship to User (#330)
Browse files Browse the repository at this point in the history
* Fix relationship to User

* Fix relationship to user

* relation to user with services

* Fix User relationship
  • Loading branch information
nicolas63 authored Sep 1, 2020
1 parent 2aae5e3 commit 7d0ce92
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,17 @@ namespace <%= namespace %>.Domain.Services {

public async Task<<%= pascalizedEntityClass %>> Save(<%= pascalizedEntityClass %> <%= camelCasedEntityClass %>)
{
if (<%= camelCasedEntityClass %>.Id != 0 && _applicationDatabaseContext.<%= pascalizedEntityClassPlural %>.Exists(x => x.Id == <%= camelCasedEntityClass %>.Id)) {
<%_ relationships.forEach( relationship => {
if (relationship.relationshipType === 'many-to-many' && relationship.ownerSide) { _%>
<%_ relationships.forEach( relationship => {
if (relationship.relationshipType === 'many-to-many' && relationship.ownerSide) { _%>
_applicationDatabaseContext.<%= relationship.joinEntityFieldNamePascalizedPlural %>.RemoveNavigationProperty(<%= camelCasedEntityClass %>, <%= camelCasedEntityClass %>.Id);
<%_ }
}); _%>
if (relationship.otherEntityNamePascalized === 'User' && relationship.relationshipType !== 'many-to-many') { _%>
<%= camelCasedEntityClass %>.UserId = <%= camelCasedEntityClass %>.User?.Id;
<%= camelCasedEntityClass %>.User = null;
<%_ }
}); _%>
if (<%= camelCasedEntityClass %>.Id != 0 && _applicationDatabaseContext.<%= pascalizedEntityClassPlural %>.Exists(x => x.Id == <%= camelCasedEntityClass %>.Id)) {
_applicationDatabaseContext.<%= pascalizedEntityClassPlural %>.Update(<%= camelCasedEntityClass %>);
<%_ let first = true;
let suffix = 0;
relationships.forEach( relationship => {
Expand All @@ -61,7 +66,6 @@ namespace <%= namespace %>.Domain.Services {
<%_ }
}
}); _%>
_applicationDatabaseContext.<%= pascalizedEntityClassPlural %>.Update(<%= camelCasedEntityClass %>);
} else {
<%_ if (relationships.length != 0) { _%>
_applicationDatabaseContext.AddGraph(<%= camelCasedEntityClass %>);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,21 @@ namespace <%= namespace %>.Domain {
const otherEntityNamePascalized = relationships[idx].otherEntityNamePascalized;
const relationshipFieldNamePascalizedPlural = relationships[idx].relationshipFieldNamePascalizedPlural;
const relationshipFieldNamePascalized = relationships[idx].relationshipFieldNamePascalized;
if (relationshipType === 'one-to-one'){_%>
if (relationshipType === 'one-to-one') {
if(otherEntityNamePascalized === 'User') { _%>
public string UserId { get; set; }
<%_ } _%>
public <%= otherEntityNamePascalized %> <%= relationshipFieldNamePascalized %> { get; set; }
<%_}
if(relationshipType === 'one-to-many') { _%>
public IList<<%= otherEntityNamePascalized %>> <%= relationshipFieldNamePascalizedPlural %> { get; set; } = new List<<%= otherEntityNamePascalized %>>();
<%_ } else if (relationshipType === 'many-to-one') {
if (relationshipRequired) { _%>
[Required]
<%_ } _%>
<%_ }
if(otherEntityNamePascalized === 'User'){ _%>
public string UserId { get; set; }
<%_ } _%>
public <%= otherEntityNamePascalized %> <%= relationshipFieldNamePascalized %> { get; set; }
<%_ } else if (relationshipType === 'many-to-many') {
const joinEntityNamePascalized = relationships[idx].joinEntityNamePascalized ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,14 @@ namespace <%= namespace %>.Controllers {
<%_ if (hasService) { _%>
await _<%= camelCasedEntityClass %>Service.Save(<%= camelCasedEntityClass %>);
<%_ } else { _%>
<%_ if (relationships.length != 0) { _%>
<%_ if (relationships.length != 0) {
if (hasDto) {
relationships.forEach( relationship => {
if (relationship.otherEntityNamePascalized === 'User' && relationship.relationshipType !== 'many-to-many') { _%>
<%= camelCasedEntityClass %>.User = null;
<%_ }
});
} _%>
_applicationDatabaseContext.AddGraph(<%= camelCasedEntityClass %>);
<%_ } else { _%>
_applicationDatabaseContext.<%= pascalizedEntityClassPlural %>.Add(<%= camelCasedEntityClass %>);
Expand Down Expand Up @@ -137,7 +144,11 @@ namespace <%= namespace %>.Controllers {
if (relationship.relationshipType === 'many-to-many' && relationship.ownerSide) { _%>
_applicationDatabaseContext.<%= relationship.joinEntityFieldNamePascalizedPlural %>.RemoveNavigationProperty(<%= camelCasedEntityClass %>, <%= camelCasedEntityClass %>.Id);
<%_ }
}); _%>
if (relationship.otherEntityNamePascalized === 'User' && relationship.relationshipType !== 'many-to-many') { _%>
<%= camelCasedEntityClass %>.UserId = <%= camelCasedEntityClass %>.User.Id;
<%= camelCasedEntityClass %>.User = null;
<%_ }
}); _%>
_applicationDatabaseContext.Update(<%= camelCasedEntityClass %>);
<%_ let first = true;
let suffix = 0;
Expand Down

0 comments on commit 7d0ce92

Please sign in to comment.