From da58bd9eacaa0c0c54879ecce59a623c45943f64 Mon Sep 17 00:00:00 2001 From: Rui Santos Date: Sun, 19 Feb 2023 22:07:23 +0000 Subject: [PATCH] Clean Code and minor fixes --- .../Contracts/AppointmentContract.cs | 6 ++--- .../Contracts/PatientAppointmentsContract.cs | 8 +++---- .../Controllers/SpecialtiesController.cs | 2 +- ...lyOptionalRouteParameterOperationFilter.cs | 2 +- .../Core/TaskExtensions.cs | 2 +- .../Factories/AppointmentsFactory.cs | 2 +- .../Factories/DoctorFactory.cs | 4 ++-- .../MedicalSpecialtiesManagementTests.cs | 2 +- RuiSantos.ZocDoc.Core/Data/IDataContext.cs | 2 +- .../Managers/AppointmentManagement.cs | 4 ++++ RuiSantos.ZocDoc.Core/Models/Appointment.cs | 4 ++-- .../Models/MedicalSpeciality.cs | 20 ++++++++--------- RuiSantos.ZocDoc.Core/Models/OfficeHour.cs | 4 ++-- RuiSantos.ZocDoc.Core/Models/Person.cs | 22 +++++++++---------- RuiSantos.ZocDoc.Data.Mongodb/MongoContext.cs | 2 +- 15 files changed, 45 insertions(+), 41 deletions(-) diff --git a/RuiSantos.ZocDoc.Api/Contracts/AppointmentContract.cs b/RuiSantos.ZocDoc.Api/Contracts/AppointmentContract.cs index 6f5604d..83f7b10 100644 --- a/RuiSantos.ZocDoc.Api/Contracts/AppointmentContract.cs +++ b/RuiSantos.ZocDoc.Api/Contracts/AppointmentContract.cs @@ -22,8 +22,8 @@ public class AppointmentContract public AppointmentContract() { - this.MedicalLicense = string.Empty; - this.PatientSecuritySocialNumber = string.Empty; - this.Date = DateTime.MinValue; + MedicalLicense = string.Empty; + PatientSecuritySocialNumber = string.Empty; + Date = DateTime.MinValue; } } diff --git a/RuiSantos.ZocDoc.Api/Contracts/PatientAppointmentsContract.cs b/RuiSantos.ZocDoc.Api/Contracts/PatientAppointmentsContract.cs index 7f8e3e1..223ac98 100644 --- a/RuiSantos.ZocDoc.Api/Contracts/PatientAppointmentsContract.cs +++ b/RuiSantos.ZocDoc.Api/Contracts/PatientAppointmentsContract.cs @@ -19,13 +19,13 @@ public class PatientAppointmentsContract public PatientAppointmentsContract() { - this.Doctor = new DoctorContract(); - this.Date = DateTime.Now; + Doctor = new DoctorContract(); + Date = DateTime.Now; } public PatientAppointmentsContract(Doctor doctor, DateTime date) { - this.Doctor = new DoctorContract(doctor); - this.Date = date; + Doctor = new DoctorContract(doctor); + Date = date; } } diff --git a/RuiSantos.ZocDoc.Api/Controllers/SpecialtiesController.cs b/RuiSantos.ZocDoc.Api/Controllers/SpecialtiesController.cs index 28b164c..00375eb 100644 --- a/RuiSantos.ZocDoc.Api/Controllers/SpecialtiesController.cs +++ b/RuiSantos.ZocDoc.Api/Controllers/SpecialtiesController.cs @@ -23,7 +23,7 @@ public SpecialtiesController(MedicalSpecialtiesManagement management) /// No medical specialties found. [HttpGet] [ProducesResponseType(StatusCodes.Status200OK)] - [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task>> GetAsync() { var result = await management.GetMedicalSpecialitiesAsync(); diff --git a/RuiSantos.ZocDoc.Api/Core/ReApplyOptionalRouteParameterOperationFilter.cs b/RuiSantos.ZocDoc.Api/Core/ReApplyOptionalRouteParameterOperationFilter.cs index ea9119d..b05520e 100644 --- a/RuiSantos.ZocDoc.Api/Core/ReApplyOptionalRouteParameterOperationFilter.cs +++ b/RuiSantos.ZocDoc.Api/Core/ReApplyOptionalRouteParameterOperationFilter.cs @@ -6,7 +6,7 @@ namespace RuiSantos.ZocDoc.Api.Core; internal class ReApplyOptionalRouteParameterOperationFilter : IOperationFilter { - const string captureName = "routeParameter"; + private const string captureName = "routeParameter"; public void Apply(OpenApiOperation operation, OperationFilterContext context) { diff --git a/RuiSantos.ZocDoc.Core.Tests/Core/TaskExtensions.cs b/RuiSantos.ZocDoc.Core.Tests/Core/TaskExtensions.cs index 25254ca..78976ce 100644 --- a/RuiSantos.ZocDoc.Core.Tests/Core/TaskExtensions.cs +++ b/RuiSantos.ZocDoc.Core.Tests/Core/TaskExtensions.cs @@ -5,7 +5,7 @@ internal static class TaskExtensions public static async Task> ToListAsync(this IAsyncEnumerable enumerable) { var result = new List(); - await foreach (var item in enumerable) result.Add(item); + await foreach (var item in enumerable) result.Add(item); return result; } } diff --git a/RuiSantos.ZocDoc.Core.Tests/Factories/AppointmentsFactory.cs b/RuiSantos.ZocDoc.Core.Tests/Factories/AppointmentsFactory.cs index 5ded5e2..3e51e2f 100644 --- a/RuiSantos.ZocDoc.Core.Tests/Factories/AppointmentsFactory.cs +++ b/RuiSantos.ZocDoc.Core.Tests/Factories/AppointmentsFactory.cs @@ -4,7 +4,7 @@ namespace RuiSantos.ZocDoc.Core.Tests.Factories; internal static class AppointmentsFactory { - static readonly DateTime StartTime = new(2022, 1, 3, 8, 0, 0); + private static readonly DateTime StartTime = new(2022, 1, 3, 8, 0, 0); public static IEnumerable Create(int elements = 1) => Enumerable.Range(0, elements).Select(i => new Appointment(StartTime.AddHours(i))); } diff --git a/RuiSantos.ZocDoc.Core.Tests/Factories/DoctorFactory.cs b/RuiSantos.ZocDoc.Core.Tests/Factories/DoctorFactory.cs index 6067003..59a1e13 100644 --- a/RuiSantos.ZocDoc.Core.Tests/Factories/DoctorFactory.cs +++ b/RuiSantos.ZocDoc.Core.Tests/Factories/DoctorFactory.cs @@ -27,10 +27,10 @@ public static Doctor SetSpecialties(this Doctor doctor, params string[] specialt public static Doctor SetOfficeHours(this Doctor doctor, params OfficeHour[] officeHours) { doctor.OfficeHours.Clear(); - + if (officeHours.Any()) doctor.OfficeHours.AddRange(officeHours); - + return doctor; } diff --git a/RuiSantos.ZocDoc.Core.Tests/MedicalSpecialtiesManagementTests.cs b/RuiSantos.ZocDoc.Core.Tests/MedicalSpecialtiesManagementTests.cs index 8bd12a5..1bf0d54 100644 --- a/RuiSantos.ZocDoc.Core.Tests/MedicalSpecialtiesManagementTests.cs +++ b/RuiSantos.ZocDoc.Core.Tests/MedicalSpecialtiesManagementTests.cs @@ -14,7 +14,7 @@ namespace RuiSantos.ZocDoc.Core.Tests; public class MedicalSpecialtiesManagementTests { private readonly Mock mockDataContext = new(); - private readonly Mock> mockLogger = new( ); + private readonly Mock> mockLogger = new(); [Fact] public async Task CreateMedicalSpecialtiesAsync_WithValidInput_ShouldStoreMedicalSpecialties() diff --git a/RuiSantos.ZocDoc.Core/Data/IDataContext.cs b/RuiSantos.ZocDoc.Core/Data/IDataContext.cs index 9d383d8..63ede98 100644 --- a/RuiSantos.ZocDoc.Core/Data/IDataContext.cs +++ b/RuiSantos.ZocDoc.Core/Data/IDataContext.cs @@ -3,7 +3,7 @@ namespace RuiSantos.ZocDoc.Core.Data; public interface IDataContext - { +{ Task ExistsAsync(Expression> expression); Task FindAsync(Guid id); Task FindAsync(Expression> expression); diff --git a/RuiSantos.ZocDoc.Core/Managers/AppointmentManagement.cs b/RuiSantos.ZocDoc.Core/Managers/AppointmentManagement.cs index 5528d8c..882d840 100644 --- a/RuiSantos.ZocDoc.Core/Managers/AppointmentManagement.cs +++ b/RuiSantos.ZocDoc.Core/Managers/AppointmentManagement.cs @@ -83,6 +83,10 @@ public async Task DeleteAppointmentAsync(string socialNumber, string medicalLice if (doctor.Appointments.RemoveAll(app => app.Id == appointment.Id) > 0) await context.StoreAsync(doctor); } + catch (ValidationFailException) + { + throw; + } catch (Exception ex) { logger?.LogException(nameof(AppointmentManagement), nameof(DeleteAppointmentAsync), ex); diff --git a/RuiSantos.ZocDoc.Core/Models/Appointment.cs b/RuiSantos.ZocDoc.Core/Models/Appointment.cs index baf59fe..3aa69aa 100644 --- a/RuiSantos.ZocDoc.Core/Models/Appointment.cs +++ b/RuiSantos.ZocDoc.Core/Models/Appointment.cs @@ -4,8 +4,8 @@ public class Appointment { public Guid Id { get; } public DayOfWeek Week { get; set; } - public DateOnly Date { get; set; } - public TimeSpan Time { get; set; } + public DateOnly Date { get; set; } + public TimeSpan Time { get; set; } public Appointment() { diff --git a/RuiSantos.ZocDoc.Core/Models/MedicalSpeciality.cs b/RuiSantos.ZocDoc.Core/Models/MedicalSpeciality.cs index 151e13a..5faf1ab 100644 --- a/RuiSantos.ZocDoc.Core/Models/MedicalSpeciality.cs +++ b/RuiSantos.ZocDoc.Core/Models/MedicalSpeciality.cs @@ -5,16 +5,16 @@ public class MedicalSpeciality public Guid Id { get; } public string Description { get; set; } - public MedicalSpeciality() - { - this.Id = Guid.NewGuid(); - this.Description = String.Empty; - } + public MedicalSpeciality() + { + Id = Guid.NewGuid(); + Description = String.Empty; + } - public MedicalSpeciality(string description) - { - this.Id = Guid.NewGuid(); - this.Description = description; - } + public MedicalSpeciality(string description) + { + Id = Guid.NewGuid(); + Description = description; + } } diff --git a/RuiSantos.ZocDoc.Core/Models/OfficeHour.cs b/RuiSantos.ZocDoc.Core/Models/OfficeHour.cs index f6d67f5..a889b83 100644 --- a/RuiSantos.ZocDoc.Core/Models/OfficeHour.cs +++ b/RuiSantos.ZocDoc.Core/Models/OfficeHour.cs @@ -7,8 +7,8 @@ public class OfficeHour public OfficeHour() { - this.Week = DayOfWeek.Monday; - this.Hours = Enumerable.Empty(); + Week = DayOfWeek.Monday; + Hours = Enumerable.Empty(); } public OfficeHour(DayOfWeek week, IEnumerable hours) diff --git a/RuiSantos.ZocDoc.Core/Models/Person.cs b/RuiSantos.ZocDoc.Core/Models/Person.cs index d2e9554..f91a25f 100644 --- a/RuiSantos.ZocDoc.Core/Models/Person.cs +++ b/RuiSantos.ZocDoc.Core/Models/Person.cs @@ -2,18 +2,18 @@ public class Person { - public string Email { get; set; } - public string FirstName { get; set; } - public string LastName { get; set; } - public IEnumerable ContactNumbers { get; set; } + public string Email { get; set; } + public string FirstName { get; set; } + public string LastName { get; set; } + public IEnumerable ContactNumbers { get; set; } - public Person() - { - this.FirstName = string.Empty; - this.LastName = string.Empty; - this.Email = string.Empty; - this.ContactNumbers = Enumerable.Empty(); - } + public Person() + { + FirstName = string.Empty; + LastName = string.Empty; + Email = string.Empty; + ContactNumbers = Enumerable.Empty(); + } public Person(string email, string firstName, string lastName, IEnumerable contactNumbers) { diff --git a/RuiSantos.ZocDoc.Data.Mongodb/MongoContext.cs b/RuiSantos.ZocDoc.Data.Mongodb/MongoContext.cs index d8b2298..cfc2d12 100644 --- a/RuiSantos.ZocDoc.Data.Mongodb/MongoContext.cs +++ b/RuiSantos.ZocDoc.Data.Mongodb/MongoContext.cs @@ -14,7 +14,7 @@ public MongoContext(string connectionString) { var mongoUrl = MongoUrl.Create(connectionString); var client = new MongoClient(mongoUrl); - this.database = client.GetDatabase(mongoUrl.DatabaseName); + database = client.GetDatabase(mongoUrl.DatabaseName); Mediator.RegisterClassMaps(); }