updated default module template to use Service consistently

This commit is contained in:
sbwalker 2024-12-23 14:09:54 -05:00
parent 36d5747b4f
commit 997e9213f2
2 changed files with 23 additions and 25 deletions

View File

@ -21,7 +21,7 @@ namespace [Owner].Module.[Module].Services
public async Task<Models.[Module]> Get[Module]Async(int [Module]Id, int ModuleId) public async Task<Models.[Module]> Get[Module]Async(int [Module]Id, int ModuleId)
{ {
return await GetJsonAsync<Models.[Module]>(CreateAuthorizationPolicyUrl($"{Apiurl}/{[Module]Id}", EntityNames.Module, ModuleId)); return await GetJsonAsync<Models.[Module]>(CreateAuthorizationPolicyUrl($"{Apiurl}/{[Module]Id}/{ModuleId}", EntityNames.Module, ModuleId));
} }
public async Task<Models.[Module]> Add[Module]Async(Models.[Module] [Module]) public async Task<Models.[Module]> Add[Module]Async(Models.[Module] [Module])
@ -36,7 +36,7 @@ namespace [Owner].Module.[Module].Services
public async Task Delete[Module]Async(int [Module]Id, int ModuleId) public async Task Delete[Module]Async(int [Module]Id, int ModuleId)
{ {
await DeleteAsync(CreateAuthorizationPolicyUrl($"{Apiurl}/{[Module]Id}", EntityNames.Module, ModuleId)); await DeleteAsync(CreateAuthorizationPolicyUrl($"{Apiurl}/{[Module]Id}/{ModuleId}", EntityNames.Module, ModuleId));
} }
} }
} }

View File

