using Admin.Core.Common.Output; using Admin.Core.Model.Admin; using Admin.Core.Repository.Admin; using Admin.Core.Service.Admin.Document.Input; using Admin.Core.Service.Admin.Document.Output; using System; using System.Linq; using System.Threading.Tasks; namespace Admin.Core.Service.Admin.Document { public class DocumentService : BaseService, IDocumentService { private readonly IDocumentRepository _documentRepository; private readonly IDocumentImageRepository _documentImageRepository; public DocumentService( IDocumentRepository DocumentRepository, IDocumentImageRepository documentImageRepository ) { _documentRepository = DocumentRepository; _documentImageRepository = documentImageRepository; } public async Task GetAsync(long id) { var result = await _documentRepository.GetAsync(id); return ResponseOutput.Ok(result); } public async Task GetGroupAsync(long id) { var result = await _documentRepository.GetAsync(id); return ResponseOutput.Ok(result); } public async Task GetMenuAsync(long id) { var result = await _documentRepository.GetAsync(id); return ResponseOutput.Ok(result); } public async Task GetContentAsync(long id) { var result = await _documentRepository.GetAsync(id); return ResponseOutput.Ok(result); } public async Task GetListAsync(string key, DateTime? start, DateTime? end) { if (end.HasValue) { end = end.Value.AddDays(1); } var data = await _documentRepository .WhereIf(key.NotNull(), a => a.Name.Contains(key) || a.Label.Contains(key)) .WhereIf(start.HasValue && end.HasValue, a => a.CreatedTime.Value.BetweenEnd(start.Value, end.Value)) .OrderBy(a => a.ParentId) .OrderBy(a => a.Sort) .ToListAsync(); return ResponseOutput.Ok(data); } public async Task GetImageListAsync(long id) { var result = await _documentImageRepository.Select .Where(a => a.DocumentId == id) .OrderByDescending(a => a.Id) .ToListAsync(a => a.Url); return ResponseOutput.Ok(result); } public async Task AddGroupAsync(DocumentAddGroupInput input) { var entity = Mapper.Map(input); var id = (await _documentRepository.InsertAsync(entity)).Id; return ResponseOutput.Result(id > 0); } public async Task AddMenuAsync(DocumentAddMenuInput input) { var entity = Mapper.Map(input); var id = (await _documentRepository.InsertAsync(entity)).Id; return ResponseOutput.Result(id > 0); } public async Task AddImageAsync(DocumentAddImageInput input) { var entity = Mapper.Map(input); var id = (await _documentImageRepository.InsertAsync(entity)).Id; return ResponseOutput.Result(id > 0); } public async Task UpdateGroupAsync(DocumentUpdateGroupInput input) { var result = false; if (input != null && input.Id > 0) { var entity = await _documentRepository.GetAsync(input.Id); entity = Mapper.Map(input, entity); result = (await _documentRepository.UpdateAsync(entity)) > 0; } return ResponseOutput.Result(result); } public async Task UpdateMenuAsync(DocumentUpdateMenuInput input) { var result = false; if (input != null && input.Id > 0) { var entity = await _documentRepository.GetAsync(input.Id); entity = Mapper.Map(input, entity); result = (await _documentRepository.UpdateAsync(entity)) > 0; } return ResponseOutput.Result(result); } public async Task UpdateContentAsync(DocumentUpdateContentInput input) { var result = false; if (input != null && input.Id > 0) { var entity = await _documentRepository.GetAsync(input.Id); entity = Mapper.Map(input, entity); result = (await _documentRepository.UpdateAsync(entity)) > 0; } return ResponseOutput.Result(result); } public async Task DeleteAsync(long id) { var result = false; if (id > 0) { result = (await _documentRepository.DeleteAsync(m => m.Id == id)) > 0; } return ResponseOutput.Result(result); } public async Task DeleteImageAsync(long documentId, string url) { var result = false; if (documentId > 0 && url.NotNull()) { result = (await _documentImageRepository.DeleteAsync(m => m.DocumentId == documentId && m.Url == url)) > 0; } return ResponseOutput.Result(result); } public async Task SoftDeleteAsync(long id) { var result = await _documentRepository.SoftDeleteAsync(id); return ResponseOutput.Result(result); } public async Task GetPlainListAsync() { var documents = await _documentRepository.Select .OrderBy(a => a.ParentId) .OrderBy(a => a.Sort) .ToListAsync(a => new { a.Id, a.ParentId, a.Label, a.Type, a.Opened }); var menus = documents .Where(a => (new[] { DocumentType.Group, DocumentType.Markdown }).Contains(a.Type)) .Select(a => new { a.Id, a.ParentId, a.Label, a.Type, a.Opened }); return ResponseOutput.Ok(menus); } } }