BCards/src/BCards.Web/Services/LivePageService.cs
Ricardo Carneiro 3d2ce1f8cf
All checks were successful
BCards Deployment Pipeline / Run Tests (push) Successful in 4s
BCards Deployment Pipeline / PR Validation (push) Has been skipped
BCards Deployment Pipeline / Build and Push Image (push) Successful in 15m22s
BCards Deployment Pipeline / Deploy to Production (ARM - OCI) (push) Successful in 1m54s
BCards Deployment Pipeline / Deploy to Test (x86 - Local) (push) Has been skipped
BCards Deployment Pipeline / Cleanup Old Resources (push) Has been skipped
BCards Deployment Pipeline / Deployment Summary (push) Successful in 0s
fix: Increase session timeout to 7 days and set SameSite=None for Cloudflare compatibility
🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-18 12:32:42 -03:00

118 lines
4.3 KiB
C#

using BCards.Web.Models;
using BCards.Web.Repositories;
using BCards.Web.ViewModels;
namespace BCards.Web.Services;
public class LivePageService : ILivePageService
{
private readonly ILivePageRepository _livePageRepository;
private readonly IUserPageRepository _userPageRepository;
private readonly ILogger<LivePageService> _logger;
public LivePageService(
ILivePageRepository livePageRepository,
IUserPageRepository userPageRepository,
ILogger<LivePageService> logger)
{
_livePageRepository = livePageRepository;
_userPageRepository = userPageRepository;
_logger = logger;
}
public async Task<LivePage?> GetByCategoryAndSlugAsync(string category, string slug)
{
return await _livePageRepository.GetByCategoryAndSlugAsync(category, slug);
}
public async Task<List<LivePage>> GetAllActiveAsync()
{
return await _livePageRepository.GetAllActiveAsync();
}
public async Task<LivePage?> GetLivePageFromUserPageId(string userPageId)
{
return await _livePageRepository.GetByOriginalPageIdAsync(userPageId);
}
public async Task<LivePage> SyncFromUserPageAsync(string userPageId)
{
var userPage = await _userPageRepository.GetByIdAsync(userPageId);
if (userPage == null)
throw new InvalidOperationException($"UserPage {userPageId} not found");
if (userPage.Status != PageStatus.Active)
throw new InvalidOperationException("UserPage must be Active to sync to LivePage");
// Verificar se já existe LivePage para este UserPage
var existingLivePage = await _livePageRepository.GetByOriginalPageIdAsync(userPageId);
var livePage = new LivePage
{
OriginalPageId = userPageId,
UserId = userPage.UserId,
Category = userPage.Category,
Slug = userPage.Slug,
DisplayName = userPage.DisplayName,
Bio = userPage.Bio,
ProfileImageId = userPage.ProfileImageId,
BusinessType = userPage.BusinessType,
Theme = userPage.Theme,
Links = userPage.Links,
SeoSettings = userPage.SeoSettings,
Language = userPage.Language,
Analytics = new LivePageAnalytics
{
TotalViews = existingLivePage?.Analytics?.TotalViews ?? 0,
TotalClicks = existingLivePage?.Analytics?.TotalClicks ?? 0,
LastViewedAt = existingLivePage?.Analytics?.LastViewedAt
},
PublishedAt = userPage.ApprovedAt ?? DateTime.UtcNow
};
if (existingLivePage != null)
{
// Atualizar existente
livePage.Id = existingLivePage.Id;
livePage.CreatedAt = existingLivePage.CreatedAt;
_logger.LogInformation("Updating existing LivePage {LivePageId} from UserPage {UserPageId}", livePage.Id, userPageId);
return await _livePageRepository.UpdateAsync(livePage);
}
else
{
// Criar nova
_logger.LogInformation("Creating new LivePage from UserPage {UserPageId}", userPageId);
return await _livePageRepository.CreateAsync(livePage);
}
}
public async Task<bool> DeleteByOriginalPageIdAsync(string originalPageId)
{
_logger.LogInformation("Deleting LivePage for UserPage {UserPageId}", originalPageId);
return await _livePageRepository.DeleteByOriginalPageIdAsync(originalPageId);
}
public async Task IncrementViewAsync(string livePageId)
{
try
{
await _livePageRepository.IncrementViewAsync(livePageId);
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to increment view for LivePage {LivePageId}", livePageId);
}
}
public async Task IncrementLinkClickAsync(string livePageId, int linkIndex)
{
try
{
await _livePageRepository.IncrementLinkClickAsync(livePageId, linkIndex);
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to increment click for LivePage {LivePageId} link {LinkIndex}", livePageId, linkIndex);
}
}
}