| ์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 | 31 |
- ์ปดํจํฐ๊ณตํ
- ๋ฆฌ์กํธ
- ๋ฐฑ์ค
- ChatGPT
- ์ฝ๋ฉํ ์คํธ
- ์น๊ฐ๋ฐ๊ธฐ๋ก
- ์๋ฃ๊ตฌ์กฐ
- ๋ฆฌ์กํธ๋ค์ดํฐ๋ธ
- ์ฐ์ ์์ํ
- ํ๋ก๊ทธ๋๋ฐ
- ์ปด๊ณต
- ๋จ์ํ ์คํธ
- spring
- ๊ทธ๋ฆฌ๋์๊ณ ๋ฆฌ์ฆ
- ๋ชจ๋ฐ์ผ์ฑํ๋ก๊ทธ๋๋ฐ
- ๋ฐฑ์ค1436
- ๊ฐ๋ฐ์
- ์ดํญ๊ณ์
- ์น๊ฐ๋ฐ
- ์๊ณ ๋ฆฌ์ฆ
- ์ฝ๋ฉ
- SSE
- ํ์ด์ฌ
- ๊ทธ๋ฆฌ๋
- ์ปด๊ณต์
- ์คํ๋ง
- boj11653
- ํ๋ก ํธ์ค๋
- ๋ฐฑ์๋
- ๋ฐฑ์คํ์ด
- Today
- Total
๐ป๐ญ๐ง๐
[์น ๊ฐ๋ฐ ํ๋ก์ ํธ] 6. ๊ฒฝ๊ธฐ ์ฐธ๊ฐ ์ ์ฒญ ๊ธฐ๋ฅ ๊ตฌํ ๋ฐ ๋ชจ์ง๊ธ ํ๊ทธ๋ณ ํํฐ๋ง ๊ตฌํ ๋ณธ๋ฌธ
[์น ๊ฐ๋ฐ ํ๋ก์ ํธ] 6. ๊ฒฝ๊ธฐ ์ฐธ๊ฐ ์ ์ฒญ ๊ธฐ๋ฅ ๊ตฌํ ๋ฐ ๋ชจ์ง๊ธ ํ๊ทธ๋ณ ํํฐ๋ง ๊ตฌํ
adorableco 2024. 1. 18. 21:20์ฌ์ค ์ ๋ฉ์ธ ๊ธฐ๋ฅ์ธ ์ฐธ๊ฐ ์ ์ฒญ ๊ธฐ๋ฅ์ด๋ค. ์ฒ ์ ํ ๊ตฌ๋ถํด์ ๊ตฌํํ๋ ๊ธฐ๋ฅ๋ค์ด ์ด์ ๋ ์ด๊ฒ์ ๊ฒ ์์ด๊ธฐ ์์ํ๋๊น ์ด ๋ฉ์๋๋ ์ฌ๊ธฐ๋ค๊ฐ ๋๋๊ฒ ๋์๊น ์ ๊ธฐ๋ค๊ฐ ๋๋๊ฒ ๋์๊น.. ํ๋ ๊ณ ๋ฏผ์ด ์๊ธฐ๊ธฐ ์์ํ๋ ๊ฒ ๊ฐ๋ค.
๊ทธ๋ฆฌ๊ณ ์ ์ ๋น์ฆ๋์ค ๋ก์ง ์ฐจ์์์ ๋ค๋ค์ผํ ์ ์ฝ ์กฐ๊ฑด๋ค์ด ์๊ธฐ๋ ์ ๋ฒ ์ฌ๋ฐ๋ค !! ๐ฅน๐
๊ฒฝ๊ธฐ ์ฐธ๊ฐ ์ ์ฒญ ๊ธฐ๋ฅ์ ์กฐ๊ฑด์ ๋ค์๊ณผ ๊ฐ๋ค.
<์ฐธ๊ฐ ์ ์ฒญ ๊ธฐ๋ฅ ์กฐ๊ฑด>
- โ user์ ์กฐ๊ฑด์ด ํด๋น ๋งค์น ํ๊ทธ๋ค์ ์กฐ๊ฑด๊ณผ ๋ถํฉํ์ง ์์ ๊ฒฝ์ฐ ์ฐธ๊ฐ ์ ์ฒญ ๋ถ๊ฐ
- โ match์ ํ์ฌ ์ฐธ๊ฐ ์ ์ฒญ ์ธ์์ด ์ ์ฒด ๋ชจ์ง ์ธ์๊ณผ ๊ฐ์ ๊ฒฝ์ฐ, (์ฐธ๊ฐ ์ธ์์ ๋ชจ๋ ๋ชจ์งํ์ ๊ฒฝ์ฐ) ์ฐธ๊ฐ ์ ์ฒญ ๋ถ๊ฐ
- โ user ๊ฐ ์ด๋ฏธ ์ฐธ๊ฐ ์ ์ฒญํ ๋งค์น๋ค๊ณผ ์๊ฐ์ด ๊ฒน์น๋ฉด ์ฐธ๊ฐ ์ ์ฒญ ๋ถ๊ฐ
- โ ์ด์ธ๋ ์ฐธ๊ฐ ์ ์ฒญ ์๋ฃ ํ์ ํด๋น ๋งค์น์ ํ์ฌ ์ฐธ๊ฐ ์ ์ฒญ ์ธ์ ์ 1 ์ฆ๊ฐ
์ด ์กฐ๊ฑด ์ค์์๋ ๋ถ๋ช ํ๋ก์ ํธ ์ด๋ฐ์ ์๊ตฌ์ฌํญ ์ ๋ฆฌ๋ฅผ ์์ธํ๊ฒ ํด๋๊ณ ์์ํ๋๋ฐ ๊ฐ๋ฐํ๋ค๋ณด๋ ์๊ฐ๋์ ๋์ค์ ์์ ํ ๊ฒ๋ค์ด ๋ง๋ค...^^
1. ParticipationService ์ save() method
@Transactional
public String save(Long matchId, String userEmail){
Match match = matchRepository.findById(matchId).get();
Tag tag = match.getTag();
User user = userRepository.findByEmail(userEmail).get();
if (!(tag.getAgeType().equals(user.getAge())) || (tag.getGenderType() != user.getGender()) || !(tag.getLevelType().equals(user.getLevel()))
){
return "์ฐธ๊ฐ ์กฐ๊ฑด ๋ฏธ์ถฉ์กฑ" ;
}else if (match.getCurrentHeadCnt() == match.getHeadCnt()) {
return "์ฐธ๊ฐ ์ธ์ ๋ง๊ฐ";
}
else if (matchRepository.findDuplicateMatch(user.getUserId(),match.getStartTime(),match.getEndTime()) > 0) {
System.out.println("num = " + matchRepository.findDuplicateMatch(user.getUserId(),match.getStartTime(),match.getEndTime()));
return "์๊ฐ์ด ๊ฒน์น๋ ์ ์ฒญ ๋งค์น ์กด์ฌ" ;
} else{
Participation build = Participation.builder()
.user(user)
.match(match)
.build();
participationRespository.save(build);
match.setCurrentHeadCnt(match.getCurrentHeadCnt()+1);
return "์ฐธ๊ฐ ์ ์ฒญ ์๋ฃ";
}
}
if ๋ฌธ์์ ์ด 3๋ฒ์ ๋ถ๊ธฐ๊ฐ ์์์ ์ ์ ์๋ค. ๋ชจ๋ ์์์ ๋ช ์ํ ์ฐธ๊ฐ ์ ์ฒญ ์กฐ๊ฑด ์ ๋ฐ๋ผ ๋ถ๊ธฐํ ๊ฒ์ผ๋ก ์กฐ๊ฑด์ ๋ฐ๋ผ ์ฐธ๊ฐ ์ ์ฒญ์ด ์น์ธ๋๊ฑฐ๋, ๊ฑฐ์ ๋ ๊ฒ์ด๋ค.
if (!(tag.getAgeType().equals(user.getAge())) || (tag.getGenderType() != user.getGender()) || !(tag.getLevelType().equals(user.getLevel()))
)
โก๏ธ ๋งค์น ํ๊ทธ์ ์ ์ ์ ์กฐ๊ฑด์ด ๋ถํฉํ์ง ์์ผ๋ฉด ์ฐธ๊ฐ ์ ์ฒญ์ด ๋ถ๊ฐํ๋ค.
- ๊ทผ๋ฐ ์ง๊ธ ๊ธ์ ์ฐ๊ณ ์๋ค๊ฐ ์๊ฐ๋๊ฑด๋ฐ tag์ ๊ฐ ์ธ ๊ฐ์ ํ๋๋ค์
null์ด ๊ฐ๋ฅํด์..null์ธ ๊ฒฝ์ฐ์๋ ๋น๊ตํ ํ์๋ ์์ด์ผํ๋๊ฑด๋ฐ ์ด ์ฝ๋์์ ๋ค๋ฅด๋ค๊ณ ํ๋จ์ ํ๊ฒ ๊ตฌ๋... ๊ณง ํฝ์คํด๋ฌ์ผ๊ฒ ๋ค......
else if (match.getCurrentHeadCnt() == match.getHeadCnt())
โก๏ธ ๋งค์น์ ํ์ฌ ์ฐธ๊ฐ ์ธ์ ์์ ์ ์ฒด ์ฐธ๊ฐ ์ธ์ ์๊ฐ ๊ฐ๋ค๋ฉด ์ฐธ๊ฐ ์ ์ฒญ์ด ๋ถ๊ฐํ๋ค.
- ์ฝ๊ฒ ๋งํด์ ๋ชจ์ง ์ธ์์ด ๋ค ๋ง๊ฐ๋ ๋งค์น์ ๊ฒฝ์ฐ์๋ ์ฐธ๊ฐ ์ ์ฒญ์ ํ ์ ์๋ ๊ฒ์ด๋ค.
else if (matchRepository.findDuplicateMatch(user.getUserId(),match.getStartTime(),match.getEndTime()) > 0)
โก๏ธ ์ ์ฒญํ๊ณ ์ ํ๋ ๋งค์น์ ์์ ์๊ฐ, ์ข
๋ฃ ์๊ฐ, ๊ทธ๋ฆฌ๊ณ userId๋ฅผ ์ ๋ฌ ์ธ์๋ก ๋ฐ๋ findDuplicateMatch ๋ฉ์๋์ ๋ฆฌํด๊ฐ์ด 0 ์ด์, ์ฆ ํด๋น ์ ์ ๊ฐ ์ด๋ฏธ ์ฐธ๊ฐ ์ ์ฒญํ ๋งค์น๋ค ์ค์ ์๊ฐ์ด ๊ฒน์น๋ ๋งค์น๊ฐ ์กด์ฌํ๋ฉด ์ฐธ๊ฐ ์ ์ฒญ์ด ๋ถ๊ฐํ๋ค.
findDuplicateMatch() ๋ MatchRepository ์์ ๋ค๋ฃฐ๊น, ParticipationRepository ์์ ๋ค๋ฃฐ๊น ๊ณ ๋ฏผํ๋ค๊ฐ, Participation ๊ณผ ์ฐ๊ด์ด ์๋ ๋ฉ์๋์ด๊ธดํ๋ ์ด์จ๋ ๋งค์น ๊ฐ์๋ฅผ ๋ฆฌํดํ๋ ๊ฒ์ด๋ ์ ์์์ ๋ค๋ฃจ๊ธฐ๋ก ํ๋ค.
public interface MatchRepository extends JpaRepository<Match, Long> {
@Query("select count(m.matchId) from Match m join Participation p on m = p.match join User u on p.user = u where u.userId = :userId and m.startTime <= :endTime and m.endTime >= :startTime")
int findDuplicateMatch(Long userId, Date startTime, Date endTime);
}
โก๏ธ Match, Participation, User ์ join์ผ๋ก ๋ฌถ์ด์ ํด๋น ์ ์ ์ ์ฐธ๊ฐ ์ ์ฒญ ๋งค์น ์ค ์๊ฐ์ด ๊ฒน์น๋ ๋งค์น์ ์๋ฅผ ์นด์ดํธํด์ ๋ฐํํ๋๋ก ํ๋ค. ์ฌ๊ธฐ์์๋ ์์ ์๊ฐ์ด (์ฐธ๊ฐ ์ ์ฒญํ ) ๋งค์น์ ์ข
๋ฃ ์๊ฐ๋ณด๋ค ๊ฐ๊ฑฐ๋ ๋จผ์ ์ด๊ณ , ์ข
๋ฃ ์๊ฐ์ด (์ฐธ๊ฐ ์ ์ฒญํ ) ๋งค์น์ ์์ ์๊ฐ๋ณด๋ค ๋์ค ์ด๋ผ๋ ์กฐ๊ฑด์ผ๋ก ์๊ฐ์ด ๊ฒน์น๋ ๋งค์น๋ฅผ ์ฐพ์ ์ ์๋๋ก ํ๋ค.
2. ParticipationController
@RequiredArgsConstructor
@RestController
public class ParticipationController {
private final ParticipationService participationService;
@PostMapping("/api/participation/{matchId}")
ResponseEntity<String> applyMatch(@PathVariable Long matchId, @RequestBody AddParticipationRequest request){
return ResponseEntity.ok()
.body(participationService.save(matchId, request.getUserEmail()));
}
}
3. AddParticipationRequest dto
@Setter
@Getter
@NoArgsConstructor
public class AddParticipationRequest {
String userEmail;
}
์ ์ ํ์ธ์ฉ์ผ๋ก ์ฌ์ฉํ userEmail ์ ๋ฐ๋๋ค. ์ด๊ฒ๋ ๋นจ๋ฆฌ ํ ํฐ์ผ๋ก ๋ฐ๊ฟ์ผํ๋๋ฐ..
๊ทธ๋ฆฌ๊ณ ์ด๊ฑฐ ํ๋๋ง ๋ฐ์๋ ๋๋๋ฐ dto๊น์ง ๋ง๋ค์ด์ผํ๋ ๋ผ๋ ์๋ฌธ์ด ๋ค์๋ค. ๋ญ๊ฐ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ด ์์ ๊ฒ ๊ฐ์๋ฐ ... ์ฐพ์๋ด์ผ์ง
์ ๋๋ก ์๋๋จ๋ ํ์ธ ์๋ฃ! ๐

๋ค์ ํ ์ผ
- ์ด์ ๋ ๋ฏธ๋ฃจ๊ณ ๋ฏธ๋ค๋๋ jwt ํ ํฐ์ ๋ค๋ค์ผ ํ ๋