Skip to content

Commit

Permalink
refactor: adding pagination to teams
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsenk committed Aug 12, 2024
1 parent cbfa232 commit 2e3b6b6
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
id 'com.google.cloud.tools.jib' version '3.3.2'
}

group = 'app.workive'
group = 'app.teamwize'
version = '0.9.0'

java {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package app.teamwize.api.team.controller;

import app.teamwize.api.auth.service.SecurityService;
import app.teamwize.api.base.domain.model.request.PaginationRequest;
import app.teamwize.api.organization.exception.OrganizationNotFoundException;
import app.teamwize.api.team.domain.exception.TeamNotFoundException;
import app.teamwize.api.team.domain.request.TeamCreateRequest;
import app.teamwize.api.team.domain.response.TeamResponse;
import app.teamwize.api.team.mapper.TeamMapper;
import app.teamwize.api.team.service.TeamService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

Expand All @@ -23,8 +26,8 @@ public class TeamController {
private final SecurityService securityService;

@GetMapping
public List<TeamResponse> getTeams() {
return teamMapper.toTeamResponseList(teamService.getTeams(securityService.getUserOrganizationId()));
public List<TeamResponse> getTeams(@ParameterObject @Valid PaginationRequest pagination) {
return teamMapper.toTeamResponseList(teamService.getTeams(securityService.getUserOrganizationId(),pagination));
}

@PostMapping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import app.teamwize.api.team.domain.TeamStatus;
import app.teamwize.api.team.domain.entity.Team;
import io.hypersistence.utils.spring.repository.BaseJpaRepository;
import org.springframework.data.domain.PageRequest;

import java.util.List;
import java.util.Optional;
Expand All @@ -15,5 +16,5 @@ public interface TeamRepository extends BaseJpaRepository<Team, Long> {

Optional<Team> findByOrganizationIdAndId(Long organizationId, Long id);

List<Team> findByOrganizationIdAndStatusIsIn(Long organizationId, List<TeamStatus> statuses);
List<Team> findByOrganizationIdAndStatusIsIn(Long organizationId, List<TeamStatus> statuses, PageRequest page);
}
9 changes: 7 additions & 2 deletions src/main/java/app/teamwize/api/team/service/TeamService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package app.teamwize.api.team.service;

import app.teamwize.api.base.domain.model.request.PaginationRequest;
import app.teamwize.api.organization.service.OrganizationService;
import app.teamwize.api.team.repository.TeamRepository;
import app.teamwize.api.organization.exception.OrganizationNotFoundException;
Expand All @@ -8,6 +9,8 @@
import app.teamwize.api.team.domain.exception.TeamNotFoundException;
import app.teamwize.api.team.domain.request.TeamCreateRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -20,8 +23,10 @@ public class TeamService {
private final TeamRepository teamRepository;
private final OrganizationService organizationService;

public List<Team> getTeams(Long organizationId) {
return teamRepository.findByOrganizationIdAndStatusIsIn(organizationId,List.of(TeamStatus.DEFAULT));
public List<Team> getTeams(Long organizationId, PaginationRequest pagination) {
var sort = Sort.by("id").descending();
var pageRequest = PageRequest.of(pagination.getPageNumber(), pagination.getPageSize(), sort);
return teamRepository.findByOrganizationIdAndStatusIsIn(organizationId,List.of(TeamStatus.DEFAULT),pageRequest);
}

@Transactional
Expand Down

0 comments on commit 2e3b6b6

Please sign in to comment.