using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Strata.ApiLib.Standard.Models; using Strata.ContinuousImprovement.Biz.Encounter.Opportunities.Filters; using Strata.ContinuousImprovement.Biz.Network; using Strata.ContinuousImprovement.Biz.Pagination; using Strata.CoreLib.Claims.Extensions; using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace Strata.ContinuousImprovement.Api.Controllers { [ApiController] [ApiVersion("1.0")] [Route("api/v{api-version:apiVersion}/[controller]")] public class NetworkOpportunitiesController : ControllerBase { private readonly INetworkOpportunityService _networkOpportunityService; private readonly List _creators; private readonly ILogger _logger; public NetworkOpportunitiesController(INetworkOpportunityService networkOpportunityService, IOptions sectionAccessor, ILogger logger) { _networkOpportunityService = networkOpportunityService; var networkOpportunity = sectionAccessor.Value ?? throw new ArgumentNullException(nameof(NetworkOpportunitySection)); _creators = networkOpportunity.Creators?.Distinct().ToList() ?? new List(); _logger = logger; var dict = new Dictionary { { "service", nameof(NetworkOpportunitiesController) }, { "creators", _creators } }; using (_logger.BeginScope(dict)) _logger.LogInformation("Network Opportunity Creators"); } [HttpGet("[action]")] public async Task GetEnabled(CancellationToken cancellationToken) { var dict = new Dictionary { { "service", nameof(NetworkOpportunitiesController) }, { "creators", _creators }, { "isSdtEmployee", User.IsSdtEmployee() } }; using (_logger.BeginScope(dict)) _logger.LogInformation("Network Opportunity Enabled"); if (User.IsSdtEmployee()) { return true; } var databaseGuid = ClaimsPrincipalExtensions.GetDatabaseGuid(User); return await _networkOpportunityService.GetEnabledAsync(databaseGuid, cancellationToken); } [HttpGet("[action]")] public async Task IsCreator() { var user = User?.Identity?.Name ?? ""; var isCreator = _creators.Any() && (_creators.Contains(user) || _creators.Any(x => x == "*")); var dict = new Dictionary { { "service", nameof(NetworkOpportunitiesController) }, { "creators", _creators }, { "user", user }, { "isCreator", isCreator } }; using (_logger.BeginScope(dict)) _logger.LogInformation("Network Opportunity IsCreator"); return isCreator; } [HttpPost("[action]")] public async Task> GetOpportunities(NetworkOpportunityLoadOptions loadOptions, CancellationToken cancellationToken) { return await _networkOpportunityService.GetOpportunitiesAsync(User, loadOptions.PagingOptions, loadOptions.Filters, cancellationToken); } [HttpPost("[action]")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesDefaultResponseType] public async Task> Create([FromBody] NetworkOpportunitySaveModel opportunitySaveModel, CancellationToken cancellationToken) { return await _networkOpportunityService.CreateAsync(opportunitySaveModel, cancellationToken); } [HttpPut("{networkOpportunityId}")] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesDefaultResponseType] public async Task Update(int networkOpportunityId, NetworkOpportunitySaveModel opportunitySaveModel, CancellationToken cancellationToken) { if (networkOpportunityId != opportunitySaveModel.NetworkOpportunityId) { return BadRequest(); } await _networkOpportunityService.UpdateAsync(opportunitySaveModel, cancellationToken); return NoContent(); } [HttpDelete("{networkOpportunityId}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesDefaultResponseType] public async Task> Delete(int networkOpportunityId, CancellationToken cancellationToken) { return Ok(await _networkOpportunityService.DeleteAsync(networkOpportunityId, cancellationToken)); } } [ExcludeFromCodeCoverage] public class NetworkOpportunityLoadOptions { public PagingOptions PagingOptions { get; set; } public NetworkOpportunityFilters Filters { get; set; } } }