Skip to content

Commit 0146709

Browse files
committed
add department for a nice hasone and other model code
1 parent a10c528 commit 0146709

File tree

9 files changed

+145
-31
lines changed

9 files changed

+145
-31
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using JsonApiDotNetCore.Controllers;
2+
using JsonApiDotNetCore.Services;
3+
using JsonApiDotNetCoreExample.Models.Resources;
4+
using Microsoft.Extensions.Logging;
5+
6+
namespace ResourceEntitySeparationExample.Controllers
7+
{
8+
public class CoursesController : JsonApiController<CourseResource>
9+
{
10+
public CoursesController(
11+
IJsonApiContext jsonApiContext,
12+
IResourceService<CourseResource> resourceService,
13+
ILoggerFactory loggerFactory)
14+
: base(jsonApiContext, resourceService, loggerFactory)
15+
{ }
16+
}
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using JsonApiDotNetCore.Controllers;
3+
using JsonApiDotNetCore.Services;
4+
using JsonApiDotNetCoreExample.Models.Resources;
5+
using Microsoft.Extensions.Logging;
6+
7+
namespace ResourceEntitySeparationExample.Controllers
8+
{
9+
public class DepartmentsController : JsonApiController<DepartmentResource>
10+
{
11+
public DepartmentsController(
12+
IJsonApiContext jsonApiContext,
13+
IResourceService<DepartmentResource> resourceService,
14+
ILoggerFactory loggerFactory)
15+
: base(jsonApiContext, resourceService, loggerFactory)
16+
{ }
17+
}
18+
}

src/Examples/ResourceEntitySeparationExample/Controllers/StudentsController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
using JsonApiDotNetCore.Controllers;
22
using JsonApiDotNetCore.Services;
3-
using JsonApiDotNetCoreExample.Models;
3+
using JsonApiDotNetCoreExample.Models.Resources;
44
using Microsoft.Extensions.Logging;
55

66
namespace ResourceEntitySeparationExample.Controllers
77
{
8-
public class StudentsController : JsonApiController<StudentDto>
8+
public class StudentsController : JsonApiController<StudentResource>
99
{
1010
public StudentsController(
1111
IJsonApiContext jsonApiContext,
12-
IResourceService<StudentDto> resourceService,
12+
IResourceService<StudentResource> resourceService,
1313
ILoggerFactory loggerFactory)
1414
: base(jsonApiContext, resourceService, loggerFactory)
1515
{ }
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using AutoMapper;
2+
using JsonApiDotNetCoreExample.Models.Entities;
3+
using JsonApiDotNetCoreExample.Models.Resources;
4+
using System.Collections.Generic;
5+
6+
namespace ResourceEntitySeparationExample.Profiles
7+
{
8+
public class CourseProfile : Profile
9+
{
10+
public CourseProfile()
11+
{
12+
CreateMap<CourseEntity, CourseResource>()
13+
.ForMember(r => r.Students, opt => opt.MapFrom(e => StudentsFromRegistrations(e.Students)))
14+
.ForMember(r => r.Department, opt => opt.MapFrom(e => new DepartmentResource
15+
{
16+
Id = e.Department.Id,
17+
Name = e.Department.Name
18+
}))
19+
;
20+
21+
CreateMap<CourseResource, CourseEntity>()
22+
// .ForMember(e => e.Registrations, opt => opt.MapFrom(r => RegistrationsFromResource(r)))
23+
;
24+
}
25+
26+
private ICollection<StudentResource> StudentsFromRegistrations(ICollection<CourseStudentEntity> registrations)
27+
{
28+
ICollection<StudentResource> students = new HashSet<StudentResource>();
29+
foreach(CourseStudentEntity reg in registrations)
30+
{
31+
StudentEntity e = reg.Student;
32+
students.Add(new StudentResource
33+
{
34+
Id = e.Id,
35+
FirstName = e.FirstName,
36+
LastName = e.LastName,
37+
Address = e.Address
38+
});
39+
}
40+
return students.Count == 0 ? null : students;
41+
}
42+
43+
private ICollection<CourseStudentEntity> RegistrationsFromResource(CourseResource course)
44+
{
45+
ICollection<CourseStudentEntity> registrations = new HashSet<CourseStudentEntity>();
46+
foreach (StudentResource s in course.Students)
47+
{
48+
registrations.Add(new CourseStudentEntity(course.Id, s.Id));
49+
}
50+
return registrations.Count == 0 ? null : registrations;
51+
}
52+
}
53+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using AutoMapper;
2+
using JsonApiDotNetCoreExample.Models.Entities;
3+
using JsonApiDotNetCoreExample.Models.Resources;
4+
5+
namespace ResourceEntitySeparationExample.Profiles
6+
{
7+
public class DepartmentProfile : Profile
8+
{
9+
public DepartmentProfile()
10+
{
11+
CreateMap<DepartmentEntity, DepartmentResource>();
12+
}
13+
}
14+
}
Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,39 @@
1+
using System.Collections.Generic;
12
using AutoMapper;
2-
using JsonApiDotNetCoreExample.Models;
3+
using JsonApiDotNetCoreExample.Models.Entities;
4+
using JsonApiDotNetCoreExample.Models.Resources;
35

46
namespace ResourceEntitySeparationExample.Profiles
57
{
68
public class StudentProfile : Profile
79
{
810
public StudentProfile()
911
{
10-
CreateMap<StudentDto, StudentEntity>()
11-
.ForMember(e => e.FirstName, opt => opt.MapFrom(d => StringSplit(d.Name, " ", 0)))
12-
.ForMember(e => e.LastName, opt => opt.MapFrom(d => StringSplit(d.Name, " ", 1)));
13-
CreateMap<StudentEntity, StudentDto>()
14-
.ForMember(d => d.Name, opt => opt.MapFrom(e => e.FirstName + " " + e.LastName));
12+
CreateMap<StudentEntity, StudentResource>()
13+
.ForMember(d => d.Courses, opt => opt.MapFrom(e => CoursesFromRegistrations(e.Courses)))
14+
;
15+
16+
CreateMap<StudentResource, StudentEntity>()
17+
.ForMember(e => e.Courses, opt =>
18+
{
19+
});
1520
}
1621

17-
private string StringSplit(string value, string split, int pos)
22+
private ICollection<CourseResource> CoursesFromRegistrations(ICollection<CourseStudentEntity> registrations)
1823
{
19-
if (value == null)
24+
ICollection<CourseResource> courses = new HashSet<CourseResource>();
25+
foreach (CourseStudentEntity reg in registrations)
2026
{
21-
return null;
27+
CourseEntity e = reg.Course;
28+
courses.Add(new CourseResource
29+
{
30+
Id = e.Id,
31+
Number = e.Number,
32+
Title = e.Title,
33+
Description = e.Description
34+
});
2235
}
23-
24-
var pieces = value.Split(split);
25-
if (pieces.Length < pos+1)
26-
{
27-
return null;
28-
}
29-
30-
return pieces[pos];
36+
return courses.Count == 0 ? null : courses;
3137
}
3238
}
3339
}

src/Examples/ResourceEntitySeparationExample/Program.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ namespace ResourceEntitySeparationExample
55
{
66
public class Program
77
{
8-
public static void Main(string[] args)
9-
{
10-
BuildWebHost(args).Run();
11-
}
8+
public static void Main(string[] args) => BuildWebHost(args).Run();
129

1310
public static IWebHost BuildWebHost(string[] args) =>
1411
WebHost.CreateDefaultBuilder(args)

src/Examples/ResourceEntitySeparationExample/Startup.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using JsonApiDotNetCore.Services;
66
using JsonApiDotNetCoreExample.Data;
77
using JsonApiDotNetCoreExample.Models;
8+
using JsonApiDotNetCoreExample.Models.Entities;
9+
using JsonApiDotNetCoreExample.Models.Resources;
810
using Microsoft.AspNetCore.Builder;
911
using Microsoft.AspNetCore.Hosting;
1012
using Microsoft.EntityFrameworkCore;
@@ -35,24 +37,30 @@ public virtual IServiceProvider ConfigureServices(IServiceCollection services)
3537
loggerFactory.AddConsole(LogLevel.Warning);
3638
services.AddSingleton<ILoggerFactory>(loggerFactory);
3739

38-
services.AddDbContext<AppDbContext>(options =>
39-
options.UseNpgsql(GetDbConnectionString()), ServiceLifetime.Transient);
40+
services.AddDbContext<AppDbContext>(options => options
41+
.UseNpgsql(GetDbConnectionString())
42+
/*.UseLazyLoadingProxies()*/,
43+
ServiceLifetime.Transient);
4044
services.AddScoped<IDbContextResolver, DbContextResolver<AppDbContext>>();
4145

4246
var mvcBuilder = services.AddMvcCore();
4347

4448
services.AddJsonApi(options => {
4549
options.Namespace = "api/v1";
46-
options.DefaultPageSize = 5;
50+
options.DefaultPageSize = 10;
4751
options.IncludeTotalRecordCount = true;
4852
options.BuildContextGraph((builder) => {
49-
builder.AddResource<StudentDto>("students");
53+
builder.AddResource<CourseResource>("courses");
54+
builder.AddResource<DepartmentResource>("departments");
55+
builder.AddResource<StudentResource>("students");
5056
});
5157
}, mvcBuilder);
5258

59+
// inject automapper and mapping resources
5360
services.AddAutoMapper();
54-
55-
services.AddScoped<IResourceService<StudentDto>, MappingResourceService<StudentDto, StudentEntity>>();
61+
services.AddScoped<IResourceService<CourseResource>, MappingResourceService<CourseResource, CourseEntity>>();
62+
services.AddScoped<IResourceService<DepartmentResource>, MappingResourceService<DepartmentResource, DepartmentEntity>>();
63+
services.AddScoped<IResourceService<StudentResource>, MappingResourceService<StudentResource, StudentEntity>>();
5664

5765
var provider = services.BuildServiceProvider();
5866
var appContext = provider.GetRequiredService<AppDbContext>();

src/Examples/ResourceEntitySeparationExample/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"Logging": {
66
"IncludeScopes": false,
77
"LogLevel": {
8-
"Default": "Warning"
8+
"Default": "Warning",
9+
"Microsoft.EntityFrameworkCore.Database.Command": "Information"
910
}
1011
}
1112
}

0 commit comments

Comments
 (0)