Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package es.princip.ringus.application.mentor.service;

import es.princip.ringus.domain.exception.MenteeErrorCode;
import es.princip.ringus.domain.exception.MentorErrorCode;
import es.princip.ringus.domain.exception.MentoringErrorCode;
import es.princip.ringus.domain.mentee.Mentee;
import es.princip.ringus.domain.mentee.MenteeRepository;
import es.princip.ringus.domain.mentor.Mentor;
import es.princip.ringus.domain.mentor.MentorRepository;
import es.princip.ringus.domain.mentoring.Mentoring;
import es.princip.ringus.domain.mentoring.MentoringRepository;
import es.princip.ringus.domain.mentoring.MentoringTime;
import es.princip.ringus.domain.mentoring.accept.MentoringAccept;
import es.princip.ringus.domain.mentoring.accept.MentoringAcceptRepository;
import es.princip.ringus.global.exception.CustomRuntimeException;
import es.princip.ringus.presentation.mentoring.dto.AcceptMentoringRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class AcceptMentoringService {
private final MentorRepository mentorRepository;
private final MenteeRepository menteeRepository;
private final MentoringRepository mentoringRepository;
private final MentoringAcceptRepository mentoringAcceptRepository;

private boolean isRequestedTimeInMentoringTime(Mentoring mentoring, AcceptMentoringRequest request) {
return mentoring.getApplyTimes().stream()
.anyMatch(time -> MentoringTime.equals(time, request.mentoringTime()));
}

public Long accept(AcceptMentoringRequest request, Long memberId) {
Mentor mentor = mentorRepository.findByMemberId(memberId)
.orElseThrow(() -> new CustomRuntimeException(MentorErrorCode.MENTOR_NOT_FOUND));
Mentee mentee = menteeRepository.findById(request.menteeId())
.orElseThrow(() -> new CustomRuntimeException(MenteeErrorCode.MENTEE_NOT_FOUND));

Mentoring mentoring = mentoringRepository.findByMenteeIdAndMentorId(mentee.getId(), mentor.getId())
.orElseThrow(() -> new CustomRuntimeException(MentoringErrorCode.MENTORING_NOT_FOUND));

if (mentoring.isNotWaiting()) {
throw new CustomRuntimeException(MentoringErrorCode.MENTORING_STATUS_NOT_WAITING);
}
if (!isRequestedTimeInMentoringTime(mentoring, request)) {
throw new CustomRuntimeException(MentoringErrorCode.MENTORING_TIME_NOT_MATCH);
}

MentoringAccept accept = MentoringAccept.builder()
.mentee(mentee)
.mentor(mentor)
.mentoringTime(request.mentoringTime())
.build();
mentoring.accept();

return mentoringAcceptRepository.save(accept).getId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import es.princip.ringus.domain.exception.MenteeErrorCode;
import es.princip.ringus.domain.exception.MentorErrorCode;
import es.princip.ringus.domain.member.MemberRepository;
import es.princip.ringus.domain.mentee.Mentee;
import es.princip.ringus.domain.mentee.MenteeRepository;
import es.princip.ringus.domain.mentor.Mentor;
Expand Down Expand Up @@ -46,8 +45,5 @@ public MentoringResponse createMentoring(CreateMentoringRequest request, Long me
mentor.addMentoring(mentoring);

return MentoringResponse.from(mentoringRepository.save(mentoring));

}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ public enum MentoringErrorCode implements ErrorCode {

MENTORING_NOT_FOUND(HttpStatus.NOT_FOUND,"해당 신청이 존재하지 않음"),
MENTORING_TIME_CONVERT_ERROR(HttpStatus.NOT_MODIFIED,"신청 시간 변환 오류"),
MENTORING_TIME_ERROR(HttpStatus.BAD_REQUEST,"신청 가능 시간 오류");
MENTORING_TIME_ERROR(HttpStatus.BAD_REQUEST,"신청 가능 시간 오류"),
MENTORING_STATUS_NOT_WAITING(HttpStatus.BAD_REQUEST,"멘토링 대기 상태가 아님"),
MENTORING_TIME_NOT_MATCH(HttpStatus.BAD_REQUEST,"멘티의 제안 시간에 멘토링 시간이 포함되지 않음");

MentoringErrorCode(HttpStatus status , String message) {
this.status = status;
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/es/princip/ringus/domain/mentoring/Mentoring.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package es.princip.ringus.domain.mentoring;

import es.princip.ringus.domain.mentoring.converter.MentoringTimeConverter;
import es.princip.ringus.domain.mentoring.converter.MentoringTimeListConverter;
import es.princip.ringus.domain.base.BaseTimeEntity;
import es.princip.ringus.domain.exception.MentoringErrorCode;
import es.princip.ringus.domain.mentee.Mentee;
Expand Down Expand Up @@ -33,7 +33,7 @@ public class Mentoring extends BaseTimeEntity {
@Column(name = "mentoring_topic")
private MentoringTopic mentoringTopic;

@Convert(converter = MentoringTimeConverter.class)
@Convert(converter = MentoringTimeListConverter.class)
@Column(length = 500, nullable = false) // 500자 이내
//@Column(columnDefinition = "TEXT") // 500자 초과 시
private List<MentoringTime> applyTimes;
Expand Down Expand Up @@ -89,4 +89,12 @@ public static Mentoring of(
mentee
);
}

public boolean isNotWaiting() {
return mentoringStatus != MentoringStatus.WAITING;
}

public void accept() {
this.mentoringStatus = MentoringStatus.ACCEPTED;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface MentoringRepository extends JpaRepository<Mentoring, Long> {
Optional<Mentoring> findByMenteeIdAndMentorId(Long menteeId, Long mentorId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.persistence.Embeddable;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -33,4 +32,16 @@ public MentoringTime(@JsonProperty("startTime")final LocalDateTime startTime,
this.startTime = startTime;
this.endTime = endTime;
}

public static boolean equals(MentoringTime expected, MentoringTime actual) {
if (expected == null || actual == null) {
return false;
}
return expected.getStartTime().equals(actual.getStartTime()) && expected.getEndTime().equals(actual.getEndTime());
}

@Override
public String toString() {
return "MentoringTime(startTime=" + this.getStartTime() + ", endTime=" + this.getEndTime() + ")";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package es.princip.ringus.domain.mentoring.accept;

import es.princip.ringus.domain.base.BaseTimeEntity;
import es.princip.ringus.domain.mentee.Mentee;
import es.princip.ringus.domain.mentor.Mentor;
import es.princip.ringus.domain.mentoring.MentoringTime;
import es.princip.ringus.domain.mentoring.converter.MentoringTimeConverter;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@Table(name = "mentoring_accept")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class MentoringAccept extends BaseTimeEntity {

@Id
@Column(name = "mentoring_accept_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Convert(converter = MentoringTimeConverter.class)
@Column(length = 200, nullable = false)
private MentoringTime mentoringTime;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "mentor_id", nullable = false)
private Mentor mentor;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "mentee_id", nullable = false)
private Mentee mentee;

@Builder
public MentoringAccept(final MentoringTime mentoringTime, final Mentor mentor, final Mentee mentee) {
this.mentoringTime = mentoringTime;
this.mentor = mentor;
this.mentee = mentee;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package es.princip.ringus.domain.mentoring.accept;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface MentoringAcceptRepository extends JpaRepository<MentoringAccept, Long> {
}
Original file line number Diff line number Diff line change
@@ -1,51 +1,40 @@
package es.princip.ringus.domain.mentoring.converter;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import es.princip.ringus.domain.mentoring.MentoringTime;
import es.princip.ringus.domain.exception.MentoringErrorCode;
import es.princip.ringus.domain.mentoring.MentoringTime;
import es.princip.ringus.global.exception.CustomRuntimeException;
import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
@Slf4j
@Converter
public class MentoringTimeConverter implements AttributeConverter<MentoringTime, String> {

@Converter(autoApply = false)
public class MentoringTimeConverter implements AttributeConverter<List<MentoringTime>,String> {
private static final ObjectMapper objectMapper = new ObjectMapper()
.registerModule(new JavaTimeModule());
private final ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule());

@Override
public String convertToDatabaseColumn(List<MentoringTime> mentoringTimes) {
public String convertToDatabaseColumn(MentoringTime attribute) {
try {
if (mentoringTimes == null || mentoringTimes.isEmpty()) {
return "[]"; // 빈 리스트일 경우 빈 JSON 배열 반환
if (attribute == null) {
return "[]";
}
String json = objectMapper.writeValueAsString(mentoringTimes);
System.out.println("✅ MentoringTime JSON 변환 (DB 저장): " + json);
return json;
return objectMapper.writeValueAsString(attribute);
} catch (Exception e) {
System.err.println("❌ MentoringTime 변환 오류 (DB 저장): " + mentoringTimes);
e.printStackTrace();
log.error("Failed to convert MentoringTime to JSON {}", e.getMessage());
throw new CustomRuntimeException(MentoringErrorCode.MENTORING_TIME_CONVERT_ERROR);
}
}

@Override
public List<MentoringTime> convertToEntityAttribute(String dbData) {
public MentoringTime convertToEntityAttribute(String dbData) {
try {
if (dbData == null || dbData.isEmpty()) {
return new ArrayList<>(); // null 또는 빈 값이면 빈 리스트 반환
}
List<MentoringTime> times = objectMapper.readValue(dbData, new TypeReference<List<MentoringTime>>() {});
System.out.println("✅ MentoringTime 객체 변환 (조회 시): " + times);
return times;
return objectMapper.readValue(dbData, MentoringTime.class);
} catch (JsonProcessingException e) {
System.err.println("❌ MentoringTime 변환 오류 (조회 시): " + dbData);
e.printStackTrace();
log.error("Failed to convert JSON to MentoringTime: {}", e.getMessage());
throw new CustomRuntimeException(MentoringErrorCode.MENTORING_TIME_CONVERT_ERROR);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package es.princip.ringus.domain.mentoring.converter;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import es.princip.ringus.domain.mentoring.MentoringTime;
import es.princip.ringus.domain.exception.MentoringErrorCode;
import es.princip.ringus.global.exception.CustomRuntimeException;
import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Converter
public class MentoringTimeListConverter implements AttributeConverter<List<MentoringTime>,String> {
private static final ObjectMapper objectMapper = new ObjectMapper()
.registerModule(new JavaTimeModule());

@Override
public String convertToDatabaseColumn(List<MentoringTime> mentoringTimes) {
try {
if (mentoringTimes == null || mentoringTimes.isEmpty()) {
return "[]"; // 빈 리스트일 경우 빈 JSON 배열 반환
}
String json = objectMapper.writeValueAsString(mentoringTimes);
log.info("Success to convert MentoringTime to JSON: {}", json);
return json;
} catch (Exception e) {
log.error("Failed to convert MentoringTime to JSON {}", e.getMessage());
throw new CustomRuntimeException(MentoringErrorCode.MENTORING_TIME_CONVERT_ERROR);
}
}

@Override
public List<MentoringTime> convertToEntityAttribute(String dbData) {
try {
if (dbData == null || dbData.isEmpty()) {
return new ArrayList<>(); // null 또는 빈 값이면 빈 리스트 반환
}
List<MentoringTime> times = objectMapper.readValue(dbData, new TypeReference<List<MentoringTime>>() {});
log.info("Success to convert JSON to MentoringTime: {}", times);
return times;
} catch (JsonProcessingException e) {
log.error("Failed to convert JSON to MentoringTime: {}", e.getMessage());
throw new CustomRuntimeException(MentoringErrorCode.MENTORING_TIME_CONVERT_ERROR);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public PasswordEncoder passwordEncoder() {
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration config = new CorsConfiguration();
config.setAllowedOriginPatterns(List.of("http://localhost:5178", "https://ringus.my"));
config.setAllowedMethods(List.of("GET", "POST", "PATCH", "DELETE", "OPTIONS"));
config.setAllowedMethods(List.of("GET", "POST", "PATCH", "DELETE", "OPTIONS", "PUT"));
config.setAllowedHeaders(List.of("Authorization", "Content-Type"));
config.setExposedHeaders(List.of("Authorization"));
config.setAllowCredentials(true); // 인증 정보 포함 허용
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

public record CursorRequest(
boolean bookmarked,
boolean commissioned,
boolean suggested,
Long cursor
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package es.princip.ringus.presentation.mentor.dto;

import es.princip.ringus.domain.mentor.Mentor;
import es.princip.ringus.domain.mentor.vo.Hashtag;
import es.princip.ringus.presentation.common.dto.*;

import java.util.List;
Expand All @@ -26,7 +27,7 @@ public static MentorDetailResponse from(final Mentor mentor) {
IntroductionResponse.from(mentor.getIntroduction()),
TimezoneResponse.from(mentor.getTimezone()),
mentor.getMentoringField().stream().map(String::valueOf).collect(Collectors.toSet()),
mentor.getHashtags().stream().map(String::valueOf).toList(),
mentor.getHashtags().stream().map(Hashtag::getValue).toList(),
mentor.getMessage(),
PortfolioResponse.from(mentor.getPortfolio())
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package es.princip.ringus.presentation.mentor.dto;

import es.princip.ringus.domain.mentor.Mentor;
import es.princip.ringus.domain.mentor.vo.Hashtag;
import es.princip.ringus.presentation.common.dto.*;

import java.util.List;
Expand All @@ -26,7 +27,7 @@ public static MyMentorResponse from(final Mentor mentor) {
IntroductionResponse.from(mentor.getIntroduction()),
TimezoneResponse.from(mentor.getTimezone()),
mentor.getMentoringField().stream().map(String::valueOf).collect(Collectors.toSet()),
mentor.getHashtags().stream().map(String::valueOf).toList(),
mentor.getHashtags().stream().map(Hashtag::getValue).toList(),
mentor.getMessage(),
PortfolioResponse.from(mentor.getPortfolio())
);
Expand Down
Loading