diff --git a/src/main/kotlin/ingsis/snippetmanager/domains/test/controller/TestController.kt b/src/main/kotlin/ingsis/snippetmanager/domains/test/controller/TestController.kt index 3ad1bef..8631ff4 100644 --- a/src/main/kotlin/ingsis/snippetmanager/domains/test/controller/TestController.kt +++ b/src/main/kotlin/ingsis/snippetmanager/domains/test/controller/TestController.kt @@ -61,4 +61,11 @@ class TestController { return ResponseEntity(testService.getTestsByUser("auth0|"+id), HttpStatus.OK) } + // get test by id + @GetMapping("/test/{id}") + @ResponseBody + fun getTestById(principal: Principal, @PathVariable id: UUID): ResponseEntity { + return ResponseEntity(testService.getTestById(id, principal.name), HttpStatus.OK) + } + } \ No newline at end of file diff --git a/src/main/kotlin/ingsis/snippetmanager/domains/test/dto/TestDTO.kt b/src/main/kotlin/ingsis/snippetmanager/domains/test/dto/TestDTO.kt index bcf13b2..fed1c15 100644 --- a/src/main/kotlin/ingsis/snippetmanager/domains/test/dto/TestDTO.kt +++ b/src/main/kotlin/ingsis/snippetmanager/domains/test/dto/TestDTO.kt @@ -1,5 +1,6 @@ package ingsis.snippetmanager.domains.test.dto +import ingsis.snippetmanager.domains.snippet.dto.SnippetDTO import ingsis.snippetmanager.domains.test.model.Test import java.util.* @@ -11,6 +12,7 @@ class TestDTO { var snippetId: UUID? = null var id: UUID? = null var ownerId: String? = null + var snippet: SnippetDTO? = null constructor(description: String?, input: List, output: List, snippetId: UUID?, id: UUID?, ownerId: String?) { this.description = description @@ -21,6 +23,8 @@ class TestDTO { this.ownerId = ownerId } + + constructor(test: Test){ this.description = test.description this.input = parseStringToList(test.input!!) @@ -28,8 +32,10 @@ class TestDTO { this.snippetId = test.snippet!!.id this.id = test.id this.ownerId = test.ownerId + this.snippet = SnippetDTO(test.snippet!!) } + private fun parseStringToList(string: String): List { return string.substring(1,string.length-1).split(",").map { it.trim() } } diff --git a/src/main/kotlin/ingsis/snippetmanager/domains/test/model/Test.kt b/src/main/kotlin/ingsis/snippetmanager/domains/test/model/Test.kt index d18f78c..867557e 100644 --- a/src/main/kotlin/ingsis/snippetmanager/domains/test/model/Test.kt +++ b/src/main/kotlin/ingsis/snippetmanager/domains/test/model/Test.kt @@ -44,20 +44,6 @@ class Test { // Default constructor required by JPA } - constructor( - description: String?, - ownerId: String?, - input: String?, - output: String?, - snippetId: UUID? - ) { - this.description = description - this.createdAt = Date() - this.ownerId = ownerId - this.input = input - this.output = output - this.snippet = Snippet().apply { id = snippetId } - } constructor( description: String?, diff --git a/src/main/kotlin/ingsis/snippetmanager/domains/test/service/TestService.kt b/src/main/kotlin/ingsis/snippetmanager/domains/test/service/TestService.kt index 3a8fd95..fc87e11 100644 --- a/src/main/kotlin/ingsis/snippetmanager/domains/test/service/TestService.kt +++ b/src/main/kotlin/ingsis/snippetmanager/domains/test/service/TestService.kt @@ -9,4 +9,5 @@ interface TestService { fun updateTest(test: TestDTO, userId: String): TestDTO fun deleteTest(id: UUID, userId: String) fun getTestsByUser(id: String): List + fun getTestById(id: UUID, userId: String?): TestDTO } \ No newline at end of file diff --git a/src/main/kotlin/ingsis/snippetmanager/domains/test/service/TestServiceImpl.kt b/src/main/kotlin/ingsis/snippetmanager/domains/test/service/TestServiceImpl.kt index ec97e6e..1ec8b04 100644 --- a/src/main/kotlin/ingsis/snippetmanager/domains/test/service/TestServiceImpl.kt +++ b/src/main/kotlin/ingsis/snippetmanager/domains/test/service/TestServiceImpl.kt @@ -36,7 +36,7 @@ class TestServiceImpl : TestService{ ownerId = userId, input = testDTO.input.toString(), output = testDTO.output.toString(), - snippetId = testDTO.snippetId + snippet = snippet.get() ) return TestDTO(testRepository.save(test)) @@ -56,4 +56,10 @@ class TestServiceImpl : TestService{ override fun getTestsByUser(id: String): List { return testRepository.findByOwnerId(id).map { TestDTO(it) } } + + override fun getTestById(id: UUID, userId: String?): TestDTO { + val test = testRepository.findById(id) + if (test.get().ownerId == userId) return TestDTO(test.get()) + throw HTTPError("User must own test to edit it", HttpStatus.FORBIDDEN) + } } \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index dac4a9b..c5ee658 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,10 +1,10 @@ -#spring.datasource.url=jdbc:postgresql://${DB_HOST}:${DB_PORT}/${DB_NAME} -#spring.datasource.username=${DB_USER} -#spring.datasource.password=${DB_PASSWORD} -spring.datasource.url=jdbc:postgresql://localhost:5433/db -spring.datasource.username=postgres -spring.datasource.password=postgres -spring.jpa.hibernate.ddl-auto= create +spring.datasource.url=jdbc:postgresql://${DB_HOST}:${DB_PORT}/${DB_NAME} +spring.datasource.username=${DB_USER} +spring.datasource.password=${DB_PASSWORD} +#spring.datasource.url=jdbc:postgresql://localhost:5433/db +#spring.datasource.username=postgres +#spring.datasource.password=postgres +spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=false spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect spring.jpa.properties.hibernate.format_sql=false