From a32c3c9eb971a76e25ddd8bb0f34101a34d5830c Mon Sep 17 00:00:00 2001 From: Ricardo Carneiro Date: Fri, 7 Mar 2025 08:52:23 -0300 Subject: [PATCH] feat: canais e listagem de canais com mongodb --- Postall.Domain/Dtos/ChannelResponse.cs | 10 +++++---- Postall.Domain/Entities/ChannelData.cs | 3 ++- .../Services/ChannelVideoService.cs | 7 ++++-- .../Repositories/ChannelRepository.cs | 22 ++++++++++++++----- Postall/Controllers/ChannelsController.cs | 7 ++++-- 5 files changed, 35 insertions(+), 14 deletions(-) diff --git a/Postall.Domain/Dtos/ChannelResponse.cs b/Postall.Domain/Dtos/ChannelResponse.cs index 6f98e6f..3272c5d 100644 --- a/Postall.Domain/Dtos/ChannelResponse.cs +++ b/Postall.Domain/Dtos/ChannelResponse.cs @@ -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, diff --git a/Postall.Domain/Entities/ChannelData.cs b/Postall.Domain/Entities/ChannelData.cs index 3bd1f73..a842bc4 100644 --- a/Postall.Domain/Entities/ChannelData.cs +++ b/Postall.Domain/Entities/ChannelData.cs @@ -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, diff --git a/Postall.Domain/Services/ChannelVideoService.cs b/Postall.Domain/Services/ChannelVideoService.cs index 6dc77d8..f11342e 100644 --- a/Postall.Domain/Services/ChannelVideoService.cs +++ b/Postall.Domain/Services/ChannelVideoService.cs @@ -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; } diff --git a/Postall.Infra.MongoDB/Repositories/ChannelRepository.cs b/Postall.Infra.MongoDB/Repositories/ChannelRepository.cs index b9c3cd4..733119b 100644 --- a/Postall.Infra.MongoDB/Repositories/ChannelRepository.cs +++ b/Postall.Infra.MongoDB/Repositories/ChannelRepository.cs @@ -25,7 +25,7 @@ namespace Postall.Infra.MongoDB.Repositories var database = client.GetDatabase(mongoDbSettings.Value.DatabaseName); _channelsCollection = database.GetCollection(mongoDbSettings.Value.ChannelsCollectionName); - var indexKeysDefinition = Builders.IndexKeys.Ascending(c => c.YoutubeId); + var indexKeysDefinition = Builders.IndexKeys.Ascending(c => c.ChannelId).Ascending(c => c.UserId); _channelsCollection.Indexes.CreateOne(new CreateIndexModel(indexKeysDefinition, new CreateIndexOptions { Unique = true })); var textIndexDefinition = Builders.IndexKeys @@ -79,18 +79,30 @@ namespace Postall.Infra.MongoDB.Repositories /// public async Task 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(); } - 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; } diff --git a/Postall/Controllers/ChannelsController.cs b/Postall/Controllers/ChannelsController.cs index f9a380e..c887e98 100644 --- a/Postall/Controllers/ChannelsController.cs +++ b/Postall/Controllers/ChannelsController.cs @@ -18,7 +18,7 @@ namespace Postall.Controllers public async Task Index() { var userChannels = await _channelService.GetUserChannelsAsync(); - return View(userChannels); + return View(userChannels.IsSuccess ? userChannels.Value : new List()); } [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) {