updated default module template to use Service consistently
This commit is contained in:
		| @ -21,7 +21,7 @@ namespace [Owner].Module.[Module].Services | ||||
|  | ||||
|         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]) | ||||
| @ -36,7 +36,7 @@ namespace [Owner].Module.[Module].Services | ||||
|  | ||||
|         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)); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -5,31 +5,32 @@ using Microsoft.AspNetCore.Http; | ||||
| using Oqtane.Shared; | ||||
| using Oqtane.Enums; | ||||
| using Oqtane.Infrastructure; | ||||
| using [Owner].Module.[Module].Repository; | ||||
| using [Owner].Module.[Module].Services; | ||||
| using Oqtane.Controllers; | ||||
| using System.Net; | ||||
| using System.Threading.Tasks; | ||||
|  | ||||
| namespace [Owner].Module.[Module].Controllers | ||||
| { | ||||
|     [Route(ControllerRoutes.ApiRoute)] | ||||
|     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 | ||||
|         [HttpGet] | ||||
|         [Authorize(Policy = PolicyNames.ViewModule)] | ||||
|         public IEnumerable<Models.[Module]> Get(string moduleid) | ||||
|         public async Task<IEnumerable<Models.[Module]>> Get(string moduleid) | ||||
|         { | ||||
|             int 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 | ||||
|             { | ||||
| @ -40,18 +41,18 @@ namespace [Owner].Module.[Module].Controllers | ||||
|         } | ||||
|  | ||||
|         // GET api/<controller>/5 | ||||
|         [HttpGet("{id}")] | ||||
|         [HttpGet("{id}/{moduleid}")] | ||||
|         [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)) | ||||
|             { | ||||
|                 return [Module]; | ||||
|             } | ||||
|             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; | ||||
|                 return null; | ||||
|             } | ||||
| @ -60,12 +61,11 @@ namespace [Owner].Module.[Module].Controllers | ||||
|         // POST api/<controller> | ||||
|         [HttpPost] | ||||
|         [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)) | ||||
|             { | ||||
|                 [Module] = _[Module]Repository.Add[Module]([Module]); | ||||
|                 _logger.Log(LogLevel.Information, this, LogFunction.Create, "[Module] Added {[Module]}", [Module]); | ||||
|                 [Module] = await _[Module]Service.Add[Module]Async([Module]); | ||||
|             } | ||||
|             else | ||||
|             { | ||||
| @ -79,12 +79,11 @@ namespace [Owner].Module.[Module].Controllers | ||||
|         // PUT api/<controller>/5 | ||||
|         [HttpPut("{id}")] | ||||
|         [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]); | ||||
|                 _logger.Log(LogLevel.Information, this, LogFunction.Update, "[Module] Updated {[Module]}", [Module]); | ||||
|                 [Module] = await _[Module]Service.Update[Module]Async([Module]); | ||||
|             } | ||||
|             else | ||||
|             { | ||||
| @ -96,19 +95,18 @@ namespace [Owner].Module.[Module].Controllers | ||||
|         } | ||||
|  | ||||
|         // DELETE api/<controller>/5 | ||||
|         [HttpDelete("{id}")] | ||||
|         [HttpDelete("{id}/{moduleid}")] | ||||
|         [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)) | ||||
|             { | ||||
|                 _[Module]Repository.Delete[Module](id); | ||||
|                 _logger.Log(LogLevel.Information, this, LogFunction.Delete, "[Module] Deleted {[Module]Id}", id); | ||||
|                 await _[Module]Service.Delete[Module]Async(id, [Module].ModuleId); | ||||
|             } | ||||
|             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; | ||||
|             } | ||||
|         } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 sbwalker
					sbwalker