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