using Nuuru.Server.DTOs.Moderation;
using Nuuru.Server.Models;
using AuditLog = Nuuru.Server.Models.AuditLog;
namespace Nuuru.Server.Extensions
{
///
/// Extension methods for mapping between moderation entities and DTOs
///
public static class ModerationMappingExtensions
{
///
/// Maps an ApplicationUser to ModeratorDto
///
public static ModeratorDto ToModeratorDto(this ApplicationUser user)
{
if (user == null) return null;
return new ModeratorDto
{
Id = user.Id,
UserName = user.UserName
};
}
///
/// Maps an ApplicationUser to UserDto
///
public static UserDto ToUserDto(this ApplicationUser user)
{
if (user == null) return null;
return new UserDto
{
Id = user.Id,
UserName = user.UserName
};
}
///
/// Maps a ModerationAction entity to ModerationActionDto
///
public static ModerationActionDto ToDto(this ModerationAction action)
{
if (action == null) return null;
return new ModerationActionDto
{
Id = action.Id,
Action = action.Action,
TargetType = action.TargetType,
TargetId = action.TargetId,
Reason = action.Reason,
Details = action.Details,
Timestamp = action.Timestamp,
Moderator = action.Moderator?.ToModeratorDto()
};
}
///
/// Maps a Ban entity to BanDto
///
public static BanDto ToDto(this Ban ban)
{
if (ban == null) return null;
return new BanDto
{
Id = ban.Id,
Reason = ban.Reason,
StartTime = ban.StartTime,
EndTime = ban.EndTime,
Zone = ban.Zone,
Active = ban.Active,
IsBanActive = ban.IsBanActive(),
AppealsDenied = ban.AppealsDenied,
User = ban.User?.ToUserDto()
};
}
///
/// Maps a collection of ModerationActions to ModerationActionDtos
///
public static List ToDto(this IEnumerable actions)
{
return actions?.Select(a => a.ToDto()).ToList() ?? new List();
}
///
/// Maps a collection of Bans to BanDtos
///
public static List ToDto(this IEnumerable bans)
{
return bans?.Select(b => b.ToDto()).ToList() ?? new List();
}
///
/// Maps an IpBan entity to IpBanDto
///
public static IpBanDto ToDto(this IpBan ipBan)
{
if (ipBan == null) return null;
return new IpBanDto
{
Id = ipBan.Id,
IpAddress = ipBan.IpAddress,
Reason = ipBan.Reason,
StartTime = ipBan.StartTime,
EndTime = ipBan.EndTime,
Active = ipBan.Active,
IsActive = ipBan.IsActive(),
CreatedBy = ipBan.CreatedBy?.ToUserDto()
};
}
///
/// Maps a collection of IpBans to IpBanDtos
///
public static List ToDto(this IEnumerable ipBans)
{
return ipBans?.Select(b => b.ToDto()).ToList() ?? new List();
}
///
/// Maps a Report entity to ReportDto
///
public static ReportDto ToDto(this Report report, ReportTargetPreviewDto? targetPreview = null)
{
if (report == null) return null;
return new ReportDto
{
Id = report.Id,
TargetType = report.TargetType.ToString(),
TargetId = report.TargetId,
Reason = report.Reason,
Status = report.Status.ToString(),
CreatedAt = report.CreatedAt,
ResolvedAt = report.ResolvedAt,
ResolutionNote = report.ResolutionNote,
Reporter = report.Reporter?.ToUserDto(),
Moderator = report.Moderator?.ToModeratorDto(),
TargetPreview = targetPreview
};
}
///
/// Maps a collection of Reports to ReportDtos
///
public static List ToDto(this IEnumerable reports)
{
return reports?.Select(r => r.ToDto()).ToList() ?? new List();
}
///
/// Maps a BanAppeal entity to BanAppealDto
///
public static BanAppealDto ToDto(this BanAppeal appeal)
{
if (appeal == null) return null;
return new BanAppealDto
{
Id = appeal.Id,
BanId = appeal.BanId,
Reason = appeal.Reason,
Status = appeal.Status.ToString(),
CreatedAt = appeal.CreatedAt,
ResolvedAt = appeal.ResolvedAt,
ModeratorNote = appeal.ModeratorNote,
User = appeal.User?.ToUserDto(),
Moderator = appeal.Moderator?.ToModeratorDto(),
Ban = appeal.Ban?.ToDto()
};
}
///
/// Maps a collection of BanAppeals to BanAppealDtos
///
public static List ToDto(this IEnumerable appeals)
{
return appeals?.Select(a => a.ToDto()).ToList() ?? new List();
}
///
/// Maps an IpBanAppeal entity to IpBanAppealDto
///
public static IpBanAppealDto ToDto(this IpBanAppeal appeal)
{
if (appeal == null) return null;
return new IpBanAppealDto
{
Id = appeal.Id,
IpBanId = appeal.IpBanId,
IpAddress = appeal.IpAddress,
Reason = appeal.Reason,
Status = appeal.Status.ToString(),
CreatedAt = appeal.CreatedAt,
ResolvedAt = appeal.ResolvedAt,
ModeratorNote = appeal.ModeratorNote,
User = appeal.User?.ToUserDto(),
Moderator = appeal.Moderator?.ToModeratorDto(),
IpBan = appeal.IpBan?.ToDto()
};
}
///
/// Maps a collection of IpBanAppeals to IpBanAppealDtos
///
public static List ToDto(this IEnumerable appeals)
{
return appeals?.Select(a => a.ToDto()).ToList() ?? new List();
}
///
/// Maps an AuditLog entity to AuditLogDto
///
public static AuditLogDto ToAuditLogDto(this AuditLog auditLog)
{
if (auditLog == null) return null;
return new AuditLogDto
{
Id = auditLog.Id,
Action = auditLog.Action,
Category = auditLog.Category,
TargetType = auditLog.TargetType,
TargetId = auditLog.TargetId,
IpAddress = auditLog.IpAddress,
AssessmentId = auditLog.AssessmentId,
BrowserHash = auditLog.BrowserHash,
HttpMethod = auditLog.HttpMethod,
RequestPath = auditLog.RequestPath,
ResponseStatusCode = auditLog.ResponseStatusCode,
Timestamp = auditLog.Timestamp,
User = auditLog.User?.ToUserDto()
};
}
}
}