Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace PetClinic.Domain.Appointments.Factories
- {
- using Appointments.Models;
- using Common.Exceptions;
- using Common.SharedKernel;
- using PetClinic.Domain.Common;
- using System;
- using System.Collections.Generic;
- using System.Linq.Expressions;
- internal class AppointmentFactory : IAppointmentFactory
- {
- private readonly IClientFactory clientFactory;
- private readonly IDoctorFactory doctorFactory;
- private AppointmentDate appointmentDate = default!;
- private Client client = default!;
- private Doctor doctor = default!;
- private OfficeRoom officeRoom = default!;
- internal AppointmentFactory(
- IClientFactory clientFactory,
- IDoctorFactory doctorFactory)
- {
- this.clientFactory = clientFactory;
- this.doctorFactory = doctorFactory;
- }
- public Appointment Build()
- {
- if (this.appointmentDate is null
- || this.client is null
- || this.doctor is null
- || this.officeRoom is null)
- {
- throw new InvalidAppointmentException("Invalid appointment input.");
- }
- return new Appointment(this.doctor, this.client, this.appointmentDate, this.officeRoom);
- }
- public IAppointmentFactory WithAppointmentDate(DateTime startDate, DateTime endDate)
- {
- this.appointmentDate = new AppointmentDate(startDate, endDate);
- return this;
- }
- public IAppointmentFactory WithClient(Expression<Action<IClientFactory>> clientExpression)
- {
- var dict = new Dictionary<string, string>();
- this.ParseExpression(clientExpression, string.Empty, dict);
- this.client = this.clientFactory
- .WithName(dict[nameof(IClientFactory.WithName)])
- .WithUserId(dict[nameof(IClientFactory.WithUserId)])
- .Build();
- return this;
- }
- public IAppointmentFactory WithDoctor(Expression<Action<IDoctorFactory>> doctorExpression)
- {
- var dict = new Dictionary<string, string>();
- this.ParseExpression(doctorExpression, string.Empty, dict);
- this.doctor = this.doctorFactory
- .WithName(dict[nameof(IDoctorFactory.WithName)])
- .WithUserId(dict[nameof(IDoctorFactory.WithUserId)])
- .WithDoctorType(
- Enumeration.FromValue<DoctorType>(
- int.Parse(dict[nameof(IDoctorFactory.WithDoctorType)])))
- .Build();
- return this;
- }
- public IAppointmentFactory WithOfficeRoom(int number, OfficeRoomType officeRoomType)
- {
- this.officeRoom = new OfficeRoom(false, number, officeRoomType);
- return this;
- }
- private void ParseExpression(Expression expression, string key, IDictionary<string, string> dict)
- {
- // expression starts here in type lambda
- if (expression.NodeType == ExpressionType.Lambda)
- {
- var lambdaExpression = (LambdaExpression)expression;
- foreach (var parameter in lambdaExpression.Parameters)
- {
- ParseExpression(parameter, string.Empty, dict);
- }
- var body = lambdaExpression.Body;
- ParseExpression(body, string.Empty, dict);
- }
- // here we get the actual parameter of the method
- else if (expression.NodeType == ExpressionType.Constant)
- {
- var constantExpression = (ConstantExpression)expression;
- var value = constantExpression.Value;
- // set the method value: for instance WithName("Gosho") so we add Gosho
- dict[key] = value.ToString()!;
- }
- // here we get the name of the method
- else if (expression.NodeType == ExpressionType.Call)
- {
- var methodCallExpression = (MethodCallExpression)expression;
- // set the method name: for instance WithName
- if (!dict.ContainsKey(methodCallExpression.Method.Name))
- {
- dict.Add(methodCallExpression.Method.Name, string.Empty);
- }
- ParseExpression(methodCallExpression.Object, methodCallExpression.Method.Name, dict);
- foreach (var argument in methodCallExpression.Arguments)
- {
- ParseExpression(argument, methodCallExpression.Method.Name, dict);
- }
- }
- }
- }
- }
- namespace PetClinic.Application.Appointments.Commands.MakeAsDoctor
- {
- using Common;
- using Common.Contracts;
- using Domain.Appointments.Factories;
- using Domain.Appointments.Models;
- using Domain.Common;
- using Domain.Common.SharedKernel;
- using MediatR;
- using System;
- using System.Threading;
- using System.Threading.Tasks;
- public class MakeAsDoctorAppointmentCommand : IRequest<Result>
- {
- public string UserIdClient { get; set; } = default!;
- public string Name { get; set; } = default!;
- public int DoctorType { get; set; }
- public int RoomNumber { get; set; }
- public int OfficeRoomType { get; set; }
- public DateTime StartDate { get; set; }
- public DateTime EndDate { get; set; }
- public class MakeAsDoctorAppointmentCommandHandler : IRequestHandler<MakeAsDoctorAppointmentCommand, Result>
- {
- private readonly IAppointmentRepository appointmentRepository;
- private readonly ICurrentUser currentUser;
- private readonly IAppointmentFactory appointmentFactory;
- public MakeAsDoctorAppointmentCommandHandler(
- IAppointmentRepository appointmentRepository,
- ICurrentUser currentUser,
- IAppointmentFactory appointmentFactory)
- {
- this.appointmentRepository = appointmentRepository;
- this.currentUser = currentUser;
- this.appointmentFactory = appointmentFactory;
- }
- public async Task<Result> Handle(MakeAsDoctorAppointmentCommand request, CancellationToken cancellationToken)
- {
- var isDateAvailable = await this.appointmentRepository.IsDateAvailable(
- this.currentUser.UserId,
- request.RoomNumber,
- request.StartDate,
- request.EndDate,
- cancellationToken);
- if (!isDateAvailable)
- {
- return "The chosen date is not available.";
- }
- var appointment = this.appointmentFactory
- .WithDoctor(doctor => doctor
- .WithDoctorType(Enumeration.FromValue<DoctorType>(request.DoctorType))
- .WithName(this.currentUser.UserName)
- .WithUserId(this.currentUser.UserId)
- .Build())
- .WithClient(client => client
- .WithName(request.Name)
- .WithUserId(request.UserIdClient)
- .Build())
- .WithOfficeRoom(request.RoomNumber, Enumeration.FromValue<OfficeRoomType>(request.OfficeRoomType))
- .WithAppointmentDate(request.StartDate, request.EndDate)
- .Build();
- await this.appointmentRepository.Save(appointment, cancellationToken);
- return Result.Success;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment