updates to module template for static rendering (ActionDialog Id property, Service interface moved to Shared, Client Service using IHttpClientFactory, Async methods added to Repository, Server Service implementation added, Controller uses Server Service implementation, Server Service registered in Startup)
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using [Owner].Module.[Module].Models;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace [Owner].Module.[Module].Repository
|
||||
{
|
||||
@ -11,5 +11,12 @@ namespace [Owner].Module.[Module].Repository
|
||||
Models.[Module] Add[Module](Models.[Module] [Module]);
|
||||
Models.[Module] Update[Module](Models.[Module] [Module]);
|
||||
void Delete[Module](int [Module]Id);
|
||||
|
||||
Task<IEnumerable<Models.[Module]>> Get[Module]sAsync(int ModuleId);
|
||||
Task<Models.[Module]> Get[Module]Async(int [Module]Id);
|
||||
Task<Models.[Module]> Get[Module]Async(int [Module]Id, bool tracking);
|
||||
Task<Models.[Module]> Add[Module]Async(Models.[Module] [Module]);
|
||||
Task<Models.[Module]> Update[Module]Async(Models.[Module] [Module]);
|
||||
Task Delete[Module]Async(int [Module]Id);
|
||||
}
|
||||
}
|
||||
|
@ -2,24 +2,23 @@ using Microsoft.EntityFrameworkCore;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using Oqtane.Modules;
|
||||
using [Owner].Module.[Module].Models;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace [Owner].Module.[Module].Repository
|
||||
{
|
||||
public class [Module]Repository : I[Module]Repository, ITransientService
|
||||
{
|
||||
private readonly IDbContextFactory<[Module]Context> _factory;
|
||||
private readonly [Module]Context _queryContext;
|
||||
|
||||
public [Module]Repository(IDbContextFactory<[Module]Context> factory)
|
||||
{
|
||||
_factory = factory;
|
||||
_queryContext = _factory.CreateDbContext();
|
||||
}
|
||||
|
||||
public IEnumerable<Models.[Module]> Get[Module]s(int ModuleId)
|
||||
{
|
||||
return _queryContext.[Module].Where(item => item.ModuleId == ModuleId);
|
||||
using var db = _factory.CreateDbContext();
|
||||
return db.[Module].Where(item => item.ModuleId == ModuleId).ToList();
|
||||
}
|
||||
|
||||
public Models.[Module] Get[Module](int [Module]Id)
|
||||
@ -63,5 +62,54 @@ namespace [Owner].Module.[Module].Repository
|
||||
db.[Module].Remove([Module]);
|
||||
db.SaveChanges();
|
||||
}
|
||||
|
||||
|
||||
public async Task<IEnumerable<Models.[Module]>> Get[Module]sAsync(int ModuleId)
|
||||
{
|
||||
using var db = _factory.CreateDbContext();
|
||||
return await db.[Module].Where(item => item.ModuleId == ModuleId).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<Models.[Module]> Get[Module]Async(int [Module]Id)
|
||||
{
|
||||
return await Get[Module]Async([Module]Id, true);
|
||||
}
|
||||
|
||||
public async Task<Models.[Module]> Get[Module]Async(int [Module]Id, bool tracking)
|
||||
{
|
||||
using var db = _factory.CreateDbContext();
|
||||
if (tracking)
|
||||
{
|
||||
return await db.[Module].FindAsync([Module]Id);
|
||||
}
|
||||
else
|
||||
{
|
||||
return await db.[Module].AsNoTracking().FirstOrDefaultAsync(item => item.[Module]Id == [Module]Id);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Models.[Module]> Add[Module]Async(Models.[Module] [Module])
|
||||
{
|
||||
using var db = _factory.CreateDbContext();
|
||||
db.[Module].Add([Module]);
|
||||
await db.SaveChangesAsync();
|
||||
return [Module];
|
||||
}
|
||||
|
||||
public async Task<Models.[Module]> Update[Module]Async(Models.[Module] [Module])
|
||||
{
|
||||
using var db = _factory.CreateDbContext();
|
||||
db.Entry([Module]).State = EntityState.Modified;
|
||||
await db.SaveChangesAsync();
|
||||
return [Module];
|
||||
}
|
||||
|
||||
public async Task Delete[Module]Async(int [Module]Id)
|
||||
{
|
||||
using var db = _factory.CreateDbContext();
|
||||
Models.[Module] [Module] = db.[Module].Find([Module]Id);
|
||||
db.[Module].Remove([Module]);
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user