@@ -14,6 +14,50 @@ public ChoiceOptionController(ApplicationDbContext context, ILogger<ChoiceOption
1414 }
1515
1616
17+ [ HttpPut ]
18+ public async Task < IActionResult > UpdateChoiceOption ( [ FromBody ] ChoiceOptionViewModel choiceOptionViewModel )
19+ {
20+ try
21+ {
22+ if ( choiceOptionViewModel == null || choiceOptionViewModel . Id == 0 )
23+ {
24+ _logger . LogWarning ( "Invalid choice option data for update" ) ;
25+ return BadRequest ( "Invalid choice option data" ) ;
26+ }
27+
28+ _logger . LogInformation ( "Updating choice option with ID {ChoiceOptionId}" , choiceOptionViewModel . Id ) ;
29+
30+ var choiceOption = await _context . ChoiceOptions . FindAsync ( choiceOptionViewModel . Id ) ;
31+ if ( choiceOption == null )
32+ {
33+ _logger . LogWarning ( "Choice option with ID {ChoiceOptionId} not found" , choiceOptionViewModel . Id ) ;
34+ return NotFound ( $ "Choice option with ID { choiceOptionViewModel . Id } not found") ;
35+ }
36+
37+ // Update the branching configuration
38+ choiceOption . BranchToGroupId = choiceOptionViewModel . BranchToGroupId ;
39+ choiceOption . OptionText = choiceOptionViewModel . OptionText ;
40+ choiceOption . Order = choiceOptionViewModel . Order ;
41+
42+ await _context . SaveChangesAsync ( ) ;
43+
44+ _logger . LogInformation ( "Successfully updated choice option with ID {ChoiceOptionId}" , choiceOption . Id ) ;
45+ return Ok ( choiceOptionViewModel ) ;
46+ }
47+ catch ( DbUpdateException dbEx )
48+ {
49+ _logger . LogError ( dbEx , "Database error occurred while updating choice option with ID {ChoiceOptionId}: {Message}" ,
50+ choiceOptionViewModel ? . Id , dbEx . Message ) ;
51+ return StatusCode ( StatusCodes . Status500InternalServerError , "A database error occurred. Please try again later." ) ;
52+ }
53+ catch ( Exception ex )
54+ {
55+ _logger . LogError ( ex , "Error updating choice option with ID {ChoiceOptionId}: {Message}" ,
56+ choiceOptionViewModel ? . Id , ex . Message ) ;
57+ return StatusCode ( StatusCodes . Status500InternalServerError , "An unexpected error occurred. Please try again later." ) ;
58+ }
59+ }
60+
1761 [ HttpDelete ( "{id}" ) ]
1862 public async Task < IActionResult > Delete ( int id )
1963 {
0 commit comments