using System.Collections.Generic; using System.Threading.Tasks; using API.Constants; using API.Data; using API.DTOs.Dashboard; using API.DTOs.SideNav; using API.Middleware; using API.Services; using Microsoft.AspNetCore.Mvc; namespace API.Controllers; #nullable enable /// /// Responsible for anything that deals with Streams (SmartFilters, ExternalSource, DashboardStream, SideNavStream) /// public class StreamController : BaseApiController { private readonly IStreamService _streamService; private readonly IUnitOfWork _unitOfWork; private readonly ILocalizationService _localizationService; public StreamController(IStreamService streamService, IUnitOfWork unitOfWork, ILocalizationService localizationService) { _streamService = streamService; _unitOfWork = unitOfWork; _localizationService = localizationService; } /// /// Returns the layout of the user's dashboard /// /// [HttpGet("dashboard")] public async Task>> GetDashboardLayout(bool visibleOnly = true) { return Ok(await _streamService.GetDashboardStreams(UserId, visibleOnly)); } /// /// Return's the user's side nav /// [HttpGet("sidenav")] public async Task>> GetSideNav(bool visibleOnly = true) { return Ok(await _streamService.GetSidenavStreams(UserId, visibleOnly)); } /// /// Return's the user's external sources /// [HttpGet("external-sources")] public async Task>> GetExternalSources() { return Ok(await _streamService.GetExternalSources(UserId)); } /// /// Create an external Source /// /// /// [HttpPost("create-external-source")] public async Task> CreateExternalSource(ExternalSourceDto dto) { // Check if a host and api key exists for the current user return Ok(await _streamService.CreateExternalSource(UserId, dto)); } /// /// Updates an existing external source /// /// /// [HttpPost("update-external-source")] [DisallowRole(PolicyConstants.ReadOnlyRole)] public async Task> UpdateExternalSource(ExternalSourceDto dto) { // Check if a host and api key exists for the current user return Ok(await _streamService.UpdateExternalSource(UserId, dto)); } /// /// Validates the external source by host is unique (for this user) /// /// /// [HttpGet("external-source-exists")] [DisallowRole(PolicyConstants.ReadOnlyRole)] public async Task> ExternalSourceExists(string host, string name, string apiKey) { return Ok(await _unitOfWork.AppUserExternalSourceRepository.ExternalSourceExists(UserId, name, host, apiKey)); } /// /// Delete's the external source /// /// /// [HttpDelete("delete-external-source")] [DisallowRole(PolicyConstants.ReadOnlyRole)] public async Task ExternalSourceExists(int externalSourceId) { await _streamService.DeleteExternalSource(UserId, externalSourceId); return Ok(); } /// /// Creates a Dashboard Stream from a SmartFilter and adds it to the user's dashboard as visible /// /// /// [HttpPost("add-dashboard-stream")] [DisallowRole(PolicyConstants.ReadOnlyRole)] public async Task> AddDashboard([FromQuery] int smartFilterId) { return Ok(await _streamService.CreateDashboardStreamFromSmartFilter(UserId, smartFilterId)); } /// /// Updates the visibility of a dashboard stream /// /// /// [HttpPost("update-dashboard-stream")] [DisallowRole(PolicyConstants.ReadOnlyRole)] public async Task UpdateDashboardStream(DashboardStreamDto dto) { await _streamService.UpdateDashboardStream(UserId, dto); return Ok(); } /// /// Updates the position of a dashboard stream /// /// /// [HttpPost("update-dashboard-position")] [DisallowRole(PolicyConstants.ReadOnlyRole)] public async Task UpdateDashboardStreamPosition(UpdateStreamPositionDto dto) { await _streamService.UpdateDashboardStreamPosition(UserId, dto); return Ok(); } /// /// Creates a SideNav Stream from a SmartFilter and adds it to the user's sidenav as visible /// /// /// [HttpPost("add-sidenav-stream")] [DisallowRole(PolicyConstants.ReadOnlyRole)] public async Task> AddSideNav([FromQuery] int smartFilterId) { return Ok(await _streamService.CreateSideNavStreamFromSmartFilter(UserId, smartFilterId)); } /// /// Creates a SideNav Stream from a SmartFilter and adds it to the user's sidenav as visible /// /// /// [HttpPost("add-sidenav-stream-from-external-source")] [DisallowRole(PolicyConstants.ReadOnlyRole)] public async Task> AddSideNavFromExternalSource([FromQuery] int externalSourceId) { return Ok(await _streamService.CreateSideNavStreamFromExternalSource(UserId, externalSourceId)); } /// /// Updates the visibility of a dashboard stream /// /// /// [HttpPost("update-sidenav-stream")] [DisallowRole(PolicyConstants.ReadOnlyRole)] public async Task UpdateSideNavStream(SideNavStreamDto dto) { await _streamService.UpdateSideNavStream(UserId, dto); return Ok(); } /// /// Updates the position of a dashboard stream /// /// /// [HttpPost("update-sidenav-position")] [DisallowRole(PolicyConstants.ReadOnlyRole)] public async Task UpdateSideNavStreamPosition(UpdateStreamPositionDto dto) { await _streamService.UpdateSideNavStreamPosition(UserId, dto); return Ok(); } [HttpPost("bulk-sidenav-stream-visibility")] [DisallowRole(PolicyConstants.ReadOnlyRole)] public async Task BulkUpdateSideNavStream(BulkUpdateSideNavStreamVisibilityDto dto) { await _streamService.UpdateSideNavStreamBulk(UserId, dto); return Ok(); } /// /// Removes a Smart Filter from a user's SideNav Streams /// /// /// [HttpDelete("smart-filter-side-nav-stream")] [DisallowRole(PolicyConstants.ReadOnlyRole)] public async Task DeleteSmartFilterSideNavStream([FromQuery] int sideNavStreamId) { await _streamService.DeleteSideNavSmartFilterStream(UserId, sideNavStreamId); return Ok(); } /// /// Removes a Smart Filter from a user's Dashboard Streams /// /// /// [HttpDelete("smart-filter-dashboard-stream")] [DisallowRole(PolicyConstants.ReadOnlyRole)] public async Task DeleteSmartFilterDashboardStream([FromQuery] int dashboardStreamId) { await _streamService.DeleteDashboardSmartFilterStream(UserId, dashboardStreamId); return Ok(); } }