Skip to content

Commit

Permalink
Merge branch '#1-mypage'
Browse files Browse the repository at this point in the history
# Conflicts:
#	board_exam_project-master/.gradle/7.1.1/executionHistory/executionHistory.bin
#	board_exam_project-master/.gradle/7.1.1/executionHistory/executionHistory.lock
#	board_exam_project-master/.gradle/7.1.1/fileHashes/fileHashes.bin
#	board_exam_project-master/.gradle/7.1.1/fileHashes/fileHashes.lock
#	board_exam_project-master/.gradle/buildOutputCleanup/buildOutputCleanup.lock
#	board_exam_project-master/build/classes/java/main/com/study/board/BoardApplication.class
#	board_exam_project-master/build/classes/java/main/com/study/board/controller/FreeboardController.class
#	board_exam_project-master/build/tmp/compileJava/previous-compilation-data.bin
  • Loading branch information
Haaein committed May 19, 2023
2 parents 5e3b82c + 02242e3 commit 204e3d9
Show file tree
Hide file tree
Showing 32 changed files with 280 additions and 6 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
<a th:if="${#session != null and #session.getAttribute('user') != null}" href="/pdf" class="button">pdf리스트</a>
<a th:if="${#session == null or #session.getAttribute('user') == null}" href="/register" class="button">회원가입</a>
<a th:if="${#session == null or #session.getAttribute('user') == null}" href="/login" class="button">login</a>
<a th:if="${#session != null and #session.getAttribute('user') != null}" href="/mypage" class="button">마이페이지</a>
<a th:if="${#session != null and #session.getAttribute('user') != null}" href="/logout" class="button">logout</a>

</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<title>사용자 페이지</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
}

h1 {
color: #333;
text-align: center;
}

table {
margin: 20px auto;
border-collapse: collapse;
width: 90%;
max-width: 800px;
box-shadow: 0px 2px 6px rgba(0, 0, 0, 0.3);
overflow: hidden;
}

th, td {
padding: 12px 15px;
text-align: left;
border-bottom: 1px solid #ddd;
}

th {
background-color: #7E8EFB; /* 변경된 부분 */
color: white;
}

tr:hover {
background-color: #f5f5f5;
}

