Skip to content

Commit

Permalink
Merge pull request #15 from choboss00/feature/3-branch-mentoring
Browse files Browse the repository at this point in the history
Feature/3 branch mentoring
  • Loading branch information
choboss00 authored Oct 20, 2023
2 parents 45eb723 + 2fe55bc commit cb0a167
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 16 deletions.
5 changes: 3 additions & 2 deletions src/main/java/com/example/demo/mentoring/MentorPost.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@Where(clause = "deleted_at IS NULL")
@SQLDelete(sql = "UPDATE mentor_post_tb SET deleted_at = CURRENT_TIMESTAMP, is_deleted = TRUE where id = ?")
@Table(name = "mentorPost_tb")
@SQLDelete(sql = "UPDATE mentor_posts SET deleted_at = CURRENT_TIMESTAMP, is_deleted = TRUE where id = ?")
@Table(name = "mentor_posts")
public class MentorPost extends BaseTime {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -31,6 +31,7 @@ public class MentorPost extends BaseTime {
@Column(nullable = false)
private String title;

@Column(length = 300)
private String content;

@Convert(converter = StateConverter.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

public interface MentorPostJPARepostiory extends JpaRepository<MentorPost, Integer> {

@Query("select m from MentorPost m where m.writer.id = :writer and m.state = 'ACTIVE'")
List<MentorPost> findAllByWriter(@Param("writer") int writer);

Optional<MentorPost> findById(int id);

@Query("select count(*) from MentorPost m where m.writer.id = :userId and m.state = 'ACTIVE'")
int countContactByMentorId(int userId);

Expand Down
47 changes: 36 additions & 11 deletions src/main/java/com/example/demo/mentoring/MentorPostService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.demo.mentoring;

import com.example.demo.config.errors.exception.Exception500;
import com.example.demo.config.errors.exception.Exception400;
import com.example.demo.config.errors.exception.Exception404;
import com.example.demo.mentoring.contact.ContactJPARepository;
Expand All @@ -13,6 +14,8 @@
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.example.demo.config.errors.exception.Exception400;
import com.example.demo.config.errors.exception.Exception404;

import java.util.List;
import java.util.Optional;
Expand All @@ -30,7 +33,11 @@ public class MentorPostService {
@Transactional
public void createMentorPost(MentorPostRequest.CreateDTO createDTO, User writer) {
MentorPost mentorPost = new MentorPost( writer, createDTO.getTitle(), createDTO.getContent());
mentorPostJPARepository.save(mentorPost);
try {
mentorPostJPARepository.save(mentorPost);
} catch (Exception e) {
throw new Exception500("unknown server error");
}
}

/* 1. mentorPostList를 조회
Expand All @@ -40,10 +47,19 @@ public List<MentorPostResponse.MentorPostAllDTO> findAllMentorPost(int page) {
Pageable pageable = PageRequest.of(page,5);

Page<MentorPost> pageContent = mentorPostJPARepository.findAll(pageable);

if(pageContent.getTotalPages() == 0){
throw new Exception404("해당 글들이 존재하지 않습니다");
}

//List<MentorPost> mentorPostList = mentorPostJPARepostiory.findAll();
List<MentorPostResponse.MentorPostAllDTO> mentorPostDTOList = pageContent.getContent().stream().map(
mentorPost -> {
List<UserInterest> writerInterests = userInterestJPARepository.findAllById(mentorPost.getWriter().getId());
if(writerInterests.isEmpty()){
throw new Exception404("해당 카테고리는 존재하지 않습니다");
}

return new MentorPostResponse.MentorPostAllDTO(mentorPost,writerInterests);
}
).collect(Collectors.toList());
Expand All @@ -52,7 +68,7 @@ public List<MentorPostResponse.MentorPostAllDTO> findAllMentorPost(int page) {

public MentorPostResponse.MentorPostDTO findMentorPost(int id){
MentorPost mentorPost = mentorPostJPARepository.findById(id)
.orElseThrow(() -> new Exception404("해당 게시글이 존재하지 않습니다."));
.orElseThrow(() -> new Exception404("해당 글이 존재하지 않습니다.\n" + "id : " + id));

//writer 데이터
User mentor = mentorPost.getWriter();
Expand All @@ -72,15 +88,22 @@ public MentorPostResponse.MentorPostDTO findMentorPost(int id){
@Transactional
public void updateMentorPost(MentorPostRequest.CreateDTO createDTO, int id)
{
MentorPost mentorPost = mentorPostJPARepository.findById(id)
.orElseThrow(() -> new Exception404("해당 게시글이 존재하지 않습니다."));

mentorPost.update(createDTO.getTitle(), createDTO.getContent());

MentorPost mentorPost = mentorPostJPARepository.findById(id).
orElseThrow(() -> new Exception404("해당 글이 존재하지 않습니다."));

try {
mentorPost.update(createDTO.getTitle(), createDTO.getContent());
} catch (Exception e) {
throw new Exception500("unknown server error");
}
}

public void deleteMentorPost(int id) {
mentorPostJPARepository.deleteById(id);
try {
mentorPostJPARepository.deleteById(id);
} catch (Exception e) {
throw new Exception500("unknown server error");
}
}

//생성 시간까지 조회하는 test service 코드 입니다
Expand All @@ -90,6 +113,10 @@ public List<MentorPostResponse.MentorPostAllWithTimeStampDTO> findAllMentorPostW
List<MentorPostResponse.MentorPostAllWithTimeStampDTO> mentorPostDTOList = pageContent.stream().map(
mentorPost -> {
List<UserInterest> writerInterests = userInterestJPARepository.findAllById(mentorPost.getWriter().getId());
if(writerInterests.isEmpty()){
throw new Exception404("해당 카테고리는 존재하지 않습니다");
}

return new MentorPostResponse.MentorPostAllWithTimeStampDTO(mentorPost,writerInterests);
}
).collect(Collectors.toList());
Expand All @@ -99,10 +126,8 @@ public List<MentorPostResponse.MentorPostAllWithTimeStampDTO> findAllMentorPostW
public void changeMentorPostStatus(MentorPostRequest.StateDTO stateDTO, int id)
{
MentorPost mentorPost = mentorPostJPARepository.findById(id)
.orElseThrow(() -> new Exception404("해당 게시글이 존재하지 않습니다."));

.orElseThrow(() -> new Exception404("해당 글이 존재하지 않습니다."));;
mentorPost.changeStatus(stateDTO.getStateEnum());

}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application-ide.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ spring:
jpa:
database-platform: org.hibernate.dialect.MySQLDialect
hibernate:
ddl-auto: none
ddl-auto: validate
show-sql: true
properties:
hibernate:
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
spring:
profiles:
active: local
active: ide
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,6 @@ public void DeleteMentorPost() throws Exception{
@DisplayName("DoneTest")
public void PatchDoneMentorPost() throws Exception{
int id = 1;

MentorPostRequest.StateDTO stateDTO = new MentorPostRequest.StateDTO();
stateDTO.setStateEnum(StateEnum.DONE);
mentorPostService.changeMentorPostStatus(stateDTO, id);
Expand Down

0 comments on commit cb0a167

Please sign in to comment.