-
Notifications
You must be signed in to change notification settings - Fork 6
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
Changes from 4 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
7f4e9c5
Add Child model
Elizabeth129 90afae8
Create ChildService
Elizabeth129 e9d6631
Create ChildrenController
Elizabeth129 57ffd83
Create EntityRepository
Elizabeth129 d3dcadb
Edit ChildService and EntityRepository
Elizabeth129 78d4ffe
Edit ChildMapperProfile
Elizabeth129 3103ec8
Fix bag in ChildController
Elizabeth129 acdc729
Add new method to EntityRepository
Elizabeth129 c7b1fd5
Add SocialGroup model and create SocialGroupMapperProfile
Elizabeth129 3b42ac9
Add XML documentation
Elizabeth129 1310d68
Add functionality in ChildService and ChildController
Elizabeth129 e4d3d4e
Add roles atribute for controller
Elizabeth129 558704e
Make Children property readonly
Elizabeth129 13f1ca1
Change GetChildren method
Elizabeth129 a8bd4ea
Make Children property in SocialGroup model readonly
Elizabeth129 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")] | ||
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
OutOfSchool/OutOfSchool.DataAccess/Repository/EntityRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
Elizabeth129 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
_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) | ||
Elizabeth129 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
_dbSet.Update(entity); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
OutOfSchool/OutOfSchool.DataAccess/Repository/IEntityRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
54
OutOfSchool/OutOfSchool.WebApi/Controllers/ChildrenController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
Elizabeth129 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
[HttpGet] | ||
public async Task<ActionResult<IEnumerable<Child>>> GetChildren() | ||
{ | ||
|
||
|
||
return Ok(); | ||
} | ||
|
||
[HttpPost] | ||
public async Task<ActionResult<Child>> CreateChildren(ChildDTO childDTO) | ||
Elizabeth129 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
ChildDTO child; | ||
try | ||
{ | ||
child = await childService.Create(childDTO); | ||
} | ||
catch | ||
{ | ||
return BadRequest(); | ||
} | ||
|
||
return CreatedAtAction( | ||
nameof(GetChildren), | ||
new { id = child.ChildId }, | ||
child); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
Elizabeth129 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
OutOfSchool/OutOfSchool.WebApi/Services/Implementation/ChildService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
Elizabeth129 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var child_ = await _ChildRepository.Create(newChild); | ||
return await Task.FromResult(mapper.Map<Child, ChildDTO>(child_)); | ||
} | ||
|
||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
OutOfSchool/OutOfSchool.WebApi/Services/Interfaces/IChildService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
OutOfSchool/OutOfSchool.WebApi/Services/Mapping/ChildMapperProfile.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?