Skip to content

Commit

Permalink
Improve Entity Generator Code in Controller (#343)
Browse files Browse the repository at this point in the history
Controller  was using dbcontext instead of service layer.

Fix #342
  • Loading branch information
ivanmonteiro authored Aug 22, 2020
1 parent 23a66aa commit 2aae5e3
Showing 1 changed file with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ namespace <%= namespace %>.Controllers {
public class <%= pascalizedEntityClass %>Controller : ControllerBase {
private const string EntityName = "<%= camelCasedEntityClass %>";

private readonly ApplicationDatabaseContext _applicationDatabaseContext;
<%_ if (hasDto) { _%>
private readonly IMapper _mapper;
<%_ } _%>
<%_ if (hasService) { _%>
private readonly I<%= pascalizedEntityClass %>Service _<%= camelCasedEntityClass %>Service;
<%_ } else { _%>
private readonly ApplicationDatabaseContext _applicationDatabaseContext;
<%_ } _%>
private readonly ILogger<<%= pascalizedEntityClass %>Controller> _log;

Expand All @@ -77,18 +78,19 @@ namespace <%= namespace %>.Controllers {
IMapper mapper,
<%_ } _%>
<%_ if (hasService) { _%>
I<%= pascalizedEntityClass %>Service <%= camelCasedEntityClass %>Service,
<%_ } _%>
ApplicationDatabaseContext applicationDatabaseContext)
I<%= pascalizedEntityClass %>Service <%= camelCasedEntityClass %>Service)
<%_ } else { _%>
ApplicationDatabaseContext applicationDatabaseContext)<%_ } _%>
{
_log = log;
<%_ if (hasDto) { _%>
_mapper = mapper;
<%_ } _%>
<%_ if (hasService) { _%>
_<%= camelCasedEntityClass %>Service = <%= camelCasedEntityClass %>Service;
<%_ } _%>
<%_ } else { _%>
_applicationDatabaseContext = applicationDatabaseContext;
<%_ } _%>
}

[HttpPost("<%= kebabCasedEntityClassPlural %>")]
Expand All @@ -102,12 +104,16 @@ namespace <%= namespace %>.Controllers {
<%_ if (hasDto) { _%>
<%= pascalizedEntityClass %> <%= camelCasedEntityClass %> = _mapper.Map<<%= pascalizedEntityClass %>>(<%= asDto(camelCasedEntityClass) %>);
<%_ } _%>
<%_ if (relationships.length != 0) { _%>
_applicationDatabaseContext.AddGraph(<%= camelCasedEntityClass %>);
<%_ if (hasService) { _%>
await _<%= camelCasedEntityClass %>Service.Save(<%= camelCasedEntityClass %>);
<%_ } else { _%>
<%_ if (relationships.length != 0) { _%>
_applicationDatabaseContext.AddGraph(<%= camelCasedEntityClass %>);
<%_ } else { _%>
_applicationDatabaseContext.<%= pascalizedEntityClassPlural %>.Add(<%= camelCasedEntityClass %>);
<%_ } _%>
<%_ } _%>
await _applicationDatabaseContext.SaveChangesAsync();
<%_ }_%>
return CreatedAtAction(nameof(Get<%= pascalizedEntityClass %>), new { id = <%= camelCasedEntityClass %>.Id }, <%= camelCasedEntityClass %>)
.WithHeaders(HeaderUtil.CreateEntityCreationAlert(EntityName, <%= camelCasedEntityClass %>.Id.ToString()));
}
Expand All @@ -127,11 +133,11 @@ namespace <%= namespace %>.Controllers {
<%_ if (hasService) { _%>
await _<%= camelCasedEntityClass %>Service.Save(<%= camelCasedEntityClass %>);
<%_ } else { _%>
<%_ 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);
<%_ }
}); _%>
}); _%>
_applicationDatabaseContext.Update(<%= camelCasedEntityClass %>);
<%_ let first = true;
let suffix = 0;
Expand Down Expand Up @@ -196,7 +202,8 @@ namespace <%= namespace %>.Controllers {
.SingleOrDefaultAsync(<%= camelCasedEntityClass %> => <%= camelCasedEntityClass %>.Id == id);
<%_ } _%>
<%_ if (hasDto) { _%>
return ActionResultUtil.WrapOrNotFoundAsDto<<%= asDto(pascalizedEntityClass) %>>(result, _mapper);
<%= asDto(pascalizedEntityClass) %> <%= asDto(camelCasedEntityClass) %> = _mapper.Map<<%= asDto(pascalizedEntityClass) %>>(result);
return ActionResultUtil.WrapOrNotFound(<%= asDto(camelCasedEntityClass) %>);
<%_ } else { _%>
return ActionResultUtil.WrapOrNotFound(result);
<%_ }_%>
Expand Down

0 comments on commit 2aae5e3

Please sign in to comment.