코딩성장스토리

스프링 토이 프로젝트 진행중에 배운것 본문

백 엔드/spring

스프링 토이 프로젝트 진행중에 배운것

까르르꿍꿍 2022. 3. 14. 22:52

 

public List findByLocation(String location){ return store.values().stream() .filter(health -> health.getLocation().name().equals(location)) .collect(Collectors.toList()); }

https://github.com/MyunghyunNero/health-management-service

 

GitHub - MyunghyunNero/health-management-service

Contribute to MyunghyunNero/health-management-service development by creating an account on GitHub.

github.com

 

지금 내가 진행하고 있는 토이프로젝트이다.

 

토이프로젝트를 진행하면서 느낀점은 내가 모르지만 아주 유용한 기능들이 있음이다. 

물론 이 기능들은 외울 자신은 없고 매번 검색을 해야겠지만 유용했던 것을 정리해볼려고 한다.

 

이 토이 프로젝트를 하면서 가장 힘들 없던 것은 객체 안의 enum 값을 라이도 버튼 형식으로 타임리프를 이용해 넘기는 것이었다.

<div>운동 부위 선택 </div>
            <div th:each="type : ${locationtype}" class="form-check">
                <input type="radio" th:field="*{location}" th:value="${type}" class="form-check-input">
                <label th:for="${#ids.prev('location')}" th:text="${type.location}"  class="form-check-label"></label>
            </div>

위 코드는 라디오 버튼 (체크박스랑 비슷한데 하나만 선택가능) 을 만들 때 쓰인 코드이다. 

이 코드들은 처음 쓰다 보니 이 코드들과 서버 컨트롤러랑 비교해서 엄청 고민하고 고민했던거 같다. 

그 다음 막혔던 부분은

 public List<Health> findByLocation(String location){
        return store.values().stream()
                .filter(health -> health.getLocation().name().equals(location))
                .collect(Collectors.toList());
    }

 

enum 문에 있는 값들로 구별하여 조회하는 기능들 이다 health라는 객체에 getLocation을 이용해 enum 형을 꺼내고 

stream 과 filter를 이용해서 원하는 조건으로 탐색했다. 그리고 탐색된 것들이 하나가 아닌 여러개라서 collet 라는 기능을 이용해 리턴했다. 

여기에 쓰이는 함수들은 다 처음 쓰는 것들 이었고 이런 기능들은 익혀 두면 좋을 것 같다.

 

 

개선할 점

아직 첫 토이 프로젝트라 그런지 생각보다 Test기능도 잘 안쓰고 리다이렉트더 어정쩡 하고 예외처리도 너무 안했다. 

일다 더 공부하고 이 프로젝트를 개선해야겠다.

'백 엔드 > spring' 카테고리의 다른 글

spring 타임리프 기본기능  (0) 2022.05.05
스프링 이해를 위한 디자인 패턴  (0) 2022.04.08
스프링 빈 스코프  (0) 2022.02.20
스프링 의존관계 자동 주입  (0) 2022.02.17
스프링 컨포넌트스캔  (0) 2022.02.15