Skip to content

Commit

Permalink
Code Cleanup
Browse files Browse the repository at this point in the history
Adding Get with Spec to IRepository
  • Loading branch information
ardalis committed Feb 9, 2021
1 parent e6fcb9e commit f7d6091
Show file tree
Hide file tree
Showing 23 changed files with 200 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
public class CreatePatientRequest : BaseRequest
{
public string Name { get; set; }
public int ClientId { get; set; }
public string PatientName { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
{
public class ListPatientRequest : BaseRequest
{
public int ClientId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
public class UpdatePatientRequest : BaseRequest
{
public int ClientId { get; set; }
public int PatientId { get; set; }
public string Name { get; set; }
}
Expand Down
11 changes: 8 additions & 3 deletions FrontDesk/src/FrontDesk.Api/Endpoints/Patient/Create.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@ public override async Task<ActionResult<CreatePatientResponse>> HandleAsync(Crea
{
var response = new CreatePatientResponse(request.CorrelationId());

var toAdd = _mapper.Map<Patient>(request);
toAdd = await _repository.AddAsync<Patient, int>(toAdd);
var client = await _repository.GetByIdAsync<Client, int>(request.ClientId);
if (client == null) return NotFound();

var dto = _mapper.Map<PatientDto>(toAdd);
var newPatient = new Patient(client.Id, request.PatientName, "", new Core.ValueObjects.AnimalType("Dog", "Husky"));
client.Patients.Add(newPatient);

await _repository.UpdateAsync<Client, int>(client);

var dto = _mapper.Map<PatientDto>(newPatient);
response.Patient = dto;

return Ok(response);
Expand Down
9 changes: 4 additions & 5 deletions FrontDesk/src/FrontDesk.Api/Endpoints/Patient/List.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,13 @@ public override async Task<ActionResult<ListPatientResponse>> HandleAsync([FromQ
{
var response = new ListPatientResponse(request.CorrelationId());

var patientSpec = new PatientIncludeClientSpecification();
var patients = await _repository.ListAsync<Patient, int>(patientSpec);
if (patients is null) return NotFound();
var client = await _repository.GetByIdAsync<Client, int>(request.ClientId);
if (client == null) return NotFound();

response.Patients = _mapper.Map<List<PatientDto>>(patients);
response.Patients = _mapper.Map<List<PatientDto>>(client.Patients);
response.Count = response.Patients.Count;

return Ok(response);
}
}
}
}
16 changes: 12 additions & 4 deletions FrontDesk/src/FrontDesk.Api/Endpoints/Patient/Update.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Ardalis.ApiEndpoints;
using AutoMapper;
Expand Down Expand Up @@ -32,10 +33,17 @@ public override async Task<ActionResult<UpdatePatientResponse>> HandleAsync(Upda
{
var response = new UpdatePatientResponse(request.CorrelationId());

var toUpdate = _mapper.Map<Patient>(request);
await _repository.UpdateAsync<Patient, int>(toUpdate);
var client = await _repository.GetByIdAsync<Client, int>(request.ClientId);
if (client == null) return NotFound();

var dto = _mapper.Map<PatientDto>(toUpdate);
var patientToUpdate = client.Patients.FirstOrDefault(p => p.Id == request.PatientId);
if (patientToUpdate == null) return NotFound();

patientToUpdate.UpdateName(request.Name);

await _repository.UpdateAsync<Client, int>(client);

var dto = _mapper.Map<PatientDto>(patientToUpdate);
response.Patient = dto;

return Ok(response);
Expand Down
18 changes: 2 additions & 16 deletions FrontDesk/src/FrontDesk.Core/Aggregates/Patient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,14 @@

namespace FrontDesk.Core.Aggregates
{
public class Patient : BaseEntity<int>, IAggregateRoot
public class Patient : BaseEntity<int>
{
public int ClientId { get; private set; }
public string Name { get; private set; }
public string Sex { get; private set; }
public virtual AnimalType AnimalType { get; private set; }
public AnimalType AnimalType { get; private set; }
public int? PreferredDoctorId { get; private set; }

private Client _client;
public Client Client
{
get
{
return _client;
}
private set
{
_client = value;
}
}


public Patient(int clientId, string name, string sex, AnimalType animalType, int? preferredDoctorId = null)
{
ClientId = clientId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System.Threading;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using FrontDesk.Core.Aggregates;
using FrontDesk.Core.Events;
using FrontDesk.Core.Interfaces;
using FrontDesk.Core.Specifications;
using MediatR;
using PluralsightDdd.SharedKernel.Interfaces;

Expand Down Expand Up @@ -32,8 +34,11 @@ public async Task Handle(AppointmentScheduledEvent appointmentScheduledEvent, Ca

// if this is slow these can be parallelized or cached. MEASURE before optimizing.
var doctor = await _repository.GetByIdAsync<Doctor, int>(appt.DoctorId.Value);
var client = await _repository.GetByIdAsync<Client, int>(appt.ClientId);
var patient = await _repository.GetByIdAsync<Patient, int>(appt.PatientId);

var clientWithPatientsSpec = new ClientByIdIncludePatientsSpecification(appt.ClientId);
var client = (await _repository.ListAsync<Client, int>(clientWithPatientsSpec))
.FirstOrDefault();
var patient = client.Patients.First(p => p.Id == appt.PatientId);
var apptType = await _repository.GetByIdAsync<AppointmentType, int>(appt.AppointmentTypeId);

newMessage.AppointmentDateTime = appointmentScheduledEvent.AppointmentScheduled.TimeRange.Start;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Ardalis.Specification;
using FrontDesk.Core.Aggregates;

namespace FrontDesk.Core.Specifications
{
public class ClientByIdIncludePatientsSpecification : Specification<Client>
{
public ClientByIdIncludePatientsSpecification(int clientId)
{
Query
.Include(client => client.Patients)
.Where(client => client.Id == clientId)
.OrderBy(client => client.FullName);
}
}
}
16 changes: 14 additions & 2 deletions FrontDesk/tests/IntegrationTests/BaseEfRepoTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Xunit;

namespace IntegrationTests
{
[Collection("Sequential")]
public abstract class BaseEfRepoTestFixture
{
protected AppDbContext _dbContext;

protected static DbContextOptions<AppDbContext> CreateNewContextOptions()
protected static DbContextOptions<AppDbContext> CreateInMemoryContextOptions()
{
// Create a fresh service provider, and therefore a fresh
// InMemory database instance.
Expand All @@ -27,9 +29,19 @@ protected static DbContextOptions<AppDbContext> CreateNewContextOptions()
return builder.Options;
}

protected DbContextOptions<AppDbContext> CreateSqlLiteOptions()
{
var builder = new DbContextOptionsBuilder<AppDbContext>();
builder.UseSqlite("Data Source=test.db");

return builder.Options;
}


protected EfRepository GetRepository()
{
var options = CreateNewContextOptions();
//var options = CreateInMemoryContextOptions();
var options = CreateSqlLiteOptions();
var mockMediator = new Mock<IMediator>();

_dbContext = new AppDbContext(options, mockMediator.Object);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using UnitTests.Builders;
using Xunit;

namespace IntegrationTests.Client
namespace IntegrationTests.ClientTests
{
public class EfRepositoryAdd : BaseEfRepoTestFixture
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Linq;
using System.Threading.Tasks;
using FrontDesk.Infrastructure.Data;
using UnitTests.Builders;
using Xunit;
using FrontDesk.Core.Aggregates;
using FrontDesk.Core.Specifications;

namespace IntegrationTests.ClientTests
{
public class EfRepositoryAddWithPatient : BaseEfRepoTestFixture
{
private readonly EfRepository _repository;

public EfRepositoryAddWithPatient()
{
_repository = GetRepository();
}

[Fact]
public async Task AddsPatientAndSetsId()
{
var client = await AddClient();
var patient = client.Patients.First();

var clientWithPatientsSpec = new ClientByIdIncludePatientsSpecification(client.Id);
var clientFromDb = (await _repository.ListAsync<FrontDesk.Core.Aggregates.Client, int>(clientWithPatientsSpec)).First();
var newPatient = clientFromDb.Patients.First();

Assert.Equal(patient, newPatient);
Assert.True(newPatient?.Id > 0);
}

private async Task<FrontDesk.Core.Aggregates.Client> AddClient()
{
var client = new ClientBuilder().Id(2).Build();
var patient = new PatientBuilder().Id(3).Build();
client.Patients.Add(patient);

await _repository.AddAsync<FrontDesk.Core.Aggregates.Client, int>(client);

return client;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using UnitTests.Builders;
using Xunit;

namespace IntegrationTests.Client
namespace IntegrationTests.ClientTests
{
public class EfRepositoryDelete : BaseEfRepoTestFixture
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using UnitTests.Builders;
using Xunit;

namespace IntegrationTests.Client
namespace IntegrationTests.ClientTests
{
public class EfRepositoryGetById : BaseEfRepoTestFixture
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using UnitTests.Builders;
using Xunit;

namespace IntegrationTests.Client
namespace IntegrationTests.ClientTests
{
public class EfRepositoryList : BaseEfRepoTestFixture
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using UnitTests.Builders;
using Xunit;

namespace IntegrationTests.Client
namespace IntegrationTests.ClientTests
{
public class EfRepositoryUpdate : BaseEfRepoTestFixture
{
Expand Down
38 changes: 0 additions & 38 deletions FrontDesk/tests/IntegrationTests/Patient/EfRepositoryAdd.cs

This file was deleted.

32 changes: 16 additions & 16 deletions FrontDesk/tests/IntegrationTests/Patient/EfRepositoryDelete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using UnitTests.Builders;
using Xunit;

namespace IntegrationTests.Patient
namespace IntegrationTests.Client
{
public class EfRepositoryDelete : BaseEfRepoTestFixture
{
Expand All @@ -14,25 +14,25 @@ public EfRepositoryDelete()
_repository = GetRepository();
}

[Fact]
public async Task DeletesPatientAfterAddingIt()
{
var id = 8;
//[Fact]
//public async Task DeletesPatientAfterAddingIt()
//{
// var id = 8;

var patient = await AddPatient(id);
await _repository.DeleteAsync<FrontDesk.Core.Aggregates.Patient, int>(patient);
// var patient = await AddPatient(id);
// await _repository.DeleteAsync<FrontDesk.Core.Aggregates.Patient, int>(patient);

Assert.DoesNotContain(await _repository.ListAsync<FrontDesk.Core.Aggregates.Patient, int>(),
i => i.Id == id);
}
// Assert.DoesNotContain(await _repository.ListAsync<FrontDesk.Core.Aggregates.Patient, int>(),
// i => i.Id == id);
//}

private async Task<FrontDesk.Core.Aggregates.Patient> AddPatient(int id)
{
var patient = new PatientBuilder().Id(id).Build();
//private async Task<FrontDesk.Core.Aggregates.Patient> AddPatient(int id)
//{
// var patient = new PatientBuilder().Id(id).Build();

await _repository.AddAsync<FrontDesk.Core.Aggregates.Patient, int>(patient);
// await _repository.AddAsync<FrontDesk.Core.Aggregates.Patient, int>(patient);

return patient;
}
// return patient;
//}
}
}
Loading

0 comments on commit f7d6091

Please sign in to comment.