Skip to content

Commit

Permalink
마이페이지 자유게시판 글 조회 기능 완성
Browse files Browse the repository at this point in the history
  • Loading branch information
Haaein committed May 13, 2023
1 parent 91bc33c commit 40bdd65
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 1 deletion.
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 modified board_exam_project-master/.gradle/checksums/checksums.lock
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,35 @@ <h3>user_id = <input type="text" th:value="${#session.getAttribute('user')}" nam
</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
@@ -1,8 +1,10 @@
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.python.antlr.op.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
Expand All @@ -23,11 +25,25 @@ public class MyPageController {
public String mypage(HttpSession session, Model model) {
Object obj = session.getAttribute("user"); //사용자 정보 받아서 오브젝트로 만들기
if (obj == null) {
// user가 없는 경우 예외 처리
return "redirect:/login";// user가 없는 경우 예외 처리
} else {
//사용자 id를 Long 타입으로 변환
Long userId = Long.parseLong(obj.toString());
//사용자 정보 불러오기
model.addAttribute("user", userService.getUserInfo(userId));

//freeboard에서 사용자가 작성한 글 불러오기
/*
* 1. 사용자 id를 기준으로 contentId를 찾기. getFreeboardById는 contentId 기준 함수
* 2. contentId를 사용해서 각 게시글을 찾기
* 3. 찾은 게시글들을 List 형식으로 묶든 해서 html에 보낼 수 있게 하기
* */

List<Freeboard> contentIds = freeboardService.getContentByUserId(userId); //getContentByUserId는 userId로 작성된 contentId를 Freeboard 타입 List로 반환한다.
model.addAttribute("userFreeboard", contentIds); //반환받은 List를 userFreeboard라는 이름으로 전달한다.

}

return "users/mypage";
}
}
Expand Down
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 @@ -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
Expand Up @@ -73,5 +73,35 @@ <h3>user_id = <input type="text" th:value="${#session.getAttribute('user')}" nam
</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>

0 comments on commit 40bdd65

Please sign in to comment.