Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/rest like endpoints #180

Merged
merged 9 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class AccountController(private val service: AccountService) {
@GetMapping("/{id}")
fun getAccountById(@PathVariable id: Long) = service.getAccountById(id)

@PostMapping("/new", consumes = ["multipart/form-data"])
@PostMapping(consumes = ["multipart/form-data"])
fun createAccount(
@RequestPart account: CreateAccountDto,
@RequestParam
Expand Down Expand Up @@ -58,7 +58,7 @@ class AccountController(private val service: AccountService) {
return emptyMap()
}

@PostMapping("/changePassword/{id}")
@PostMapping("/{id}/password")
fun changePassword(@PathVariable id: Long, @RequestBody dto: ChangePasswordDto): Map<String, String> {
service.changePassword(id, dto)
return emptyMap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import pt.up.fe.ni.website.backend.service.AuthService
@RestController
@RequestMapping("/auth")
class AuthController(val authService: AuthService) {
@PostMapping("/new")
@PostMapping
fun getNewToken(@RequestBody loginDto: LoginDto): Map<String, String> {
val account = authService.authenticate(loginDto.email, loginDto.password)
val accessToken = authService.generateAccessToken(account)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import pt.up.fe.ni.website.backend.utils.validation.ValidImage
@Validated
class EventController(private val service: EventService) {
@GetMapping
fun getAllEvents() = service.getAllEvents()
fun getEvents(@RequestParam(required = false, name = "category") category: String?): List<Event> {
return category?.let { service.getEventsByCategory(it) } ?: service.getAllEvents()
}

@GetMapping("/{id:\\d+}")
fun getEventById(@PathVariable id: Long) = service.getEventById(id)
Expand All @@ -32,7 +34,7 @@ class EventController(private val service: EventService) {
@GetMapping("/{eventSlug}**")
fun getEvent(@PathVariable eventSlug: String) = service.getEventBySlug(eventSlug)

@PostMapping("/new", consumes = ["multipart/form-data"])
@PostMapping(consumes = ["multipart/form-data"])
fun createEvent(
@RequestPart event: EventDto,
@RequestParam
Expand Down Expand Up @@ -61,13 +63,13 @@ class EventController(private val service: EventService) {
return service.updateEventById(id, event)
}

@PutMapping("/{idEvent}/addTeamMember/{idAccount}")
@PutMapping("/{idEvent}/team/{idAccount}")
fun addTeamMemberById(
@PathVariable idEvent: Long,
@PathVariable idAccount: Long
) = service.addTeamMemberById(idEvent, idAccount)

@PutMapping("/{idEvent}/removeTeamMember/{idAccount}")
@DeleteMapping("/{idEvent}/team/{idAccount}")
fun removeTeamMemberById(
@PathVariable idEvent: Long,
@PathVariable idAccount: Long
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -24,7 +24,7 @@ class PostController(private val service: PostService) {
@GetMapping("/{postSlug}**")
fun getPost(@PathVariable postSlug: String) = service.getPostBySlug(postSlug)

@PostMapping("/new")
@PostMapping
fun createPost(@RequestBody dto: PostDto) = service.createPost(dto)

@PutMapping("/{postId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ProjectController(private val service: ProjectService) {
@GetMapping("/{projectSlug}**")
fun getProjectBySlug(@PathVariable projectSlug: String) = service.getProjectBySlug(projectSlug)

@PostMapping("/new", consumes = ["multipart/form-data"])
@PostMapping(consumes = ["multipart/form-data"])
fun createProject(
@RequestPart project: ProjectDto,
@RequestParam
Expand Down Expand Up @@ -65,25 +65,25 @@ class ProjectController(private val service: ProjectService) {
@PutMapping("/{id}/unarchive")
fun unarchiveProjectById(@PathVariable id: Long) = service.unarchiveProjectById(id)

@PutMapping("/{idProject}/addTeamMember/{idAccount}")
@PutMapping("/{idProject}/team/{idAccount}")
fun addTeamMemberById(
@PathVariable idProject: Long,
@PathVariable idAccount: Long
) = service.addTeamMemberById(idProject, idAccount)

@PutMapping("/{idProject}/removeTeamMember/{idAccount}")
@DeleteMapping("/{idProject}/team/{idAccount}")
fun removeTeamMemberById(
@PathVariable idProject: Long,
@PathVariable idAccount: Long
) = service.removeTeamMemberById(idProject, idAccount)

@PutMapping("/{idProject}/addHallOfFameMember/{idAccount}")
@PutMapping("/{idProject}/hallOfFame/{idAccount}")
fun addHallOfFameMemberById(
@PathVariable idProject: Long,
@PathVariable idAccount: Long
) = service.addHallOfFameMemberById(idProject, idAccount)

@PutMapping("/{idProject}/removeHallOfFameMember/{idAccount}")
@DeleteMapping("/{idProject}/hallOfFame/{idAccount}")
fun removeHallOfFameMemberById(
@PathVariable idProject: Long,
@PathVariable idAccount: Long
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class AccountControllerTest @Autowired constructor(
}

@NestedTest
@DisplayName("POST /accounts/new")
@DisplayName("POST /accounts")
inner class CreateAccount {
private val uuid: UUID = UUID.randomUUID()
private val mockedSettings = Mockito.mockStatic(UUID::class.java)
Expand All @@ -184,7 +184,7 @@ class AccountControllerTest @Autowired constructor(

@Test
fun `should create the account`() {
mockMvc.multipartBuilder("/accounts/new")
mockMvc.multipartBuilder("/accounts")
.addPart("account", testAccount.toJson())
.perform()
.andExpectAll(
Expand Down Expand Up @@ -228,7 +228,7 @@ class AccountControllerTest @Autowired constructor(
)
)

mockMvc.multipartBuilder("/accounts/new")
mockMvc.multipartBuilder("/accounts")
.addPart("account", data)
.perform()
.andExpectAll(
Expand All @@ -248,7 +248,7 @@ class AccountControllerTest @Autowired constructor(
fun `should create the account with valid image`() {
val expectedPhotoPath = "${uploadConfigProperties.staticServe}/profile/${testAccount.email}-$uuid.jpeg"

mockMvc.multipartBuilder("/accounts/new")
mockMvc.multipartBuilder("/accounts")
.addPart("account", testAccount.toJson())
.addFile()
.perform()
Expand Down Expand Up @@ -276,7 +276,7 @@ class AccountControllerTest @Autowired constructor(

@Test
fun `should fail to create account with invalid filename extension`() {
mockMvc.multipartBuilder("/accounts/new")
mockMvc.multipartBuilder("/accounts")
.addPart("account", testAccount.toJson())
.addFile(filename = "photo.pdf")
.perform()
Expand All @@ -292,7 +292,7 @@ class AccountControllerTest @Autowired constructor(

@Test
fun `should fail to create account with invalid filename media type`() {
mockMvc.multipartBuilder("/accounts/new")
mockMvc.multipartBuilder("/accounts")
.addPart("account", testAccount.toJson())
.addFile(contentType = MediaType.APPLICATION_PDF_VALUE)
.perform()
Expand All @@ -307,7 +307,7 @@ class AccountControllerTest @Autowired constructor(

@Test
fun `should fail when missing account part`() {
mockMvc.multipartBuilder("/accounts/new")
mockMvc.multipartBuilder("/accounts")
.perform()
.andExpectAll(
status().isBadRequest,
Expand All @@ -323,7 +323,7 @@ class AccountControllerTest @Autowired constructor(
inner class InputValidation {
private val validationTester = ValidationTester(
req = { params: Map<String, Any?> ->
mockMvc.multipartBuilder("/accounts/new")
mockMvc.multipartBuilder("/accounts")
.addPart("account", objectMapper.writeValueAsString(params))
.perform()
.andDocumentErrorResponse(documentation, hasRequestPayload = true)
Expand Down Expand Up @@ -463,7 +463,7 @@ class AccountControllerTest @Autowired constructor(
)
accountPart.headers.contentType = MediaType.APPLICATION_JSON

mockMvc.perform(multipart("/accounts/new").part(accountPart))
mockMvc.perform(multipart("/accounts").part(accountPart))
.andDocumentErrorResponse(documentation, hasRequestPayload = true)
},
requiredFields = mapOf(
Expand Down Expand Up @@ -538,12 +538,12 @@ class AccountControllerTest @Autowired constructor(

@Test
fun `should fail to create account with existing email`() {
mockMvc.multipartBuilder("/accounts/new")
mockMvc.multipartBuilder("/accounts")
.addPart("account", testAccount.toJson())
.perform()
.andExpect(status().isOk)

mockMvc.multipartBuilder("/accounts/new")
mockMvc.multipartBuilder("/accounts")
.addPart("account", testAccount.toJson())
.perform()
.andExpectAll(
Expand All @@ -556,7 +556,7 @@ class AccountControllerTest @Autowired constructor(
}

@NestedTest
@DisplayName("POST /accounts/changePassword/{id}")
@DisplayName("POST /accounts/{id}/password")
inner class ChangePassword {
private val password = "test_password"
private val changePasswordAccount: Account = ObjectMapper().readValue(
Expand Down Expand Up @@ -590,7 +590,7 @@ class AccountControllerTest @Autowired constructor(
@Test
fun `should change password`() {
mockMvc.perform(
post("/accounts/changePassword/{id}", changePasswordAccount.id)
post("/accounts/{id}/password", changePasswordAccount.id)
.contentType(MediaType.APPLICATION_JSON)
.content(
objectMapper.writeValueAsString(
Expand All @@ -610,7 +610,7 @@ class AccountControllerTest @Autowired constructor(
documentRequestPayload = true
)

mockMvc.post("/auth/new") {
mockMvc.post("/auth") {
contentType = MediaType.APPLICATION_JSON
content = objectMapper.writeValueAsString(
mapOf(
Expand All @@ -624,7 +624,7 @@ class AccountControllerTest @Autowired constructor(
@Test
fun `should fail due to wrong password`() {
mockMvc.perform(
post("/accounts/changePassword/{id}", changePasswordAccount.id)
post("/accounts/{id}/password", changePasswordAccount.id)
.contentType(MediaType.APPLICATION_JSON)
.content(
objectMapper.writeValueAsString(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class AuthControllerTest @Autowired constructor(
)

@NestedTest
@DisplayName("POST /auth/new")
@DisplayName("POST /auth")
inner class GetNewToken {
@BeforeEach
fun setup() {
Expand All @@ -75,7 +75,7 @@ class AuthControllerTest @Autowired constructor(
@Test
fun `should fail when email is not registered`() {
mockMvc.perform(
post("/auth/new")
post("/auth")
.contentType(MediaType.APPLICATION_JSON)
.content(
objectMapper.writeValueAsString(
Expand All @@ -96,7 +96,7 @@ class AuthControllerTest @Autowired constructor(
@Test
fun `should fail when password is incorrect`() {
mockMvc.perform(
post("/auth/new")
post("/auth")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(LoginDto(testAccount.email, "wrong_password")))
)
Expand All @@ -110,7 +110,7 @@ class AuthControllerTest @Autowired constructor(
@Test
fun `should return access and refresh tokens`() {
mockMvc.perform(
post("/auth/new")
post("/auth")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(LoginDto(testAccount.email, testPassword)))
)
Expand Down Expand Up @@ -155,7 +155,7 @@ class AuthControllerTest @Autowired constructor(

@Test
fun `should return new access token`() {
mockMvc.post("/auth/new") {
mockMvc.post("/auth") {
contentType = MediaType.APPLICATION_JSON
content = objectMapper.writeValueAsString(LoginDto(testAccount.email, testPassword))
}.andReturn().response.let { response ->
Expand Down Expand Up @@ -216,7 +216,7 @@ class AuthControllerTest @Autowired constructor(

@Test
fun `should return authenticated user`() {
mockMvc.post("/auth/new") {
mockMvc.post("/auth") {
contentType = MediaType.APPLICATION_JSON
content = objectMapper.writeValueAsString(LoginDto(testAccount.email, testPassword))
}.andReturn().response.let { response ->
Expand Down
Loading