From cbd5d7de4fc8396acdeea52c34149c0cc7d722cc Mon Sep 17 00:00:00 2001 From: Tim Cliff Date: Wed, 22 Jun 2022 22:56:59 +0100 Subject: [PATCH] Added Entry initial repo and services for a create action. --- Component/Entries/EntriesController.cs | 19 +++++++++ .../{Entry => Entries}/Repository/Entry.cs | 2 +- .../Repository/EntryConfiguration.cs | 2 +- .../Entries/Repository/EntryRepository.cs | 26 ++++++++++++ Component/Entries/Service/EntryMappings.cs | 14 +++++++ Component/Entries/Service/EntryResources.cs | 18 +++++++++ Component/Entries/Service/EntryService.cs | 40 +++++++++++++++++++ Data/DiaryDBContext.cs | 2 +- 8 files changed, 120 insertions(+), 3 deletions(-) create mode 100644 Component/Entries/EntriesController.cs rename Component/{Entry => Entries}/Repository/Entry.cs (92%) rename Component/{Entry => Entries}/Repository/EntryConfiguration.cs (84%) create mode 100644 Component/Entries/Repository/EntryRepository.cs create mode 100644 Component/Entries/Service/EntryMappings.cs create mode 100644 Component/Entries/Service/EntryResources.cs create mode 100644 Component/Entries/Service/EntryService.cs diff --git a/Component/Entries/EntriesController.cs b/Component/Entries/EntriesController.cs new file mode 100644 index 0000000..feea567 --- /dev/null +++ b/Component/Entries/EntriesController.cs @@ -0,0 +1,19 @@ +using Microsoft.AspNetCore.Mvc; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Diary.Component.Entries +{ + [ApiController] + [Route("[controller]")] + public class EntriesController:ControllerBase + { + public EntriesController() + { + + } + } +} diff --git a/Component/Entry/Repository/Entry.cs b/Component/Entries/Repository/Entry.cs similarity index 92% rename from Component/Entry/Repository/Entry.cs rename to Component/Entries/Repository/Entry.cs index bca7047..642f836 100644 --- a/Component/Entry/Repository/Entry.cs +++ b/Component/Entries/Repository/Entry.cs @@ -6,7 +6,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Diary.Component.Entry.Repository +namespace Diary.Component.Entries.Repository { [Table("Entries", Schema = "Diary")] public class Entry diff --git a/Component/Entry/Repository/EntryConfiguration.cs b/Component/Entries/Repository/EntryConfiguration.cs similarity index 84% rename from Component/Entry/Repository/EntryConfiguration.cs rename to Component/Entries/Repository/EntryConfiguration.cs index 0512f2d..b72d7ae 100644 --- a/Component/Entry/Repository/EntryConfiguration.cs +++ b/Component/Entries/Repository/EntryConfiguration.cs @@ -1,7 +1,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; -namespace Diary.Component.Entry.Repository +namespace Diary.Component.Entries.Repository { public class EntryConfiguration : IEntityTypeConfiguration { diff --git a/Component/Entries/Repository/EntryRepository.cs b/Component/Entries/Repository/EntryRepository.cs new file mode 100644 index 0000000..28ac4f5 --- /dev/null +++ b/Component/Entries/Repository/EntryRepository.cs @@ -0,0 +1,26 @@ +using Diary.Data; + +namespace Diary.Component.Entries.Repository +{ + public interface IEntryRepository + { + Task CreateAsync(Entry entry); + } + + + public class EntryRepository : IEntryRepository + { + private readonly IDiaryDBContext _diaryDbContext; + + public EntryRepository(IDiaryDBContext diaryDbContext) + { + _diaryDbContext = diaryDbContext ?? throw new ArgumentNullException(nameof(diaryDbContext)); + } + + public async Task CreateAsync(Entry entry) + { + await _diaryDbContext.Entries.AddAsync(entry); + return entry; + } + } +} \ No newline at end of file diff --git a/Component/Entries/Service/EntryMappings.cs b/Component/Entries/Service/EntryMappings.cs new file mode 100644 index 0000000..06241f7 --- /dev/null +++ b/Component/Entries/Service/EntryMappings.cs @@ -0,0 +1,14 @@ +using AutoMapper; +using Diary.Component.Entries.Repository; + +namespace Diary.Component.Entries.Service +{ + public class EntryMappings : Profile + { + public EntryMappings() + { + CreateMap(); + CreateMap(); + } + } +} \ No newline at end of file diff --git a/Component/Entries/Service/EntryResources.cs b/Component/Entries/Service/EntryResources.cs new file mode 100644 index 0000000..6b4ad80 --- /dev/null +++ b/Component/Entries/Service/EntryResources.cs @@ -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; } + } +} \ No newline at end of file diff --git a/Component/Entries/Service/EntryService.cs b/Component/Entries/Service/EntryService.cs new file mode 100644 index 0000000..6fa1fcf --- /dev/null +++ b/Component/Entries/Service/EntryService.cs @@ -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 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 CreateAsync(CreateEntryResource createEntryResource) + { + Entry entry = _mapper.Map(createEntryResource); + + await _entryRepository.CreateAsync(entry); + + return _mapper.Map(entry); + + } + + } +} diff --git a/Data/DiaryDBContext.cs b/Data/DiaryDBContext.cs index eb9bce9..1148494 100644 --- a/Data/DiaryDBContext.cs +++ b/Data/DiaryDBContext.cs @@ -1,4 +1,4 @@ -using Diary.Component.Entry.Repository; +using Diary.Component.Entries.Repository; using Microsoft.EntityFrameworkCore; namespace Diary.Data