-
Notifications
You must be signed in to change notification settings - Fork 10
/
ArticlesAPIController.cs
1281 lines (1081 loc) · 54 KB
/
ArticlesAPIController.cs
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using CnGalWebSite.APIServer.Application.Articles;
using CnGalWebSite.APIServer.Application.Entries;
using CnGalWebSite.APIServer.Application.Examines;
using CnGalWebSite.APIServer.Application.Helper;
using CnGalWebSite.APIServer.Application.Users;
using CnGalWebSite.APIServer.DataReositories;
using CnGalWebSite.APIServer.ExamineX;
using CnGalWebSite.DataModel.Application.Dtos;
using CnGalWebSite.DataModel.ExamineModel;
using CnGalWebSite.DataModel.Helper;
using CnGalWebSite.DataModel.Model;
using CnGalWebSite.DataModel.ViewModel;
using CnGalWebSite.DataModel.ViewModel.Admin;
using CnGalWebSite.DataModel.ViewModel.Articles;
using CnGalWebSite.DataModel.ViewModel.Entries;
using CnGalWebSite.DataModel.ViewModel.Search;
using CnGalWebSite.Helper.Extensions;
using Markdig;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace CnGalWebSite.APIServer.Controllers
{
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[ApiController]
[Route("api/articles/[action]")]
public class ArticlesAPIController : ControllerBase
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly IRepository<Entry, int> _entryRepository;
private readonly IRepository<ApplicationUser, string> _userRepository;
private readonly IRepository<Article, long> _articleRepository;
private readonly IRepository<ThumbsUp, long> _thumbsUpRepository;
private readonly IRepository<Comment, long> _commentUpRepository;
private readonly IRepository<Examine, long> _examineRepository;
private readonly IArticleService _articleService;
private readonly IEntryService _entryService;
private readonly IAppHelper _appHelper;
private readonly IExamineService _examineService;
private readonly IUserService _userService;
private readonly IWebHostEnvironment _webHostEnvironment;
private readonly IEditRecordService _editRecordService;
public ArticlesAPIController(IArticleService articleService, IRepository<Comment, long> commentUpRepository, IRepository<ThumbsUp, long> thumbsUpRepository, IUserService userService,
IExamineService examineService, IEntryService entryService, IRepository<ApplicationUser, string> userRepository, IWebHostEnvironment webHostEnvironment, IEditRecordService editRecordService,
UserManager<ApplicationUser> userManager, IRepository<Article, long> articleRepository, IAppHelper appHelper, IRepository<Examine, long> examineRepository,
IRepository<Entry, int> entryRepository)
{
_userManager = userManager;
_entryRepository = entryRepository;
_appHelper = appHelper;
_articleRepository = articleRepository;
_thumbsUpRepository = thumbsUpRepository;
_commentUpRepository = commentUpRepository;
_articleService = articleService;
_examineRepository = examineRepository;
_examineService = examineService;
_entryService = entryService;
_userService = userService;
_userRepository = userRepository;
_webHostEnvironment = webHostEnvironment;
_editRecordService = editRecordService;
}
[AllowAnonymous]
[HttpGet("{id}")]
public async Task<ActionResult<ArticleViewModel>> GetArticleViewAsync(long id)
{
//获取当前用户ID
var user = await _appHelper.GetAPICurrentUserAsync(HttpContext);
//通过Id获取文章
var article = await _articleRepository.GetAll().AsNoTracking().Include(s => s.Disambig)
.Include(s => s.CreateUser).Include(s => s.ThumbsUps)
.Include(s => s.ArticleRelationFromArticleNavigation).ThenInclude(s => s.ToArticleNavigation)
.Include(s => s.Entries)
.Include(s => s.Outlinks)
.Include(s => s.Examines).ThenInclude(s => s.ApplicationUser)
.FirstOrDefaultAsync(x => x.Id == id);
if (article == null)
{
return NotFound();
}
//判断当前是否隐藏
if (article.IsHidden == true)
{
if (user == null || await _userManager.IsInRoleAsync(user, "Admin") != true)
{
return NotFound();
}
}
List<Examine> examineQuery = null;
//读取审核信息
if (user != null)
{
examineQuery = await _examineRepository.GetAll().AsNoTracking()
.Where(s => s.ArticleId == article.Id && s.ApplicationUserId == user.Id && s.IsPassed == null
&& (s.Operation == Operation.EditArticleMain || s.Operation == Operation.EditArticleMainPage || s.Operation == Operation.EditArticleRelevanes))
.Select(s => new Examine
{
Operation = s.Operation,
Context = s.Context
})
.ToListAsync();
}
//读取当前登入用户审核信息 获取待审核的内容
Examine examine = null;
if (user != null)
{
examine = examineQuery.FirstOrDefault(s => s.Operation == Operation.EditArticleMain);
if (examine != null)
{
await _articleService.UpdateArticleData(article, examine);
}
examine = examineQuery.FirstOrDefault(s => s.Operation == Operation.EditArticleMainPage);
if (examine != null)
{
await _articleService.UpdateArticleData(article, examine);
}
examine = examineQuery.FirstOrDefault(s => s.Operation == Operation.EditArticleRelevanes);
if (examine != null)
{
await _articleService.UpdateArticleData(article, examine);
}
}
//建立视图模型
var model = _articleService.GetArticleViewModelAsync(article);
model.CommentCount = await _commentUpRepository.LongCountAsync(s => s.ArticleId == id);
if (user != null)
{
if (examineQuery.Any(s => s.Operation == Operation.EditArticleMain))
{
model.MainState = EditState.Preview;
}
if (examineQuery.Any(s => s.Operation == Operation.EditArticleMain))
{
model.MainPageState = EditState.Preview;
}
if (examineQuery.Any(s => s.Operation == Operation.EditArticleRelevanes))
{
model.RelevancesState = EditState.Preview;
}
}
//复制数据
var createUser = article.CreateUser;
if (createUser == null)
{
article.CreateUser = createUser = article.Examines.First(s => s.IsPassed == true).ApplicationUser;
}
model.UserInfor = await _userService.GetUserInforViewModel(createUser);
model.LastExamineId = article.Examines.Last().Id;
//判断是否有权限编辑
if (user != null && await _userManager.IsInRoleAsync(user, "Editor") == true)
{
model.Authority = true;
}
else
{
if (user != null && user.Id == article.CreateUser.Id)
{
model.Authority = true;
}
else
{
model.Authority = false;
}
}
//判断是否已经点赞
if (user != null && article.ThumbsUps != null)
{
if (article.ThumbsUps.Find(s => s.ApplicationUserId == user.Id) == null)
{
model.IsThumbsUp = false;
}
else
{
model.IsThumbsUp = true;
}
}
else
{
model.IsThumbsUp = false;
}
//序列化相关性列表
//读取当前用户等待审核的信息
if (user != null)
{
examine = examineQuery.Find(s => s.Operation == Operation.EditArticleRelevanes);
if (examine != null)
{
model.RelevancesState = EditState.Preview;
await _articleService.UpdateArticleData(article, examine);
}
}
var examiningList = new List<Operation>();
if (user != null)
{
examiningList = await _examineRepository.GetAll().Where(s => s.ArticleId == article.Id && s.ApplicationUserId != user.Id && s.IsPassed == null).Select(s => s.Operation).ToListAsync();
}
//获取各部分状态
if (user != null)
{
if (model.MainState != EditState.Preview)
{
if (examiningList.Any(s => s == Operation.EditArticleMain))
{
model.MainState = EditState.Locked;
}
else
{
model.MainState = EditState.Normal;
}
}
if (model.MainPageState != EditState.Preview)
{
if (examiningList.Any(s => s == Operation.EditArticleMainPage))
{
model.MainPageState = EditState.Locked;
}
else
{
model.MainPageState = EditState.Normal;
}
}
if (model.RelevancesState != EditState.Preview)
{
if (examiningList.Any(s => s == Operation.EditArticleRelevanes))
{
model.RelevancesState = EditState.Locked;
}
else
{
model.RelevancesState = EditState.Normal;
}
}
}
//增加文章阅读次数
await _appHelper.ArticleReaderNumUpAsync(article.Id);
return model;
}
[HttpGet("{id}")]
public async Task<ActionResult<ArticleAsyncInforViewModel>> GetArticleAsyncInfor(long id)
{
//获取当前用户ID
var user = await _appHelper.GetAPICurrentUserAsync(HttpContext);
//通过Id获取文章
var article = await _articleRepository.GetAll().AsNoTracking().Include(s => s.CreateUser).Include(s => s.ThumbsUps).FirstOrDefaultAsync(x => x.Id == id);
if (article == null)
{
return NotFound();
}
//建立视图模型
var model = new ArticleAsyncInforViewModel();
//判断当前是否隐藏
if (article.IsHidden == true)
{
if (user == null || await _userManager.IsInRoleAsync(user, "Editor") != true)
{
return NotFound();
}
}
//判断是否有权限编辑
if (user != null && await _userManager.IsInRoleAsync(user, "Editor") == true)
{
model.Authority = true;
}
else
{
if (user != null && user.Id == article.CreateUser.Id)
{
model.Authority = true;
}
else
{
model.Authority = false;
}
}
//判断是否已经点赞
if (article.ThumbsUps != null && article.ThumbsUps.Find(s => s.ApplicationUserId == user.Id) == null)
{
model.IsThumbsUp = false;
}
else
{
model.IsThumbsUp = true;
}
return model;
}
[HttpPost]
public async Task<ActionResult<Result>> CreateArticleAsync(CreateArticleViewModel model)
{
try
{
//获取当前用户ID
var user = await _appHelper.GetAPICurrentUserAsync(HttpContext);
//检查是否超过编辑上限
if (await _examineRepository.CountAsync(s => s.ApplicationUserId == user.Id && s.IsPassed == null) > ToolHelper.MaxEditorCount)
{
return new Result { Successful = false, Error = "当前已超过最大待审核编辑数目,请等待审核通过后继续编辑,长时间未更新请联系管理员" };
}
//判断是否是上传图片
//判断名称是否重复
if (await _articleRepository.FirstOrDefaultAsync(s => s.Name == model.Main.Name) != null)
{
return new Result { Error = "该文章的名称与其他文章重复", Successful = false };
}
if (model.Main.Type == ArticleType.Notice && await _userManager.IsInRoleAsync(user, "Admin") == false)
{
return new Result { Error = "只有管理员才有权限发布公告", Successful = false };
}
//处理原始数据 删除空项目
model.Relevances.Roles.RemoveAll(s => string.IsNullOrWhiteSpace(s.DisplayName));
model.Relevances.Staffs.RemoveAll(s => string.IsNullOrWhiteSpace(s.DisplayName));
model.Relevances.Groups.RemoveAll(s => string.IsNullOrWhiteSpace(s.DisplayName));
model.Relevances.Games.RemoveAll(s => string.IsNullOrWhiteSpace(s.DisplayName));
model.Relevances.Articles.RemoveAll(s => string.IsNullOrWhiteSpace(s.DisplayName));
model.Relevances.Others.RemoveAll(s => string.IsNullOrWhiteSpace(s.DisplayName));
//预处理 建立词条关联信息
//判断关联是否存在
var entryIds = new List<int>();
var entryNames = new List<string>();
var articleIds = new List<long>();
var articleNames = new List<string>();
entryNames.AddRange(model.Relevances.Games.Where(s => string.IsNullOrWhiteSpace(s.DisplayName) == false).Select(s => s.DisplayName));
entryNames.AddRange(model.Relevances.Groups.Where(s => string.IsNullOrWhiteSpace(s.DisplayName) == false).Select(s => s.DisplayName));
entryNames.AddRange(model.Relevances.Staffs.Where(s => string.IsNullOrWhiteSpace(s.DisplayName) == false).Select(s => s.DisplayName));
entryNames.AddRange(model.Relevances.Roles.Where(s => string.IsNullOrWhiteSpace(s.DisplayName) == false).Select(s => s.DisplayName));
//建立文章关联信息
articleNames.AddRange(model.Relevances.Articles.Where(s => string.IsNullOrWhiteSpace(s.DisplayName) == false).Select(s => s.DisplayName));
try
{
entryIds = await _entryService.GetEntryIdsFromNames(entryNames);
articleIds = await _articleService.GetArticleIdsFromNames(articleNames);
}
catch (Exception ex)
{
return new Result { Successful = false, Error = ex.Message };
}
var entries = await _entryRepository.GetAll().Where(s => entryIds.Contains(s.Id)).ToListAsync();
var articles = await _articleRepository.GetAll().Where(s => articleIds.Contains(s.Id)).ToListAsync();
var newArticle = new Article
{
CreateTime = DateTime.Now.ToCstTime()
};
_articleService.SetDataFromEditArticleMainViewModel(newArticle, model.Main);
_articleService.SetDataFromEditArticleMainPageViewModel(newArticle, model.MainPage);
_articleService.SetDataFromEditArticleRelevancesViewModel(newArticle, model.Relevances, entries, articles);
var article = new Article();
//获取审核记录
try
{
article = await _examineService.AddNewArticleExaminesAsync(newArticle, user, model.Note);
}
catch (Exception ex)
{
return new Result { Successful = false, Error = ex.Message };
}
return new Result { Successful = true, Error = article.Id.ToString() };
}
catch
{
return new Result { Error = "发表文章的过程中发生未知错误,请确保数据格式正确后联系管理员", Successful = false };
}
}
[HttpGet("{id}")]
public async Task<ActionResult<EditArticleMainViewModel>> EditArticleMainAsync(long Id)
{
//获取当前用户ID
var user = await _appHelper.GetAPICurrentUserAsync(HttpContext);
var article = await _articleRepository.GetAll().AsNoTracking()
.FirstOrDefaultAsync(s => s.Id == Id);
if (article == null)
{
return NotFound();
}
//判断是否为锁定状态
if (await _examineRepository.GetAll().AnyAsync(s => s.ApplicationUserId != user.Id && s.IsPassed == null && s.ArticleId == Id
&& (s.Operation == Operation.EditArticleMain)))
{
return NotFound();
}
var model = new EditArticleMainViewModel
{
Id = article.Id,
Name = article.Name,
};
//获取审核记录
var examines = await _examineRepository.GetAllListAsync(s => s.ArticleId == article.Id && s.ApplicationUserId == user.Id
&& (s.Operation == Operation.EditArticleMain) && s.IsPassed == null);
//第一步 获取主要信息
var examine = examines.FirstOrDefault(s => s.Operation == Operation.EditArticleMain);
if (examine != null)
{
await _articleService.UpdateArticleData(article, examine);
}
model.MainPicture = article.MainPicture;
model.BackgroundPicture = article.BackgroundPicture;
model.Name = article.Name;
model.BriefIntroduction = article.BriefIntroduction;
model.Type = article.Type;
model.OriginalAuthor = article.OriginalAuthor;
model.OriginalLink = article.OriginalLink;
model.PubishTime = article.PubishTime;
model.NewsType = article.NewsType;
model.RealNewsTime = article.RealNewsTime;
model.DisplayName = article.DisplayName;
model.SmallBackgroundPicture = article.SmallBackgroundPicture;
return model;
}
[HttpPost]
public async Task<ActionResult<Result>> EditArticleMainAsync(EditArticleMainViewModel model)
{
try
{
var user = await _appHelper.GetAPICurrentUserAsync(HttpContext);
//检查是否超过编辑上限
if (await _examineRepository.CountAsync(s => s.ApplicationUserId == user.Id && s.IsPassed == null) > ToolHelper.MaxEditorCount)
{
return new Result { Successful = false, Error = "当前已超过最大待审核编辑数目,请等待审核通过后继续编辑,长时间未更新请联系管理员" };
}
if (await _appHelper.CanUserEditArticleAsync(user, model.Id) == false)
{
return new Result { Error = "权限不足,文章有其发布者和管理员才能编辑", Successful = false };
}
//判断是否为锁定状态
if (await _examineRepository.GetAll().AnyAsync(s => s.ApplicationUserId != user.Id && s.ArticleId == model.Id && s.IsPassed == null && (s.Operation == Operation.EditArticleMain)))
{
return new Result { Error = "当前文章已经被另一名用户编辑,正在等待审核,请等待审核结束后再进行编辑", Successful = false };
}
if (model.Type == ArticleType.Notice && await _userManager.IsInRoleAsync(user, "Admin") == false)
{
return new Result { Error = "只有管理员才有权限发布公告", Successful = false };
}
//判断名称是否重复
if (await _articleRepository.GetAll().AnyAsync(s => s.Name == model.Name && s.Id != model.Id))
{
return new Result { Error = "该文章的名称与其他文章重复", Successful = false };
}
//查找当前文章
var currentArticle = await _articleRepository.GetAll()
.FirstOrDefaultAsync(s => s.Id == model.Id);
var newArticle = await _articleRepository.GetAll().AsNoTracking()
.FirstOrDefaultAsync(s => s.Id == model.Id);
if (currentArticle == null)
{
return new Result { Error = $"无法找到ID为{model.Id}的文章", Successful = false };
}
//第一步 修改文章主要信息
_articleService.SetDataFromEditArticleMainViewModel(newArticle, model);
var examines = _articleService.ExaminesCompletion(currentArticle, newArticle);
if (examines.Any(s => s.Value == Operation.EditArticleMain) == false)
{
return new Result { Successful = true };
}
var examine = examines.FirstOrDefault(s => s.Value == Operation.EditArticleMain);
//保存并尝试应用审核记录
await _editRecordService.SaveAndApplyEditRecord(currentArticle, user, examine.Key, Operation.EditArticleMain, model.Note);
return new Result { Successful = true, Error = currentArticle.Id.ToString() };
}
catch
{
return new Result { Error = "修改文章的过程中发生未知错误,请确保数据格式正确后联系管理员", Successful = false };
}
}
[HttpGet("{id}")]
public async Task<ActionResult<EditArticleMainPageViewModel>> EditArticleMainPageAsync(long Id)
{
//获取当前用户ID
var user = await _appHelper.GetAPICurrentUserAsync(HttpContext);
//获取词条
var article = await _articleRepository.GetAll().AsNoTracking()
.FirstOrDefaultAsync(s => s.Id == Id);
if (article == null)
{
return NotFound();
}
//判断是否为锁定状态
if (await _examineRepository.GetAll().AnyAsync(s => s.ApplicationUserId != user.Id && s.IsPassed == null && s.ArticleId == Id
&& (s.Operation == Operation.EditArticleMainPage)))
{
return NotFound();
}
var model = new EditArticleMainPageViewModel
{
Id = article.Id,
Name = article.Name,
};
//获取审核记录
var examines = await _examineRepository.GetAllListAsync(s => s.ArticleId == article.Id && s.ApplicationUserId == user.Id
&& (s.Operation == Operation.EditArticleMainPage) && s.IsPassed == null);
//第三步 获取正文
var examine = examines.FirstOrDefault(s => s.Operation == Operation.EditArticleMainPage);
if (examine != null)
{
await _articleService.UpdateArticleData(article, examine);
}
model.Context = article.MainPage;
return model;
}
[HttpPost]
public async Task<ActionResult<Result>> EditArticleMainPageAsync(EditArticleMainPageViewModel model)
{
try
{
var user = await _appHelper.GetAPICurrentUserAsync(HttpContext);
//检查是否超过编辑上限
if (await _examineRepository.CountAsync(s => s.ApplicationUserId == user.Id && s.IsPassed == null) > ToolHelper.MaxEditorCount)
{
return new Result { Successful = false, Error = "当前已超过最大待审核编辑数目,请等待审核通过后继续编辑,长时间未更新请联系管理员" };
}
if (await _appHelper.CanUserEditArticleAsync(user, model.Id) == false)
{
return new Result { Error = "权限不足,文章有其发布者和管理员才能编辑", Successful = false };
}
//判断是否为锁定状态
if (await _examineRepository.GetAll().AnyAsync(s => s.ApplicationUserId != user.Id && s.ArticleId == model.Id && s.IsPassed == null && (s.Operation == Operation.EditArticleMainPage)))
{
return new Result { Error = "当前文章已经被另一名用户编辑,正在等待审核,请等待审核结束后再进行编辑", Successful = false };
}
//判断名称是否重复
if (await _articleRepository.GetAll().AnyAsync(s => s.Name == model.Name && s.Id != model.Id))
{
return new Result { Error = "该文章的名称与其他文章重复", Successful = false };
}
//查找当前文章
var currentArticle = await _articleRepository.GetAll()
.FirstOrDefaultAsync(s => s.Id == model.Id);
var newArticle = await _articleRepository.GetAll().AsNoTracking()
.FirstOrDefaultAsync(s => s.Id == model.Id);
if (currentArticle == null)
{
return new Result { Error = $"无法找到ID为{model.Id}的文章", Successful = false };
}
//第三步 修改文章正文
_articleService.SetDataFromEditArticleMainPageViewModel(newArticle, model);
var examines = _articleService.ExaminesCompletion(currentArticle, newArticle);
if (examines.Any(s => s.Value == Operation.EditArticleMainPage) == false)
{
return new Result { Successful = true };
}
var examine = examines.FirstOrDefault(s => s.Value == Operation.EditArticleMainPage);
//保存并尝试应用审核记录
await _editRecordService.SaveAndApplyEditRecord(currentArticle, user, examine.Key, Operation.EditArticleMainPage, model.Note);
return new Result { Successful = true, Error = currentArticle.Id.ToString() };
}
catch
{
return new Result { Error = "修改文章的过程中发生未知错误,请确保数据格式正确后联系管理员", Successful = false };
}
}
[HttpGet("{id}")]
public async Task<ActionResult<EditArticleRelevancesViewModel>> EditArticleRelevancesAsync(long Id)
{
//获取当前用户ID
var user = await _appHelper.GetAPICurrentUserAsync(HttpContext);
//获取词条
var article = await _articleRepository.GetAll().AsNoTracking()
.Include(s => s.ArticleRelationFromArticleNavigation).ThenInclude(s => s.ToArticleNavigation)
.Include(s => s.Entries)
.Include(s => s.Outlinks)
.FirstOrDefaultAsync(s => s.Id == Id);
if (article == null)
{
return NotFound();
}
//判断是否为锁定状态
if (await _examineRepository.GetAll().AnyAsync(s => s.ApplicationUserId != user.Id && s.IsPassed == null && s.ArticleId == Id
&& (s.Operation == Operation.EditArticleRelevanes)))
{
return NotFound();
}
var model = new EditArticleRelevancesViewModel
{
Id = article.Id,
Name = article.Name,
};
//获取审核记录
var examines = await _examineRepository.GetAllListAsync(s => s.ArticleId == article.Id && s.ApplicationUserId == user.Id
&& (s.Operation == Operation.EditArticleRelevanes) && s.IsPassed == null);
var examine = examines.FirstOrDefault(s => s.Operation == Operation.EditArticleRelevanes);
if (examine != null)
{
await _articleService.UpdateArticleData(article, examine);
}
var roles = new List<RelevancesModel>();
var staffs = new List<RelevancesModel>();
var articles = new List<RelevancesModel>();
var groups = new List<RelevancesModel>();
var games = new List<RelevancesModel>();
var news = new List<RelevancesModel>();
var others = new List<RelevancesModel>();
foreach (var nav in article.ArticleRelationFromArticleNavigation)
{
var item = nav.ToArticleNavigation;
articles.Add(new RelevancesModel
{
DisplayName = item.Name
});
}
foreach (var item in article.Entries)
{
switch (item.Type)
{
case EntryType.Role:
roles.Add(new RelevancesModel
{
DisplayName = item.Name
});
break;
case EntryType.Staff:
staffs.Add(new RelevancesModel
{
DisplayName = item.Name
});
break;
case EntryType.ProductionGroup:
groups.Add(new RelevancesModel
{
DisplayName = item.Name
});
break;
case EntryType.Game:
games.Add(new RelevancesModel
{
DisplayName = item.Name
});
break;
}
}
foreach (var item in article.Outlinks)
{
others.Add(new RelevancesModel
{
DisplayName = item.Name,
DisPlayValue = item.BriefIntroduction,
Link = item.Link
});
}
model.Roles = roles;
model.Staffs = staffs;
model.Articles = articles;
model.Groups = groups;
model.Games = games;
model.Others = others;
return model;
}
[HttpPost]
public async Task<ActionResult<Result>> EditArticleRelevancesAsync(EditArticleRelevancesViewModel model)
{
try
{
var user = await _appHelper.GetAPICurrentUserAsync(HttpContext);
//检查是否超过编辑上限
if (await _examineRepository.CountAsync(s => s.ApplicationUserId == user.Id && s.IsPassed == null) > ToolHelper.MaxEditorCount)
{
return new Result { Successful = false, Error = "当前已超过最大待审核编辑数目,请等待审核通过后继续编辑,长时间未更新请联系管理员" };
}
if (await _appHelper.CanUserEditArticleAsync(user, model.Id) == false)
{
return new Result { Error = "权限不足,文章有其发布者和管理员才能编辑", Successful = false };
}
//判断是否为锁定状态
if (await _examineRepository.GetAll().AnyAsync(s => s.ApplicationUserId != user.Id && s.ArticleId == model.Id && s.IsPassed == null && (s.Operation == Operation.EditArticleMain || s.Operation == Operation.EditArticleMainPage || s.Operation == Operation.EditArticleRelevanes)))
{
return new Result { Error = "当前文章已经被另一名用户编辑,正在等待审核,请等待审核结束后再进行编辑", Successful = false };
}
//处理原始数据 删除空项目
model.Roles.RemoveAll(s => string.IsNullOrWhiteSpace(s.DisplayName));
model.Staffs.RemoveAll(s => string.IsNullOrWhiteSpace(s.DisplayName));
model.Groups.RemoveAll(s => string.IsNullOrWhiteSpace(s.DisplayName));
model.Games.RemoveAll(s => string.IsNullOrWhiteSpace(s.DisplayName));
model.Articles.RemoveAll(s => string.IsNullOrWhiteSpace(s.DisplayName));
model.Others.RemoveAll(s => string.IsNullOrWhiteSpace(s.DisplayName));
//预处理 建立词条关联信息
//判断关联是否存在
var entryIds = new List<int>();
var entryNames = new List<string>();
var articleIds = new List<long>();
var articleNames = new List<string>();
entryNames.AddRange(model.Games.Where(s => string.IsNullOrWhiteSpace(s.DisplayName) == false).Select(s => s.DisplayName));
entryNames.AddRange(model.Groups.Where(s => string.IsNullOrWhiteSpace(s.DisplayName) == false).Select(s => s.DisplayName));
entryNames.AddRange(model.Staffs.Where(s => string.IsNullOrWhiteSpace(s.DisplayName) == false).Select(s => s.DisplayName));
entryNames.AddRange(model.Roles.Where(s => string.IsNullOrWhiteSpace(s.DisplayName) == false).Select(s => s.DisplayName));
//建立文章关联信息
articleNames.AddRange(model.Articles.Where(s => string.IsNullOrWhiteSpace(s.DisplayName) == false).Select(s => s.DisplayName));
try
{
entryIds = await _entryService.GetEntryIdsFromNames(entryNames);
articleIds = await _articleService.GetArticleIdsFromNames(articleNames);
}
catch (Exception ex)
{
return new Result { Successful = false, Error = ex.Message };
}
//查找当前文章
var currentArticle = await _articleRepository.GetAll()
.Include(s => s.ArticleRelationFromArticleNavigation).ThenInclude(s => s.ToArticleNavigation)
.Include(s => s.Entries)
.Include(s => s.Outlinks)
.FirstOrDefaultAsync(s => s.Id == model.Id);
var newArticle = await _articleRepository.GetAll().AsNoTracking()
.Include(s => s.ArticleRelationFromArticleNavigation).ThenInclude(s => s.ToArticleNavigation)
.Include(s => s.Entries)
.Include(s => s.Outlinks)
.FirstOrDefaultAsync(s => s.Id == model.Id);
if (currentArticle == null)
{
return new Result { Error = $"无法找到ID为{model.Id}的文章", Successful = false };
}
var entries = await _entryRepository.GetAll().Where(s => entryIds.Contains(s.Id)).ToListAsync();
var articles = await _articleRepository.GetAll().Where(s => articleIds.Contains(s.Id)).ToListAsync();
_articleService.SetDataFromEditArticleRelevancesViewModel(newArticle, model, entries, articles);
var examines = _articleService.ExaminesCompletion(currentArticle, newArticle);
if (examines.Any(s => s.Value == Operation.EditArticleRelevanes) == false)
{
return new Result { Successful = true };
}
var examine = examines.FirstOrDefault(s => s.Value == Operation.EditArticleRelevanes);
//保存并尝试应用审核记录
await _editRecordService.SaveAndApplyEditRecord(currentArticle, user, examine.Key, Operation.EditArticleRelevanes, model.Note);
return new Result { Successful = true, Error = currentArticle.Id.ToString() };
}
catch
{
return new Result { Error = "修改文章的过程中发生未知错误,请确保数据格式正确后联系管理员", Successful = false };
}
}
[HttpPost]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme, Roles = "Admin")]
public async Task<ActionResult<Result>> HiddenArticleAsync(HiddenArticleModel model)
{
await _articleRepository.GetRangeUpdateTable().Where(s => model.Ids.Contains(s.Id)).Set(s => s.IsHidden, b => model.IsHidden).ExecuteAsync();
return new Result { Successful = true };
}
[HttpPost]
public async Task<ActionResult<Result>> ThumbsUpArticleAsync(ThumbsUpArticleModel model)
{
var user = await _appHelper.GetAPICurrentUserAsync(HttpContext);
//通过Id获取文章
var article = await _articleRepository.FirstOrDefaultAsync(x => x.Id == model.Id);
if (user == null || article == null)
{
return NotFound();
}
var thumbs = await _thumbsUpRepository.FirstOrDefaultAsync(x => x.ArticleId == model.Id && x.ApplicationUserId == user.Id);
if (model.IsThumbsUp)
{
if (thumbs == null)
{
await _thumbsUpRepository.InsertAsync(new ThumbsUp
{
ApplicationUserId = user.Id,
ApplicationUser = user,
Article = article,
ArticleId = article.Id,
ThumbsUpTime = DateTime.Now.ToCstTime()
});
}
else
{
return new Result { Successful = false, Error = "当前文章已经被该用户点赞" };
}
}
else
{
if (thumbs == null)
{
return new Result { Successful = false, Error = "该用户还没有点赞当前文章" };
}
else
{
await _thumbsUpRepository.DeleteAsync(thumbs);
}
}
//计算点赞总数
var tempCount = await _thumbsUpRepository.CountAsync(s => s.ArticleId == article.Id);
await _articleRepository.GetRangeUpdateTable().Where(s => s.Id == article.Id).Set(s => s.ThumbsUpCount, b => tempCount).ExecuteAsync();
return new Result { Successful = true };
}
[HttpPost]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme, Roles = "Admin")]
public async Task<ActionResult<Result>> EditArticlePriorityAsync(EditArticlePriorityViewModel model)
{
await _articleRepository.GetRangeUpdateTable().Where(s => model.Ids.Contains(s.Id)).Set(s => s.Priority, b => b.Priority + model.PlusPriority).ExecuteAsync();
return new Result { Successful = true };
}
[AllowAnonymous]
[HttpPost]
public async Task<PagedResultDto<ArticleInforTipViewModel>> GetArticleHomeListAsync(PagedSortedAndFilterInput input)
{
return await _articleService.GetPaginatedResult(input);
}
[AllowAnonymous]
[HttpGet]
public async Task<ActionResult<GetArticleCountModel>> GetArticleCountAsync()
{
var model = new GetArticleCountModel
{
All = await _articleRepository.CountAsync(),
Toughts = await _articleRepository.CountAsync(x => x.Type == ArticleType.Tought),
Interviews = await _articleRepository.CountAsync(x => x.Type == ArticleType.Interview),
Strategies = await _articleRepository.CountAsync(x => x.Type == ArticleType.Strategy),
News = await _articleRepository.CountAsync(x => x.Type == ArticleType.News),
Others = await _articleRepository.CountAsync(x => x.Type == ArticleType.None),
Hiddens = await _articleRepository.CountAsync(x => x.IsHidden == true)
};
return model;
}
/// <summary>
/// 获取输入提示
/// </summary>
/// <returns></returns>
[HttpGet]
[AllowAnonymous]
public async Task<ActionResult<IEnumerable<string>>> GetAllArticlesAsync()
{
return await _articleRepository.GetAll().AsNoTracking().Where(s => s.IsHidden != true && string.IsNullOrWhiteSpace(s.Name) == false).Select(s => s.Name).ToArrayAsync();
}
/// <summary>
/// 获取所有文章名称ID对
/// </summary>
/// <returns></returns>
[HttpGet]
[AllowAnonymous]
public async Task<ActionResult<IEnumerable<NameIdPairModel>>> GetAllArticlesIdNameAsync()
{
return await _articleRepository.GetAll().AsNoTracking().Where(s => s.IsHidden != true && string.IsNullOrWhiteSpace(s.Name) == false).Select(s => new NameIdPairModel { Name = s.Name, Id = s.Id }).ToArrayAsync();
}
[HttpPost]
public async Task<ActionResult<Result>> RevokeExamine(RevokeExamineModel model)
{
//获取当前用户ID
var user = await _appHelper.GetAPICurrentUserAsync(HttpContext);
//查找审核
await _examineRepository.DeleteRangeAsync(s => s.ArticleId == model.Id && s.ApplicationUserId == user.Id && s.IsPassed == null);
return new Result { Successful = true };
}
[AllowAnonymous]
[HttpGet("{time}")]
public async Task<ActionResult<List<NewsSummaryAloneViewModel>>> GetNewSummary(string time)