namespace Dpz.Core.Service.RepositoryServiceImpl;
public class VideoService:IVideoService
{
private readonly IRepository<Video> _repository;
private readonly IMapper _mapper;
public VideoService(IRepository<Video> repository,IMapper mapper)
{
_repository = repository;
_mapper = mapper;
}
public async Task<IList<VmVideo>> GetVideosAsync()
{
var list = await _repository.SearchFor(x => true).ToListAsync();
return list == null || !list.Any() ? new List<VmVideo>() : _mapper.Map<List<VmVideo>>(list);
}
public async Task SaveInformationAsync(VmVideo video)
{
if (video == null || string.IsNullOrEmpty(video.Id))
{
throw new ArgumentNullException(nameof(video), "argument video or video.id is null or empty");
}
var inVideo = _mapper.Map<Video>(video);
var filter = Builders<Video>.Filter.Eq(x => x.Id, inVideo.Id);
var find = await _repository.Collection.FindAsync(filter);
var entity = await find.FirstOrDefaultAsync();
if (entity == null)
{
await _repository.InsertAsync(inVideo);
}
else
{
await _repository.UpdateAsync(inVideo);
}
}
public async Task<VmVideo?> GetVideoAsync(string id)
{
var entity = await _repository.SearchFor(x => x.Id == id).FirstOrDefaultAsync();
if (entity == null) return null;
return _mapper.Map<VmVideo>(entity);
}
}