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

Pivotal #ID 176479707: Mongo implement draft funtionality #327

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -1,7 +1,7 @@
package ac.uk.ebi.biostd.client.api

import ac.uk.ebi.biostd.client.integration.web.DraftSubmissionOperations
import ebi.ac.uk.model.SubmissionDraft
import ebi.ac.uk.model.WebSubmissionDraft
import org.springframework.web.client.RestTemplate
import org.springframework.web.client.getForObject
import org.springframework.web.client.postForObject
Expand All @@ -10,14 +10,14 @@ import org.springframework.web.util.UriComponentsBuilder
private const val SUBMISSION_DRAFT_URL = "/submissions/drafts"

class SubmissionDraftClient(private val template: RestTemplate) : DraftSubmissionOperations {
override fun createSubmissionDraft(content: String): SubmissionDraft =
override fun createSubmissionDraft(content: String): WebSubmissionDraft =
template.postForObject(SUBMISSION_DRAFT_URL, content)!!

override fun getSubmissionDraft(accNo: String): SubmissionDraft =
override fun getSubmissionDraft(accNo: String): WebSubmissionDraft =
template.getForObject("$SUBMISSION_DRAFT_URL/$accNo")!!

override fun getAllSubmissionDrafts(limit: Int, offset: Int): List<SubmissionDraft> =
template.getForObject<Array<SubmissionDraft>>(buildDraftsUrl(limit, offset)).orEmpty().toList()
override fun getAllSubmissionDrafts(limit: Int, offset: Int): List<WebSubmissionDraft> =
template.getForObject<Array<WebSubmissionDraft>>(buildDraftsUrl(limit, offset)).orEmpty().toList()

override fun deleteSubmissionDraft(accNo: String) = template.delete("$SUBMISSION_DRAFT_URL/$accNo")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import ebi.ac.uk.extended.model.ExtSubmission
import ebi.ac.uk.model.Group
import ebi.ac.uk.model.Project
import ebi.ac.uk.model.Submission
import ebi.ac.uk.model.SubmissionDraft
import ebi.ac.uk.model.WebSubmissionDraft
import java.io.File

interface SubmissionClient :
Expand Down Expand Up @@ -88,11 +88,11 @@ interface GeneralOperations {
}

interface DraftSubmissionOperations {
fun getAllSubmissionDrafts(limit: Int = 15, offset: Int = 0): List<SubmissionDraft>
fun getSubmissionDraft(accNo: String): SubmissionDraft
fun getAllSubmissionDrafts(limit: Int = 15, offset: Int = 0): List<WebSubmissionDraft>
fun getSubmissionDraft(accNo: String): WebSubmissionDraft
fun deleteSubmissionDraft(accNo: String)
fun updateSubmissionDraft(accNo: String, content: String)
fun createSubmissionDraft(content: String): SubmissionDraft
fun createSubmissionDraft(content: String): WebSubmissionDraft
}

interface ExtSubmissionOperations {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package ebi.ac.uk.model

import com.fasterxml.jackson.databind.JsonNode

class SubmissionDraft(val key: String, val content: JsonNode)
class WebSubmissionDraft(val key: String, val content: JsonNode)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ac.uk.ebi.biostd.persistence.common.service

import ac.uk.ebi.biostd.persistence.common.request.PaginationFilter
import ebi.ac.uk.model.SubmissionDraft

interface SubmissionDraftService {

fun getSubmissionDraft(userId: Long, key: String): SubmissionDraft

fun updateSubmissionDraft(userId: Long, key: String, content: String): SubmissionDraft

fun deleteSubmissionDraft(userId: Long, key: String)

fun getSubmissionsDraft(userId: Long, filter: PaginationFilter = PaginationFilter()): List<SubmissionDraft>

fun createSubmissionDraft(userId: Long, content: String): SubmissionDraft
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package ebi.ac.uk.model

class SubmissionDraft(val key: String, val content: String)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ac.uk.ebi.biostd.itest.common.BaseIntegrationTest
import ac.uk.ebi.biostd.itest.common.SecurityTestService
import ac.uk.ebi.biostd.itest.entities.SuperUser
import ebi.ac.uk.dsl.json.jsonObj
import ebi.ac.uk.model.SubmissionDraft
import ebi.ac.uk.model.WebSubmissionDraft
import ebi.ac.uk.util.collections.second
import ebi.ac.uk.util.collections.third
import io.github.glytching.junit.extension.folder.TemporaryFolder
Expand Down Expand Up @@ -36,7 +36,7 @@ internal class SubmissionDraftListApiTest(tempFolder: TemporaryFolder) : BaseInt
private var serverPort: Int = 0

private lateinit var webClient: BioWebClient
private lateinit var testDrafts: List<SubmissionDraft>
private lateinit var testDrafts: List<WebSubmissionDraft>

@BeforeAll
fun init() {
Expand Down Expand Up @@ -72,8 +72,8 @@ internal class SubmissionDraftListApiTest(tempFolder: TemporaryFolder) : BaseInt
assertDraft(testDrafts.third().key, "ABC-2", page2.first())
}

private fun createDrafts(): List<SubmissionDraft> {
val drafts = mutableListOf<SubmissionDraft>()
private fun createDrafts(): List<WebSubmissionDraft> {
val drafts = mutableListOf<WebSubmissionDraft>()

for (idx in 0..2) {
val pageTab = jsonObj {
Expand All @@ -87,7 +87,7 @@ internal class SubmissionDraftListApiTest(tempFolder: TemporaryFolder) : BaseInt
return drafts.toList()
}

private fun assertDraft(key: String, accNo: String, draft: SubmissionDraft) {
private fun assertDraft(key: String, accNo: String, draft: WebSubmissionDraft) {
assertThat(draft.key).isEqualTo(key)
assertThat(draft.content.toString().contains(accNo)).isTrue()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ac.uk.ebi.biostd.data

import ac.uk.ebi.biostd.data.service.SubmissionDraftService
import ac.uk.ebi.biostd.data.service.SubmissionSqlDraftService
import ac.uk.ebi.biostd.data.service.UserDataService
import ac.uk.ebi.biostd.persistence.repositories.UserDataDataRepository
import ac.uk.ebi.biostd.submission.domain.service.SubmissionService
Expand All @@ -13,7 +13,7 @@ class UserDataConfig(
private val submissionService: SubmissionService
) {
@Bean
fun tmpSubService() = SubmissionDraftService(userDataService(), submissionService)
fun tmpSubService() = SubmissionSqlDraftService(userDataService(), submissionService)

@Bean
fun userDataService() = UserDataService(userDataDataRepository)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package ac.uk.ebi.biostd.data.service

import ac.uk.ebi.biostd.persistence.common.request.PaginationFilter
import ac.uk.ebi.biostd.persistence.common.service.SubmissionDraftService
import ac.uk.ebi.biostd.persistence.model.DbUserData
import ac.uk.ebi.biostd.submission.domain.service.SubmissionService
import ebi.ac.uk.model.SubmissionDraft
import org.springframework.transaction.annotation.Transactional
import java.time.Instant

open class SubmissionSqlDraftService(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest the name be SubmissionDraftSqlService since SubmissionDraft is the object in question and adding the "Sql" in the middle kind of breaks its meaning

private val userDataService: UserDataService,
private val submissionService: SubmissionService
) : SubmissionDraftService {

@Transactional(readOnly = true)
override fun getSubmissionDraft(userId: Long, key: String): SubmissionDraft {
val dbUser = userDataService.getUserData(userId, key) ?: create(userId, key)
return SubmissionDraft(dbUser.key, dbUser.data)
}

@Transactional
override fun updateSubmissionDraft(userId: Long, key: String, content: String): SubmissionDraft {
val dbUser = userDataService.saveUserData(userId, key, content)
return SubmissionDraft(dbUser.key, dbUser.data)
}

@Transactional
override fun deleteSubmissionDraft(userId: Long, key: String): Unit = userDataService.delete(userId, key)

@Transactional(readOnly = true)
override fun getSubmissionsDraft(userId: Long, filter: PaginationFilter): List<SubmissionDraft> =
userDataService.findAll(userId, filter).map { SubmissionDraft(it.key, it.data) }

@Transactional
override fun createSubmissionDraft(userId: Long, content: String): SubmissionDraft {
val dbUser = userDataService.saveUserData(userId, "TMP_${Instant.now().toEpochMilli()}", content)
return SubmissionDraft(dbUser.key, dbUser.data)
}

private fun create(userId: Long, key: String): DbUserData {
val submission = submissionService.getSubmissionAsJson(key)
return userDataService.saveUserData(userId, key, submission)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package ac.uk.ebi.biostd.data.web

import ac.uk.ebi.biostd.data.service.SubmissionDraftService
import ac.uk.ebi.biostd.data.service.SubmissionSqlDraftService
import ac.uk.ebi.biostd.persistence.common.request.PaginationFilter
import ac.uk.ebi.biostd.submission.converters.BioUser
import com.fasterxml.jackson.annotation.JsonRawValue
import com.fasterxml.jackson.annotation.JsonValue
import ebi.ac.uk.model.SubmissionDraft
import ebi.ac.uk.security.integration.model.api.SecurityUser
import io.swagger.annotations.Api
import io.swagger.annotations.ApiImplicitParam
Expand All @@ -28,47 +29,51 @@ import org.springframework.web.bind.annotation.RestController
@RequestMapping(value = ["submissions/drafts"], produces = [APPLICATION_JSON_VALUE])
@PreAuthorize("isAuthenticated()")
@Api(tags = ["Submission Drafts"])
internal class SubmissionDraftResource(private val subDraftService: SubmissionDraftService) {
internal class SubmissionDraftResource(private val subDraftService: SubmissionSqlDraftService) {
@GetMapping
@ResponseBody
@ApiOperation("Get the submission drafts that matches the given filter")
@ApiImplicitParams(value = [
ApiImplicitParam(name = "X-Session-Token", value = "User authentication token", required = true),
ApiImplicitParam(
name = "limit",
value = "Optional query parameter used to set the maximum amount of drafts in the response"),
ApiImplicitParam(
name = "offset",
value = "Optional query parameter used to indicate from which submission should the response start")
])
@ApiImplicitParams(
value = [
ApiImplicitParam(name = "X-Session-Token", value = "User authentication token", required = true),
ApiImplicitParam(
name = "limit",
value = "Optional query parameter used to set the maximum amount of drafts in the response"
),
ApiImplicitParam(
name = "offset",
value = "Optional query parameter used to indicate from which submission should the response start"
)
]
)
fun getDraftSubmissions(
@BioUser user: SecurityUser,
@ModelAttribute filter: PaginationFilter
): List<SubmissionDraft> =
subDraftService.getSubmissionsDraft(user.id, filter).map { SubmissionDraft(it.key, it.data) }
): List<ResponseSubmissionDraft> = subDraftService.getSubmissionsDraft(user.id, filter).map { it.asResponseDraft() }

@GetMapping("/{key}")
@ResponseBody
@ApiOperation("Get the submission drafts that matches the given key and filter")
@ApiImplicitParams(value = [
ApiImplicitParam(name = "X-Session-Token", value = "User authentication token", required = true),
ApiImplicitParam(
name = "limit",
value = "Optional query parameter used to set the maximum amount of drafts in the response"),
ApiImplicitParam(
name = "offset",
value = "Optional query parameter used to indicate from which submission should the response start")
])
@ApiImplicitParams(
value = [
ApiImplicitParam(name = "X-Session-Token", value = "User authentication token", required = true),
ApiImplicitParam(
name = "limit",
value = "Optional query parameter used to set the maximum amount of drafts in the response"
),
ApiImplicitParam(
name = "offset",
value = "Optional query parameter used to indicate from which submission should the response start"
)
]
)
fun getDraftSubmission(
@BioUser user: SecurityUser,
@ModelAttribute filter: PaginationFilter,

@ApiParam(name = "Key", value = "The submission draft key")
@PathVariable key: String
): SubmissionDraft {
val draft = subDraftService.getSubmissionDraft(user.id, key)
return SubmissionDraft(draft.key, draft.data)
}
): ResponseSubmissionDraft = subDraftService.getSubmissionDraft(user.id, key).asResponseDraft()

@GetMapping("/{key}/content")
@ResponseBody
Expand All @@ -79,7 +84,8 @@ internal class SubmissionDraftResource(private val subDraftService: SubmissionDr

@ApiParam(name = "Key", value = "The submission draft key")
@PathVariable key: String
): SubmissionDraftContent = SubmissionDraftContent(subDraftService.getSubmissionDraft(user.id, key).data)
): ResponseSubmissionDraftContent =
ResponseSubmissionDraftContent(subDraftService.getSubmissionDraft(user.id, key).content)

@DeleteMapping("/{key}")
@ApiOperation("Delete the submission draft with the given key")
Expand All @@ -103,9 +109,7 @@ internal class SubmissionDraftResource(private val subDraftService: SubmissionDr

@ApiParam(name = "Key", value = "The submission draft key")
@PathVariable key: String
) {
subDraftService.updateSubmissionDraft(user.id, key, content)
}
): ResponseSubmissionDraft = subDraftService.updateSubmissionDraft(user.id, key, content).asResponseDraft()

@PostMapping
@ResponseBody
Expand All @@ -116,11 +120,10 @@ internal class SubmissionDraftResource(private val subDraftService: SubmissionDr

@ApiParam(name = "Content", value = "The content for the submission draft")
@RequestBody content: String
): SubmissionDraft {
val draft = subDraftService.createSubmissionDraft(user.id, content)
return SubmissionDraft(draft.key, draft.data)
}
): ResponseSubmissionDraft = subDraftService.createSubmissionDraft(user.id, content).asResponseDraft()
}

internal class SubmissionDraft(val key: String, @JsonRawValue val content: String)
internal class SubmissionDraftContent(@JsonRawValue @JsonValue val value: String)
internal class ResponseSubmissionDraft(val key: String, @JsonRawValue val content: String)
internal class ResponseSubmissionDraftContent(@JsonRawValue @JsonValue val value: String)

internal fun SubmissionDraft.asResponseDraft() = ResponseSubmissionDraft(key, content)
Loading