Skip to content

Commit

Permalink
Implemented snippet test
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardolopezb committed Jul 8, 2023
1 parent 672163f commit eaed1a2
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<TestDTO> {
return ResponseEntity(testService.getTestById(id, principal.name), HttpStatus.OK)
}

}
Original file line number Diff line number Diff line change
@@ -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.*

Expand All @@ -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<String?>, output: List<String?>, snippetId: UUID?, id: UUID?, ownerId: String?) {
this.description = description
Expand All @@ -21,15 +23,19 @@ class TestDTO {
this.ownerId = ownerId
}



constructor(test: Test){
this.description = test.description
this.input = parseStringToList(test.input!!)
this.output = parseStringToList(test.output!!)
this.snippetId = test.snippet!!.id
this.id = test.id
this.ownerId = test.ownerId
this.snippet = SnippetDTO(test.snippet!!)
}


private fun parseStringToList(string: String): List<String?> {
return string.substring(1,string.length-1).split(",").map { it.trim() }
}
Expand Down
14 changes: 0 additions & 14 deletions src/main/kotlin/ingsis/snippetmanager/domains/test/model/Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ interface TestService {
fun updateTest(test: TestDTO, userId: String): TestDTO
fun deleteTest(id: UUID, userId: String)
fun getTestsByUser(id: String): List<TestDTO>
fun getTestById(id: UUID, userId: String?): TestDTO
}
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -56,4 +56,10 @@ class TestServiceImpl : TestService{
override fun getTestsByUser(id: String): List<TestDTO> {
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)
}
}
14 changes: 7 additions & 7 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit eaed1a2

Please sign in to comment.