Skip to content

Commit

Permalink
refactor(backend/frontend): Added working tags view/edit UI
Browse files Browse the repository at this point in the history
Also
* Added and refactored more tests
* Changed DAO facade to return only DTOs
  This avoids exposed proxies errors for objetcs outside a transaction
  • Loading branch information
Grohden committed Aug 2, 2020
1 parent c11d14c commit befc3f8
Show file tree
Hide file tree
Showing 17 changed files with 602 additions and 244 deletions.
31 changes: 28 additions & 3 deletions backend/src/com/grohden/repotagger/api/Repository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import io.ktor.auth.authentication
import io.ktor.http.HttpStatusCode
import io.ktor.response.respond
import io.ktor.routing.Route
import io.ktor.routing.delete
import io.ktor.routing.get
import io.ktor.routing.route

Expand Down Expand Up @@ -61,7 +62,6 @@ fun Route.repository(dao: DAOFacade, github: GithubClient) {
val user = call.authentication.principal<User>()!!
val repos = dao
.findRepositoriesByUserTag(user.id.value, tagId)
.toDTOList()

call.respond(HttpStatusCode.OK, repos)
}
Expand All @@ -71,12 +71,12 @@ fun Route.repository(dao: DAOFacade, github: GithubClient) {
*
* returns a list of [DetailedRepository]
*/
get("/detail/{githubId}") {
get("/details/{githubId}") {
val githubId = call.parameters["githubId"]!!.toInt()
val user = call.authentication.principal<User>()!!
val taggerRepo = dao.findUserRepositoryByGithubId(user, githubId)
val tags = taggerRepo?.let {
dao.findUserTagsByRepository(user.id.value, it.id.value)
dao.findUserTagsByRepository(user.id.value, it.id)
} ?: listOf()
val githubRepo = github.repositoryById(githubId)

Expand All @@ -92,6 +92,31 @@ fun Route.repository(dao: DAOFacade, github: GithubClient) {
)
)
}

/**
* Removes a tag registry from a repository
*
* Receives a repository githubId and a userTagId
*
* Returns OK response
*/
delete("{githubId}/remove-tag/{userTagId}") {
val user = call.authentication.principal<User>()!!
val githubId = call.parameters["githubId"]!!.toInt()
val tagId = call.parameters["userTagId"]!!.toInt()

val tag = dao.findUserTagById(
tagId = tagId,
userId = user.id.value
)!!
val repo = dao.findUserRepositoryByGithubId(
user = user,
githubId = githubId
)!!

dao.removeUserTagFromRepository(tag.id, repo.id)
call.respond(HttpStatusCode.OK)
}
}
}
}
28 changes: 12 additions & 16 deletions backend/src/com/grohden/repotagger/api/UserTag.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ import io.ktor.auth.authentication
import io.ktor.http.HttpStatusCode
import io.ktor.request.receiveOrNull
import io.ktor.response.respond
import io.ktor.routing.Route
import io.ktor.routing.get
import io.ktor.routing.post
import io.ktor.routing.route
import io.ktor.routing.*

fun Route.userTag(
dao: DAOFacade,
Expand All @@ -31,19 +28,19 @@ fun Route.userTag(
*/
get("/list") {
val user = call.authentication.principal<User>()!!
val tags = dao
.findUserTags(user)
.toDTOList()
val tags = dao.findUserTags(user)

call.respond(HttpStatusCode.OK, tags)
}

/**
* FIXME: should be moved to repository
*
* Registers a user tag on a repository
*
* Receives a [CreateTagInput]
*
* Returns OK response
* Returns OK response with the newly created [UserTagDTO]
*/
post("/add") {
val input = call.receiveOrNull<CreateTagInput>()!!
Expand All @@ -62,15 +59,14 @@ fun Route.userTag(
)
}

val tag = dao.createOrFindUserTag(user, input.tagName)

dao.addUserTagInRepository(tag, repo)
call.respond(HttpStatusCode.OK)
}

post("/remove") {
val input = call.receiveOrNull<CreateTagInput>()
val tag = dao.createOrFindUserTag(user, input.tagName).also { tag ->
dao.createTagRelationToRepository(
tagId = tag.id,
repositoryId = repo.id
)
}

call.respond(HttpStatusCode.OK, tag)
}
}
}
Expand Down
Loading

0 comments on commit befc3f8

Please sign in to comment.