Skip to content

Commit

Permalink
정렬 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
mintaek22 committed Dec 31, 2023
1 parent 3a8a032 commit 28ef431
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ public ApiResponse<ResGuestDto> getGuest(@PathVariable @Schema(description = "Gu

@Operation(summary = "모든 게스트 정보 조회")
@GetMapping("/guests")
public ApiResponse<List<ResGuestSimpleDto>> getWorkerList() {
public ApiResponse<List<ResGuestSimpleDto>> getWorkerList(@RequestParam(name = "search",defaultValue = "") String name,
@RequestParam(name = "sort", defaultValue = "0") int sort,
@RequestParam(name = "dir", defaultValue = "1") int dir) {

List<Guest> guestList = guestService.findAllGuestAndWharf();

List<ResGuestSimpleDto> resGuestSimpleDtoList = guestList.stream().map(ResGuestSimpleDto::new).toList();
List<ResGuestSimpleDto> resGuestSimpleDtoList = guestService.findAllGuestAndWharf(name,sort,dir).stream().map(ResGuestSimpleDto::new).toList();

return successResponse(resGuestSimpleDtoList);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,30 @@
@Tag(name = "출입관리 API")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/person")
@RequestMapping("/api/worker")
public class StatusController {

private final StatusService statusService;

@Operation(summary = "들어온 시간 등록")
@PostMapping("/{id}/enter/{wharfId}")
public ApiResponse<?> personEnter(@PathVariable Long id, @PathVariable Long wharfId){
statusService.registerPersonEnter(id, wharfId);
public ApiResponse<?> workerEnter(@PathVariable Long id, @PathVariable Long wharfId){
statusService.registerWorkerEnter(id, wharfId);
return successResponseNoContent();
}

@Operation(summary = "퇴장 시간 등록")
@PostMapping("/{id}/out/{wharfId}")
public ApiResponse<?> personOut(@PathVariable Long id,@PathVariable Long wharfId){
statusService.registerPersonOut(id, wharfId);
public ApiResponse<?> workerOut(@PathVariable Long id,@PathVariable Long wharfId){
statusService.registerWorkerOut(id, wharfId);
return successResponseNoContent();
}

@Operation(summary = "부두내 인원 조회")
@Operation(summary = "부두내 직원 조회")
@GetMapping("/wharf/{wharfId}")
public ApiResponse<List<ResStatusDto>> personInWharf(@PathVariable Long wharfId){

List<ResStatusDto> statues;

//wharfId가 없으면 부두내에 있는 인원 모두 조회
if (wharfId == null) {
statues = statusService.getAllWorkerInWharf();
}
//wharfId가 있으면 해당 부두내에 있는 인원만 조회
else{
statues = statusService.getWorkerInWharf(wharfId);
}

return successResponse(statues);
List<ResStatusDto> resStatusDtoList = statusService.findWorkerInWharf(wharfId).stream().map(ResStatusDto::new).toList();
return successResponse(resStatusDtoList);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public ApiResponse<ResWorkerDto> getWorker(@PathVariable @Schema(description = "

@Operation(summary = "모든 직원 정보 조회")
@GetMapping("/workers")
public ApiResponse<List<ResWorkerSimpleDto>> getWorkerList() {
public ApiResponse<List<ResWorkerSimpleDto>> getWorkerList(@RequestParam(name = "search",defaultValue = "") String name,
@RequestParam(name = "sort", defaultValue = "0") int sort,
@RequestParam(name = "dir", defaultValue = "1") int dir) {

List<Worker> workerList = workerService.findAllWorkerAndWharf();

List<ResWorkerSimpleDto> resWorkerSimpleDtoList = workerList.stream().map(ResWorkerSimpleDto::new).toList();
List<ResWorkerSimpleDto> resWorkerSimpleDtoList = workerService.findAllWorkerAndWharf(name,sort,dir).stream().map(ResWorkerSimpleDto::new).toList();

return successResponse(resWorkerSimpleDtoList);
}
Expand Down Expand Up @@ -87,15 +87,4 @@ public ApiResponse<?> deleteWorkerById(@PathVariable @Schema(description = "Work
return successResponseNoContent();
}


//직원들 조회


// //직원 이름으로 조회
// @GetMapping("/worker/search")
// public ResponseEntity<List<ResWorkerDto>> searchPersonsByName(@RequestParam String name) {
// List<ResWorkerDto> workers = workerService.searchWorkerByName(name);
// return ResponseEntity.ok(workers);
// }

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.Han2m.portLogistics.user.repository;

import com.Han2m.portLogistics.user.domain.Guest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
Expand All @@ -11,8 +12,8 @@
@Repository
public interface GuestRepository extends JpaRepository<Guest, Long> {

@Query("select g from Guest g join fetch g.permissionList p join fetch p.wharf")
List<Guest> findAllGuestWithWharf();
@Query("select g from Guest g join fetch g.permissionList p join fetch p.wharf WHERE (:name IS NULL OR g.name LIKE %:name%)")
List<Guest> findAllGuestWithWharf(String name,Sort sort);

@Query("select g from Guest g join fetch g.permissionList p join fetch g.worker join fetch p.wharf where g.personId = :personId")
Optional<Guest> findById(Long personId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

@Repository
public interface StatusRepository extends JpaRepository<Status, Long> {
List<Status> findByOutTimeIsNull();

List<Status> findByOutTimeIsNullAndWharfWharfId(Long wharfId);
@Query("SELECT s FROM Status s WHERE s.person = :person AND s.wharf = :wharf AND s.outTime IS NULL")
Optional<Status> findByPersonAndWharf(Person person, Wharf wharf);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.Han2m.portLogistics.user.repository;

import com.Han2m.portLogistics.user.domain.Worker;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
Expand All @@ -12,8 +13,8 @@
@Repository
public interface WorkerRepository extends JpaRepository<Worker, Long> {

@Query("SELECT w FROM Worker w JOIN FETCH w.permissionList p JOIN FETCH p.wharf")
List<Worker> findAllWorkerWithWharf();
@Query("SELECT w FROM Worker w JOIN FETCH w.permissionList p JOIN FETCH p.wharf WHERE (:name IS NULL OR w.name LIKE %:name%)")
List<Worker> findAllWorkerWithWharf(String name,Sort sort);

@Query("SELECT w FROM Worker w JOIN FETCH w.permissionList p JOIN FETCH p.wharf WHERE w.personId = :personId")
Optional<Worker> findById(@Param("personId") Long personId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.Han2m.portLogistics.user.repository.GuestRepository;
import com.Han2m.portLogistics.user.repository.WharfRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -33,8 +34,13 @@ public void deleteGuest(Long personId) {
guestRepository.delete(guestRepository.findById(personId).orElseThrow(EntityNotFoundException::new));
}

public List<Guest> findAllGuestAndWharf(){
return guestRepository.findAllGuestWithWharf();
public List<Guest> findAllGuestAndWharf(String name,int sort,int dir){
Sort.Direction direction = (dir == 1) ? Sort.Direction.ASC : Sort.Direction.DESC;

String sortField = (sort == 0) ? "name" : "position";
Sort dynamicSort = Sort.by(direction, sortField);

return guestRepository.findAllGuestWithWharf(name,dynamicSort);
}

public Guest registerGuest(ReqGuestDto reqGuestDto) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.Han2m.portLogistics.user.service;

import com.Han2m.portLogistics.exception.EntityNotFoundException;
import com.Han2m.portLogistics.user.domain.Person;
import com.Han2m.portLogistics.user.repository.PersonRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class PersonService {

private final PersonRepository personRepository;

public Person find(Long personId){
return personRepository.findById(personId).orElseThrow(EntityNotFoundException::new);
}

}
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
package com.Han2m.portLogistics.user.service;


import com.Han2m.portLogistics.exception.EntityNotFoundException;
import com.Han2m.portLogistics.user.dto.res.ResStatusDto;
import com.Han2m.portLogistics.user.domain.Person;
import com.Han2m.portLogistics.user.domain.Status;
import com.Han2m.portLogistics.user.domain.Wharf;
import com.Han2m.portLogistics.user.repository.PersonRepository;
import com.Han2m.portLogistics.user.repository.StatusRepository;
import com.Han2m.portLogistics.user.repository.WharfRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Transactional
public class StatusService {

private final StatusRepository statusRepository;
private final PersonRepository personRepository;
private final WharfRepository wharfRepository;
private final PersonService personService;
private final WharfService wharfService;

public void registerPersonEnter(Long id, Long wharfId){
Person person = personRepository.findById(id).orElseThrow(EntityNotFoundException::new);
Wharf wharf = wharfRepository.findById(wharfId).orElseThrow(EntityNotFoundException::new);
public void registerWorkerEnter(Long id, Long wharfId){
Person person = personService.find(id);
Wharf wharf = wharfService.find(wharfId);

// 입장 시간 (현재 시간)
Timestamp currentTime = new Timestamp(System.currentTimeMillis());
Expand All @@ -50,9 +45,9 @@ public void registerPersonEnter(Long id, Long wharfId){
}
}

public void registerPersonOut(Long id, Long wharfId){
Person person = personRepository.findById(id).orElseThrow(EntityNotFoundException::new);
Wharf wharf = wharfRepository.findById(wharfId).orElseThrow(EntityNotFoundException::new);
public void registerWorkerOut(Long id, Long wharfId){
Person person = personService.find(id);
Wharf wharf = wharfService.find(wharfId);

Optional<Status> status = statusRepository.findByPersonAndWharf(person, wharf);
if(status.isPresent()){
Expand All @@ -63,14 +58,7 @@ public void registerPersonOut(Long id, Long wharfId){


@Transactional(readOnly = true)
public List<ResStatusDto> getAllWorkerInWharf(){
List<Status> statuses = statusRepository.findByOutTimeIsNull();
return statuses.stream().map(ResStatusDto::new).collect(Collectors.toList());
}

@Transactional(readOnly = true)
public List<ResStatusDto> getWorkerInWharf(Long wharfId){
List<Status> statuses = statusRepository.findByOutTimeIsNullAndWharfWharfId(wharfId);
return statuses.stream().map(ResStatusDto::new).collect(Collectors.toList());
public List<Status> findWorkerInWharf(Long wharfId){
return statusRepository.findByOutTimeIsNullAndWharfWharfId(wharfId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.Han2m.portLogistics.user.repository.WorkerRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -32,8 +33,14 @@ public Worker find(Long personId) {
}

@Transactional(readOnly = true)
public List<Worker> findAllWorkerAndWharf() {
return workerRepository.findAllWorkerWithWharf();
public List<Worker> findAllWorkerAndWharf(String name,int sort,int dir) {

Sort.Direction direction = (dir == 1) ? Sort.Direction.ASC : Sort.Direction.DESC;

String sortField = (sort == 0) ? "name" : "position";
Sort dynamicSort = Sort.by(direction, sortField);

return workerRepository.findAllWorkerWithWharf(name,dynamicSort);
}

public void deleteWorker(Long personId) {
Expand Down

0 comments on commit 28ef431

Please sign in to comment.