Skip to content
Snippets Groups Projects
Commit 98089eaa authored by Dominika Zemanovičová's avatar Dominika Zemanovičová Committed by Martin Gargalovič
Browse files

Add exercise update to ExerciseController

parent 4455ca49
No related branches found
No related tags found
3 merge requests!31M2,!28M2 user,!27Draft: M2 user
package org.fuseri.moduleexercise.exercise;
import jakarta.persistence.EntityNotFoundException;
import org.fuseri.model.dto.common.Result;
import org.fuseri.model.dto.exercise.ExerciseCreateDto;
import org.fuseri.model.dto.exercise.ExerciseDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
/**
* Represent a REST API controller for exercises
......@@ -67,4 +70,26 @@ public class ExerciseController {
return mapper.toResult(exercise);
}
/**
* Update an existing exercise
*
* @param id the ID of the exercise to update
* @param dto the ExerciseCreateDto object containing information about the exercise to update
* @return an ExerciseDto object representing the updated exercise
*/
@PutMapping("/{id}")
public ExerciseDto update(@PathVariable String id, @RequestBody ExerciseCreateDto dto) {
Exercise exercise = mapper.fromCreateDto(dto);
exercise.setId(id);
try {
Exercise updatedExercise = service.update(exercise);
return mapper.toDto(updatedExercise);
} catch (IllegalArgumentException e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Null exercise");
} catch (EntityNotFoundException e) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Exercise not found with id: " + id);
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment