Added Entry initial repo and services for a create action.
This commit is contained in:
19
Component/Entries/EntriesController.cs
Normal file
19
Component/Entries/EntriesController.cs
Normal file
@@ -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()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
29
Component/Entries/Repository/Entry.cs
Normal file
29
Component/Entries/Repository/Entry.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Diary.Component.Entries.Repository
|
||||
{
|
||||
[Table("Entries", Schema = "Diary")]
|
||||
public class Entry
|
||||
{
|
||||
[Key]
|
||||
public int EnrtyID { get; set; }
|
||||
|
||||
[Required]
|
||||
[Column(TypeName="date")]
|
||||
public DateTime Date { get; set; }
|
||||
|
||||
[Required]
|
||||
public DateTime ValidFrom { get; set; }
|
||||
public DateTime? ValidTo { get; set; }
|
||||
|
||||
public string Note { get; set; }
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
13
Component/Entries/Repository/EntryConfiguration.cs
Normal file
13
Component/Entries/Repository/EntryConfiguration.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
namespace Diary.Component.Entries.Repository
|
||||
{
|
||||
public class EntryConfiguration : IEntityTypeConfiguration<Entry>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Entry> builder)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
26
Component/Entries/Repository/EntryRepository.cs
Normal file
26
Component/Entries/Repository/EntryRepository.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using Diary.Data;
|
||||
|
||||
namespace Diary.Component.Entries.Repository
|
||||
{
|
||||
public interface IEntryRepository
|
||||
{
|
||||
Task<Entry> 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<Entry> CreateAsync(Entry entry)
|
||||
{
|
||||
await _diaryDbContext.Entries.AddAsync(entry);
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
}
|
||||
14
Component/Entries/Service/EntryMappings.cs
Normal file
14
Component/Entries/Service/EntryMappings.cs
Normal 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>();
|
||||
}
|
||||
}
|
||||
}
|
||||
18
Component/Entries/Service/EntryResources.cs
Normal file
18
Component/Entries/Service/EntryResources.cs
Normal 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; }
|
||||
}
|
||||
}
|
||||
40
Component/Entries/Service/EntryService.cs
Normal file
40
Component/Entries/Service/EntryService.cs
Normal 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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user