[Project] HR Project

dong's memory·2024년 3월 26일

Spring

목록 보기
8/11

[인사 관리 시스템 생성 코드 리뷰]

  • 추후 코드 공부를 위한 정리

  • 연차 신청(직원용)

    Entity

    @Entity
    		@Data
    		public class AnnualList {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "annual_list_id")
    private Long id;
    
    private String annualYear;
    
    @Temporal(TemporalType.DATE)
    private Date startDate;
    
    @Temporal(TemporalType.DATE)
    private Date endDate;
    
    //사용 개수
    private Long annualCnt;
    
    @ManyToOne
    @JoinColumn(name = "emp_id")
    private Employees employees;

    Controller

    @RestController
    		public class AnnualController {
    
    private final AnnualListService annualService;
    
    public AnnualController(AnnualListService annualService) {
        this.annualService = annualService;
    }
    
    // "/save" 경로로의 HTTP POST 요청을 처리하는 메서드
    @PostMapping("/save")
    public ResponseEntity<Object> annualSave(@RequestBody @Valid AnnualListDTO annualListDTO, HttpServletResponse response) {
    
        // SecurityContextHolder에서 현재 인증된 사용자의 이름을 가져온다
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        
        // annualService의 annualList 메서드를 호출하여 annualListDTO와 사용자 이름을 전달
        annualService.annualList(annualListDTO, name);
    
        // HTTP 상태 코드 OK(200)와 바디 없이 ResponseEntity를 반환
        return ResponseEntity.status(HttpStatus.OK).build();
    }
    		}

    Service

    @Service
    public class AnnualListService {
    
    private final AnnualListRepository annualListRepository;
    
    private final EmployeesRepository employeesRepository;
    
    public AnnualListService(AnnualListRepository annualListRepository, EmployeesRepository employeesRepository) {
        this.annualListRepository = annualListRepository;
        this.employeesRepository = employeesRepository;
    }
    
    public void annualList(AnnualListDTO annualListDTO, String username) {
        AnnualList list = new AnnualList();
        list.setAnnualYear(annualListDTO.getAnnualYear());
        list.setStartDate(annualListDTO.getStartDate());
        list.setEndDate(annualListDTO.getEndDate());
    
        Employees employees = new Employees();
        
        employees = employeesRepository.findByUsername(username);
        
        list.setEmployees(employees);
    
         annualListRepository.save(list);
    	}
    		}

연차 신청일수 계산 로직

  • Service 단
	public long calculateDaysDifference(LocalDate startDate, LocalDate endDate) {
        		return ChronoUnit.DAYS.between(startDate, endDate);
    		}
    
	// 시작일을 LocalDate 형식으로 변환
	LocalDate startDate = annualListDTO.getStartDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    
	// 종료일을 LocalDate 형식으로 변환
	LocalDate endDate = annualListDTO.getEndDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    
	// 시작일부터 종료일까지의 일 수를 계산하고, 1을 더함(시작일과 종료일을 모두 포함)
	long daysDifference = calculateDaysDifference(startDate, endDate) + 1;
    
	// 계산된 일 수를 AnnualList 객체의 annualCnt 필드에 설정
	list.setAnnualCnt(daysDifference);    
  • 속성 설명

1. annualListDTO.getEndDate()

  • getEndDate(): AnnualListDTO 클래스 내에 정의된 메소드로, 종료일을 반환

2. toInstant()

  • toInstant(): java.util.Date 클래스의 메소드로, 날짜를 Instant 객체로 변환합니다. Instant 는 날짜와 시간의 측정값을 나타내는 클래스

3. atZone(ZoneId.systemDefault())

  • atZone(): Instant 객체에서 ZoneId 를 사용하여 특정 시간대의 시간으로 변환
  • ZoneId.systemDefault(): 시스템의 기본 시간대를 나타내는 ZoneId 객체를 반환

4. toLocalDate()

  • toLocalDate(): java.time.ZonedDateTime 클래스의 메소드로, 시간대가 적용된 날짜와 시간에서 날짜 부분만 추출하여 LocalDate 객체로 반환 LocalDate 는 시간대가 적용되지 않은 날짜를 나타내는 클래스

5. calculateDaysDifference(startDate, endDate) + 1

  • calculateDaysDifference(startDate, endDate): startDateendDate 간의 날짜 차이를 계산하는 메소드
  • + 1: 시작일과 종료일을 모두 포함하기 위해 일수 차이에 1을 더함

6. list.setAnnualCnt(daysDifference)

  • list: AnnualList 클래스의 객체를 나타냄
  • setAnnualCnt(): AnnualList 클래스 내에 정의된 메소드로, annualCnt 속성을 설정합니다. 이 속성은 시작일부터 종료일까지의 일 수를 나타냄

0개의 댓글