0

i have the following entity

using MedRaise.Domain.Enums;

namespace MedRaise.Domain.Entities;

public class Appointment : Entity
{
    private Guid _clinicId;
    private DateTime _date;
    private Guid _doctorId;
    private Guid _patientId;

    public Appointment()
    {
    }

    public Appointment(Guid id, DateTime date, Guid patientId, Guid doctorId, Guid clinicId,
        AppointmentStatus appointmentStatus)
    {
        _date = date;
        _patientId = patientId;
        _doctorId = doctorId;
        _clinicId = clinicId;
        AppointmentStatus = appointmentStatus;
    }

    public DateTime Date
    {
        get => _date;
        set
        {
            _date = value;
        }
    }

    public Guid PatientId
    {
        get => _patientId;
        set
        {
            if (value == Guid.Empty) throw new ArgumentException($"The {nameof(PatientId)} is required");

            _patientId = value;
        }
    }

    public Patient? Patient { get; set; }

    public Guid DoctorId
    {
        get => _doctorId;
        set
        {
            if (value == Guid.Empty) throw new ArgumentException($"The {nameof(DoctorId)} is required");

            _doctorId = value;
        }
    }

    public Doctor? Doctor { get; set; }

    public Guid ClinicId
    {
        get => _clinicId;
        set
        {
            if (value == Guid.Empty) throw new ArgumentException($"The {nameof(ClinicId)} is required");

            _clinicId = value;
        }
    }

    public Clinic? Clinic { get; set; }
    public AppointmentStatus AppointmentStatus { get; set; }
}

And 3 value objects

namespace MedRaise.Domain.ValueObjects;
public class Address : ValueObject
{
    public string Country { get; set; }
    public string City { get; set; }
    public string Street { get; set; }
    public string Apartments { get; set; }
    public string PostalCode { get; set; }

    public Address() {}
    public Address(string country, string city, string street, string apartments, string postalCode)
    {
        if (string.IsNullOrWhiteSpace(country))
        {
            throw new ArgumentException("Country cannot be empty", nameof(country));
        } 
        if (string.IsNullOrWhiteSpace(city))
        {
            throw new ArgumentException("City cannot be empty", nameof(city));
        } 
        if (string.IsNullOrWhiteSpace(street))
        {
            throw new ArgumentException("Street cannot be empty", nameof(street));
        } 
        if (string.IsNullOrWhiteSpace(apartments))
        {
            throw new ArgumentException("Apartments cannot be empty", nameof(apartments));
        }
        if (string.IsNullOrWhiteSpace(postalCode))
        {
            throw new ArgumentException("Postal code cannot be empty", nameof(postalCode));
        }

        Country = country;
        City = city;
        Street = street;
        Apartments = apartments;
        PostalCode = postalCode;
    }

    protected override IEnumerable<object> GetEqualityComponents()
    {
        yield return Country;
        yield return City;
        yield return Street;
        yield return Apartments;
        yield return PostalCode;
    }
}

namespace MedRaise.Domain.ValueObjects;

public class FullName : ValueObject
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Patronymic { get; set; }

    public FullName() {}
    public FullName(string firstname, string lastname, string patronymic)
    {
        FirstName = firstname;
        LastName = lastname;
        Patronymic = patronymic;
    }
    protected override IEnumerable<object> GetEqualityComponents()
    {
        yield return FirstName;
        yield return LastName;
        yield return Patronymic;
    }
}

using MedRaise.Domain.Enums;
namespace MedRaise.Domain.ValueObjects;

public class ContactInformation : ValueObject
{
    public string? Email { get; set; }
    public string? Phone { get; set; }

    public ContactInformation() {}
    public ContactInformation(string? email, string? phone)
    {
        Email = email;
        Phone = phone;
    }
    protected override IEnumerable<object> GetEqualityComponents()
    {
        yield return Email;
        yield return Phone;
    }
}

I need a select appointment with patients....I have following method but it don't work, i'll get appointment with null fields in patient property and i can't understand, why....Chat GPT didn't help me

    public async Task<Appointment?> GetByIdWithPatientDoctorClinic(Guid id)
    {
        await using var connection = new NpgsqlConnection(connectionString);
        await connection.OpenAsync();
        const string query = """
                                 SELECT 
                                 a.*, 
                                 p.id AS PatientId, p.dob as Dob, 
                             p.address_country AS Country,
                             p.address_city AS City, 
                             p.address_street AS Street,
                             p.address_apartments AS Apartments, 
                             p.sex
                             FROM 
                                 appointments a
                             INNER JOIN 
                                 patients p ON a.patientid = p.id
                             WHERE 
                                 a.id = @Id
                             """;

        var appointments =
            await connection.QueryAsync<Appointment, Patient, Appointment>(
                query,
                (appointment, patient) =>
                {
                    appointment.Patient = patient;
                    return appointment;
                },
                new { Id = id },
                splitOn: "PatientId");

        return appointments.FirstOrDefault();
    }

I tried to do it myself but it didn't work....Can someone help me solve this problem, i'm working with dapper second time

0

Browse other questions tagged or ask your own question.