很高兴知道:这是一个有点奇怪的解决方案,但作为我在 OpenClassrooms 学习的一部分,这是我必须做的练习。 在 Asp Net Core 解决方案中,我有 4 个项目:
在预约表格中,我输入了顾问的姓名,但我必须使用顾问指南来检查空位的可用性。这是我遇到问题的地方。从 BookingAppointmentApi 我将在 ConsultantCalendarApi 中查找 Guid,如下所示:
var consultantIdString = $"https://localhost:8884/consultants/checkAvailability/GetGuidConsultant" +
$"?consultantName={model.ConsultantName}";
var _httpClient = _httpClientFactory.CreateClient();
var consultantIdResponse = await _httpClient.GetAsync(consultantIdString);
此时,model.ConsultantName = "Bob"。 但在这儿: 顾问姓名=“GetGuidConsultant”
[HttpGet("checkAvailability/{consultantName}")]
public async Task<ActionResult> GetGuidConsultant(string consultantName)
我给你我认为对我很重要的文件。
HspFrontEnd 中的 BookingController:
[HttpPost]
public async Task<IActionResult> Create(AppointmentDto appointment)
{
var content = new StringContent(JsonSerializer.Serialize(appointment), Encoding.UTF8, "application/json");
var response = await _httpClient.PostAsync("/gateway/appointments", content);
if (response.IsSuccessStatusCode)
{
_logger.LogInformation($"{response.StatusCode} : Appointment added successfully");
return RedirectToAction("Index");
}
else
{
_logger.LogError($"{response.StatusCode} : Something went wrong");
return View();
}
}
HspFrontEnd中的AppointmentDto:
public class AppointmentDto
{
public Guid Id { get; set; }
public string PatientName { get; set; }
public string ConsultantName { get; set; }
public DateTime AppointmentDate { get; set; }
}
BookingAppointmentApi 中的 AppointmentsController:
[HttpPost]
public async Task<ActionResult<AppointmentModel>> CreateOneAppointment(AppointmentModel model)
{
if (model == null)
{
_logger.LogError("Invalid appointment data");
return NotFound();
}
var consultantIdString = $"https://localhost:8884/consultants/checkAvailability/GetGuidConsultant" +
$"?consultantName={model.ConsultantName}";
var _httpClient = _httpClientFactory.CreateClient();
var consultantIdResponse = await _httpClient.GetAsync(consultantIdString);
if (consultantIdResponse.IsSuccessStatusCode)
{
var consultantIdStringContent = await consultantIdResponse.Content.ReadAsStringAsync();
var consultantId = Guid.Parse(consultantIdStringContent);
var checkAvailabilityString = $"https://localhost:8884/consultantcalendars/checkAvailability/GetAvailability" +
$"?consultantId={consultantId}" +
$"&appointmentDate={model.AppointmentDate}";
var isAvailableResponse = await _httpClient.GetAsync(checkAvailabilityString);
if (isAvailableResponse.IsSuccessStatusCode)
{
var isAvailableStringContent = await isAvailableResponse.Content.ReadAsStringAsync();
var isAvailable = bool.Parse(isAvailableStringContent);
if (!isAvailable)
{
return BadRequest("Ko");
}
}
}
await _repository.CreateAppointmentAsync(model);
_logger.LogInformation($"New appointment created successfully at {DateTime.UtcNow.ToLocalTime()}");
return CreatedAtAction(nameof(GetOneAppointment), new { appointmentId = model.Id }, model);
}
BookingAppointmentApi 中的预约模型:
public class AppointmentModel
{
public Guid Id { get; set; }
public string PatientName { get; set; }
public string ConsultantName { get; set; }
public DateTime AppointmentDate { get; set; }
}
ConsultantCalendarApi 中的ConsultantsController:
[HttpGet("checkAvailability/{consultantName}")]
public async Task<ActionResult> GetGuidConsultant(string consultantName)
{
_logger.LogInformation($"Consultant name: {consultantName}");
var consultant = await _repository.GetConsultantIdByNameAsync(consultantName);
if (consultant == null)
{
_logger.LogError($"Consultant {consultantName} not found");
return NotFound();
}
_logger.LogInformation($"Consultant {consultantName} retrieved successfully at {DateTime.UtcNow.ToLocalTime()}");
return Ok(consultant);
}
GetGuidConsultant 方法适用于 Swagger。
在调试模式下,在用户界面中输入的顾问姓名为 Bob,直到此时:
var consultantIdString = $"https://localhost:8884/consultants/checkAvailability/GetGuidConsultant" +
$"?consultantName={model.ConsultantName}";
问题是,在您的 URL 处,您将
consultantName
设置为查询参数,但在您的 BookingController
处,您希望它作为路线的一部分。
最简单的方法是将您的网址更改为:
var consultantIdString = $"https://localhost:8884/consultants/checkAvailability/{model.ConsultantName}";