@ -5,31 +5,32 @@ using Microsoft.AspNetCore.Http;
using Oqtane.Shared; using Oqtane.Shared;
using Oqtane.Enums; using Oqtane.Enums;
using Oqtane.Infrastructure; using Oqtane.Infrastructure;
using [Owner].Module.[Module].Repository; using [Owner].Module.[Module].Services;
using Oqtane.Controllers; using Oqtane.Controllers;
using System.Net; using System.Net;
using System.Threading.Tasks;
namespace [Owner].Module.[Module].Controllers namespace [Owner].Module.[Module].Controllers
{ {
[Route(ControllerRoutes.ApiRoute)] [Route(ControllerRoutes.ApiRoute)]
public class [Module]Controller : ModuleControllerBase public class [Module]Controller : ModuleControllerBase
{ {
private readonly I[Module]Repository _[Module]Repository; private readonly I[Module]Service _[Module]Service;
public [Module]Controller(I[Module]Repository [Module]Repository, ILogManager logger, IHttpContextAccessor accessor) : base(logger, accessor) public [Module]Controller(I[Module]Service [Module]Service, ILogManager logger, IHttpContextAccessor accessor) : base(logger, accessor)
{ {
_[Module]Repository = [Module]Repository; _[Module]Service = [Module]Service;
} }
// GET: api/<controller>?moduleid=x // GET: api/<controller>?moduleid=x
[HttpGet] [HttpGet]
[Authorize(Policy = PolicyNames.ViewModule)] [Authorize(Policy = PolicyNames.ViewModule)]
public IEnumerable<Models.[Module]> Get(string moduleid) public async Task<IEnumerable<Models.[Module]>> Get(string moduleid)
{ {
int ModuleId; int ModuleId;
if (int.TryParse(moduleid, out ModuleId) && IsAuthorizedEntityId(EntityNames.Module, ModuleId)) if (int.TryParse(moduleid, out ModuleId) && IsAuthorizedEntityId(EntityNames.Module, ModuleId))
{ {
return _[Module]Repository.Get[Module]s(ModuleId); return await _[Module]Service.Get[Module]sAsync(ModuleId);
} }
else else
{ {
@ -40,18 +41,18 @@ namespace [Owner].Module.[Module].Controllers
} }
// GET api/<controller>/5 // GET api/<controller>/5
[HttpGet("{id}")] [HttpGet("{id}/{moduleid}")]
[Authorize(Policy = PolicyNames.ViewModule)] [Authorize(Policy = PolicyNames.ViewModule)]
public Models.[Module] Get(int id) public async Task<Models.[Module]> Get(int id, int moduleid)
{ {
Models.[Module] [Module] = _[Module]Repository.Get[Module](id); Models.[Module] [Module] = await _[Module]Service.Get[Module]Async(id, moduleid);
if ([Module] != null && IsAuthorizedEntityId(EntityNames.Module, [Module].ModuleId)) if ([Module] != null && IsAuthorizedEntityId(EntityNames.Module, [Module].ModuleId))
{ {
return [Module]; return [Module];
} }
else else
{ {
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized [Module] Get Attempt {[Module]Id}", id); _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized [Module] Get Attempt {[Module]Id} {ModuleId}", id, moduleid);
HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden; HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden;
return null; return null;
} }
@ -60,12 +61,11 @@ namespace [Owner].Module.[Module].Controllers
// POST api/<controller> // POST api/<controller>
[HttpPost] [HttpPost]
[Authorize(Policy = PolicyNames.EditModule)] [Authorize(Policy = PolicyNames.EditModule)]
public Models.[Module] Post([FromBody] Models.[Module] [Module]) public async Task<Models.[Module]> Post([FromBody] Models.[Module] [Module])
{ {
if (ModelState.IsValid && IsAuthorizedEntityId(EntityNames.Module, [Module].ModuleId)) if (ModelState.IsValid && IsAuthorizedEntityId(EntityNames.Module, [Module].ModuleId))
{ {
[Module] = _[Module]Repository.Add[Module]([Module]); [Module] = await _[Module]Service.Add[Module]Async([Module]);
_logger.Log(LogLevel.Information, this, LogFunction.Create, "[Module] Added {[Module]}", [Module]);
} }
else else
{ {
@ -79,12 +79,11 @@ namespace [Owner].Module.[Module].Controllers
// PUT api/<controller>/5 // PUT api/<controller>/5
[HttpPut("{id}")] [HttpPut("{id}")]
[Authorize(Policy = PolicyNames.EditModule)] [Authorize(Policy = PolicyNames.EditModule)]
public Models.[Module] Put(int id, [FromBody] Models.[Module] [Module]) public async Task<Models.[Module]> Put(int id, [FromBody] Models.[Module] [Module])
{ {
if (ModelState.IsValid && [Module].[Module]Id == id && IsAuthorizedEntityId(EntityNames.Module, [Module].ModuleId) && _[Module]Repository.Get[Module]([Module].[Module]Id, false) != null) if (ModelState.IsValid && [Module].[Module]Id == id && IsAuthorizedEntityId(EntityNames.Module, [Module].ModuleId))
{ {
[Module] = _[Module]Repository.Update[Module]([Module]); [Module] = await _[Module]Service.Update[Module]Async([Module]);
_logger.Log(LogLevel.Information, this, LogFunction.Update, "[Module] Updated {[Module]}", [Module]);
} }
else else
{ {
@ -96,19 +95,18 @@ namespace [Owner].Module.[Module].Controllers
} }
// DELETE api/<controller>/5 // DELETE api/<controller>/5
[HttpDelete("{id}")] [HttpDelete("{id}/{moduleid}")]
[Authorize(Policy = PolicyNames.EditModule)] [Authorize(Policy = PolicyNames.EditModule)]
public void Delete(int id) public async Task Delete(int id, int moduleid)
{ {
Models.[Module] [Module] = _[Module]Repository.Get[Module](id); Models.[Module] [Module] = await _[Module]Service.Get[Module]Async(id, moduleid);
if ([Module] != null && IsAuthorizedEntityId(EntityNames.Module, [Module].ModuleId)) if ([Module] != null && IsAuthorizedEntityId(EntityNames.Module, [Module].ModuleId))
{ {
_[Module]Repository.Delete[Module](id); await _[Module]Service.Delete[Module]Async(id, [Module].ModuleId);
_logger.Log(LogLevel.Information, this, LogFunction.Delete, "[Module] Deleted {[Module]Id}", id);
} }
else else
{ {
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized [Module] Delete Attempt {[Module]Id}", id); _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized [Module] Delete Attempt {[Module]Id} {ModuleId}", id, moduleid);
HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden; HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden;
} }
} }