Skip to content

Commit

Permalink
Refactor posts endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Sirze01 committed Oct 6, 2023
1 parent b7f9acc commit 80b7e39
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 28 deletions.
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,13 +65,13 @@ 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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package pt.up.fe.ni.website.backend.controller

import pt.up.fe.ni.website.backend.model.constants.AccountConstants as Constants
import com.epages.restdocs.apispec.ResourceDocumentation.parameterWithName
import com.fasterxml.jackson.databind.ObjectMapper
import java.util.Calendar
Expand Down Expand Up @@ -30,6 +29,7 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import pt.up.fe.ni.website.backend.config.upload.UploadConfigProperties
import pt.up.fe.ni.website.backend.model.Account
import pt.up.fe.ni.website.backend.model.CustomWebsite
import pt.up.fe.ni.website.backend.model.constants.AccountConstants as Constants
import pt.up.fe.ni.website.backend.repository.AccountRepository
import pt.up.fe.ni.website.backend.utils.TestUtils
import pt.up.fe.ni.website.backend.utils.ValidationTester
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package pt.up.fe.ni.website.backend.controller

import pt.up.fe.ni.website.backend.model.constants.EventConstants as Constants
import com.epages.restdocs.apispec.ResourceDocumentation.parameterWithName
import com.fasterxml.jackson.databind.ObjectMapper
import java.util.Calendar
Expand All @@ -27,6 +26,7 @@ import pt.up.fe.ni.website.backend.model.Account
import pt.up.fe.ni.website.backend.model.CustomWebsite
import pt.up.fe.ni.website.backend.model.Event
import pt.up.fe.ni.website.backend.model.constants.ActivityConstants
import pt.up.fe.ni.website.backend.model.constants.EventConstants as Constants
import pt.up.fe.ni.website.backend.model.embeddable.DateInterval
import pt.up.fe.ni.website.backend.repository.AccountRepository
import pt.up.fe.ni.website.backend.repository.EventRepository
Expand Down Expand Up @@ -143,7 +143,7 @@ internal class EventControllerTest @Autowired constructor(
null
),
null,
null,
null
),
Event(
"Mid event",
Expand All @@ -158,7 +158,7 @@ internal class EventControllerTest @Autowired constructor(
null
),
null,
"Other category",
"Other category"
),
Event(
"Cool event",
Expand All @@ -173,7 +173,7 @@ internal class EventControllerTest @Autowired constructor(
null
),
null,
"Great Events",
"Great Events"
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ internal class PostControllerTest @Autowired constructor(
}

@NestedTest
@DisplayName("POST /posts/new")
@DisplayName("POST /posts")
inner class CreatePost {
@BeforeEach
fun clearPosts() {
Expand All @@ -197,7 +197,7 @@ internal class PostControllerTest @Autowired constructor(
@Test
fun `should create a new post`() {
mockMvc.perform(
post("/posts/new")
post("/posts")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(testPost))
)
Expand All @@ -221,13 +221,13 @@ internal class PostControllerTest @Autowired constructor(

@Test
fun `should fail to create post with existing slug`() {
mockMvc.post("/posts/new") {
mockMvc.post("/posts") {
contentType = MediaType.APPLICATION_JSON
content = objectMapper.writeValueAsString(testPost)
}.andExpect { status { isOk() } }

mockMvc.perform(
post("/posts/new")
post("/posts")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(testPost))
)
Expand All @@ -246,7 +246,7 @@ internal class PostControllerTest @Autowired constructor(
private val validationTester = ValidationTester(
req = { params: Map<String, Any?> ->
mockMvc.perform(
post("/posts/new")
post("/posts")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(params))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ internal class ProjectControllerTest @Autowired constructor(
}

@NestedTest
@DisplayName("POST /projects/new")
@DisplayName("POST /projects")
inner class CreateProject {
private val uuid: UUID = UUID.randomUUID()
private val mockedSettings = Mockito.mockStatic(UUID::class.java)
Expand Down Expand Up @@ -315,7 +315,7 @@ internal class ProjectControllerTest @Autowired constructor(
)
)

mockMvc.multipartBuilder("/projects/new")
mockMvc.multipartBuilder("/projects")
.addPart("project", projectPart)
.addFile(name = "image")
.perform()
Expand Down Expand Up @@ -368,13 +368,13 @@ internal class ProjectControllerTest @Autowired constructor(
mutableListOf(testAccount)
)

mockMvc.multipartBuilder("/projects/new")
mockMvc.multipartBuilder("/projects")
.addPart("project", objectMapper.writeValueAsString(testProject))
.addFile(name = "image")
.perform()
.andExpect { status().isOk }

mockMvc.multipartBuilder("/projects/new")
mockMvc.multipartBuilder("/projects")
.addPart("project", objectMapper.writeValueAsString(duplicatedSlugProject))
.addFile(name = "image")
.perform()
Expand All @@ -389,7 +389,7 @@ internal class ProjectControllerTest @Autowired constructor(

@Test
fun `should fail to create project with invalid filename extension`() {
mockMvc.multipartBuilder("/projects/new")
mockMvc.multipartBuilder("/projects")
.addPart("project", objectMapper.writeValueAsString(testProject))
.addFile(name = "image", filename = "image.pdf")
.perform()
Expand All @@ -405,7 +405,7 @@ internal class ProjectControllerTest @Autowired constructor(

@Test
fun `should fail to create project with invalid filename media type`() {
mockMvc.multipartBuilder("/projects/new")
mockMvc.multipartBuilder("/projects")
.addPart("project", objectMapper.writeValueAsString(testProject))
.addFile(name = "image", contentType = MediaType.APPLICATION_PDF_VALUE)
.perform()
Expand All @@ -421,7 +421,7 @@ internal class ProjectControllerTest @Autowired constructor(

@Test
fun `should fail when missing project part`() {
mockMvc.multipartBuilder("/projects/new")
mockMvc.multipartBuilder("/projects")
.addFile(name = "image")
.perform()
.andExpectAll(
Expand All @@ -438,7 +438,7 @@ internal class ProjectControllerTest @Autowired constructor(
inner class InputValidation {
private val validationTester = ValidationTester(
req = { params: Map<String, Any?> ->
mockMvc.multipartBuilder("/projects/new")
mockMvc.multipartBuilder("/projects")
.addPart("project", objectMapper.writeValueAsString(params))
.addFile(name = "image")
.perform()
Expand Down Expand Up @@ -1289,7 +1289,7 @@ internal class ProjectControllerTest @Autowired constructor(
}

@NestedTest
@DisplayName("PUT /projects/{projectId}/addTeamMember/{accountId}")
@DisplayName("PUT /projects/{projectId}/team/{accountId}")
inner class AddTeamMember {

private val newAccount = Account(
Expand Down Expand Up @@ -1324,7 +1324,7 @@ internal class ProjectControllerTest @Autowired constructor(
@Test
fun `should add a team member`() {
mockMvc.perform(
put("/projects/{projectId}/addTeamMember/{accountId}", testProject.id, newAccount.id)
put("/projects/{projectId}/team/{accountId}", testProject.id, newAccount.id)
)
.andExpectAll(
status().isOk, content().contentType(MediaType.APPLICATION_JSON),
Expand Down Expand Up @@ -1358,7 +1358,7 @@ internal class ProjectControllerTest @Autowired constructor(

@Test
fun `should fail if the team member does not exist`() {
mockMvc.perform(put("/projects/{projectId}/addTeamMember/{accountId}", testProject.id, 1234))
mockMvc.perform(put("/projects/{projectId}/team/{accountId}", testProject.id, 1234))
.andExpectAll(
status().isNotFound,
content().contentType(MediaType.APPLICATION_JSON),
Expand All @@ -1373,7 +1373,7 @@ internal class ProjectControllerTest @Autowired constructor(
}

@NestedTest
@DisplayName("PUT /projects/{projectId}/removeTeamMember/{accountId}")
@DisplayName("DELETE /projects/{projectId}/team/{accountId}")
inner class RemoveTeamMember {

@BeforeEach
Expand All @@ -1392,7 +1392,7 @@ internal class ProjectControllerTest @Autowired constructor(

@Test
fun `should remove a team member`() {
mockMvc.perform(put("/projects/{projectId}/removeTeamMember/{accountId}", testProject.id, testAccount.id))
mockMvc.perform(delete("/projects/{projectId}/team/{accountId}", testProject.id, testAccount.id))
.andExpectAll(
status().isOk,
content().contentType(MediaType.APPLICATION_JSON),
Expand All @@ -1408,7 +1408,7 @@ internal class ProjectControllerTest @Autowired constructor(

@Test
fun `should fail if the team member does not exist`() {
mockMvc.perform(put("/projects/{projectId}/removeTeamMember/{accountId}", testProject.id, 1234))
mockMvc.perform(delete("/projects/{projectId}/team/{accountId}", testProject.id, 1234))
.andExpectAll(
status().isNotFound,
content().contentType(MediaType.APPLICATION_JSON),
Expand Down

0 comments on commit 80b7e39

Please sign in to comment.