a {
color: #007bff;
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

.add-button {
display: block;
text-align: center;
margin-top: 20px;
}
</style>

</head>
<body>
<h1>사용자 정보 보여주기~~<h1>
<h3>user_id = <input type="text" th:value="${#session.getAttribute('user')}" name="user.id" id="user" readonly/></h3>
<table>
<thead>
<tr>
<th>ID</th>
<th>Nickname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr th:each="u : ${user}" th:if="${u.id == #session.getAttribute('user')}">
<td th:text="${u.id}"></td>
<td th:text="${u.nickname}"></td>
<td th:text="${u.email}"></td>
</tr>
</tbody>
</table>

<h1>사용자가 그동안 작성한 자유게시판 게시글 목록</h1>
<table>
<thead>
<tr>
<th>user</th>
<th>contentId</th>
<th>Title</th>
<th>Content</th>
<th>Created At</th>
<th>Updated At</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr th:each="freeboard : ${userFreeboard}"> <!--컨트롤러에서 받은 Freeboard 타입의 리스트(현재 접속한 user id로 작성된 글들)를 받아서 데이터를 표형식으로 출력한다.-->
<td th:text="${freeboard.user.id}"></td>
<td th:text="${freeboard.contentId}"></td>
<td th:text="${freeboard.title}"></td>
<td th:text="${freeboard.content}"></td>
<td th:text="${freeboard.contentAt}"></td>
<td th:text="${freeboard.updatedAt}"></td>
<td>
<a th:href="@{/edit/{id}(id=${freeboard.contentId})}">Edit</a>
<a th:href="@{/delete/{id}(id=${freeboard.contentId})}">Delete</a>
</td>
</tr>
</tbody>
</table>

</body>
</html>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.study.board.controller;

import com.study.board.entity.Freeboard;
import com.study.board.entity.User;
import com.study.board.service.FreeboardService;
import com.study.board.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class MyPageController {
@Autowired
private UserService userService;
@Autowired
private FreeboardService freeboardService;

@GetMapping("/mypage")
@ResponseBody
public ResponseEntity<?> mypage(HttpSession session) {
Object obj = session.getAttribute("user"); // 사용자 정보 받아서 오브젝트로 만들기
if (obj == null) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("사용자가 로그인되어 있지 않습니다.");
} else {
// 사용자 id를 Long 타입으로 변환
Long userId = Long.parseLong(obj.toString());
// 사용자 정보 불러오기
User user = userService.getUserInfo(userId);

// freeboard에서 사용자가 작성한 글 불러오기
List<Freeboard> userFreeboards = freeboardService.getContentByUserId(userId);

return ResponseEntity.ok().body(userFreeboards);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,5 @@ public int getIsDeleted() {
public void setIsDeleted(int isDeleted) {
this.isDeleted = isDeleted;
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package com.study.board.repository;

import com.study.board.entity.Freeboard;
import com.study.board.entity.Pdf;
import com.study.board.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface FreeboardRepository extends JpaRepository<Freeboard, Integer> {

List<Freeboard> findAllByUserId(Long userId); //마이페이지에서 유저가 작성한 자유게시판 글들을 찾을 때 사용하는 함수
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@
import java.util.Optional;
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByEmail(String email);
Optional<User> findByNickname(String nickname);
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ public void deleteFreeboard(Integer id) {
.orElseThrow(() -> new NoSuchElementException("Freeboard not found with id " + id));
freeboardRepository.delete(freeboard);
}

public List<Freeboard> getContentByUserId(Long userId){ //user의 id를 받아서 user Id로 작성된 글들을 찾아 Freeboard 타입 리스트로 반환
return freeboardRepository.findAllByUserId(userId);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.study.board.service;

import com.study.board.entity.Freeboard;
import com.study.board.entity.User;
import com.study.board.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -28,10 +29,6 @@ public void register(User user) {
// Save the user to the database
userRepository.save(user);
}




public boolean authenticate(Long id, String password) {
Optional<User> optionalUser = userRepository.findById(id);
if (optionalUser.isPresent()) {
Expand All @@ -43,6 +40,13 @@ public boolean authenticate(Long id, String password) {
return false;
}

public User getUserInfo(Long id) {
Optional<User> user = userRepository.findById(id);
return user.orElse(null);
}






Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
<a th:if="${#session != null and #session.getAttribute('user') != null}" href="/pdf" class="button">pdf리스트</a>
<a th:if="${#session == null or #session.getAttribute('user') == null}" href="/register" class="button">회원가입</a>
<a th:if="${#session == null or #session.getAttribute('user') == null}" href="/login" class="button">login</a>
<a th:if="${#session != null and #session.getAttribute('user') != null}" href="/mypage" class="button">마이페이지</a>
<a th:if="${#session != null and #session.getAttribute('user') != null}" href="/logout" class="button">logout</a>

</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<title>사용자 페이지</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
}

h1 {
color: #333;
text-align: center;
}

table {
margin: 20px auto;
border-collapse: collapse;
width: 90%;
max-width: 800px;
box-shadow: 0px 2px 6px rgba(0, 0, 0, 0.3);
overflow: hidden;
}

th, td {
padding: 12px 15px;
text-align: left;
border-bottom: 1px solid #ddd;
}

th {
background-color: #7E8EFB; /* 변경된 부분 */
color: white;
}

tr:hover {
background-color: #f5f5f5;
}

a {
color: #007bff;
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

.add-button {
display: block;
text-align: center;
margin-top: 20px;
}
</style>

</head>
<body>
<h1>사용자 정보 보여주기~~<h1>
<h3>user_id = <input type="text" th:value="${#session.getAttribute('user')}" name="user.id" id="user" readonly/></h3>
<table>
<thead>
<tr>
<th>ID</th>
<th>Nickname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr th:each="u : ${user}" th:if="${u.id == #session.getAttribute('user')}">
<td th:text="${u.id}"></td>
<td th:text="${u.nickname}"></td>
<td th:text="${u.email}"></td>
</tr>
</tbody>
</table>

<h1>사용자가 그동안 작성한 자유게시판 게시글 목록</h1>
<table>
<thead>
<tr>
<th>user</th>
<th>contentId</th>
<th>Title</th>
<th>Content</th>
<th>Created At</th>
<th>Updated At</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr th:each="freeboard : ${userFreeboard}"> <!--컨트롤러에서 받은 Freeboard 타입의 리스트(현재 접속한 user id로 작성된 글들)를 받아서 데이터를 표형식으로 출력한다.-->
<td th:text="${freeboard.user.id}"></td>
<td th:text="${freeboard.contentId}"></td>
<td th:text="${freeboard.title}"></td>
<td th:text="${freeboard.content}"></td>
<td th:text="${freeboard.contentAt}"></td>
<td th:text="${freeboard.updatedAt}"></td>
<td>
<a th:href="@{/edit/{id}(id=${freeboard.contentId})}">Edit</a>
<a th:href="@{/delete/{id}(id=${freeboard.contentId})}">Delete</a>
</td>
</tr>
</tbody>
</table>

</body>
</html>
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 204e3d9

Please sign in to comment.