-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminController.cs
More file actions
155 lines (134 loc) · 6.81 KB
/
AdminController.cs
File metadata and controls
155 lines (134 loc) · 6.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MangaReader.Data;
using MangaReader.Models;
using MangaReader.Services;
using System.IO;
using System.Text.Json;
using System.Data;
namespace MangaReader.Controllers;
[Authorize]
public class AdminController : Controller
{
private readonly ApplicationDbContext _context;
private readonly ILogger<AdminController> _logger;
private readonly IWebHostEnvironment _webHostEnvironment;
private readonly IChapterService _chapterService;
private bool IsCurrentUserAdmin()
{
return (User?.FindFirst("IsAdmin")?.Value == "True");
}
private bool IsCurrentUserSubAdmin()
{
return (User?.FindFirst("IsSubAdmin")?.Value == "True");
}
public AdminController(ApplicationDbContext context, ILogger<AdminController> logger, IWebHostEnvironment webHostEnvironment, IChapterService chapterService)
{
_context = context;
_logger = logger;
_webHostEnvironment = webHostEnvironment;
_chapterService = chapterService;
}
// Dashboard
public async Task<IActionResult> Index()
{
var stats = new AdminDashboardViewModel
{
TotalManga = await _context.Mangas.CountAsync(),
TotalChapters = await _context.Chapters.CountAsync(),
TotalPages = await _context.ChapterPages.CountAsync(),
TotalViews = await _context.Chapters.SumAsync(c => c.ViewCount),
RecentManga = await _context.Mangas.OrderByDescending(m => m.CreatedAt).Take(5).ToListAsync(),
RecentChapters = await _context.Chapters.Include(c => c.Manga).OrderByDescending(c => c.CreatedAt).Take(5).ToListAsync()
};
// Check if database fix is needed
try
{
using (var command = _context.Database.GetDbConnection().CreateCommand())
{
await _context.Database.OpenConnectionAsync();
// Check ParentCommentId column
command.CommandText = "SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_NAME = 'ChapterComments' AND COLUMN_NAME = 'ParentCommentId' AND TABLE_SCHEMA = DATABASE();";
var colResult = await command.ExecuteScalarAsync();
var colMissing = Convert.ToInt32(colResult) == 0;
// Check SiteSettings table
command.CommandText = "SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_NAME = 'SiteSettings' AND TABLE_SCHEMA = DATABASE();";
var tableResult = await command.ExecuteScalarAsync();
var tableMissing = Convert.ToInt32(tableResult) == 0;
// Check Notifications table
command.CommandText = "SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_NAME = 'Notifications' AND TABLE_SCHEMA = DATABASE();";
var notifyResult = await command.ExecuteScalarAsync();
var notifyMissing = Convert.ToInt32(notifyResult) == 0;
// Check AniListId column in Mangas table
command.CommandText = "SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_NAME = 'Mangas' AND COLUMN_NAME = 'AniListId' AND TABLE_SCHEMA = DATABASE();";
var aniListColResult = await command.ExecuteScalarAsync();
var aniListColMissing = Convert.ToInt32(aniListColResult) == 0;
// Check FollowChangelog column in Users table
command.CommandText = "SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_NAME = 'Users' AND COLUMN_NAME = 'FollowChangelog' AND TABLE_SCHEMA = DATABASE();";
var followChangelogColResult = await command.ExecuteScalarAsync();
var followChangelogColMissing = Convert.ToInt32(followChangelogColResult) == 0;
ViewBag.DatabaseFixNeeded = colMissing || tableMissing || notifyMissing || aniListColMissing || followChangelogColMissing;
ViewBag.SiteSettingsTableMissing = tableMissing;
ViewBag.ParentCommentIdMissing = colMissing;
ViewBag.NotificationsTableMissing = notifyMissing;
ViewBag.AniListIdMissing = aniListColMissing;
ViewBag.FollowChangelogMissing = followChangelogColMissing;
}
}
catch
{
ViewBag.DatabaseFixNeeded = false;
}
// Load site settings with fallback for missing table
try
{
ViewBag.EnableDecorations = (await _context.SiteSettings.FirstOrDefaultAsync(s => s.Key == "EnableDecorations"))?.Value ?? "true";
ViewBag.EnableTitles = (await _context.SiteSettings.FirstOrDefaultAsync(s => s.Key == "EnableTitles"))?.Value ?? "true";
ViewBag.EnableBannerShadow = (await _context.SiteSettings.FirstOrDefaultAsync(s => s.Key == "EnableBannerShadow"))?.Value ?? "false";
ViewBag.BannerShadowStrength = (await _context.SiteSettings.FirstOrDefaultAsync(s => s.Key == "BannerShadowStrength"))?.Value ?? "0.8";
ViewBag.BannerShadowDepth = (await _context.SiteSettings.FirstOrDefaultAsync(s => s.Key == "BannerShadowDepth"))?.Value ?? "4";
}
catch
{
ViewBag.EnableDecorations = "true";
ViewBag.EnableTitles = "true";
ViewBag.EnableBannerShadow = "false";
ViewBag.BannerShadowStrength = "0.8";
ViewBag.BannerShadowDepth = "4";
}
// Auto-generate previous changelogs if they don't exist
await SeedChangelogsAsync();
await EnsureInitialChangelogAsync();
return View(stats);
}
private async Task EnsureInitialChangelogAsync()
{
var title = "Welcome to your new Manga Reader!";
var entry = await _context.ChangelogEntries.FirstOrDefaultAsync(e => e.Title == title);
var content = @"### 🌟 Getting Started
- **Admin Panel**: You can manage your manga, chapters, and site settings from here.
- **Customization**: Enable or disable features like XP, Titles, and Decorations in the settings.
- **Social**: Link your own Discord or social media in the layout file.
### 🛠️ Key Features
- **XP System**: Users earn XP for reading and commenting.
- **Notifications**: Stay updated on new chapters and replies.
- **Responsive**: Works great on mobile and desktop.";
if (entry == null)
{
entry = new ChangelogEntry
{
Title = title,
Content = content,
CreatedAt = DateTime.UtcNow
};
_context.ChangelogEntries.Add(entry);
await _context.SaveChangesAsync();
}
}
private async Task SeedChangelogsAsync()
{
// This method can be used to seed initial history if needed.
// For a public version, we keep it simple.
}
}