Newsbot.Collector/Newsbot.Collector.Api/Controllers/v1/DiscordNotificationController.cs
James Tombleson 82dea60126
All checks were successful
continuous-integration/drone/pr Build is passing
Updated DiscordWebHooks to handle userId
2023-07-29 09:44:15 -07:00

239 lines
8.5 KiB
C#

using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newsbot.Collector.Api.Middleware;
using Newsbot.Collector.Domain.Dto;
using Newsbot.Collector.Domain.Entities;
using Newsbot.Collector.Domain.Interfaces;
using Newsbot.Collector.Domain.Requests;
using Newsbot.Collector.Domain.Results;
namespace Newsbot.Collector.Api.Controllers.v1;
[ApiController]
[Route("api/v1/subscriptions")]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public class DiscordNotificationController : ControllerBase
{
private readonly ILogger<DiscordNotificationController> _logger;
private readonly IDiscordWebHooksRepository _discord;
private readonly ISourcesRepository _sources;
private readonly IDiscordNotificationRepository _discordNotification;
public DiscordNotificationController(ILogger<DiscordNotificationController> logger, IDiscordWebHooksRepository discord, ISourcesRepository sources, IDiscordNotificationRepository discordNotification)
{
_logger = logger;
_discord = discord;
_sources = sources;
_discordNotification = discordNotification;
}
[HttpGet(Name = "ListSubscriptions")]
public ActionResult<DiscordNotificationResult> List(int page)
{
var userId = HttpContext.GetUserId();
if (userId.Equals(string.Empty))
{
return new BadRequestObjectResult(new DiscordNotificationResult
{
IsSuccessful = false,
ErrorMessage = new List<string> { "User Id is missing from the request." }
});
}
var res = new List<DiscordNotificationDto>();
var items = _discordNotification.List(userId, page);
foreach (var item in items) res.Add(DiscordNotificationDto.Convert(item));
return new OkObjectResult(new DiscordNotificationResult
{
IsSuccessful = true,
Items = res
});
}
[HttpGet("{id}")]
public ActionResult<DiscordNotificationResult> GetById(Guid id)
{
var userId = HttpContext.GetUserId();
if (userId.Equals(string.Empty))
{
return new BadRequestObjectResult(new DiscordNotificationResult
{
IsSuccessful = false,
ErrorMessage = new List<string> { "User Id is missing from the request." }
});
}
var res = DiscordNotificationDto.Convert(_discordNotification.GetById(userId, id));
return new OkObjectResult(new DiscordNotificationResult
{
IsSuccessful = true,
Items = new List<DiscordNotificationDto>
{
res
}
});
}
[HttpGet("{id}/details")]
public ActionResult<DiscordNotificationDetailsResult> GetDetailsById(Guid id)
{
var userId = HttpContext.GetUserId();
if (userId.Equals(string.Empty))
{
return new BadRequestObjectResult(new DiscordNotificationResult
{
IsSuccessful = false,
ErrorMessage = new List<string> { "User Id is missing from the request." }
});
}
var sub = _discordNotification.GetById(userId, id);
var webhook = _discord.GetById(userId, sub.DiscordWebHookId);
var source = _sources.GetById(sub.SourceId);
return new OkObjectResult(new DiscordNotificationDetailsResult
{
IsSuccessful = true,
Item = DiscordNotificationDetailsDto.Convert(sub, source, webhook)
});
}
[HttpPost("{id}/delete")]
public ActionResult<DiscordNotificationResult> DeleteById(Guid id)
{
var userId = HttpContext.GetUserId();
if (userId.Equals(string.Empty))
{
return new BadRequestObjectResult(new DiscordNotificationResult
{
IsSuccessful = false,
ErrorMessage = new List<string> { "User Id is missing from the request." }
});
}
var rowsUpdated = _discordNotification.Delete(userId, id);
if (rowsUpdated == -1)
{
return new OkObjectResult(new DiscordNotificationResult
{
IsSuccessful = false,
ErrorMessage = new List<string> { "Record was not own by requested user." }
});
}
return new OkObjectResult(new DiscordNotificationResult
{
IsSuccessful = true
});
}
[HttpGet("by/discordId/{id}")]
public ActionResult<DiscordNotificationResult> GetByDiscordId(Guid id)
{
var userId = HttpContext.GetUserId();
if (userId.Equals(string.Empty))
{
return new BadRequestObjectResult(new DiscordNotificationResult
{
IsSuccessful = false,
ErrorMessage = new List<string> { "User Id is missing from the request." }
});
}
var res = new List<DiscordNotificationDto>();
var items = _discordNotification.ListByWebhook(userId, id);
foreach (var item in items) res.Add(DiscordNotificationDto.Convert(item));
return new OkObjectResult(new DiscordNotificationResult
{
IsSuccessful = true,
Items = res
});
}
[HttpGet("by/sourceId/{id}")]
public ActionResult<DiscordNotificationResult> GetBySourceId(Guid id)
{
var userId = HttpContext.GetUserId();
if (userId.Equals(string.Empty))
{
return new BadRequestObjectResult(new DiscordNotificationResult
{
IsSuccessful = false,
ErrorMessage = new List<string> { "User Id is missing from the request." }
});
}
var res = new List<DiscordNotificationDto>();
var items = _discordNotification.ListBySourceId(userId, id);
foreach (var item in items) res.Add(DiscordNotificationDto.Convert(item));
return new OkObjectResult(new DiscordNotificationResult
{
IsSuccessful = true,
Items = res
});
}
[HttpPost(Name = "New Subscription")]
public ActionResult<DiscordNotificationDto> New([FromBody] NewDiscordNotificationRequest request)
{
var userId = HttpContext.GetUserId();
if (userId.Equals(string.Empty))
{
return new BadRequestObjectResult(new DiscordNotificationResult
{
IsSuccessful = false,
ErrorMessage = new List<string> { "User Id is missing from the request." }
});
}
if (request.SourceId == Guid.Empty) return new BadRequestObjectResult(new DiscordNotificationResult
{
IsSuccessful = false,
ErrorMessage = new List<string> { "SourceId is missing from the request." }
});
if (request.DiscordId == Guid.Empty) return new BadRequestObjectResult(new DiscordNotificationResult
{
IsSuccessful = false,
ErrorMessage = new List<string> { "DiscordId is missing from the request." }
});
var exists = _discordNotification.GetByWebhookAndSource(userId, request.DiscordId, request.SourceId);
if (exists.Id != Guid.Empty) return DiscordNotificationDto.Convert(exists);
var discord = _discord.GetById(userId, request.DiscordId);
if (discord.Id == Guid.Empty) return new BadRequestObjectResult(new DiscordNotificationResult
{
IsSuccessful = false,
ErrorMessage = new List<string> { "Unable to find the requested DiscordId in the database." }
});
var source = _sources.GetById(request.SourceId);
if (source.Id == Guid.Empty) return new BadRequestObjectResult(new DiscordNotificationResult
{
IsSuccessful = false,
ErrorMessage = new List<string> { "Unable to find the requested SourceId in the database." }
});
var item = _discordNotification.New(new DiscordNotificationEntity
{
Id = Guid.NewGuid(),
SourceId = request.SourceId,
DiscordWebHookId = request.DiscordId,
CodeAllowCommits = request.AllowCommits,
CodeAllowReleases = request.AllowReleases,
UserId = userId
});
return new OkObjectResult(new DiscordNotificationResult
{
IsSuccessful = true,
Items = new List<DiscordNotificationDto> { DiscordNotificationDto.Convert(item) }
});
}
}