网站首页 网站源码
website
站点相关全部源代码,隐藏了一些关于服务器的信息
using Dpz.Core.Public.ViewModel.RequestEvent;

namespace Dpz.Core.Service.RepositoryServiceImpl;

public class TimelineService(IRepository<Timeline> repository, IMapper mapper, IMediator mediator)
    : ITimelineService
{
    public async Task<List<VmTimeline>> GetTimelinesAsync(string account)
    {
        var list = await repository
            .SearchFor(x => x.Author.Id == account)
            .OrderByDescending(x => x.Date)
            .ToListAsync();
        return mapper.Map<List<VmTimeline>>(list);
    }

    public async Task SaveAsync(VmTimeline viewModel)
    {
        var entity = mapper.Map<Timeline>(viewModel);
        if (entity == null)
            throw new ArgumentException("viewModel is null", nameof(viewModel));
        if (entity.Id != ObjectId.Empty)
        {
            entity = await repository.TryGetAsync(viewModel.Id);
            if (entity == null)
                return;

            var request = new EditMarkdownRequest
            {
                Markdown = viewModel.Content,
                OriginalMarkdown = entity.Content
            };
            await mediator.Send(request);

            var update = Builders<Timeline>
                .Update.Set(x => x.Title, viewModel.Title)
                .Set(x => x.Content, viewModel.Content)
                .Set(x => x.Date, viewModel.Date)
                .Set(x => x.More, viewModel.More)
                .Set(x => x.LastUpdateTime, DateTime.Now);
            await repository.UpdateAsync(x => x.Id == entity.Id, update);
            return;
        }

        await repository.InsertAsync(entity);
    }

    public async Task DeleteAsync(params string[] id)
    {
        var ids = id.Select(x => ObjectId.TryParse(x, out var oid) ? oid : (ObjectId?)null)
            .Where(x => x.HasValue)
            .Select(x => x!.Value)
            .ToArray();
        var list = await repository.SearchFor(x => ids.Contains(x.Id)).ToListAsync();
        var contents = list.Select(x => x.Content).ToList();
        await mediator.Send(new DeleteMarkdownRequest { Markdown = contents });
        await repository.DeleteAsync(x => ids.Contains(x.Id));
    }

    public async Task<IPagedList<VmTimeline>> GetPageAsync(
        int pageIndex,
        int pageSize,
        string? content = null,
        string? account = null
    )
    {
        var predicate = repository.SearchFor(x => true);
        if (!string.IsNullOrEmpty(content))
        {
            //x.Content.Contains(content) ||
            predicate = predicate.Where(x =>
                x.Title.Contains(content) || x.Content.Contains(content)
            );
        }

        if (!string.IsNullOrEmpty(account))
        {
            predicate = predicate.Where(x => x.Author.Id == account);
        }

        return await predicate
            .OrderByDescending(x => x.Date)
            .ToPagedListAsync<Timeline, VmTimeline>(pageIndex, pageSize);
    }

    public async Task<VmTimeline?> FindAsync(string id)
    {
        var entity = await repository.TryGetAsync(id);
        if (entity == null)
            return null;
        return mapper.Map<VmTimeline>(entity);
    }
}
loading