Skip to content

Commit

Permalink
Update speaker name error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec committed Apr 16, 2024
1 parent c7c18ee commit 71e89c0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
33 changes: 21 additions & 12 deletions Backend/Controllers/SpeakerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ public async Task<IActionResult> GetSpeaker(string projectId, string speakerId)
return Ok(speaker);
}

/// <summary> Checks if given speaker name is valid for the project with given id. </summary>
/// <returns> null if valid; a BadRequestObjectResult if invalid. </returns>
private async Task<IActionResult?> CheckSpeakerName(string projectId, string name)
{
if (string.IsNullOrEmpty(name))
{
return BadRequest("projectSettings.speaker.nameEmpty");
}
if (await _speakerRepo.IsSpeakerNameInProject(projectId, name))
{
return BadRequest("projectSettings.speaker.nameTaken");
}
return null;
}

/// <summary> Creates a <see cref="Speaker"/> for the specified projectId </summary>
/// <returns> Id of created Speaker </returns>
[HttpGet("create/{name}", Name = "CreateSpeaker")]
Expand All @@ -94,13 +109,10 @@ public async Task<IActionResult> CreateSpeaker(string projectId, string name)

// Ensure the new name is valid
name = name.Trim();
if (string.IsNullOrEmpty(name))
var nameError = await CheckSpeakerName(projectId, name);
if (nameError is not null)
{
return BadRequest("projectSettings.speaker.nameEmpty");
}
if (await _speakerRepo.IsSpeakerNameInProject(projectId, name))
{
return BadRequest("projectSettings.speaker.nameTaken");
return nameError;
}

// Create speaker and return id
Expand Down Expand Up @@ -201,13 +213,10 @@ public async Task<IActionResult> UpdateSpeakerName(string projectId, string spea

// Ensure the new name is valid
name = name.Trim();
if (string.IsNullOrEmpty(name))
var nameError = await CheckSpeakerName(projectId, name);
if (nameError is not null)
{
return BadRequest("projectSettings.speaker.nameEmpty");
}
if (await _speakerRepo.IsSpeakerNameInProject(projectId, name))
{
return BadRequest("projectSettings.speaker.nameTaken");
return nameError;
}

// Update name and return result with id
Expand Down
2 changes: 1 addition & 1 deletion public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@
"add": "Add a speaker",
"enterName": "Enter the name of a new speaker",
"nameEmpty": "Speaker name is empty",
"nameTaken": "Speaker name is already taken",
"nameTaken": "Speaker name is already taken in this project",
"delete": "Delete this speaker",
"edit": "Edit speaker's name",
"consent": {
Expand Down

0 comments on commit 71e89c0

Please sign in to comment.