Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ivanytskay/Create Entity Repository #28

Merged
merged 15 commits into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions OutOfSchool/OutOfSchool.DataAccess/Models/Child.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;

namespace OutOfSchool.Services.Models
{
public class Child
{
public long ChildId { get; set; }
[DataType(DataType.Text)]
[Required(ErrorMessage = "First name is required")]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we move strings to resources so that we can localize them later?

public string FirstName { get; set; }
[DataType(DataType.Text)]
[Required(ErrorMessage = "Last name is required")]
public string LastName { get; set; }
[DataType(DataType.Text)]
[Required(ErrorMessage = "Middle name is required")]
public string MiddleName { get; set; }
public DateTime DateOfBirth { get; set; }
public int ParentId { get; set; }
public virtual Parent Parent { get; set; }
}
}
9 changes: 8 additions & 1 deletion OutOfSchool/OutOfSchool.DataAccess/Models/Parent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace OutOfSchool.Services.Models
{
Expand All @@ -13,5 +14,11 @@ public class Parent
[DataType(DataType.Text)]
[Required(ErrorMessage = "Last name is required")]
public string LastName { get; set; }
public virtual ICollection<Child> Children { get; set; }

public Parent()
{
Children = new List<Child>();
}
}
}
1 change: 1 addition & 0 deletions OutOfSchool/OutOfSchool.DataAccess/OutOfSchoolDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

public DbSet<Parent> Parents { get; set; }
public DbSet<Organization> Organizations { get; set; }
public DbSet<Child> Children { get; set; }
}
}
48 changes: 48 additions & 0 deletions OutOfSchool/OutOfSchool.DataAccess/Repository/EntityRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace OutOfSchool.Services.Repository
{
public class EntityRepository<T> : IEntityRepository<T> where T : class, new()
{
private OutOfSchoolDbContext _dbContext;
private DbSet<T> _dbSet;

public EntityRepository(OutOfSchoolDbContext dbContext)
{
_dbContext = dbContext;
_dbSet = _dbContext.Set<T>();
}

public async Task<T> Create(T entity)
{
await _dbSet.AddAsync(entity);
await _dbContext.SaveChangesAsync();
return await Task.FromResult(entity);
}

public void Delete(T entity)
{
_dbSet.Remove(entity);
_dbContext.SaveChanges();
}

public IEnumerable<T> GetAll()
{
return _dbSet;
}

public async Task<T> GetById(long id)
{
return await _dbSet.FindAsync(id).AsTask();
}

public void Update(T entity)
{
_dbSet.Update(entity);
}
}
}
16 changes: 16 additions & 0 deletions OutOfSchool/OutOfSchool.DataAccess/Repository/IEntityRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace OutOfSchool.Services.Repository
{
public interface IEntityRepository<T> where T : class, new()
{
Task<T> Create(T entity);
void Update(T entity);
void Delete(T entity);
IEnumerable<T> GetAll();
Task<T> GetById(long id);
}
}
54 changes: 54 additions & 0 deletions OutOfSchool/OutOfSchool.WebApi/Controllers/ChildrenController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OutOfSchool.Services;
using OutOfSchool.Services.Models;
using OutOfSchool.WebApi.Models;
using OutOfSchool.WebApi.Services;
using OutOfSchool.WebApi.Services.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace OutOfSchool.WebApi.Controllers
{
[ApiController]
[Route("[controller]/[action]")]
[Authorize]
public class ChildrenController : ControllerBase
{
private IChildService childService;

public ChildrenController(IChildService childService)
{
this.childService = childService;
}

[HttpGet]
public async Task<ActionResult<IEnumerable<Child>>> GetChildren()
{


return Ok();
}

[HttpPost]
public async Task<ActionResult<Child>> CreateChildren(ChildDTO childDTO)
{
ChildDTO child;
try
{
child = await childService.Create(childDTO);
}
catch
{
return BadRequest();
}

return CreatedAtAction(
nameof(GetChildren),
new { id = child.ChildId },
child);
}
}
}
17 changes: 17 additions & 0 deletions OutOfSchool/OutOfSchool.WebApi/Models/ChildDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace OutOfSchool.WebApi.Models
{
public class ChildDTO
{
public long ChildId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
public DateTime DateOfBirth { get; set; }
public int ParentId { get; set; }
}
}
7 changes: 7 additions & 0 deletions OutOfSchool/OutOfSchool.WebApi/OutOfSchool.WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="10.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.2" />
<PackageReference Include="IdentityModel" Version="5.0.0" />
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="3.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.11">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.8.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.4" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="5.6.3" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="5.6.3" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using OutOfSchool.Services;
using OutOfSchool.Services.Models;
using OutOfSchool.Services.Repository;
using OutOfSchool.WebApi.Models;
using OutOfSchool.WebApi.Services.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace OutOfSchool.WebApi.Services
{
public class ChildService : IChildService
{
private IEntityRepository<Child> _ChildRepository { get; set; }
private readonly IMapper mapper;

public ChildService(OutOfSchoolDbContext context)
{
_ChildRepository = new EntityRepository<Child>(context);
}
public ChildService(IEntityRepository<Child> entityRepository, IMapper mapper)
{
_ChildRepository = entityRepository;
this.mapper = mapper;
}
public async Task<ChildDTO> Create(ChildDTO child)
{
if (child.DateOfBirth > DateTime.Now)
{
throw new ArgumentException("Invalid Date of birth");
}
Child newChild = mapper.Map<ChildDTO, Child>(child);
var child_ = await _ChildRepository.Create(newChild);
return await Task.FromResult(mapper.Map<Child, ChildDTO>(child_));
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using OutOfSchool.WebApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace OutOfSchool.WebApi.Services.Interfaces
{
public interface IChildService
{
Task<ChildDTO> Create(ChildDTO child);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using AutoMapper;
using OutOfSchool.Services.Models;
using OutOfSchool.WebApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace OutOfSchool.WebApi.Services.Mapping
{
public class ChildMapperProfile : Profile
{
public ChildMapperProfile()
{
CreateMap<Child, ChildDTO>().ReverseMap();
}
}
}
11 changes: 10 additions & 1 deletion OutOfSchool/OutOfSchool.WebApi/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
Expand All @@ -12,6 +13,11 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using OutOfSchool.Services;
using OutOfSchool.Services.Models;
using OutOfSchool.Services.Repository;
using OutOfSchool.WebApi.Services;
using OutOfSchool.WebApi.Services.Interfaces;
using OutOfSchool.WebApi.Services.Mapping;

namespace OutOfSchool
{
Expand All @@ -30,6 +36,7 @@ public Startup(IConfiguration configuration, IWebHostEnvironment environment)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
var childMapper = new MapperConfiguration(x => x.AddProfile(new ChildMapperProfile())).CreateMapper();
services.AddAuthentication("Bearer")
.AddIdentityServerAuthentication("Bearer", options =>
{
Expand All @@ -49,7 +56,9 @@ public void ConfigureServices(IServiceCollection services)

services.AddDbContext<OutOfSchoolDbContext>(builder =>
builder.UseSqlServer(Configuration.GetConnectionString("OutOfSchoolConnectionString")));

services.AddTransient<IChildService, ChildService>();
services.AddTransient<IEntityRepository<Child>, EntityRepository<Child>>();
services.AddSingleton(childMapper);
// Register the Swagger generator, defining 1 or more Swagger documents
services.AddSwaggerGen();
}
Expand Down