generated from ricardo/MVCLogin
feat: canais e listagem de canais com mongodb
This commit is contained in:
parent
4cca23cb35
commit
a32c3c9eb9
@ -11,6 +11,7 @@ namespace Postall.Domain.Dtos
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string UserId { get; set; }
|
||||
public string ChannelId { get; set; }
|
||||
public string YoutubeId { get; set; }
|
||||
public string Title { get; set; }
|
||||
public string Description { get; set; }
|
||||
@ -23,15 +24,16 @@ namespace Postall.Domain.Dtos
|
||||
public bool IsSelected { get; set; }
|
||||
|
||||
// URL do canal no YouTube
|
||||
public string ChannelUrl => $"https://www.youtube.com/channel/{Id}";
|
||||
public string ChannelUrl => $"https://www.youtube.com/channel/{this.ChannelId}";
|
||||
|
||||
public ChannelData ToChannelData()
|
||||
{
|
||||
return new ChannelData
|
||||
{
|
||||
Id = Id,
|
||||
UserId = UserId,
|
||||
YoutubeId = YoutubeId,
|
||||
Id = Guid.NewGuid().ToString("N"),
|
||||
UserId = this.UserId,
|
||||
ChannelId = this.ChannelId,
|
||||
YoutubeId = this.YoutubeId,
|
||||
Title = Title,
|
||||
Description = Description,
|
||||
ThumbnailUrl = ThumbnailUrl,
|
||||
|
||||
@ -49,13 +49,14 @@ namespace Postall.Domain.Entities
|
||||
public bool IsSelected { get; set; }
|
||||
|
||||
[BsonIgnore]
|
||||
public string ChannelUrl => $"https://www.youtube.com/channel/{YoutubeId}";
|
||||
public string ChannelUrl => $"https://www.youtube.com/channel/{ChannelId}";
|
||||
|
||||
public ChannelResponse ToChannelResponse() => new ChannelResponse
|
||||
{
|
||||
Id = Id,
|
||||
UserId = UserId,
|
||||
YoutubeId = YoutubeId,
|
||||
ChannelId = ChannelId,
|
||||
Title = Title,
|
||||
Description = Description,
|
||||
ThumbnailUrl = ThumbnailUrl,
|
||||
|
||||
@ -114,8 +114,11 @@ namespace Postall.Infra.Services
|
||||
return false;
|
||||
|
||||
var channelDetails = await GetChannelDetailsAsync(channelId);
|
||||
|
||||
await _channelRepository.AddAsync(channelDetails.Value.ToChannelData());
|
||||
var data = channelDetails.Value.ToChannelData();
|
||||
data.ChannelId = channelId;
|
||||
data.UserId = userId;
|
||||
data.YoutubeId = channelId;
|
||||
await _channelRepository.AddAsync(data);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -25,7 +25,7 @@ namespace Postall.Infra.MongoDB.Repositories
|
||||
var database = client.GetDatabase(mongoDbSettings.Value.DatabaseName);
|
||||
_channelsCollection = database.GetCollection<ChannelData>(mongoDbSettings.Value.ChannelsCollectionName);
|
||||
|
||||
var indexKeysDefinition = Builders<ChannelData>.IndexKeys.Ascending(c => c.YoutubeId);
|
||||
var indexKeysDefinition = Builders<ChannelData>.IndexKeys.Ascending(c => c.ChannelId).Ascending(c => c.UserId);
|
||||
_channelsCollection.Indexes.CreateOne(new CreateIndexModel<ChannelData>(indexKeysDefinition, new CreateIndexOptions { Unique = true }));
|
||||
|
||||
var textIndexDefinition = Builders<ChannelData>.IndexKeys
|
||||
@ -79,18 +79,30 @@ namespace Postall.Infra.MongoDB.Repositories
|
||||
/// </summary>
|
||||
public async Task<ChannelData> AddAsync(ChannelData ChannelData)
|
||||
{
|
||||
// Verifica se o canal já existe pelo ID do YouTube
|
||||
var existingChannel = await GetByYoutubeIdAsync(ChannelData.YoutubeId);
|
||||
var existingChannel = await GetByUserIdAndChannelIdAsync(ChannelData.UserId, ChannelData.ChannelId);
|
||||
if (existingChannel != null)
|
||||
return null;
|
||||
|
||||
// Gera novo ID para o MongoDB se não for fornecido
|
||||
if (string.IsNullOrEmpty(ChannelData.Id) || !ObjectId.TryParse(ChannelData.Id, out _))
|
||||
{
|
||||
ChannelData.Id = ObjectId.GenerateNewId().ToString();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await _channelsCollection.InsertOneAsync(ChannelData);
|
||||
}
|
||||
catch (MongoWriteException ex)
|
||||
{
|
||||
if (ex.WriteError.Category == ServerErrorCategory.DuplicateKey)
|
||||
{
|
||||
return await GetByYoutubeIdAsync(ChannelData.YoutubeId);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
return ChannelData;
|
||||
}
|
||||
|
||||
|
||||
@ -18,7 +18,7 @@ namespace Postall.Controllers
|
||||
public async Task<IActionResult> Index()
|
||||
{
|
||||
var userChannels = await _channelService.GetUserChannelsAsync();
|
||||
return View(userChannels);
|
||||
return View(userChannels.IsSuccess ? userChannels.Value : new List<string>());
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
@ -87,7 +87,10 @@ namespace Postall.Controllers
|
||||
try
|
||||
{
|
||||
var results = await _channelService.SearchChannelsAsync(query);
|
||||
return Json(new { success = true, data = results });
|
||||
if (results.IsSuccess)
|
||||
return Json(new { success = true, data = results.Value });
|
||||
|
||||
return Json(new { success = false, message = "Erro ao buscar canais" });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
Loading…
Reference in New Issue
Block a user