features/api-cleanup #15

Merged
jtom38 merged 13 commits from features/api-cleanup into main 2023-07-29 09:47:30 -07:00
6 changed files with 69 additions and 24 deletions
Showing only changes of commit 82dea60126 - Show all commits

View File

@ -91,7 +91,7 @@ public class DiscordNotificationController : ControllerBase
} }
var sub = _discordNotification.GetById(userId, id); var sub = _discordNotification.GetById(userId, id);
var webhook = _discord.GetById(sub.DiscordWebHookId); var webhook = _discord.GetById(userId, sub.DiscordWebHookId);
var source = _sources.GetById(sub.SourceId); var source = _sources.GetById(sub.SourceId);
return new OkObjectResult(new DiscordNotificationDetailsResult return new OkObjectResult(new DiscordNotificationDetailsResult
@ -206,7 +206,7 @@ public class DiscordNotificationController : ControllerBase
var exists = _discordNotification.GetByWebhookAndSource(userId, request.DiscordId, request.SourceId); var exists = _discordNotification.GetByWebhookAndSource(userId, request.DiscordId, request.SourceId);
if (exists.Id != Guid.Empty) return DiscordNotificationDto.Convert(exists); if (exists.Id != Guid.Empty) return DiscordNotificationDto.Convert(exists);
var discord = _discord.GetById(request.DiscordId); var discord = _discord.GetById(userId, request.DiscordId);
if (discord.Id == Guid.Empty) return new BadRequestObjectResult(new DiscordNotificationResult if (discord.Id == Guid.Empty) return new BadRequestObjectResult(new DiscordNotificationResult
{ {
IsSuccessful = false, IsSuccessful = false,

View File

@ -7,6 +7,8 @@ using Newsbot.Collector.Domain.Dto;
using Newsbot.Collector.Domain.Entities; using Newsbot.Collector.Domain.Entities;
using Newsbot.Collector.Domain.Interfaces; using Newsbot.Collector.Domain.Interfaces;
using Newsbot.Collector.Domain.Models; using Newsbot.Collector.Domain.Models;
using Newsbot.Collector.Domain.Requests;
using Newsbot.Collector.Domain.Results;
namespace Newsbot.Collector.Api.Controllers.v1; namespace Newsbot.Collector.Api.Controllers.v1;
@ -45,30 +47,44 @@ public class DiscordWebHookController : ControllerBase
} }
[HttpPost(Name = "New")] [HttpPost(Name = "New")]
public DiscordWebHookDto New(string url, string server, string channel) public ActionResult<DiscordWebhookResult> New([FromBody] NewDiscordWebhookRequest request)
{ {
var exists = _webhooks.GetByUrl(url); var userId = HttpContext.GetUserId();
var exists = _webhooks.GetByUrl(request.Url ?? "");
if (exists.Id != Guid.Empty) if (exists.Id != Guid.Empty)
{ {
return DiscordWebHookDto.Convert(exists); return new BadRequestObjectResult(new DiscordWebhookResult
{
IsSuccessful = true,
Items = new List<DiscordWebHookDto> { DiscordWebHookDto.Convert(exists) }
});
} }
var res = _webhooks.New(new DiscordWebhookEntity var res = _webhooks.New(new DiscordWebhookEntity
{ {
Url = url, UserId = userId,
Server = server, Url = request.Url ?? "",
Channel = channel, Server = request.Server ?? "",
Channel = request.Channel ?? "",
Enabled = true, Enabled = true,
}); });
return DiscordWebHookDto.Convert(res); return new OkObjectResult(new DiscordWebhookResult
{
IsSuccessful = true,
Items = new List<DiscordWebHookDto>
{
DiscordWebHookDto.Convert(res)
}
});
} }
[HttpGet("by/serverAndChannel")] [HttpGet("by/serverAndChannel")]
public IEnumerable<DiscordWebHookDto> GetByServerAndChannel(string server, string channel) public IEnumerable<DiscordWebHookDto> GetByServerAndChannel(string server, string channel)
{ {
var items = new List<DiscordWebHookDto>(); var items = new List<DiscordWebHookDto>();
var res = _webhooks.ListByServerAndChannel(server, channel, 25); var res = _webhooks.ListByServerAndChannel(HttpContext.GetUserId(), server, channel, 25);
foreach (var item in res) foreach (var item in res)
{ {
@ -78,21 +94,21 @@ public class DiscordWebHookController : ControllerBase
} }
[HttpGet("{id}")] [HttpGet("{id}")]
public DiscordWebHookDto GetById(Guid id) public DiscordWebHookDto GetById(string userId, Guid id)
{ {
var res = _webhooks.GetById(id); var res = _webhooks.GetById(userId, id);
return DiscordWebHookDto.Convert(res); return DiscordWebHookDto.Convert(res);
} }
[HttpPost("{id}/disable")] [HttpPost("{id}/disable")]
public void DisableById(Guid id) public void DisableById(Guid id)
{ {
_webhooks.Disable(id); _webhooks.Disable(HttpContext.GetUserId(), id);
} }
[HttpPost("{id}/enable")] [HttpPost("{id}/enable")]
public void EnableById(Guid id) public void EnableById(Guid id)
{ {
_webhooks.Enable(id); _webhooks.Enable(HttpContext.GetUserId(), id);
} }
} }

View File

@ -34,10 +34,20 @@ public class DiscordWebhooksTable : IDiscordWebHooksRepository
return model; return model;
} }
public DiscordWebhookEntity GetById(Guid id) public DiscordWebhookEntity GetById(string userId, Guid id)
{ {
//using var context = new DatabaseContext(_connectionString); //using var context = new DatabaseContext(_connectionString);
var res = _context.DiscordWebhooks.FirstOrDefault(d => d.Id.Equals(id)); var res = _context.DiscordWebhooks
.Where(i => i.UserId != null && i.UserId.Equals(userId))
.FirstOrDefault(d => d.Id.Equals(id));
res ??= new DiscordWebhookEntity();
return res;
}
public DiscordWebhookEntity GetById(Guid id)
{
var res = _context.DiscordWebhooks
.FirstOrDefault(d => d.Id.Equals(id));
res ??= new DiscordWebhookEntity(); res ??= new DiscordWebhookEntity();
return res; return res;
} }
@ -82,10 +92,11 @@ public class DiscordWebhooksTable : IDiscordWebHooksRepository
return res; return res;
} }
public List<DiscordWebhookEntity> ListByServerAndChannel(string server, string channel, int limit = 25) public List<DiscordWebhookEntity> ListByServerAndChannel(string userId, string server, string channel, int limit = 25)
{ {
//using var context = new DatabaseContext(_connectionString); //using var context = new DatabaseContext(_connectionString);
var res = _context.DiscordWebhooks var res = _context.DiscordWebhooks
.Where(i => i.UserId != null && i.UserId.Equals(userId))
.Where(s => s.Server.Equals(server)) .Where(s => s.Server.Equals(server))
.Where(c => c.Channel.Equals(channel)) .Where(c => c.Channel.Equals(channel))
.Take(limit) .Take(limit)
@ -94,9 +105,9 @@ public class DiscordWebhooksTable : IDiscordWebHooksRepository
return res; return res;
} }
public int Disable(Guid id) public int Disable(string userId, Guid id)
{ {
var res = GetById(id); var res = GetById(userId, id);
//using var context = new DatabaseContext(_connectionString); //using var context = new DatabaseContext(_connectionString);
res.Enabled = true; res.Enabled = true;
@ -114,9 +125,9 @@ public class DiscordWebhooksTable : IDiscordWebHooksRepository
} }
} }
public int Enable(Guid id) public int Enable(string userId, Guid id)
{ {
var res = GetById(id); var res = GetById(userId, id);
//using var context = new DatabaseContext(_connectionString); //using var context = new DatabaseContext(_connectionString);
res.Enabled = false; res.Enabled = false;

View File

@ -6,14 +6,16 @@ public interface IDiscordWebHooksRepository
{ {
DiscordWebhookEntity New(DiscordWebhookEntity model); DiscordWebhookEntity New(DiscordWebhookEntity model);
DiscordWebhookEntity GetById(string userId, Guid id);
DiscordWebhookEntity GetById(Guid id); DiscordWebhookEntity GetById(Guid id);
DiscordWebhookEntity GetByUrl(string url); DiscordWebhookEntity GetByUrl(string url);
List<DiscordWebhookEntity> List(int page, int count = 25); List<DiscordWebhookEntity> List(int page, int count = 25);
List<DiscordWebhookEntity> ListByUserId(string userId, int page); List<DiscordWebhookEntity> ListByUserId(string userId, int page);
List<DiscordWebhookEntity> ListByServer(string server, int limit); List<DiscordWebhookEntity> ListByServer(string server, int limit);
List<DiscordWebhookEntity> ListByServerAndChannel(string server, string channel, int limit); List<DiscordWebhookEntity> ListByServerAndChannel(string userId, string server, string channel, int limit);
int Disable(Guid id); int Disable(string userId, Guid id);
int Enable(Guid id); int Enable(string userId, Guid id);
} }

View File

@ -0,0 +1,8 @@
namespace Newsbot.Collector.Domain.Requests;
public class NewDiscordWebhookRequest
{
public string? Url { get; set; }
public string? Server { get; set; }
public string? Channel { get; set; }
}

View File

@ -0,0 +1,8 @@
using Newsbot.Collector.Domain.Dto;
namespace Newsbot.Collector.Domain.Results;
public class DiscordWebhookResult : BaseResult
{
public List<DiscordWebHookDto>? Items { get; set; }
}