.net 在WebApi c#中为约会实现日历类(System.Globalization)?

9fkzdhlc  于 2023-03-09  发布在  .NET
关注(0)|答案(1)|浏览(137)

我有实体CustomerDentist,我想用C#并使用Web API创建一个服务,客户可以通过该服务进行预约。
更准确地说,创建一个PUT,通过它可以显示日历,选择他想要的日期(取决于Dentist的可用性),从而创建一个PUT
我想我需要从System.Globalization实现Calendar类,但我不知道这是否是最好的选择。你能帮我一些提示吗?
我创建了一个类Appointment,如下所示:

public class Appointment 
{ 
    public Guid Id { get; set; }

    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime Day { get; set; }

    [DataType(DataType.Time)]
    [DisplayFormat(DataFormatString ="{0:hh:mm}")]
    public DateTime StartTime { get; set; }

    [DataType(DataType.Time)]
    [DisplayFormat(DataFormatString = "{0:hh:mm}")]
    public DateTime FinishTime { get; set; }

    public TimeSpan Duration => FinishTime - StartTime;

    public Guid CustomerId { get; set; }
    public Customer Customer { get; set; }
}
erhoui1w

erhoui1w1#

Calendar类主要用于处理不同日历系统中的日期和时间,可能不直接适用于约会计划方案。
相反,您可以将DateTimeOffsetTimeSpan类与Appointment类结合使用来表示约会的开始和结束时间。要向客户显示可用的时间段,您可以使用一个简单的算法来检查牙医的现有约会,并根据每个约会的持续时间计算可用的时间段。
用于选择合适的时隙用于接收的服务的一个可能实现可以看起来像这样:

public class AppointmentService
{
    // your database context
    private readonly ApplicationDbContext _dbContext;
    
    public AppointmentService(ApplicationDbContext dbContext)
    {
        _dbContext = dbContext;
    }

    public IEnumerable<DateTimeOffset> GetAvailableTimeSlots(DateTime date, int durationInMinutes)
    {
        // Get all appointments for the specified date
        var existingAppointments = _dbContext
            .Appointments
            .Where(a => a.Day == date)
            .ToList();
        
        // Calculate the available time slots based on the duration of each appointment
        var startOfDay = new DateTimeOffset(date.Year, date.Month, date.Day, 9, 0, 0);
        var endOfDay = new DateTimeOffset(date.Year, date.Month, date.Day, 17, 0, 0);
        var currentSlot = startOfDay;
        var availableSlots = new List<DateTimeOffset>();
        
        while (currentSlot + TimeSpan.FromMinutes(durationInMinutes) <= endOfDay)
        {
            // Check if the current time slot is available
            var isAvailable = true;
            foreach (var appointment in existingAppointments)
            {
                if (currentSlot >= appointment.StartTime && currentSlot < appointment.FinishTime)
                {
                    isAvailable = false;
                    break;
                }
            }
            
            // If the time slot is available, add it to the list
            if (isAvailable)
            {
                availableSlots.Add(currentSlot);
            }
            
            // Move to the next time slot
            currentSlot += TimeSpan.FromMinutes(durationInMinutes);
        }
        
        return availableSlots;
    }
    
    public void ScheduleAppointment(Appointment appointment)
    {
        // Add the appointment to the database
        _dbContext.Appointments.Add(appointment);
        _dbContext.SaveChanges();
    }
}

相关问题