- Added image upload system (JPG/PNG, max 5MB) with live preview and removal option - Fixed Concurrency Exception during deletion (split transactions for reports and entries) - Optimized card layout: consistent height and height-based truncation for descriptions - Added sort direction toggle (Ascending/Descending) with arrow icons for Date, Name, and Year - Refactored HallOfFameService to use streams for Server/Wasm compatibility - Improved error handling and UI feedback for upload and delete operations
221 lines
9.9 KiB
C#
221 lines
9.9 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Http;
|
|
using System.Net.Http;
|
|
using Oqtane.Enums;
|
|
using Oqtane.Infrastructure;
|
|
using Oqtane.Models;
|
|
using Oqtane.Security;
|
|
using Oqtane.Shared;
|
|
using SZUAbsolventenverein.Module.HallOfFame.Repository;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using System.IO;
|
|
using System;
|
|
|
|
namespace SZUAbsolventenverein.Module.HallOfFame.Services
|
|
{
|
|
public class ServerHallOfFameService : IHallOfFameService
|
|
{
|
|
private readonly IHallOfFameRepository _HallOfFameRepository;
|
|
private readonly IUserPermissions _userPermissions;
|
|
private readonly ILogManager _logger;
|
|
private readonly IHttpContextAccessor _accessor;
|
|
private readonly Alias _alias;
|
|
private readonly IWebHostEnvironment _environment;
|
|
|
|
public ServerHallOfFameService(IHallOfFameRepository HallOfFameRepository, IUserPermissions userPermissions, ITenantManager tenantManager, ILogManager logger, IHttpContextAccessor accessor, IWebHostEnvironment environment)
|
|
{
|
|
_HallOfFameRepository = HallOfFameRepository;
|
|
_userPermissions = userPermissions;
|
|
_logger = logger;
|
|
_accessor = accessor;
|
|
_alias = tenantManager.GetAlias();
|
|
_environment = environment;
|
|
}
|
|
|
|
public Task<List<Models.HallOfFame>> GetHallOfFamesAsync(int ModuleId)
|
|
{
|
|
if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, ModuleId, PermissionNames.View))
|
|
{
|
|
return Task.FromResult(_HallOfFameRepository.GetHallOfFames(ModuleId).ToList());
|
|
}
|
|
else
|
|
{
|
|
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized HallOfFame Get Attempt {ModuleId}", ModuleId);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public Task<Models.HallOfFame> GetHallOfFameAsync(int HallOfFameId, int ModuleId)
|
|
{
|
|
if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, ModuleId, PermissionNames.View))
|
|
{
|
|
return Task.FromResult(_HallOfFameRepository.GetHallOfFame(HallOfFameId));
|
|
}
|
|
else
|
|
{
|
|
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized HallOfFame Get Attempt {HallOfFameId} {ModuleId}", HallOfFameId, ModuleId);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public Task<Models.HallOfFame> GetHallOfFameByUserIdAsync(int UserId, int ModuleId)
|
|
{
|
|
if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, ModuleId, PermissionNames.View))
|
|
{
|
|
// Assuming Repository doesn't have specific method yet, using LINQ on GetHallOfFames
|
|
return Task.FromResult(_HallOfFameRepository.GetHallOfFames(ModuleId).FirstOrDefault(item => item.UserId == UserId));
|
|
}
|
|
else
|
|
{
|
|
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized HallOfFame GetByUserId Attempt {UserId} {ModuleId}", UserId, ModuleId);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public Task<Models.HallOfFame> AddHallOfFameAsync(Models.HallOfFame HallOfFame)
|
|
{
|
|
if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, HallOfFame.ModuleId, PermissionNames.Edit))
|
|
{
|
|
HallOfFame = _HallOfFameRepository.AddHallOfFame(HallOfFame);
|
|
_logger.Log(LogLevel.Information, this, LogFunction.Create, "HallOfFame Added {HallOfFame}", HallOfFame);
|
|
}
|
|
else
|
|
{
|
|
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized HallOfFame Add Attempt {HallOfFame}", HallOfFame);
|
|
HallOfFame = null;
|
|
}
|
|
return Task.FromResult(HallOfFame);
|
|
}
|
|
|
|
public Task<Models.HallOfFame> UpdateHallOfFameAsync(Models.HallOfFame HallOfFame)
|
|
{
|
|
if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, HallOfFame.ModuleId, PermissionNames.Edit))
|
|
{
|
|
HallOfFame = _HallOfFameRepository.UpdateHallOfFame(HallOfFame);
|
|
_logger.Log(LogLevel.Information, this, LogFunction.Update, "HallOfFame Updated {HallOfFame}", HallOfFame);
|
|
}
|
|
else
|
|
{
|
|
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized HallOfFame Update Attempt {HallOfFame}", HallOfFame);
|
|
HallOfFame = null;
|
|
}
|
|
return Task.FromResult(HallOfFame);
|
|
}
|
|
|
|
public Task DeleteHallOfFameAsync(int HallOfFameId, int ModuleId)
|
|
{
|
|
if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, ModuleId, PermissionNames.Edit))
|
|
{
|
|
_HallOfFameRepository.DeleteHallOfFame(HallOfFameId);
|
|
_logger.Log(LogLevel.Information, this, LogFunction.Delete, "HallOfFame Deleted {HallOfFameId}", HallOfFameId);
|
|
}
|
|
else
|
|
{
|
|
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized HallOfFame Delete Attempt {HallOfFameId} {ModuleId}", HallOfFameId, ModuleId);
|
|
}
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public Task ReportAsync(int HallOfFameId, int ModuleId, string reason)
|
|
{
|
|
if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, ModuleId, PermissionNames.View))
|
|
{
|
|
var report = new Models.HallOfFameReport
|
|
{
|
|
HallOfFameId = HallOfFameId,
|
|
Reason = reason
|
|
};
|
|
_HallOfFameRepository.AddHallOfFameReport(report);
|
|
|
|
var hallOfFame = _HallOfFameRepository.GetHallOfFame(HallOfFameId);
|
|
if (hallOfFame != null && !hallOfFame.IsReported)
|
|
{
|
|
hallOfFame.IsReported = true;
|
|
_HallOfFameRepository.UpdateHallOfFame(hallOfFame);
|
|
}
|
|
_logger.Log(LogLevel.Information, this, LogFunction.Update, "HallOfFame Reported {HallOfFameId}", HallOfFameId);
|
|
}
|
|
else
|
|
{
|
|
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized HallOfFame Report Attempt {HallOfFameId} {ModuleId}", HallOfFameId, ModuleId);
|
|
}
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public Task<List<Models.HallOfFameReport>> GetHallOfFameReportsAsync(int HallOfFameId, int ModuleId)
|
|
{
|
|
if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, ModuleId, PermissionNames.Edit))
|
|
{
|
|
return Task.FromResult(_HallOfFameRepository.GetHallOfFameReports(HallOfFameId).ToList());
|
|
}
|
|
else
|
|
{
|
|
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized HallOfFame Get Reports Attempt {HallOfFameId} {ModuleId}", HallOfFameId, ModuleId);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public Task DeleteHallOfFameReportAsync(int HallOfFameReportId, int ModuleId)
|
|
{
|
|
if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, ModuleId, PermissionNames.Edit))
|
|
{
|
|
var report = _HallOfFameRepository.GetHallOfFameReport(HallOfFameReportId);
|
|
if (report != null)
|
|
{
|
|
int hallOfFameId = report.HallOfFameId;
|
|
_HallOfFameRepository.DeleteHallOfFameReport(HallOfFameReportId);
|
|
|
|
// Check if there are any reports left for this entry
|
|
var remainingReports = _HallOfFameRepository.GetHallOfFameReports(hallOfFameId);
|
|
if (!remainingReports.Any())
|
|
{
|
|
var hallOfFame = _HallOfFameRepository.GetHallOfFame(hallOfFameId);
|
|
if (hallOfFame != null)
|
|
{
|
|
hallOfFame.IsReported = false;
|
|
_HallOfFameRepository.UpdateHallOfFame(hallOfFame);
|
|
}
|
|
}
|
|
}
|
|
_logger.Log(LogLevel.Information, this, LogFunction.Delete, "HallOfFame Report Deleted {HallOfFameReportId}", HallOfFameReportId);
|
|
}
|
|
else
|
|
{
|
|
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized HallOfFame Delete Report Attempt {HallOfFameReportId} {ModuleId}", HallOfFameReportId, ModuleId);
|
|
}
|
|
return Task.CompletedTask;
|
|
}
|
|
public async Task<string> UploadFileAsync(Stream stream, string fileName, int ModuleId)
|
|
{
|
|
if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, ModuleId, PermissionNames.Edit))
|
|
{
|
|
var extension = Path.GetExtension(fileName).ToLower();
|
|
|
|
if (extension != ".jpg" && extension != ".jpeg" && extension != ".png")
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var folder = Path.Combine(_environment.WebRootPath, "Content", "HallOfFame");
|
|
if (!Directory.Exists(folder))
|
|
{
|
|
Directory.CreateDirectory(folder);
|
|
}
|
|
|
|
var newFileName = Guid.NewGuid().ToString() + extension;
|
|
var path = Path.Combine(folder, newFileName);
|
|
|
|
using (var fileStream = new FileStream(path, FileMode.Create))
|
|
{
|
|
await stream.CopyToAsync(fileStream);
|
|
}
|
|
|
|
return "/Content/HallOfFame/" + newFileName;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|