-
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 all 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,28 @@ | ||
using OutOfSchool.Services.Enums; | ||
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 Gender Gender { get; set; } | ||
public int ParentId { get; set; } | ||
public int SocialGroupId { get; set; } | ||
public virtual Parent Parent { get; set; } | ||
public virtual SocialGroup SocialGroup { 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace OutOfSchool.Services.Models | ||
{ | ||
public class SocialGroup | ||
{ | ||
public long SocialGroupId { get; set; } | ||
public string Name { get; set; } | ||
public virtual IReadOnlyCollection<Child> Children { get; set; } | ||
|
||
public SocialGroup() | ||
{ | ||
Children = new List<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
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
76 changes: 76 additions & 0 deletions
76
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,76 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace OutOfSchool.Services.Repository | ||
{ | ||
/// <summary> | ||
/// Repository for accessing the database. | ||
/// </summary> | ||
/// <typeparam name="T">Entity.</typeparam> | ||
public class EntityRepository<T> : IEntityRepository<T> | ||
where T : class, new() | ||
{ | ||
private OutOfSchoolDbContext dbContext; | ||
private DbSet<T> dbSet; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="EntityRepository{T}"/> class. | ||
/// </summary> | ||
/// <param name="dbContext">OutOfSchoolDbContext</param> | ||
public EntityRepository(OutOfSchoolDbContext dbContext) | ||
{ | ||
this.dbContext = dbContext; | ||
this.dbSet = this.dbContext.Set<T>(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async Task<T> Create(T entity) | ||
{ | ||
await this.dbSet.AddAsync(entity); | ||
await this.dbContext.SaveChangesAsync(); | ||
return await Task.FromResult(entity); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async Task Delete(T entity) | ||
{ | ||
this.dbSet.Remove(entity); | ||
await this.dbContext.SaveChangesAsync(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public IEnumerable<T> GetAll() | ||
{ | ||
return this.dbSet; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public IEnumerable<T> GetAllWithDetails(string includeProperties = "") | ||
{ | ||
IQueryable<T> query = this.dbSet; | ||
foreach (var includeProperty in includeProperties.Split( | ||
new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) | ||
{ | ||
query = query.Include(includeProperty); | ||
} | ||
|
||
return query; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async Task<T> GetById(long id) | ||
{ | ||
return await this.dbSet.FindAsync(id).AsTask(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public void Update(T entity) | ||
Elizabeth129 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
this.dbSet.Update(entity); | ||
this.dbContext.SaveChanges(); | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
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,53 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
|
||
namespace OutOfSchool.Services.Repository | ||
{ | ||
/// <summary> | ||
/// Interface of repository for accessing the database. | ||
/// </summary> | ||
/// <typeparam name="T">Entity.</typeparam> | ||
public interface IEntityRepository<T> | ||
where T : class, new() | ||
{ | ||
/// <summary> | ||
/// Add new element. | ||
/// </summary> | ||
/// <param name="entity">Entity.</param> | ||
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns> | ||
Task<T> Create(T entity); | ||
|
||
/// <summary> | ||
/// Update information about element. | ||
/// </summary> | ||
/// <param name="entity">Entity.</param> | ||
void Update(T entity); | ||
|
||
/// <summary> | ||
/// Delete element. | ||
/// </summary> | ||
/// <param name="entity">Entity.</param> | ||
/// <returns>A <see cref="Task"/> representing the result of the asynchronous operation.</returns> | ||
Task Delete(T entity); | ||
|
||
/// <summary> | ||
/// Get all elements. | ||
/// </summary> | ||
/// <returns>List of all elements.</returns> | ||
IEnumerable<T> GetAll(); | ||
|
||
/// <summary> | ||
/// Get all elements with details. | ||
/// </summary> | ||
/// <param name="includeProperties">Name of properties which should be included.</param> | ||
/// <returns>List of all elements with included propertires.</returns> | ||
IEnumerable<T> GetAllWithDetails(string includeProperties = ""); | ||
|
||
/// <summary> | ||
/// Get element by Id. | ||
/// </summary> | ||
/// <param name="id">Key in database.</param> | ||
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns> | ||
Task<T> GetById(long id); | ||
} | ||
} |
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,14 @@ | ||
{ | ||
// ACTION REQUIRED: This file was automatically added to your project, but it | ||
// will not take effect until additional steps are taken to enable it. See the | ||
// following page for additional information: | ||
// | ||
// https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/EnableConfiguration.md | ||
|
||
"$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json", | ||
"settings": { | ||
"documentationRules": { | ||
"companyName": "PlaceholderCompany" | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
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?