Added Entry initial repo and services for a create action.

This commit is contained in:
2022-06-22 22:56:59 +01:00
parent e83cac2838
commit cbd5d7de4f
8 changed files with 120 additions and 3 deletions

View File

@@ -0,0 +1,14 @@
using AutoMapper;
using Diary.Component.Entries.Repository;
namespace Diary.Component.Entries.Service
{
public class EntryMappings : Profile
{
public EntryMappings()
{
CreateMap<CreateEntryResource, Entry>();
CreateMap<Entry, EntryResource>();
}
}
}

View File

@@ -0,0 +1,18 @@
namespace Diary.Component.Entries.Service
{
public class EntryResource
{
public int EnrtyID { get; set; }
public DateTime Date { get; set; }
public DateTime ValidFrom { get; set; }
public DateTime? ValidTo { get; set; }
public string Note { get; set; }
}
public class CreateEntryResource
{
public DateTime Date { get; set; }
public string Note { get; set; }
}
}

View File

@@ -0,0 +1,40 @@
using AutoMapper;
using Diary.Component.Entries.Repository;
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;
public EntryService(IEntryRepository entryRepository, IMapper mapper)
{
_entryRepository = entryRepository ?? throw new ArgumentNullException(nameof(entryRepository));
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
}
public async Task<EntryResource> CreateAsync(CreateEntryResource createEntryResource)
{
Entry entry = _mapper.Map<Entry>(createEntryResource);
await _entryRepository.CreateAsync(entry);
return _mapper.Map<EntryResource>(entry);
}
}
}