using Dpz.Core.Public.ViewModel.RequestEvent;
namespace Dpz.Core.Service.V4.Implements;
public class MumbleService(IRepository<Mumble> repository, IMapper mapper, IMediator mediator)
: IMumbleService
{
public async Task<VmMumble> CreateAsync(VmMumble talk)
{
var entity = mapper.Map<Mumble>(talk);
await repository.InsertAsync(entity);
return mapper.Map<VmMumble>(entity);
}
public async Task<VmMumble?> EditContentAsync(VmMumble mumble)
{
if (mumble == null)
throw new ArgumentNullException(nameof(mumble));
if (mumble.Markdown == null)
throw new ArgumentNullException(nameof(mumble.Markdown));
if (mumble.HtmlContent == null)
throw new ArgumentNullException(nameof(mumble.HtmlContent));
var entity = await repository.TryGetAsync(mumble.Id);
if (entity == null)
return null;
var request = new EditMarkdownRequest
{
Markdown = mumble.Markdown,
OriginalMarkdown = entity.Markdown
};
await mediator.Send(request);
var update = Builders<Mumble>
.Update.Set(x => x.HtmlContent, mumble.HtmlContent)
.Set(x => x.Markdown, mumble.Markdown)
.Set(x => x.LastUpdateTime, DateTime.Now);
await repository.UpdateAsync(x => x.Id == entity.Id, update);
return mapper.Map<VmMumble>(await repository.FindAsync(entity.Id));
}
public async Task<IPagedList<VmMumble>> GetPagesAsync(
int pageIndex,
int pageSize,
string? content = null,
string? account = null
)
{
var predicate = repository.SearchFor(x => true);
if (!string.IsNullOrEmpty(content))
predicate = predicate.Where(x => x.Markdown.Contains(content));
if (!string.IsNullOrEmpty(account))
predicate = predicate.Where(x => x.Author.Id == account);
return await predicate
.OrderByDescending(x => x.CreateTime)
.ToPagedListAsync<Mumble, VmMumble>(pageIndex, pageSize);
}
public async Task<VmMumble?> FindAsync(string id)
{
return mapper.Map<VmMumble?>(await repository.TryGetAsync(id));
}
public async Task DeleteAsync(params string[] id)
{
var list = await repository.TryGetListAsync(id);
var request = new DeleteMarkdownRequest
{
Markdown = list.Select(x => x.Markdown).ToList()
};
await mediator.Send(request);
await repository.TryDeleteAsync(id);
}
public async Task<VmMumble?> LikeAsync(string id)
{
var entity = await repository.TryGetAsync(id);
if (entity is null)
return null;
entity.Like += 1;
var update = Builders<Mumble>.Update.Inc(x => x.Like, 1);
await repository.UpdateAsync(z => z.Id == entity.Id, update);
return mapper.Map<VmMumble>(entity);
}
}