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 all 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
28 changes: 28 additions & 0 deletions OutOfSchool/OutOfSchool.DataAccess/Models/Child.cs
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")]

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 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; }
}
}
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 IReadOnlyCollection<Child> Children { get; set; }

public Parent()
{
Children = new List<Child>();
}
}
}
19 changes: 19 additions & 0 deletions OutOfSchool/OutOfSchool.DataAccess/Models/SocialGroup.cs
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>();
}

}
}
14 changes: 14 additions & 0 deletions OutOfSchool/OutOfSchool.DataAccess/OutOfSchool.DataAccess.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,25 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>OutOfSchool.Services</RootNamespace>
</PropertyGroup>
<PropertyGroup>
<CodeAnalysisRuleSet>$(SolutionDir)\StyleCopRules\YourStyleCopFile.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<None Remove="stylecop.json" />
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="stylecop.json" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.2" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
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; }
}
}
76 changes: 76 additions & 0 deletions OutOfSchool/OutOfSchool.DataAccess/Repository/EntityRepository.cs
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)
{
this.dbSet.Update(entity);
this.dbContext.SaveChanges();
}
}
}
53 changes: 53 additions & 0 deletions OutOfSchool/OutOfSchool.DataAccess/Repository/IEntityRepository.cs
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);
}
}
14 changes: 14 additions & 0 deletions OutOfSchool/OutOfSchool.DataAccess/stylecop.json
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"
}
}
}
4 changes: 4 additions & 0 deletions OutOfSchool/OutOfSchool.Tests/OutOfSchool.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

</ItemGroup>

Expand Down
Loading