DocumentService.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using Admin.Core.Repository.Admin;
  5. using Admin.Core.Model.Admin;
  6. using Admin.Core.Common.Output;
  7. using Admin.Core.Service.Admin.Document.Input;
  8. using Admin.Core.Service.Admin.Document.Output;
  9. namespace Admin.Core.Service.Admin.Document
  10. {
  11. public class DocumentService : BaseService, IDocumentService
  12. {
  13. private readonly IDocumentRepository _documentRepository;
  14. private readonly IDocumentImageRepository _documentImageRepository;
  15. public DocumentService(
  16. IDocumentRepository DocumentRepository,
  17. IDocumentImageRepository documentImageRepository
  18. )
  19. {
  20. _documentRepository = DocumentRepository;
  21. _documentImageRepository = documentImageRepository;
  22. }
  23. public async Task<IResponseOutput> GetAsync(long id)
  24. {
  25. var result = await _documentRepository.GetAsync(id);
  26. return ResponseOutput.Ok(result);
  27. }
  28. public async Task<IResponseOutput> GetGroupAsync(long id)
  29. {
  30. var result = await _documentRepository.GetAsync<DocumentGetGroupOutput>(id);
  31. return ResponseOutput.Ok(result);
  32. }
  33. public async Task<IResponseOutput> GetMenuAsync(long id)
  34. {
  35. var result = await _documentRepository.GetAsync<DocumentGetMenuOutput>(id);
  36. return ResponseOutput.Ok(result);
  37. }
  38. public async Task<IResponseOutput> GetContentAsync(long id)
  39. {
  40. var result = await _documentRepository.GetAsync<DocumentGetContentOutput>(id);
  41. return ResponseOutput.Ok(result);
  42. }
  43. public async Task<IResponseOutput> GetListAsync(string key, DateTime? start, DateTime? end)
  44. {
  45. if (end.HasValue)
  46. {
  47. end = end.Value.AddDays(1);
  48. }
  49. var data = await _documentRepository
  50. .WhereIf(key.NotNull(), a => a.Name.Contains(key) || a.Label.Contains(key))
  51. .WhereIf(start.HasValue && end.HasValue, a => a.CreatedTime.Value.BetweenEnd(start.Value, end.Value))
  52. .OrderBy(a => a.ParentId)
  53. .OrderBy(a => a.Sort)
  54. .ToListAsync<DocumentListOutput>();
  55. return ResponseOutput.Ok(data);
  56. }
  57. public async Task<IResponseOutput> GetImageListAsync(long id)
  58. {
  59. var result = await _documentImageRepository.Select
  60. .Where(a => a.DocumentId == id)
  61. .OrderByDescending(a=>a.Id)
  62. .ToListAsync(a => a.Url);
  63. return ResponseOutput.Ok(result);
  64. }
  65. public async Task<IResponseOutput> AddGroupAsync(DocumentAddGroupInput input)
  66. {
  67. var entity = Mapper.Map<DocumentEntity>(input);
  68. var id = (await _documentRepository.InsertAsync(entity)).Id;
  69. return ResponseOutput.Result(id > 0);
  70. }
  71. public async Task<IResponseOutput> AddMenuAsync(DocumentAddMenuInput input)
  72. {
  73. var entity = Mapper.Map<DocumentEntity>(input);
  74. var id = (await _documentRepository.InsertAsync(entity)).Id;
  75. return ResponseOutput.Result(id > 0);
  76. }
  77. public async Task<IResponseOutput> AddImageAsync(DocumentAddImageInput input)
  78. {
  79. var entity = Mapper.Map<DocumentImageEntity>(input);
  80. var id = (await _documentImageRepository.InsertAsync(entity)).Id;
  81. return ResponseOutput.Result(id > 0);
  82. }
  83. public async Task<IResponseOutput> UpdateGroupAsync(DocumentUpdateGroupInput input)
  84. {
  85. var result = false;
  86. if (input != null && input.Id > 0)
  87. {
  88. var entity = await _documentRepository.GetAsync(input.Id);
  89. entity = Mapper.Map(input, entity);
  90. result = (await _documentRepository.UpdateAsync(entity)) > 0;
  91. }
  92. return ResponseOutput.Result(result);
  93. }
  94. public async Task<IResponseOutput> UpdateMenuAsync(DocumentUpdateMenuInput input)
  95. {
  96. var result = false;
  97. if (input != null && input.Id > 0)
  98. {
  99. var entity = await _documentRepository.GetAsync(input.Id);
  100. entity = Mapper.Map(input, entity);
  101. result = (await _documentRepository.UpdateAsync(entity)) > 0;
  102. }
  103. return ResponseOutput.Result(result);
  104. }
  105. public async Task<IResponseOutput> UpdateContentAsync(DocumentUpdateContentInput input)
  106. {
  107. var result = false;
  108. if (input != null && input.Id > 0)
  109. {
  110. var entity = await _documentRepository.GetAsync(input.Id);
  111. entity = Mapper.Map(input, entity);
  112. result = (await _documentRepository.UpdateAsync(entity)) > 0;
  113. }
  114. return ResponseOutput.Result(result);
  115. }
  116. public async Task<IResponseOutput> DeleteAsync(long id)
  117. {
  118. var result = false;
  119. if (id > 0)
  120. {
  121. result = (await _documentRepository.DeleteAsync(m => m.Id == id)) > 0;
  122. }
  123. return ResponseOutput.Result(result);
  124. }
  125. public async Task<IResponseOutput> DeleteImageAsync(long documentId, string url)
  126. {
  127. var result = false;
  128. if (documentId > 0 && url.NotNull())
  129. {
  130. result = (await _documentImageRepository.DeleteAsync(m => m.DocumentId == documentId && m.Url == url)) > 0;
  131. }
  132. return ResponseOutput.Result(result);
  133. }
  134. public async Task<IResponseOutput> SoftDeleteAsync(long id)
  135. {
  136. var result = await _documentRepository.SoftDeleteAsync(id);
  137. return ResponseOutput.Result(result);
  138. }
  139. public async Task<IResponseOutput> GetPlainListAsync()
  140. {
  141. var documents = await _documentRepository.Select
  142. .OrderBy(a => a.ParentId)
  143. .OrderBy(a => a.Sort)
  144. .ToListAsync(a => new { a.Id, a.ParentId, a.Label, a.Type, a.Opened });
  145. var menus = documents
  146. .Where(a => (new[] { DocumentType.Group, DocumentType.Markdown }).Contains(a.Type))
  147. .Select(a => new
  148. {
  149. a.Id,
  150. a.ParentId,
  151. a.Label,
  152. a.Type,
  153. a.Opened
  154. });
  155. return ResponseOutput.Ok(menus);
  156. }
  157. }
  158. }