Skip to content

Commit

Permalink
feat: tasks filters
Browse files Browse the repository at this point in the history
  • Loading branch information
stepan662 committed Jul 26, 2024
1 parent 2c3a278 commit 8e682cb
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.tolgee.api.v2.controllers

import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import io.tolgee.dtos.request.task.TaskFilters
import io.tolgee.hateoas.task.TaskWithProjectModel
import io.tolgee.hateoas.task.TaskWithProjectModelAssembler
import io.tolgee.model.task.Task
Expand Down Expand Up @@ -31,13 +32,15 @@ class UserTasksController(
@UseDefaultPermissions
@AllowApiAccess
fun getTasks(
@ParameterObject
filters: TaskFilters,
@ParameterObject
pageable: Pageable,
@RequestParam("search", required = false)
search: String?,
): PagedModel<TaskWithProjectModel> {
val user = authenticationFacade.authenticatedUser
val tasks = taskService.getUserTasksPaged(user.id, pageable, search)
val tasks = taskService.getUserTasksPaged(user.id, pageable, search, filters)
return pagedTaskResourcesAssembler.toModel(tasks, taskWithProjectModelAssembler)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,46 @@ package io.tolgee.dtos.request.task
import io.swagger.v3.oas.annotations.Parameter
import io.swagger.v3.oas.annotations.media.ExampleObject
import io.tolgee.model.enums.TaskState
import io.tolgee.model.enums.TaskType

open class TaskFilters {
@field:Parameter(
description = """Filter tasks with the state""",
description = """Filter tasks by state""",
)
var filterState: List<TaskState>? = null

@field:Parameter(
description = """Filter tasks without the state""",
description = """Filter tasks without state""",
)
var filterNotState: List<TaskState>? = null

@field:Parameter(
description = """Filter tasks by assignee""",
)
var filterAssignee: List<Long>? = null

@field:Parameter(
description = """Filter tasks by type""",
)
var filterType: List<TaskType>? = null

@field:Parameter(
description = """Filter tasks by id""",
)
var filterId: List<Long>? = null

@field:Parameter(
description = """Filter tasks without id""",
)
var filterNotId: List<Long>? = null

@field:Parameter(
description = """Filter tasks by project""",
)
var filterProject: List<Long>? = null

@field:Parameter(
description = """Filter tasks without project""",
)
var filterNotProject: List<Long>? = null
}
71 changes: 51 additions & 20 deletions backend/data/src/main/kotlin/io/tolgee/repository/TaskRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,59 @@ import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import org.springframework.stereotype.Repository

const val SEARCH = """
(
cast(:search as text) is null
or lower(t.name) like lower(concat('%', cast(:search as text),'%'))
)
"""

const val FILTERS = """
(
:#{#filters.filterNotState} is null
or t.state not in :#{#filters.filterNotState}
)
and (
:#{#filters.filterState} is null
or t.state in :#{#filters.filterState}
)
and (
:#{#filters.filterAssignee} is null
or u.id in :#{#filters.filterAssignee}
)
and (
:#{#filters.filterType} is null
or t.type in :#{#filters.filterType}
)
and (
:#{#filters.filterId} is null
or t.id in :#{#filters.filterId}
)
and (
:#{#filters.filterNotId} is null
or t.id not in :#{#filters.filterNotId}
)
and (
:#{#filters.filterProject} is null
or t.project.id in :#{#filters.filterProject}
)
and (
:#{#filters.filterNotProject} is null
or t.project.id not in :#{#filters.filterNotProject}
)
"""

@Repository
interface TaskRepository : JpaRepository<Task, TaskId> {
@Query(
"""
select t
from Task t
left join t.assignees u
where
t.project.id = :projectId
and (
lower(t.name) like lower(concat('%', cast(:search as text),'%'))
or cast(:search as text) is null
)
and (
cast(:#{#filters.filterNotState} as text) is null
or t.state not in :#{#filters.filterNotState}
)
and (
cast(:#{#filters.filterState} as text) is null
or t.state in :#{#filters.filterState}
)
t.project.id = :projectId
and $SEARCH
and $FILTERS
""",
)
fun getAllByProjectId(
Expand All @@ -47,17 +79,16 @@ interface TaskRepository : JpaRepository<Task, TaskId> {
select t
from Task t
left join t.assignees u
where u.id = :userId and
(
lower(t.name) like lower(concat('%', cast(:search as text),'%'))
or cast(:search as text) is null
)
where u.id = :userId
and $SEARCH
and $FILTERS
""",
)
fun getAllByAssignee(
userId: Long,
pageable: Pageable,
search: String?
search: String?,
filters: TaskFilters
): Page<Task>

@Query(
Expand Down
5 changes: 3 additions & 2 deletions backend/data/src/main/kotlin/io/tolgee/service/TaskService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ class TaskService(
fun getUserTasksPaged(
userId: Long,
pageable: Pageable,
search: String?
search: String?,
filters: TaskFilters
): Page<TaskWithScopeView> {
val pagedTasks = taskRepository.getAllByAssignee(userId, pageable, search)
val pagedTasks = taskRepository.getAllByAssignee(userId, pageable, search, filters)
val withPrefetched = taskRepository.getByIdsWithAllPrefetched(pagedTasks.content)
return PageImpl(getTasksWithScope(withPrefetched), pageable, pagedTasks.totalElements)
}
Expand Down
Loading

0 comments on commit 8e682cb

Please sign in to comment.