Files
Diary/Component/Entries/Service/EntryService.cs
2022-09-01 23:02:49 +01:00

46 lines
1.2 KiB
C#

using AutoMapper;
using Diary.Component.Entries.Repository;
using Diary.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Diary.Component.Entries.Service
{
public interface IEntryService
{
Task<EntryResource> CreateAsync(CreateEntryResource createEntryResource);
}
public class EntryService : IEntryService
{
private readonly IEntryRepository _entryRepository;
private readonly IMapper _mapper;
private readonly IUnitOfWork _unitOfWork;
public EntryService(IEntryRepository entryRepository, IMapper mapper, IUnitOfWork unitOfWork)
{
_entryRepository = entryRepository ?? throw new ArgumentNullException(nameof(entryRepository));
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
_unitOfWork = unitOfWork ?? throw new ArgumentNullException(nameof(unitOfWork));
}
public async Task<EntryResource> CreateAsync(CreateEntryResource createEntryResource)
{
Entry entry = _mapper.Map<Entry>(createEntryResource);
await _entryRepository.CreateAsync(entry);
await _unitOfWork.CompleteAsync();
return _mapper.Map<EntryResource>(entry);
}
}
}