Skip to content

Commit

Permalink
Refactor generations endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Sirze01 committed Oct 6, 2023
1 parent d98d23a commit b7f9acc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import pt.up.fe.ni.website.backend.service.GenerationService
@RestController
@RequestMapping("/generations")
class GenerationController(private val service: GenerationService) {
@GetMapping
fun getAllGenerations() = service.getAllGenerations()
@GetMapping("/years")
fun getGenerationsSchoolYears() = service.getGenerationsSchoolYears()

@GetMapping("/{id:\\d+}")
fun getGenerationById(@PathVariable id: Long) = service.getGenerationById(id)
Expand All @@ -28,7 +28,7 @@ class GenerationController(private val service: GenerationService) {
@GetMapping("/latest")
fun getLatestGeneration() = service.getLatestGeneration()

@PostMapping("/new")
@PostMapping
fun createNewGeneration(
@RequestBody dto: GenerationDto
) = service.createNewGeneration(dto)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class GenerationService(
private val activityService: ActivityService
) {

fun getAllGenerations(): List<String> = repository.findAllSchoolYearOrdered()
fun getGenerationsSchoolYears(): List<String> = repository.findAllSchoolYearOrdered()

fun getGenerationById(id: Long): GetGenerationDto {
val generation =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class GenerationControllerTest @Autowired constructor(
)

@NestedTest
@DisplayName("GET /generations")
@DisplayName("GET /generations/years")
inner class GetAllGenerations {

@BeforeEach
Expand All @@ -105,7 +105,7 @@ class GenerationControllerTest @Autowired constructor(

@Test
fun `should return all generation years`() {
mockMvc.perform(get("/generations"))
mockMvc.perform(get("/generations/years"))
.andExpectAll(
status().isOk,
content().contentType(MediaType.APPLICATION_JSON),
Expand All @@ -124,7 +124,7 @@ class GenerationControllerTest @Autowired constructor(
}

@NestedTest
@DisplayName("GET /generations/year")
@DisplayName("GET /generations/{year}")
inner class GetGenerationByYear {
@BeforeEach
fun addGenerations() {
Expand Down Expand Up @@ -393,12 +393,12 @@ class GenerationControllerTest @Autowired constructor(
}

@NestedTest
@DisplayName("POST /generations/new")
@DisplayName("POST /generations")
inner class CreateGeneration {
@Test
fun `should create a new generation`() {
mockMvc.perform(
post("/generations/new").contentType(MediaType.APPLICATION_JSON).content(
post("/generations").contentType(MediaType.APPLICATION_JSON).content(
objectMapper.writeValueAsString(GenerationDto("22-23", emptyList()))
)
)
Expand Down Expand Up @@ -438,7 +438,7 @@ class GenerationControllerTest @Autowired constructor(
)

mockMvc.perform(
post("/generations/new")
post("/generations")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(generationDtoWithRoles))
)
Expand Down Expand Up @@ -484,7 +484,7 @@ class GenerationControllerTest @Autowired constructor(
)

mockMvc.perform(
post("/generations/new")
post("/generations")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(generationDtoWithRoles))
)
Expand All @@ -511,7 +511,7 @@ class GenerationControllerTest @Autowired constructor(
@Test
fun `should fail if year is not specified and there are no generations`() {
mockMvc.perform(
post("/generations/new")
post("/generations")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(GenerationDto(null, emptyList())))
)
Expand All @@ -536,7 +536,7 @@ class GenerationControllerTest @Autowired constructor(
@Test
fun `should infer the year if not specified and create generation`() {
mockMvc.perform(
post("/generations/new")
post("/generations")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(GenerationDto(null, emptyList())))
)
Expand Down Expand Up @@ -564,7 +564,7 @@ class GenerationControllerTest @Autowired constructor(
)

mockMvc.perform(
post("/generations/new")
post("/generations")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(generationDtoWithAccounts))
)
Expand Down Expand Up @@ -615,7 +615,7 @@ class GenerationControllerTest @Autowired constructor(
)

mockMvc.perform(
post("/generations/new")
post("/generations")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(generationDtoWithActivities))
)
Expand Down Expand Up @@ -669,7 +669,7 @@ class GenerationControllerTest @Autowired constructor(
@Test
fun `should fail if the year already exists`() {
mockMvc.perform(
post("/generations/new")
post("/generations")
.contentType(MediaType.APPLICATION_JSON)
.content(
objectMapper.writeValueAsString(GenerationDto("22-23", emptyList()))
Expand All @@ -686,7 +686,7 @@ class GenerationControllerTest @Autowired constructor(
}

@NestedTest
@DisplayName("PATCH /generations/year")
@DisplayName("PATCH /generations/{year}")
inner class UpdateGenerationByYear {
@BeforeEach
fun addGenerations() {
Expand Down

0 comments on commit b7f9acc

Please sign in to comment.