-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCollaborationPage.razor
More file actions
178 lines (154 loc) · 6.54 KB
/
Copy pathCollaborationPage.razor
File metadata and controls
178 lines (154 loc) · 6.54 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
@attribute [Authorize]
@page "/collaborations/{PageId:int}"
@using StartSch.Services
@rendermode InteractiveServerWithoutPrerendering
@layout MainLayout
@inherits ResourcePage<Page>
@inject IDbContextFactory<Db> DbFactory
@inject InterestService InterestService
@inject AuthorizationService AuthorizationService
@if (!IsResourceAvailable(_page, AuthorizationService.CanCreatePost, out var status))
{
<ResourceUnavailable Status="@status"/>
return;
}
<main>
<article class="post">
<p class="display">
<h1>Oldal együttműködési kérelmei</h1>
</p>
@if (_items.Count == 0)
{
<p>Nincsenek együttműködési kérelmek.</p>
}
else
{
<ul class="collaboration-request-list"
style="list-style: none; padding: 0; display: flex; flex-direction: column; gap: 16px;">
@foreach (var request in _items)
{
<li style="border: 1px solid #ccc; border-radius: 12px; padding: 16px;">
@if (request is EventCollaborationRequest eventRequest)
{
<div style="font-weight: bold; margin-bottom: 8px;">
Esemény együttműködés
</div>
<div>
<a href="/events/@eventRequest.Event.Id">@eventRequest.Event.Title</a>
</div>
<div class="small text" style="margin-top: 4px;">
Szervező: @(eventRequest.Event.Categories.FirstOrDefault()?.Page.Name ?? eventRequest.Event.Categories.FirstOrDefault()?.Page.PekName ?? "Ismeretlen")
</div>
}
else if (request is PostCollaborationRequest postRequest)
{
<div style="font-weight: bold; margin-bottom: 8px;">
Poszt együttműködés
</div>
<div>
<a href="/posts/@postRequest.Post.Id">@postRequest.Post.Title</a>
</div>
<div class="small text" style="margin-top: 4px;">
Szerző: @(postRequest.Post.Categories.FirstOrDefault()?.Page.Name ?? postRequest.Post.Categories.FirstOrDefault()?.Page.PekName ?? "Ismeretlen")
</div>
}
<div style="display: flex; gap: 8px; margin-top: 12px">
<button type="button" @onclick="() => Accept(request)" class="small filled round">
Elfogadás
</button>
<button type="button" @onclick="() => Deny(request)"
class="small text round error standard">Elutasítás
</button>
</div>
</li>
}
</ul>
}
</article>
</main>
@code {
private Page? _page;
private readonly List<CollaborationRequest> _items = [];
[Parameter] public int PageId { get; set; }
protected override async Task OnInitializedAsync()
{
await InterestService.LoadIndex;
await using var db = await DbFactory.CreateDbContextAsync();
_page = await db.Pages.FirstOrDefaultAsync(g => g.Id == PageId);
if (_page == null)
return;
_items.AddRange(await db.EventCollaborationRequests
.Include(r => r.Event)
.ThenInclude(e => e.Categories)
.ThenInclude(c => c.Page)
.Where(r => r.PageId == PageId)
.ToListAsync());
_items.AddRange(await db.PostCollaborationRequests
.Include(r => r.Post)
.ThenInclude(p => p.Categories)
.ThenInclude(c => c.Page)
.Where(r => r.PageId == PageId)
.ToListAsync());
}
private async Task Accept(CollaborationRequest request)
{
await using var db = await DbFactory.CreateDbContextAsync();
var page = await db.Pages
.Include(p => p.Categories)
.FirstOrDefaultAsync(p => p.Id == PageId);
if (page == null || page.Categories.Count == 0) return;
var category = page.Categories[0];
if (request is EventCollaborationRequest evInfo)
{
var ev = await db.Events
.Include(e => e.Categories)
.FirstOrDefaultAsync(e => e.Id == evInfo.EventId);
if (ev != null)
{
if (!ev.Categories.Any(c => c.Id == category.Id))
ev.Categories.Add(category);
var toRemove = await db.EventCollaborationRequests
.FirstOrDefaultAsync(r => r.Id == evInfo.Id);
if (toRemove != null)
db.EventCollaborationRequests.Remove(toRemove);
}
}
else if (request is PostCollaborationRequest postInfo)
{
var post = await db.Posts
.Include(p => p.Categories)
.FirstOrDefaultAsync(p => p.Id == postInfo.PostId);
if (post != null)
{
if (!post.Categories.Any(c => c.Id == category.Id))
post.Categories.Add(category);
var toRemove = await db.PostCollaborationRequests
.FirstOrDefaultAsync(r => r.Id == postInfo.Id);
if (toRemove != null)
db.PostCollaborationRequests.Remove(toRemove);
}
}
await db.SaveChangesAsync();
_items.Remove(request);
}
private async Task Deny(CollaborationRequest request)
{
await using var db = await DbFactory.CreateDbContextAsync();
if (request is EventCollaborationRequest evInfo)
{
var toRemove = await db.EventCollaborationRequests
.FirstOrDefaultAsync(r => r.Id == evInfo.Id);
if (toRemove != null)
db.EventCollaborationRequests.Remove(toRemove);
}
else if (request is PostCollaborationRequest postInfo)
{
var toRemove = await db.PostCollaborationRequests
.FirstOrDefaultAsync(r => r.Id == postInfo.Id);
if (toRemove != null)
db.PostCollaborationRequests.Remove(toRemove);
}
await db.SaveChangesAsync();
_items.Remove(request);
}
}