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

Added test to shared snippets #11

Merged
merged 1 commit into from
Jul 14, 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 @@ -7,15 +7,7 @@ import ingsis.snippetmanager.domains.test.service.TestService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.CrossOrigin
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.*
import java.security.Principal
import java.util.*

Expand All @@ -33,8 +25,8 @@ class TestController {

@PostMapping("/test")
@ResponseBody
fun createTest(principal: Principal, @RequestBody testDto: CreateTestDTO): ResponseEntity<TestDTO> {
return ResponseEntity(testService.createTest(testDto, principal.name), HttpStatus.CREATED)
fun createTest(@RequestHeader("Authorization") token: String, principal: Principal, @RequestBody testDto: CreateTestDTO): ResponseEntity<TestDTO> {
return ResponseEntity(testService.createTest(token, testDto, principal.name), HttpStatus.CREATED)
}

@PutMapping("/test")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ingsis.snippetmanager.domains.test.dto.TestDTO
import java.util.*

interface TestService {
fun createTest(testDTO: CreateTestDTO, userId: String): TestDTO
fun createTest(token: String, testDTO: CreateTestDTO, userId: String): TestDTO
fun updateTest(test: TestDTO, userId: String): TestDTO
fun deleteTest(id: UUID, userId: String)
fun getTestsByUser(id: String): List<TestDTO>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ingsis.snippetmanager.domains.test.dto.TestDTO
import ingsis.snippetmanager.domains.test.model.Test
import ingsis.snippetmanager.domains.test.repository.TestRepository
import ingsis.snippetmanager.error.HTTPError
import ingsis.snippetmanager.service.ShareSnippetService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service
Expand All @@ -25,11 +26,11 @@ class TestServiceImpl : TestService{
this.snippetRepository = snippetRepository
}

override fun createTest(testDTO: CreateTestDTO, userId: String): TestDTO {
override fun createTest(token: String, testDTO: CreateTestDTO, userId: String): TestDTO {

val snippet = this.snippetRepository.findById(testDTO.snippetId!!)

if (snippet.get().ownerId != userId) throw HTTPError("User must own snippet to create a test", HttpStatus.FORBIDDEN)
val sharedIds = ShareSnippetService.getSharedWithMeSnippetsIds(token)
if (snippet.get().ownerId != userId && !sharedIds.contains(testDTO.snippetId)) throw HTTPError("User must have access to snippet to create a test", HttpStatus.FORBIDDEN)

val test = Test(
description = testDTO.description,
Expand Down