Skip to content

Commit

Permalink
Merge branch 'main' into dev2
Browse files Browse the repository at this point in the history
  • Loading branch information
KimKyoHwee authored Aug 20, 2024
2 parents 59f9b70 + 460e3d6 commit c026568
Show file tree
Hide file tree
Showing 15 changed files with 206 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,7 @@ out/
### HTTP TEST ###
*.http


application.log
/src/main/resources/logback.xml
/src/main/resources/logback.xml

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
@Configuration
@EnableWebSecurity
public class SecurityConfig {

private final CustomOAuth2UserService customOAuth2UserService;
private final CustomSuccessHandler customSuccessHandler;
private final JWTUtil jwtUtil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

@Configuration
public class AsyncConfig {

@Bean(name = "mailExecutor")
public Executor mailExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
Expand All @@ -19,3 +20,4 @@ public Executor mailExecutor() {
return executor;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static com.soongsil.CoffeeChat.enums.RequestUri.*;


import java.util.List;

import org.springframework.http.ResponseEntity;
Expand All @@ -20,6 +21,7 @@
import com.soongsil.CoffeeChat.dto.ApplicationGetResponseDto;
import com.soongsil.CoffeeChat.dto.ApplicationMatchResponseDto;
import com.soongsil.CoffeeChat.dto.Oauth.CustomOAuth2User;

import com.soongsil.CoffeeChat.service.ApplicationService;

import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -33,6 +35,7 @@
@Tag(name = "APPLICATION", description = "Application 관련 api")
public class ApplicationController {


private final ApplicationService applicationService;

@PostMapping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
public class EmailController {
private final EmailUtil emailUtil;


@GetMapping()
public CompletableFuture<String> sendAuthenticationMail(@RequestParam("email") String receiver) throws
MessagingException,
InterruptedException {
return emailUtil.sendAuthenticationEmail(receiver);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.util.List;


import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -14,6 +15,7 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import com.soongsil.CoffeeChat.dto.MentorGetListResponseDto;
import com.soongsil.CoffeeChat.dto.MentorGetUpdateDetailDto;
import com.soongsil.CoffeeChat.dto.MentorIntroductionUpdateRequestDto;
Expand All @@ -23,6 +25,7 @@
import com.soongsil.CoffeeChat.dto.PossibleDateCreateGetResponseDto;
import com.soongsil.CoffeeChat.enums.ClubEnum;
import com.soongsil.CoffeeChat.enums.PartEnum;

import com.soongsil.CoffeeChat.service.MentorService;

import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -33,8 +36,10 @@
@RestController
@Tag(name = "MENTOR", description = "멘토 관련 api")
public class MentorController {

private final MentorService mentorService;


public MentorController(MentorService mentorService) {
this.mentorService = mentorService;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,10 @@ public ResponseEntity<UserGetUpdateDto> getUserInfo(Authentication authenticatio
HttpStatus.OK);
}

@PostMapping("/create")
public ResponseEntity<String> createUsersAndMentors() {
userService.createUsersAndMentors();
return ResponseEntity.ok("500 Users and Mentors have been created successfully.");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.soongsil.CoffeeChat.dto;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class ApplicationPerformanceRequestDto {
private ApplicationCreateRequest applicationCreateRequest;
private PerformanceRequest performanceRequest;
private int apiNum;
}
40 changes: 40 additions & 0 deletions src/main/java/com/soongsil/CoffeeChat/dto/PerformanceRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.soongsil.CoffeeChat.dto;

import lombok.Getter;
import lombok.Setter;

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

@Getter
@Setter
public class PerformanceRequest {
private int userCount;
private int durationInSeconds;
private int totalRequests;

// Getters and setters
public int getUserCount() {
return userCount;
}

public void setUserCount(int userCount) {
this.userCount = userCount;
}

public int getDurationInSeconds() {
return durationInSeconds;
}

public void setDurationInSeconds(int durationInSeconds) {
this.durationInSeconds = durationInSeconds;
}

public int getTotalRequests() {
return totalRequests;
}

public void setTotalRequests(int totalRequests) {
this.totalRequests = totalRequests;
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/soongsil/CoffeeChat/dto/PerformanceResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.soongsil.CoffeeChat.dto;

import lombok.Getter;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
public class PerformanceResult {
private List<Double> averageTimesPerThread;
private double overallAverageTime;

public PerformanceResult(List<Double> averageTimesPerThread, double overallAverageTime) {
this.averageTimesPerThread = averageTimesPerThread;
this.overallAverageTime = overallAverageTime;
}

// Getters and Setters
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.soongsil.CoffeeChat.service;


import static com.soongsil.CoffeeChat.controller.exception.enums.ApplicationErrorCode.*;
import static com.soongsil.CoffeeChat.controller.exception.enums.MentorErrorCode.*;
import static com.soongsil.CoffeeChat.controller.exception.enums.PossibleDateErrorCode.*;
Expand All @@ -15,9 +16,11 @@
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import com.soongsil.CoffeeChat.controller.exception.CustomException;
Expand All @@ -42,26 +45,49 @@
import jakarta.persistence.EntityManager;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;

import lombok.extern.slf4j.Slf4j;

@Service
@RequiredArgsConstructor
@Slf4j

public class ApplicationService {

private final EntityManager em;
private final ApplicationRepository applicationRepository;
private final MentorRepository mentorRepository;
private final MenteeRepository menteeRepository;
private final UserRepository userRepository;
private final PossibleDateRepository possibleDateRepository;
private final EmailUtil emailUtil;
private final ThreadPoolTaskExecutor executor;

public ApplicationService(EntityManager em,
ApplicationRepository applicationRepository,
MentorRepository mentorRepository,
MenteeRepository menteeRepository,
UserRepository userRepository,
PossibleDateRepository possibleDateRepository,
EmailUtil emailUtil,
@Qualifier("performanceExecutor") ThreadPoolTaskExecutor executor) {
this.em = em;
this.applicationRepository = applicationRepository;
this.mentorRepository = mentorRepository;
this.menteeRepository = menteeRepository;
this.userRepository = userRepository;
this.possibleDateRepository = possibleDateRepository;
this.emailUtil = emailUtil;
this.executor = executor;
}

@Autowired
private ApplicationContext applicationContext; // 프록시를 통해 자신을 호출하기 위해 ApplicationContext 주입

@Autowired
private RedisTemplate<String, String> redisTemplate;


@Transactional
public ApplicationCreateResponseDto createApplication(ApplicationCreateRequestDto request, String userName) throws
Exception {
Expand Down Expand Up @@ -160,6 +186,62 @@ public ApplicationCreateResponseDto createApplication(ApplicationCreateRequestDt
);
}

@Transactional
public ApplicationCreateResponse createApplicationWithJPA(ApplicationCreateRequest request, String userName) throws Exception {
System.out.println("여긴들어옴");
String lockKey = "lock:" + request.getMentorId() + ":" +request.getDate()+":"+ request.getStartTime();
ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();

boolean isLockAcquired = valueOperations.setIfAbsent(lockKey, "locked", 10, TimeUnit.SECONDS);
if (!isLockAcquired) {
throw new ResponseStatusException(HttpStatus.CONFLICT, "Lock을 획득하지 못하였습니다."); //409반환
}

try {
System.out.println("mentorid: " + request.getMentorId() + ", " + request.getDate() + ", " + request.getStartTime() + ", " + request.getEndTime());
Mentor findMentor = mentorRepository.findById(request.getMentorId()).get();
User findMentorUser = userRepository.findByMentor(findMentor);
User findMenteeUser = userRepository.findByUsername(userName);
Mentee findMentee = findMenteeUser.getMentee();

LocalTime startTime = request.getStartTime();
LocalDate date = request.getDate();

// possibleDate 불러오는 JPQL
TypedQuery<PossibleDate> query = em.createQuery(
"SELECT p FROM PossibleDate p JOIN p.mentor m WHERE m.id = :mentorId AND p.startTime = :startTime AND p.date = :date",
PossibleDate.class);
query.setParameter("mentorId", request.getMentorId());
query.setParameter("startTime", startTime);
query.setParameter("date", date);

Optional<PossibleDate> possibleDateOpt = query.getResultList().stream().findFirst();

if (possibleDateOpt.isPresent()) {
PossibleDate possibleDate = possibleDateOpt.get();
if (!possibleDate.isActive()) {
throw new ResponseStatusException(HttpStatus.GONE, "이미 신청된 시간입니다."); //410 반환
}
System.out.println("possibleDate.getId() = " + possibleDate.getId());
possibleDate.setActive(false);
possibleDateRepository.save(possibleDate);
} else {
throw new Exception("NOT FOUND");
}

Application savedApplication = applicationRepository.save(request.toEntity(findMentor, findMentee));

ApplicationService proxy = applicationContext.getBean(ApplicationService.class);
proxy.sendApplicationMatchedEmailAsync(findMenteeUser.getEmail(), findMentorUser.getName(),
findMenteeUser.getName(), savedApplication.getDate(), savedApplication.getStartTime(),
savedApplication.getEndTime());

return ApplicationCreateResponse.from(savedApplication);
} finally {
redisTemplate.delete(lockKey);
}
}

@Async("mailExecutor")
public void sendApplicationMatchedEmailAsync(String email, String mentorName, String menteeName, LocalDate date,
LocalTime startTime, LocalTime endTime) throws MessagingException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import static com.soongsil.CoffeeChat.controller.exception.enums.MentorErrorCode.*;


import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -34,6 +36,7 @@ public class MentorService {
private final MentorRepository mentorRepository;
private final UserRepository userRepository;
private final PossibleDateRepository possibleDateRepository;
private final ThreadPoolTaskExecutor executor;

public List<MentorGetListResponseDto> getMentorDtoListByPart(PartEnum part) {
return mentorRepository.getMentorListByPart(part); //일반join
Expand All @@ -45,6 +48,7 @@ public List<MentorGetListResponseDto> getMentorDtoListByClub(ClubEnum club) {

public List<MentorGetListResponseDto> getMentorDtoListByPartAndClub(PartEnum part, ClubEnum club) {
return mentorRepository.getMentorListByPartAndClub(part, club);

}

public List<PossibleDateCreateGetResponseDto> findPossibleDateListByMentor(Long mentorId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,11 @@ public ResponseEntity<?> reissueByRefreshToken(HttpServletRequest request, HttpS
String role = jwtUtil.getRole(refresh);

// Make new JWT

String newAccess = jwtUtil.createJwt("access", username, role, 1800000000L);
String newRefresh = jwtUtil.createJwt("refresh", username, role, 86400000L);


// Refresh 토큰 저장: DB에 기존의 Refresh 토큰 삭제 후 새 Refresh 토큰 저장
refreshRepository.deleteByRefresh(refresh);
addRefreshEntity(username, newRefresh, 86400000L);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.soongsil.CoffeeChat.service;


import java.util.HashMap;
import java.util.Map;

import com.soongsil.CoffeeChat.entity.Introduction;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
Expand All @@ -22,8 +24,10 @@

import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;

import lombok.extern.slf4j.Slf4j;


@Service
@RequiredArgsConstructor
@Slf4j
Expand Down Expand Up @@ -110,4 +114,5 @@ public UserGetUpdateDto findUserInfo(String username) {
User user = userRepository.findByUsername(username);
return UserGetUpdateDto.toDto(user);
}

}
Loading

0 comments on commit c026568

Please sign in to comment.