Skip to content
Snippets Groups Projects
Commit 3ee9f0e1 authored by Jan Pokorný's avatar Jan Pokorný :lifter_tone2:
Browse files

Updating server ports

parent 5e7948e9
No related branches found
No related tags found
1 merge request!46LanguageSchool fixes + enrol expel rework
Pipeline #
package org.fuseri.modulelanguageschool.course; package org.fuseri.modulelanguageschool.course;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponse;
...@@ -12,6 +13,7 @@ import org.fuseri.model.dto.course.CourseDto; ...@@ -12,6 +13,7 @@ import org.fuseri.model.dto.course.CourseDto;
import org.fuseri.model.dto.course.LanguageTypeDto; import org.fuseri.model.dto.course.LanguageTypeDto;
import org.fuseri.model.dto.course.ProficiencyLevelDto; import org.fuseri.model.dto.course.ProficiencyLevelDto;
import org.fuseri.modulelanguageschool.ModuleLanguageSchoolApplication; import org.fuseri.modulelanguageschool.ModuleLanguageSchoolApplication;
import org.fuseri.modulelanguageschool.user.UserService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
...@@ -19,6 +21,7 @@ import org.springframework.data.domain.Sort; ...@@ -19,6 +21,7 @@ import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.NativeWebRequest;
import java.util.List; import java.util.List;
...@@ -44,7 +47,7 @@ public class CourseController { ...@@ -44,7 +47,7 @@ public class CourseController {
* @param dto the CourseCreateDto containing the course data * @param dto the CourseCreateDto containing the course data
* @return the newly created CourseDto * @return the newly created CourseDto
*/ */
@Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME,scopes = {"test_1"})) @Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME, scopes = {"test_1", "test_2"}))
@ApiOperation(value = "Create a new course") @ApiOperation(value = "Create a new course")
@PostMapping @PostMapping
@ApiResponses({ @ApiResponses({
...@@ -68,7 +71,7 @@ public class CourseController { ...@@ -68,7 +71,7 @@ public class CourseController {
@ApiResponse(code = 200, message = "Course found"), @ApiResponse(code = 200, message = "Course found"),
@ApiResponse(code = 404, message = "Course not found") @ApiResponse(code = 404, message = "Course not found")
}) })
@Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME,scopes = {"test_1","test_2"})) @Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME))
public ResponseEntity<CourseDto> find(@PathVariable Long id) { public ResponseEntity<CourseDto> find(@PathVariable Long id) {
CourseDto courseDto = courseFacade.findById(id); CourseDto courseDto = courseFacade.findById(id);
return ResponseEntity.ok(courseDto); return ResponseEntity.ok(courseDto);
...@@ -80,8 +83,7 @@ public class CourseController { ...@@ -80,8 +83,7 @@ public class CourseController {
* @param page the page number to retrieve * @param page the page number to retrieve
* @return the Result containing the requested page of CourseDtos * @return the Result containing the requested page of CourseDtos
*/ */
@Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME))
@Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME,scopes = {"test_1","test_2"}))
@ApiOperation(value = "Retrieve a paginated list of courses") @ApiOperation(value = "Retrieve a paginated list of courses")
@GetMapping("/findAll") @GetMapping("/findAll")
@ApiResponses(value = { @ApiResponses(value = {
...@@ -99,7 +101,7 @@ public class CourseController { ...@@ -99,7 +101,7 @@ public class CourseController {
* @param lang the language to find courses of * @param lang the language to find courses of
* @return the Result containing the requested page of CourseDtos * @return the Result containing the requested page of CourseDtos
*/ */
@Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME,scopes = {"test_1","test_2"})) @Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME))
@ApiOperation(value = "Retrieve a paginated list of courses of a given language") @ApiOperation(value = "Retrieve a paginated list of courses of a given language")
@GetMapping("/findAllByLang") @GetMapping("/findAllByLang")
@ApiResponses({ @ApiResponses({
...@@ -118,7 +120,7 @@ public class CourseController { ...@@ -118,7 +120,7 @@ public class CourseController {
* @param prof the proficiency of the language * @param prof the proficiency of the language
* @return the Result containing the requested page of CourseDtos * @return the Result containing the requested page of CourseDtos
*/ */
@Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME,scopes = {"test_1","test_2"})) @Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME))
@ApiOperation(value = "Retrieve a paginated list of courses of a given language and proficiency") @ApiOperation(value = "Retrieve a paginated list of courses of a given language and proficiency")
@GetMapping("/findAllByLangProf") @GetMapping("/findAllByLangProf")
@ApiResponses({ @ApiResponses({
...@@ -138,7 +140,7 @@ public class CourseController { ...@@ -138,7 +140,7 @@ public class CourseController {
* @param dto the CourseCreateDto containing the updated course data * @param dto the CourseCreateDto containing the updated course data
* @return the updated CourseDto * @return the updated CourseDto
*/ */
@Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME,scopes = {"test_1","test_2"})) @Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME, scopes = {"test_1", "test_2"}))
@ApiOperation(value = "Update an existing course") @ApiOperation(value = "Update an existing course")
@PutMapping("/update/{id}") @PutMapping("/update/{id}")
@ApiResponses({ @ApiResponses({
...@@ -162,7 +164,7 @@ public class CourseController { ...@@ -162,7 +164,7 @@ public class CourseController {
@ApiResponse(code = 204, message = "Course deleted successfully"), @ApiResponse(code = 204, message = "Course deleted successfully"),
@ApiResponse(code = 404, message = "Course not found") @ApiResponse(code = 404, message = "Course not found")
}) })
@Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME,scopes = {"test_1"})) @Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME, scopes = {"test_1"}))
public ResponseEntity<Void> delete(@PathVariable Long id) { public ResponseEntity<Void> delete(@PathVariable Long id) {
courseFacade.delete(id); courseFacade.delete(id);
return ResponseEntity.noContent().build(); return ResponseEntity.noContent().build();
...@@ -171,13 +173,13 @@ public class CourseController { ...@@ -171,13 +173,13 @@ public class CourseController {
/** /**
* Adds student to the existing course * Adds student to the existing course
* *
* @param id id of course to update * @param id id of course to update
* @param studentId UserDto for the student * @param studentId id of the student
* @return the CourseDto representing the updated course * @return the CourseDto representing the updated course
*/ */
@Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME,scopes = {"test_1","test_2"})) @Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME, scopes = {"test_1", "test_2"}))
@ApiOperation(value = "Add student to the existing course") @ApiOperation(value = "Add student to the existing course")
@PatchMapping("/enrol/{id}") @PatchMapping("/enrolStudent/{id}")
@ApiResponses(value = { @ApiResponses(value = {
@ApiResponse(code = 200, message = "Successfully enrolled student in course"), @ApiResponse(code = 200, message = "Successfully enrolled student in course"),
@ApiResponse(code = 404, message = "Course not found") @ApiResponse(code = 404, message = "Course not found")
...@@ -188,23 +190,59 @@ public class CourseController { ...@@ -188,23 +190,59 @@ public class CourseController {
} }
/** /**
* Removes student from the existing course * Adds currently signed-in student to the existing course
* *
* @param id id of lecture to update * @param id id of lecture to update
* @param studentId UserDto for the student * @param request http request received with user email
* @return the CourseDto representing the updated course * @return the CourseDto representing the updated course
*/ */
@Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME,scopes = {"test_1","test_2"})) @Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME))
@ApiOperation(value = "Add me to the existing course")
@PatchMapping("/enrol/{id}")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successfully enrolled student in course"),
@ApiResponse(code = 404, message = "Course not found")
})
public ResponseEntity<CourseDto> enrol(@PathVariable Long id, NativeWebRequest request) throws JsonProcessingException {
String email = UserService.getEmailFromRequest(request);
return ResponseEntity.ok(courseFacade.enrol(id, email));
}
/**
* Removes student from the existing course
*
* @param id id of lecture to update
* @param studentId ID of the student
* @return the CourseDto representing the updated course
*/
@Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME, scopes = {"test_1", "test_2"}))
@ApiOperation(value = "Remove student from the existing course") @ApiOperation(value = "Remove student from the existing course")
@PatchMapping("/expel/{id}") @PatchMapping("/expelStudent/{id}")
@ApiResponses(value = { @ApiResponses(value = {
@ApiResponse(code = 200, message = "Successfully expelled student from course"), @ApiResponse(code = 200, message = "Successfully expelled student from course"),
@ApiResponse(code = 404, message = "Course not found") @ApiResponse(code = 404, message = "Course not found")
}) })
public ResponseEntity<CourseDto> expel(@PathVariable Long id, @RequestParam Long studentId) { public ResponseEntity<CourseDto> expel(@PathVariable Long id, @RequestParam Long studentId) {
return ResponseEntity.ok(courseFacade.expel(id, studentId));
}
CourseDto updatedCourse = courseFacade.expel(id, studentId); /**
return ResponseEntity.ok(updatedCourse); * Removes currently signed-in student from the existing course
*
* @param id id of lecture to update
* @param request http request received with user email
* @return the CourseDto representing the updated course
*/
@Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME))
@ApiOperation(value = "Add me to the existing course")
@PatchMapping("/expel/{id}")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successfully expelled student in course"),
@ApiResponse(code = 404, message = "Course not found")
})
public ResponseEntity<CourseDto> expel(@PathVariable Long id, NativeWebRequest request) throws JsonProcessingException {
String email = UserService.getEmailFromRequest(request);
return ResponseEntity.ok(courseFacade.expel(id, email));
} }
} }
package org.fuseri.modulelanguageschool.course; package org.fuseri.modulelanguageschool.course;
import jakarta.persistence.EntityNotFoundException;
import org.fuseri.model.dto.course.CourseCreateDto; import org.fuseri.model.dto.course.CourseCreateDto;
import org.fuseri.model.dto.course.CourseDto; import org.fuseri.model.dto.course.CourseDto;
import org.fuseri.model.dto.course.LanguageTypeDto; import org.fuseri.model.dto.course.LanguageTypeDto;
...@@ -70,8 +71,20 @@ public class CourseFacade { ...@@ -70,8 +71,20 @@ public class CourseFacade {
return courseMapper.mapToDto(courseService.enrol(id, student)); return courseMapper.mapToDto(courseService.enrol(id, student));
} }
public CourseDto enrol(Long id, String email) {
var student = userService.findUserByEmail(email)
.orElseThrow(() -> new EntityNotFoundException("User with " + email + " email not found."));
return courseMapper.mapToDto(courseService.enrol(id, student));
}
public CourseDto expel(Long id, Long studentId) { public CourseDto expel(Long id, Long studentId) {
var student = userService.find(studentId); var student = userService.find(studentId);
return courseMapper.mapToDto(courseService.expel(id, student)); return courseMapper.mapToDto(courseService.expel(id, student));
} }
public CourseDto expel(Long id, String email) {
var student = userService.findUserByEmail(email)
.orElseThrow(() -> new EntityNotFoundException("User with " + email + " email not found."));
return courseMapper.mapToDto(courseService.expel(id, student));
}
} }
package org.fuseri.modulelanguageschool.lecture; package org.fuseri.modulelanguageschool.lecture;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponse;
...@@ -10,10 +11,12 @@ import jakarta.validation.Valid; ...@@ -10,10 +11,12 @@ import jakarta.validation.Valid;
import org.fuseri.model.dto.lecture.LectureCreateDto; import org.fuseri.model.dto.lecture.LectureCreateDto;
import org.fuseri.model.dto.lecture.LectureDto; import org.fuseri.model.dto.lecture.LectureDto;
import org.fuseri.modulelanguageschool.ModuleLanguageSchoolApplication; import org.fuseri.modulelanguageschool.ModuleLanguageSchoolApplication;
import org.fuseri.modulelanguageschool.user.UserService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.NativeWebRequest;
import java.util.List; import java.util.List;
...@@ -40,6 +43,7 @@ public class LectureController { ...@@ -40,6 +43,7 @@ public class LectureController {
* @param lecture the LectureDto representing the lecture to be created * @param lecture the LectureDto representing the lecture to be created
* @return the LectureDto representing the newly created lecture * @return the LectureDto representing the newly created lecture
*/ */
@Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME, scopes = {"test_1", "test_2"}))
@ApiOperation(value = "Create a new lecture") @ApiOperation(value = "Create a new lecture")
@PostMapping @PostMapping
@ApiResponses(value = { @ApiResponses(value = {
...@@ -58,7 +62,7 @@ public class LectureController { ...@@ -58,7 +62,7 @@ public class LectureController {
* @return the LectureDto representing the found lecture * @return the LectureDto representing the found lecture
*/ */
@Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME,scopes = {"test_1","test_2"})) @Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME))
@ApiOperation(value = "Retrieve a lecture by its ID") @ApiOperation(value = "Retrieve a lecture by its ID")
@GetMapping("find/{courseId}") @GetMapping("find/{courseId}")
@ApiResponses(value = { @ApiResponses(value = {
...@@ -76,7 +80,7 @@ public class LectureController { ...@@ -76,7 +80,7 @@ public class LectureController {
* @param courseId the course to retrieve lectures from * @param courseId the course to retrieve lectures from
* @return the list of LectureDtos * @return the list of LectureDtos
*/ */
@Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME,scopes = {"test_1","test_2"})) @Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME))
@ApiOperation(value = "Retrieve a list of lectures for the corresponding course") @ApiOperation(value = "Retrieve a list of lectures for the corresponding course")
@GetMapping("/findByCourse") @GetMapping("/findByCourse")
@ApiResponses(value = { @ApiResponses(value = {
...@@ -93,7 +97,7 @@ public class LectureController { ...@@ -93,7 +97,7 @@ public class LectureController {
* @param lecture the CourseCreateDto representing the updated lecture * @param lecture the CourseCreateDto representing the updated lecture
* @return the LectureDto representing the updated lecture * @return the LectureDto representing the updated lecture
*/ */
@Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME,scopes = {"test_1","test_2"})) @Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME, scopes = {"test_1", "test_2"}))
@ApiOperation(value = "Update an existing lecture") @ApiOperation(value = "Update an existing lecture")
@PutMapping("/update/{id}") @PutMapping("/update/{id}")
@ApiResponses(value = { @ApiResponses(value = {
...@@ -110,7 +114,7 @@ public class LectureController { ...@@ -110,7 +114,7 @@ public class LectureController {
* *
* @param id the ID of the lecture to delete * @param id the ID of the lecture to delete
*/ */
@Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME,scopes = {"test_1","test_2"})) @Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME, scopes = {"test_1"}))
@ApiOperation(value = "Delete a lecture by its ID") @ApiOperation(value = "Delete a lecture by its ID")
@DeleteMapping("/delete/{id}") @DeleteMapping("/delete/{id}")
@ApiResponses(value = { @ApiResponses(value = {
...@@ -130,7 +134,7 @@ public class LectureController { ...@@ -130,7 +134,7 @@ public class LectureController {
* @param lecturerId UserDto for the course lecturer * @param lecturerId UserDto for the course lecturer
* @return the LectureDto representing the updated lecture * @return the LectureDto representing the updated lecture
*/ */
@Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME,scopes = {"test_1","test_2"})) @Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME, scopes = {"test_1", "test_2"}))
@ApiOperation(value = "Add lecturer to the existing lecture") @ApiOperation(value = "Add lecturer to the existing lecture")
@PatchMapping("/setLecturer/{id}") @PatchMapping("/setLecturer/{id}")
@ApiResponses(value = { @ApiResponses(value = {
...@@ -149,18 +153,36 @@ public class LectureController { ...@@ -149,18 +153,36 @@ public class LectureController {
* @param studentId id for the course student * @param studentId id for the course student
* @return the LectureDto representing the updated lecture * @return the LectureDto representing the updated lecture
*/ */
@Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME,scopes = {"test_1","test_2"})) @Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME, scopes = {"test_1", "test_2"}))
@ApiOperation(value = "Add student to the existing lecture") @ApiOperation(value = "Add student to the existing lecture")
@PatchMapping("/enrol/{id}") @PatchMapping("/enrol/{id}")
@ApiResponses(value = { @ApiResponses(value = {
@ApiResponse(code = 200, message = "The lecture has been successfully updated"), @ApiResponse(code = 200, message = "The lecture has been successfully updated"),
@ApiResponse(code = 400, message = "The request body is invalid"),
@ApiResponse(code = 404, message = "The lecture with the specified ID does not exist") @ApiResponse(code = 404, message = "The lecture with the specified ID does not exist")
}) })
public ResponseEntity<LectureDto> enrol(@PathVariable Long id, @RequestParam Long studentId) { public ResponseEntity<LectureDto> enrol(@PathVariable Long id, @RequestParam Long studentId) {
return ResponseEntity.ok(lectureFacade.enrol(id, studentId)); return ResponseEntity.ok(lectureFacade.enrol(id, studentId));
} }
/**
* Adds student to the existing lecture resource
*
* @param id id of lecture to update
* @param request http request received with user email
* @return the LectureDto representing the updated lecture
*/
@Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME))
@ApiOperation(value = "Add student to the existing lecture")
@PatchMapping("/enrol/{id}")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "The lecture has been successfully updated"),
@ApiResponse(code = 404, message = "The lecture with the specified ID does not exist")
})
public ResponseEntity<LectureDto> enrol(@PathVariable Long id, NativeWebRequest request) throws JsonProcessingException {
String email = UserService.getEmailFromRequest(request);
return ResponseEntity.ok(lectureFacade.enrol(id, email));
}
/** /**
* Removes student from the existing lecture resource * Removes student from the existing lecture resource
* *
...@@ -168,15 +190,33 @@ public class LectureController { ...@@ -168,15 +190,33 @@ public class LectureController {
* @param studentId id for the course student * @param studentId id for the course student
* @return the LectureDto representing the updated lecture * @return the LectureDto representing the updated lecture
*/ */
@Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME,scopes = {"test_1","test_2"})) @Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME, scopes = {"test_1", "test_2"}))
@ApiOperation(value = "Remove student from the existing lecture") @ApiOperation(value = "Remove student from the existing lecture")
@PatchMapping("/expel/{id}") @PatchMapping("/expelStudent/{id}")
@ApiResponses(value = { @ApiResponses(value = {
@ApiResponse(code = 200, message = "The lecture has been successfully updated"), @ApiResponse(code = 200, message = "The lecture has been successfully updated"),
@ApiResponse(code = 400, message = "The request body is invalid"),
@ApiResponse(code = 404, message = "The lecture with the specified ID does not exist") @ApiResponse(code = 404, message = "The lecture with the specified ID does not exist")
}) })
public ResponseEntity<LectureDto> expel(@PathVariable Long id, @RequestParam Long studentId) { public ResponseEntity<LectureDto> expel(@PathVariable Long id, @RequestParam Long studentId) {
return ResponseEntity.ok(lectureFacade.expel(id, studentId)); return ResponseEntity.ok(lectureFacade.expel(id, studentId));
} }
/**
* Removes student from the existing lecture resource
*
* @param id id of lecture to update
* @param request http request received with user email
* @return the LectureDto representing the updated lecture
*/
@Operation(security = @SecurityRequirement(name = ModuleLanguageSchoolApplication.SECURITY_SCHEME_NAME))
@ApiOperation(value = "Remove student from the existing lecture")
@PatchMapping("/expel/{id}")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "The lecture has been successfully updated"),
@ApiResponse(code = 404, message = "The lecture with the specified ID does not exist")
})
public ResponseEntity<LectureDto> expel(@PathVariable Long id, NativeWebRequest request) throws JsonProcessingException {
String email = UserService.getEmailFromRequest(request);
return ResponseEntity.ok(lectureFacade.expel(id, email));
}
} }
\ No newline at end of file
package org.fuseri.modulelanguageschool.lecture; package org.fuseri.modulelanguageschool.lecture;
import jakarta.persistence.EntityNotFoundException;
import org.fuseri.model.dto.course.LanguageTypeDto; import org.fuseri.model.dto.course.LanguageTypeDto;
import org.fuseri.model.dto.course.ProficiencyLevelDto; import org.fuseri.model.dto.course.ProficiencyLevelDto;
import org.fuseri.model.dto.lecture.LectureCreateDto; import org.fuseri.model.dto.lecture.LectureCreateDto;
...@@ -70,16 +71,28 @@ public class LectureFacade { ...@@ -70,16 +71,28 @@ public class LectureFacade {
return lectureMapper.mapToList(lectureService.findAll(Language.valueOf(lang.name()), ProficiencyLevel.valueOf(prof.name()))); return lectureMapper.mapToList(lectureService.findAll(Language.valueOf(lang.name()), ProficiencyLevel.valueOf(prof.name())));
} }
public LectureDto enrol(Long id, long studentId) { public LectureDto enrol(Long id, Long studentId) {
var student = userService.find(studentId); var student = userService.find(studentId);
return lectureMapper.mapToDto(lectureService.enrol(id, student)); return lectureMapper.mapToDto(lectureService.enrol(id, student));
} }
public LectureDto enrol(Long id, String email) {
var student = userService.findUserByEmail(email)
.orElseThrow(() -> new EntityNotFoundException("User with " + email + " email not found."));
return lectureMapper.mapToDto(lectureService.enrol(id, student));
}
public LectureDto expel(Long id, Long studentId) { public LectureDto expel(Long id, Long studentId) {
var student = userService.find(studentId); var student = userService.find(studentId);
return lectureMapper.mapToDto(lectureService.expel(id, student)); return lectureMapper.mapToDto(lectureService.expel(id, student));
} }
public LectureDto expel(Long id, String email) {
var student = userService.findUserByEmail(email)
.orElseThrow(() -> new EntityNotFoundException("User with " + email + " email not found."));
return lectureMapper.mapToDto(lectureService.expel(id, student));
}
public LectureDto setLecturer(Long id, Long lecturerId) { public LectureDto setLecturer(Long id, Long lecturerId) {
var lecturer = userService.find(lecturerId); var lecturer = userService.find(lecturerId);
return lectureMapper.mapToDto(lectureService.setLecturer(id, lecturer)); return lectureMapper.mapToDto(lectureService.setLecturer(id, lecturer));
......
package org.fuseri.modulelanguageschool.user; package org.fuseri.modulelanguageschool.user;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.EntityNotFoundException;
import jakarta.servlet.http.HttpServletRequest;
import lombok.Getter; import lombok.Getter;
import org.fuseri.modulelanguageschool.common.DomainService; import org.fuseri.modulelanguageschool.common.DomainService;
import org.fuseri.modulelanguageschool.common.UserWithEmailAlreadyExists; import org.fuseri.modulelanguageschool.common.UserWithEmailAlreadyExists;
...@@ -10,7 +14,9 @@ import org.fuseri.modulelanguageschool.course.ProficiencyLevel; ...@@ -10,7 +14,9 @@ import org.fuseri.modulelanguageschool.course.ProficiencyLevel;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.NativeWebRequest;
import java.util.Base64;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
...@@ -61,4 +67,24 @@ public class UserService extends DomainService<User> { ...@@ -61,4 +67,24 @@ public class UserService extends DomainService<User> {
throw new EntityNotFoundException("User '" + id + "' not found."); throw new EntityNotFoundException("User '" + id + "' not found.");
} }
} }
public static String getEmailFromRequest(NativeWebRequest request) throws JsonProcessingException {
// retrieve the HttpServletRequest object
HttpServletRequest servletRequest = (HttpServletRequest) request.getNativeRequest();
// extract the authorization header
String authorizationHeader = servletRequest.getHeader("Authorization");
// extract the bearer token
String accessToken = authorizationHeader.substring(7);
// decode the JWT token payload
String[] jwtParts = accessToken.split("\\.");
String jwtPayload = new String(Base64.getDecoder().decode(jwtParts[1]));
// parse the JSON payload to retrieve the email
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jwtPayload);
return jsonNode.get("sub").asText();
}
} }
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