In my doctor table I have 3 columns named start time, end time and duration.
My plan is when a user types in city name and selects the specialization of doctor and clicks search button a table of doctors meeting that criteria will be shown. Including start time, end time and a time slot.
If a doctor has start time of 8 am and end time of 8 pm and a duration of 30 minutes on average for per patient then 24 slots will be created.
I have most things working except I can't figure out how to combine everything with a dropdown of time slots (which will of course not show the already booked times on a particular day).
Now I already have the details of the doctor(including start and end time) in a var details so I should be able to extract start time and end time from there right?
And send those two pieces of data to slots.cs class for creating slots?
This is my action method:
public ActionResult Dashboard(string city, string specialization)
{
// ?City=Dhaka&specialization=Medicine&date=2020-11-29&btn_find=Find ---> example query string
string city_local = Request.Params["city"];
string specialization_local = Request.Params["specialization"];
var details = db.doctors
.Where(x => x.city.Equals(city_local) &&
x.specialization.Equals(specialization_local)).ToList(); -->this guy right here
return View(details);
}
This is my view:
<table class="table table table-borderless table-hover text-center">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Start Time</th>
<th>End Time</th>
<th>Available Slot</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@if(Model.Count() == 0)
{
<tr>
<td>No Records Found. Try Again</td>
</tr>
}
else
{
foreach(var data in Model)
{
<tr>
<td>@data.doctor_id</td>
<td>@data.doctor_fname</td>
<td>@data.start_time</td>
<td>@data.end_time</td>
<td>A dropdown of slots needed</td>
<td><a href="#">Book</a></td>
</tr>
}
}
</tbody>
</table>
And finally comes my slots.cs which should work for creating time slots:
public class slot
{
public TimeSpan StartTime { get; set; }
public TimeSpan Duration { get; set; }
public string DisplayString
{
get
{
return StartTime.ToString(@"hh\:mm") + " - " + (StartTime + Duration).ToString(@"hh\:mm");
}
}
public slot(TimeSpan StartTime, TimeSpan Duration)
{
this.StartTime = StartTime;
this.Duration = Duration;
}
public List<slot> TimeSlots(TimeSpan start, TimeSpan end, TimeSpan duration)
{
List<slot> slots = new List<slot>();
while (start < end)
{
slots.Add(new slot(start, duration));
start = start + duration;
}
return slots;
}
}
Can anyone help figure this out?
"Now I already have the details of the doctor(including start and end time) in a var details so I should be able to extract start time and end time from there right? And send those two data to slots.cs class for creating slots??"
--Short answer, yes.
I am assuming those members are part of the objects in the list.
Digging in a bit more: I am probably showing the obvious here...
I added some more details around the code. The LINQ query you have works right.
*Sorry for the ugly List initialization (scrolling right is bad, but compact code is good).
void Main()
{
var doctors = new List<Doctor>{
new Doctor{Name = "Doc1", City = "Memphis", Specialization ="surgery", StartTime = TimeSpan.Parse("06:00:00"), EndTime = TimeSpan.Parse("15:00:00")},
new Doctor{Name = "Doc2", City = "Memphis", Specialization ="surgery", StartTime = TimeSpan.Parse("08:00"), EndTime = TimeSpan.Parse("17:00")},
new Doctor{Name = "Doc3", City = "Nashville", Specialization ="surgery", StartTime = TimeSpan.Parse("10:00"), EndTime = TimeSpan.Parse("14:00")},
new Doctor{Name = "Doc4", City = "Houston", Specialization ="surgery", StartTime = TimeSpan.Parse("09:00"), EndTime = TimeSpan.Parse("15:00")},
new Doctor{Name = "Doc5", City = "Memphis", Specialization ="Quack", StartTime = TimeSpan.Parse("08:30"), EndTime = TimeSpan.Parse("15:00")},
};
// ?City=Dhaka&specialization=Medicine&date=2020-11-29&btn_find=Find ---> example query string
string city_local = "Memphis";
string specialization_local = "surgery";
List<Doctor> details = doctors.Where(x => x.City.Equals(city_local) &&
x.Specialization.Equals(specialization_local)).ToList();
;
//for breakpoint :)
//Do something with the list of Doctor (use start times and end times as needed).
}
public class Doctor
{
public string Name {get; set;}
public string City {get; set;}
public string Specialization {get; set;}
public TimeSpan StartTime {get; set;}
public TimeSpan EndTime {get; set;}
}