๋ฐ์ํ
Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
Tags
- ์ปด๊ณต์
- ์ดํญ๊ณ์
- ์น๊ฐ๋ฐ
- ์ฐ์ ์์ํ
- ์๊ณ ๋ฆฌ์ฆ
- ๋จ์ํ ์คํธ
- ChatGPT
- ๋ฐฑ์คํ์ด
- ๊ทธ๋ฆฌ๋
- ๊ฐ๋ฐ์
- ๋ฐฑ์๋
- ํ์ด์ฌ
- ์๋ฃ๊ตฌ์กฐ
- ๋ฐฑ์ค
- ์ปด๊ณต
- ์ฝ๋ฉ
- ํ๋ก ํธ์ค๋
- SSE
- ๋ฐฑ์ค1436
- ๋ฆฌ์กํธ
- boj11653
- ์ฝ๋ฉํ ์คํธ
- ์น๊ฐ๋ฐ๊ธฐ๋ก
- ๋ชจ๋ฐ์ผ์ฑํ๋ก๊ทธ๋๋ฐ
- ์ปดํจํฐ๊ณตํ
- ์คํ๋ง
- ํ๋ก๊ทธ๋๋ฐ
- ๋ฆฌ์กํธ๋ค์ดํฐ๋ธ
- spring
- ๊ทธ๋ฆฌ๋์๊ณ ๋ฆฌ์ฆ
Archives
- Today
- Total
๐ป๐ญ๐ง๐
[์น ๊ฐ๋ฐ ํ๋ก์ ํธ] 8. ์คํ๋ง Spring QR ์ฝ๋๋ก ์ถ์ ์ฒดํฌ ๊ธฐ๋ฅ ๋ณธ๋ฌธ
์น ๊ฐ๋ฐ ํ๋ก์ ํธ
[์น ๊ฐ๋ฐ ํ๋ก์ ํธ] 8. ์คํ๋ง Spring QR ์ฝ๋๋ก ์ถ์ ์ฒดํฌ ๊ธฐ๋ฅ
adorableco 2024. 1. 27. 17:25๋ฐ์ํ
์๊ตฌ์ฌํญ
- ๊ฒฝ๊ธฐ ์์ 30๋ถ ์ ๋ถํฐ ๊ฒฝ๊ธฐ ๋ฆฌ๋ ํ๋ฉด์์ QR ์ฝ๋๋ฅผ ๋์ธ ์ ์๋ค.
- ์ฐธ๊ฐ ์ธ์๋ค์ ์ด QR ์ฝ๋๋ฅผ ์ดฌ์ํ์ฌ ์ถ์ ์ฒดํฌ๋ฅผ ํ๋ค.
์คํ ์์ค์ธ zxing ์ ์ฌ์ฉํ์๋ค. ์ฝ๋๊ฐ ๊ฐ๊ฒฐํด์ ์ฌ์ฉํ๊ธฐ ์ฌ์ ๋ค!
build.gradle ํ์ผ์ ์์กด์ฑ์ ์ถ๊ฐํด์ค๋ค.
implementation group: 'com.google.zxing', name: 'javase', version: '3.5.0'
implementation group: 'com.google.zxing', name: 'core', version: '3.5.0'
createQr(User user, Long matchId)
QR ์ฝ๋๋ฅผ ์์ฑํ์ฌ ๋ฐ์ดํธ ํ์์ผ๋ก ๋ฐํํ๋ ๋ฉ์๋
public ResponseEntity<byte[]> createQr(User user, Long matchId) throws Exception {
Match match = matchRepository.findById(matchId).get();
int width = 200;
int height = 200;
long time = differenceTime(match);
if (match.getUser().getUserId() != user.getUserId()){
return ResponseEntity.badRequest()
.body("๊ฒฝ๊ธฐ ๋ฆฌ๋๊ฐ ์๋".getBytes(StandardCharsets.UTF_8));
}
else if (time > 30 * 60000 || time < -10 * 60000) {
return ResponseEntity.badRequest().body("์ถ์ ์ฒดํฌ ๊ฐ๋ฅ ์๊ฐ์ด ์๋".getBytes(StandardCharsets.UTF_8));
}
String url = "http://localhost:8080/api/attendance/"+matchId;
BitMatrix encode = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, width, height);
ByteArrayOutputStream out = new ByteArrayOutputStream();
MatrixToImageWriter.writeToStream(encode, "PNG", out);
return ResponseEntity.ok()
.contentType(MediaType.IMAGE_PNG)
.body(out.toByteArray());
}
- if๋ฌธ๋ถํฐ ๋จผ์ ๋ณด๋ฉด match ๋ฅผ ๋ฑ๋กํ ์ ์ , ์ฆ ๊ฒฝ๊ธฐ ๋ฆฌ๋์ธ์ง๋ฅผ ๋จผ์ ํ์ธํ ํ ์๋๋ฉด badRequest๋ก ์ฒ๋ฆฌํ๋ค.
โ ๊ทผ๋ฐ ๋ฐํํ์ ์ด ResponseEntity<byte[]> ์ผ ๋ ์ค๋ฅ๋ฅผ String ์ผ๋ก ๋ฐํํ๊ณ ์ถ์ผ๋ฉด ์ด๋ป๊ฒ ํด์ผํ๋ ค๋.. ์ ๋ฒ๋ถํฐ ๊ถ๊ธํ๋๋ฐ ๊ท์ฐฎ์์ ๋์ค์ ์ฐพ์๋ณผ ์์ ...^^ - ๋ค์ ์ ์ฝ์กฐ๊ฑด์ ์ถ์์ฒดํฌ ๊ฐ๋ฅ ์๊ฐ์ ๊ดํ ๊ฒ์ธ๋ฐ if๋ฌธ ์์์ ํ์ฌ ์๊ฐ๊ณผ ๊ฒฝ๊ธฐ ์์ ์๊ฐ์ ๋น๊ตํด์ ์ฐจ์ด๊ฐ์ ์ด ๋จ์๋ก ๋ฐํํ๋
differenceTime
๋ฉ์๋์ ๊ฒฐ๊ณผ๋ฅผtime
๋ณ์์ ๋ด์๋์๋ค. ๊ฒฝ๊ธฐ ์์ ์๊ฐ 30๋ถ ์ ์์ ๊ฒฝ๊ธฐ ์์ ํ 10๋ถ ์ฌ์ด๊ฐ ์๋๋ผ๋ฉด ์ถ์์ฒดํฌ๊ฐ ๋ถ๊ฐ๋ฅํ๋ฏ๋ก badRequest๋ก ์ฒ๋ฆฌํ๋ค.
โ
differenceTime()
์ ๋ค์๊ณผ ๊ฐ๋ค.
private long differenceTime(Match match) throws ParseException {
String now= LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).toString();
String matchTime= match.getStartTime().toString();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1 = format.parse(now);
Date d2 = format.parse(matchTime);
long sec = (d2.getTime() - d1.getTime()) / 1000;
long min = (d2.getTime() - d1.getTime()) / 60000;
long hour = (d2.getTime() - d1.getTime()) / 3600000;
long days = sec / (24*60*60);
return sec + min + hour + days;
}
์ด์ ๋ชจ๋ ์กฐ๊ฑด์ ๋ง์กฑํ๋ฉด qr์ฝ๋๋ฅผ ์์ฑํ๋ค!
- String
url
์๋ QR์ฝ๋ ์ค์บ์ ํตํด ์ด๋ํ url์ ๋ด์์ค๋ค.- ์ฌ์ค ๋ด๊ฐ ์ํ๋ ์ด์์ ์ธ ๋ฐฉ๋ฒ์ QR์ฝ๋๋ฅผ ์ค์บํด์ ์ด๋ํ๊ฒ๋ url์ด ๋ฐ๋ก ์ถ์ ์ฒดํฌ api๋ก ์ฐ๊ฒฐ๋์ด์ ์ถ์ ์ฒดํฌ๊ฐ ์๋ฃ๋๋ ๊ฒ์ธ๋ฐ
BitMatrix
๊ฐ์ฒด์์ ๊ตฌํํ ์๋ ์๋๊ฑด์ง ๊ฐ์ด ๋ํต ์์์.. ์ผ๋จ ํ๋ก ํธ์ชฝ์์ ๋ค์ POST๋ก ์์ฒญ์ ์ค url์ ์ ๋ฌํ๋ ๋ฐฉ์์ผ๋ก ํ๊ณ ํ๋ก ํธ์์ response๋ก ๋ฐ์๊ฑธ ๋ค์ api ์์ฒญ์ ํด์ฃผ๋ฉด ๋๋ค. ์กฐ๊ธ.. ๋ฒ๊ฑฐ๋ก์ด ๊ฒ ๊ฐ๋ค
- ์ฌ์ค ๋ด๊ฐ ์ํ๋ ์ด์์ ์ธ ๋ฐฉ๋ฒ์ QR์ฝ๋๋ฅผ ์ค์บํด์ ์ด๋ํ๊ฒ๋ url์ด ๋ฐ๋ก ์ถ์ ์ฒดํฌ api๋ก ์ฐ๊ฒฐ๋์ด์ ์ถ์ ์ฒดํฌ๊ฐ ์๋ฃ๋๋ ๊ฒ์ธ๋ฐ
BitMatrix
๊ฐ์ฒด๋width
,height
, ๋ฐ์ฝ๋ ํฌ๋งท ์คQR_CODE
, ๊ทธ๋ฆฌ๊ณ ์์์ ์ง์ ํurl
์ ๋ด์ ์์ฑํ๋ค.
qrToAttendance(Long matchId, String userEmail)
๊ฒฝ๊ธฐ ์ฐธ๊ฐ ์ธ์์ด ์์ฑ๋ qr ์ฝ๋๋ฅผ ์ค์บํ ํ์ ์ถ์์ฒดํฌ๋ฅผ ์ฒ๋ฆฌ๊ฐ ๋๋ ๋ฉ์๋
public ResponseEntity<String> qrToAttendance(Long matchId, String userEmail) throws Exception {
Match match = matchRepository.findById(matchId).get();
Optional<Participation> participation = participationService.findByMatchAndUser(matchId, userEmail);
long time = differenceTime(match);
if (time > 30 * 40 || time < -10 * 40) {
return ResponseEntity.badRequest().body("์ถ์ ์ฒดํฌ ๊ฐ๋ฅ ์๊ฐ์ด ์๋");
}
else if (participation.get().isAttendance()) {
return ResponseEntity.badRequest().body("์ด๋ฏธ ์ถ์ ์ฒดํฌ ์๋ฃ");
} else {
return participationService.checkAttendance(matchId, userEmail);
}
}
- ์ฌ๊ธฐ์๋ qr์์ฑ ๋ฉ์๋์ ๊ฐ์ด ์๊ฐ ์ ์ฝ ์กฐ๊ฑด์ ๋ฐ๋ผ ์ฒ๋ฆฌํ๋ค.
- ์ด์ฐจํผ qr์ฝ๋ ์์ฑ ์์ฒด๊ฐ ์๋๋ฉด ์ถ์ ์ฒดํฌ ์๋๋ ํ ์ ์์ง๋ง?.. ์๊ฐ ๋ด์ ์์ฑํ qr์ฝ๋๋ฅผ ์บก์ฒํ ํ์ ๋์ค์ ์ค์บํ๋ ๊ฒฝ์ฐ๋ ์์ ์ ์๊ธฐ ๋๋ฌธ์ ์ฌ๊ธฐ์๋ ์๊ฐ ์ ์ฝ ์กฐ๊ฑด์ ์ถ๊ฐํด์ค์ผ ํ๊ธดํ๋ค.
- ๋ํ ์ด๋ฏธ ์ถ์ ์ฒดํฌ๊ฐ ์๋ฃ๋ ๊ฒฝ์ฐ์๋ badRequest ๋ก ์ฒ๋ฆฌํ๋ค.
- ๋ชจ๋ ์กฐ๊ฑด์ ๋ง์กฑํ๋ฉด participationService์
checkAttendance
๋ฉ์๋๋ฅผ ํตํด ์ถ์์ฒดํฌ๋ฅผ ํด์ค๋ค.
checkAttendance(Long matchId, String userEmail)
@Transactional
public ResponseEntity<String> checkAttendance(Long matchId, String userEmail) {
try {
Match match = matchRepository.findById(matchId).get();
User user = userRepository.findByEmail(userEmail).get();
Participation participation = participationRespository.findByMatchAndUser(match, user).get();
participation.setAttendance(true);
int attendanceCount = match.getAttendanceCnt();
match.setAttendanceCnt(attendanceCount + 1);
return ResponseEntity.ok().body("์ถ์ ์ฒดํฌ ์๋ฃ");
}catch (Exception e){
return ResponseEntity.badRequest().body("์ถ์ ์ฒดํฌ ์คํจ");
}
}
participation.setAttendance(true);
โก๏ธ ์ ์ ์ participation (๊ฐ์ธ ๊ฒฝ๊ธฐ ์ฐธ๊ฐ ์ ๋ณด) ์ ์ถ์ ์ฌ๋ถ๋ฅผ true
๋ก ์ธํ
ํด์ฃผ๊ณ
int attendanceCount = match.getAttendanceCnt();
match.setAttendanceCnt(attendanceCount + 1);
โก๏ธ match (๊ฒฝ๊ธฐ ์ฐธ๊ฐ ๊ณตํต ์ ๋ณด)์ ์ถ์์ธ์ ์๋ฅผ 1 ์ฆ๊ฐ์์ผ์ค๋ค.
๊ฑฑ์ ํ๋ ๊ธฐ๋ฅ์ด์๋๋ฐ ์๊ฐํด๋ณด๋ค ์ํํ๊ฒ ์ ๋๋๋ค! ๐
์คํ๊ฒฐ๊ณผ
- ์ฑ๊ณต!
- ๊ฒฝ๊ธฐ๋ฆฌ๋๊ฐ ์๋ ๊ฒฝ์ฐ
- ์ถ์ ๊ฐ๋ฅ ์๊ฐ์ด ์๋ ๊ฒฝ์ฐ
๋ค์ ํ ์ผ
- ์ถ์๋ฅ , ์น๋ฅ ๋ฑ์ ๊ธฐ๋ฐ์ผ๋ก ํ์ ํ์ด์ง์์ ์ฌ์ฉํ api๋ฅผ ๊ตฌํ
๋ฐ์ํ