본문 바로가기

Server Programming/Spring Boot Full-Stack Programming

[스프링 풀스택 클론 코딩 - 계정 설정] (2-7) 알림 설정

반응형

Account

	//생성, 가입, 갱신정보 알림 설정 -> Email, Web, 둘다
	private boolean studyCreatedByEmail;
	
	private boolean studyCreatedByWeb;
	
	private boolean studyEnrollmentResultByEmail;
	
	private boolean studyEnrollmentResultByWeb;
	
	private boolean studyUpdatedByEmail;
	
	private boolean studyUpdatedByWeb;

 

	//생성, 가입, 갱신정보 알림 설정 -> Email, Web, 둘다
	private boolean studyCreatedByEmail;
	
	private boolean studyCreatedByWeb=true;
	
	private boolean studyEnrollmentResultByEmail;
	
	private boolean studyEnrollmentResultByWeb=true;
	
	private boolean studyUpdatedByEmail;
	
	private boolean studyUpdatedByWeb=true;

알림 설정은 웹으로 기본값 설정

 

 

SettingsController의 updateNotificationsForm과 updateNotifications 메서드

	//알림 설정
	@GetMapping(SETTINGS_NOTIFICATIONS_URL)
	public String updateNotificationsForm(@CurrentUser Account account, Model model) {
		//모델에 현재유저의 정보를 담는다.
		model.addAttribute(account);
		//또한, form을 채울 객체 생성
		model.addAttribute(new Notifications(account));
		
		return SETTINGS_NOTIFICATIONS_VIEW_NAME;
	}

	//알림 설정 변경 처리
	@PostMapping(SETTINGS_NOTIFICATIONS_URL)
	public String updateNotifications (@CurrentUser Account account,@Valid Notifications notifications, Errors errors,
            Model model, RedirectAttributes attributes) {
		if (errors.hasErrors()) {
			model.addAttribute(account);
			return SETTINGS_NOTIFICATIONS_VIEW_NAME;
		}
		
		//알림 설정 변경 -> account을 notifications로 변경 
		accountService.updateNotifications(account, notifications);
		attributes.addFlashAttribute("message", "알림 설정을 변경했습니다.");
		
		return "redirect:" + SETTINGS_NOTIFICATIONS_URL;
	}

 

AccountService의 updateNotifications메서드

	//form객체인 notifications에서 가져와서  account에 변경
	public void updateNotifications(Account account, @Valid Notifications notifications) {
		// TODO Auto-generated method stub
		account.setStudyCreatedByEmail(notifications.isStudyCreatedByEmail());
		account.setStudyCreatedByWeb(notifications.isStudyCreatedByWeb());
		account.setStudyEnrollmentResultByEmail(notifications.isStudyEnrollmentResultByEmail());
		account.setStudyEnrollmentResultByWeb(notifications.isStudyEnrollmentResultByWeb());
		account.setStudyUpdatedByEmail(notifications.isStudyUpdatedByEmail());
		account.setStudyUpdatedByWeb(notifications.isStudyUpdatedByWeb());
		accountRepository.save(account);
	}

 

Notifications.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:replace="fragments.html :: head"></head>
<body class="bg-light">
    <nav th:replace="fragments.html :: main-nav"></nav>
    <div class="container">
        <div class="row mt-5 justify-content-center">
            <div class="col-2">
                <div th:replace="fragments.html :: settings-menu(currentMenu='notifications')"></div>
            </div>
            <div class="col-8">
                <div th:if="${message}" class="alert alert-info alert-dismissible fade show mt-3" role="alert">
                    <span th:text="${message}">완료</span>
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="row">
                    <h2 class="col-12">알림 설정</h2>
                </div>
                <div class="row mt-3" th:fragment="profile-form">
                    <form class="col-12" action="#" th:action="@{/settings/notifications}" th:object="${notifications}" method="post" novalidate>
                        <div class="alert alert-light" role="alert">
                            <strong><a href="#" th:href="@{/settings/locations}">주요 활동 지역</a>에
                            <a href="#" th:href="@{/settings/keywords}">관심있는 주제</a>의 스터디가 만들어졌을 때</strong> 알림을 받을 방법을 설정하세요.
                        </div>
                        <div class="form-group">
                            <div class="custom-control custom-switch custom-control-inline">
                                <input type="checkbox" th:field="*{studyCreatedByEmail}" class="custom-control-input" id="studyCreatedByEmail">
                                <label class="custom-control-label" for="studyCreatedByEmail">이메일로 받기</label>
                            </div>
                            <div class="custom-control custom-switch custom-control-inline">
                                <input type="checkbox" th:field="*{studyCreatedByWeb}" class="custom-control-input" id="studyCreatedByWeb">
                                <label class="custom-control-label" for="studyCreatedByWeb">웹으로 받기</label>
                            </div>
                        </div>
                        <div class="alert alert-light" role="alert">
                            <strong>스터디 모임 참가 신청</strong> 결과 알림을 받을 방법을 설정하세요.
                        </div>
                        <div class="form-group">
                            <div class="custom-control custom-switch custom-control-inline">
                                <input type="checkbox" th:field="*{studyEnrollmentResultByEmail}" class="custom-control-input" id="studyEnrollmentResultByEmil">
                                <label class="custom-control-label" for="studyEnrollmentResultByEmil">이메일로 받기</label>
                            </div>
                            <div class="custom-control custom-switch custom-control-inline">
                                <input type="checkbox" th:field="*{studyEnrollmentResultByWeb}" class="custom-control-input" id="studyEnrollmentResultByWeb">
                                <label class="custom-control-label" for="studyEnrollmentResultByWeb">웹으로 받기</label>
                            </div>
                        </div>
                        <div class="alert alert-light" role="alert">
                            <strong>참여중인 스터디</strong>에 대한 알림을 받을 방법을 설정하세요.
                        </div>
                        <div class="form-group">
                            <div class="custom-control custom-switch custom-control-inline">
                                <input type="checkbox" th:field="*{studyUpdatedByEmail}" class="custom-control-input" id="studyWatchByEmail">
                                <label class="custom-control-label" for="studyWatchByEmail">이메일로 받기</label>
                            </div>
                            <div class="custom-control custom-switch custom-control-inline">
                                <input type="checkbox" th:field="*{studyUpdatedByWeb}" class="custom-control-input" id="studyWatchByWeb">
                                <label class="custom-control-label" for="studyWatchByWeb">웹으로 받기</label>
                            </div>
                        </div>
                        <div class="form-group">
                            <button class="btn btn-outline-primary" type="submit" aria-describedby="submitHelp">저장하기</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <div th:replace="fragments.html :: footer"></div>
    </div>
</body>
</html>
반응형