프로필 뷰

Yuri Lee·2020년 11월 11일
0

정보가 없는 프로필 뷰와 정보가 있는 프로필 뷰의 구분

한줄 소개는 프로필을 수정할 수 있는 본인에게만 보여질 수 있어야 한다. 다른 유저들이 와서 봤을 때 이런 메시지나 프로필 수정 버튼이 보여서는 안된다. 추가 정보가 있을 경우 더 보여주도록 한다.

컨트롤러 추가

AccountController.java

    @GetMapping("/profile/{nickname}")
    public String viewProfile(@PathVariable String nickname, Model model, @CurrentUser Account account) {
        Account byNickname = accountRepository.findByNickname(nickname);
        if (nickname == null) {
            throw new IllegalArgumentException(nickname + "에 해당하는 사용자가 없습니다.");
        }

        model.addAttribute(byNickname);
        model.addAttribute("isOwner", byNickname.equals(account));
        return "account/profile";
    }

닉네임으로 요청이 들어오면 PathVariable 어노테이션을 사용해서 문자열을 파싱을 받는다. 그 다음에 model 에다가 닉네임에 해당하는 account 정보를 넣어준다. 현재 유저가 프로필 주인인지 확인해야 한다. (무언가를 조작할 수 있는 권한이 있는 유저) accountRepository에서 닉네임에 해당하는 유저를 찾고, null이면 에러페이지를 보여주거나 던진다. 해당하는 유저가 있으면 model에 byNickname을 담아서 보내준다.

Q. 이 addAttribute 에 넣어준 객체를 지칭하는 이름은 무엇일까?
이 객체는 account 타입의 객체이다.

model.addAttribute("email", account.getEmail()); // 이메일 주소

원래는 이런 식으로 우리가 넣어준 Attribute 이름과 실제 객체를 넣어준다. 하지만 여기서 이름을 주는 것을 생략했지만 실제로는 "account"가 들어가야 한다. 이것을 주지 않으면 기본값으로 여기 들어가는 객체 타입의 camel case를 이름으로 사용한다.

현재 접속한 유저가 오너 여부를 넣어준다. (isOwner)

Owner.java

 <p class="lead" th:if="${#strings.isEmpty(account.bio) && isOwner}">

bio가 없고 owner인 경우에는 한줄 소개를 추가한다.

public boolean isOwner(Principal owner);

부트스트랩

  • listgroup
  • grid
 <svg th:if="${#strings.isEmpty(account.profileImage)}" class="img-fluid float-left rounded img-thumbnail"
                     th:data-jdenticon-value="${account.nickname}" width="125" height="125"></svg>
                <img th:if="${!#strings.isEmpty(account.profileImage)}" class="img-fluid float-left rounded img-thumbnail"
                     th:src="${account.profileImage}"
                     width="125" height="125"/>

account 의 프로필 이미지가 비어있으면 Jdenticon 으로 생성을 한다. account 의 프로필 이미지가 비어있지 않으면 (있으면) account의 profileImage를 보여준다.

해결해야 할 버그가 남아있음! 😭

temporals

 <span th:text="${#temporals.format(account.joinedAt, 'yyyy년 M월 가입')}" class="col-9"></span>

가입 시간을 보여줄 때 temporals 라는 것을 사용하는 데 이게 무엇일까? 날짜를 나타낼 경우 타임리프에서 LocalDateTime을 변환하기 위해서 temporals개체를 이용한다고 한다.

public final class Temporals {

    private final TemporalCreationUtils temporalCreationUtils;
    private final TemporalFormattingUtils temporalFormattingUtils;
    private final TemporalArrayUtils temporalArrayUtils;
    private final TemporalListUtils temporalListUtils;
    private final TemporalSetUtils temporalSetUtils;

    public Temporals(final Locale locale) {
        this(locale, ZoneId.systemDefault());
    }

    public Temporals(final Locale locale, final ZoneId defaultZoneId) {
        super();
        Validate.notNull(locale, "Locale cannot be null");
        this.temporalCreationUtils = new TemporalCreationUtils();
        this.temporalFormattingUtils = new TemporalFormattingUtils(locale, defaultZoneId);
        this.temporalArrayUtils = new TemporalArrayUtils(locale, defaultZoneId);
        this.temporalListUtils = new TemporalListUtils(locale, defaultZoneId);
        this.temporalSetUtils = new TemporalSetUtils(locale, defaultZoneId);
    }

출처 : 인프런 백기선님의 스프링과 JPA 기반 웹 애플리케이션 개발
https://jongminlee0.github.io/2020/03/12/thymeleaf/

profile
Step by step goes a long way ✨

0개의 댓글