Skip to content

Commit f2a98ea

Browse files
authored
Merge pull request #12 from Lunawood/master
feat: Add ToothController Function, it works tooth data report.
2 parents 5859c68 + a82bb53 commit f2a98ea

File tree

10 files changed

+191
-12
lines changed

10 files changed

+191
-12
lines changed

docker-test-server/sql/init.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ CREATE TABLE user_tb (
55
id BIGINT PRIMARY KEY,
66
pet_name VARCHAR(50) NOT NULL,
77
pet_weight INT NOT NULL,
8-
random_id VARCHAR(255) NOT NULL
8+
random_id VARCHAR(255) NOT NULL,
9+
tooth_seq INT NOT NULL DEFAULT 0,
10+
tooth_date_renew Date NOT NULL,
911
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

docker-test-server/src/main/java/com/example/server/controller/RegisterController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public ResponseEntity<?> register(
6969

7070
// 4. 모든 정보 저장.
7171
try {
72-
registerService.createUserInfo(userId, randomId, userPet.getPetName(), userPet.getPetWeight());;
72+
registerService.createUserInfo(userId, randomId, userPet.getPetName(), userPet.getPetWeight());
7373
} catch(Exception e) {
7474
throw new CException(ErrorBase.INTERNAL_SERVER_ERROR);
7575
}

docker-test-server/src/main/java/com/example/server/controller/ToothController.java

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
package com.example.server.controller;
22

3+
import java.time.LocalDate;
4+
import java.util.ArrayList;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
39
import org.springframework.http.ResponseEntity;
410
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.RequestBody;
512
import org.springframework.web.bind.annotation.RequestHeader;
613
import org.springframework.web.bind.annotation.RequestMapping;
714
import org.springframework.web.bind.annotation.RestController;
@@ -11,7 +18,11 @@
1118
import com.example.server.Response.ErrorBase;
1219
import com.example.server.Response.SuccessBase;
1320
import com.example.server.jwt.JwtTokenService;
21+
import com.example.server.model.ToothDataAnalyzer;
22+
import com.example.server.model.User;
23+
import com.example.server.model.ToothDataAnalyzer.ToothReport;
1424
import com.example.server.service.InvalidTokenService;
25+
import com.example.server.service.ToothService;
1526

1627
import lombok.RequiredArgsConstructor;
1728

@@ -21,9 +32,15 @@
2132
public class ToothController {
2233
private final JwtTokenService jwtTokenService;
2334
private final InvalidTokenService invalidTokenService;
35+
private final ToothService toothService;
36+
private final Double totalCnt = 500.0;
2437

2538
@GetMapping("")
26-
public ResponseEntity<?> mypageGetinfo(@RequestHeader("AccessToken") String AccessToken) {
39+
public ResponseEntity<?> mypageGetinfo
40+
(
41+
@RequestHeader("AccessToken") String AccessToken,
42+
@RequestBody List<String> Instructions
43+
) {
2744
// 1. RefreshToken Valid?
2845
try {
2946
if(jwtTokenService.validateAccessToken(AccessToken) == false) {
@@ -44,11 +61,69 @@ public ResponseEntity<?> mypageGetinfo(@RequestHeader("AccessToken") String Acce
4461
throw new CException(ErrorBase.INVALID_TOKEN);
4562
}
4663

47-
// 3. Create New Data
64+
// 3. Count Frequency
65+
Map<String, Integer> frequency = new HashMap<>();
66+
67+
for (String instruction : Instructions) {
68+
frequency.put(instruction, frequency.getOrDefault(instruction, 0) + 1);
69+
}
70+
71+
List<ToothReport> toothReports = new ArrayList<>();
72+
73+
for (Map.Entry<String, Integer> entry : frequency.entrySet()) {
74+
ToothReport toothReport = new ToothReport();
75+
System.out.println(entry.getKey() + ": " + entry.getValue());
76+
String koreanPart = toothService.toothPartEngToKorFunction(entry.getKey());
77+
if(koreanPart == null) {
78+
continue;
79+
}
80+
String percentStr = String.format("%.2f", entry.getValue() / totalCnt);
81+
if(100 < Double.parseDouble(percentStr))
82+
percentStr = "100.00";
83+
toothReport.setName(koreanPart);
84+
toothReport.setPercent(percentStr);
85+
toothReport.setDescription(toothService.evaluationPercentValue(Double.parseDouble(percentStr)));
86+
toothReports.add(toothReport);
87+
}
88+
89+
// 4. Get Sequence Data
90+
User user = null;
91+
try {
92+
user = toothService.getToothSeqByUserId(userId);
93+
} catch (Exception e) {
94+
throw new CException(ErrorBase.INTERNAL_SERVER_ERROR);
95+
}
96+
97+
// 5. Check Date if Date yesterday or after, change value
98+
LocalDate userDateRenew = user.getToothDateRenew();
99+
LocalDate nowDate = LocalDate.now();
100+
if(!userDateRenew.isEqual(nowDate)) {
101+
if(userDateRenew.plusDays(1).isEqual(nowDate)) {
102+
// Seq += 1
103+
user.setToothSeq(user.getToothSeq() + 1);
104+
}
105+
else if (userDateRenew.plusDays(1).isAfter(nowDate)) {
106+
// init Seq = 0
107+
user.setToothSeq(0);
108+
}
109+
110+
// 6. 모든 값 저장
111+
try {
112+
toothService.setSeqAndDateByUserId(userId, user.getToothSeq(), nowDate);
113+
} catch(Exception e) {
114+
throw new CException(ErrorBase.INTERNAL_SERVER_ERROR);
115+
}
116+
}
117+
118+
119+
48120

121+
ToothDataAnalyzer toothDataAnalyzer = new ToothDataAnalyzer();
122+
toothDataAnalyzer.setReports(toothReports);
123+
toothDataAnalyzer.setSeq(user.getToothSeq());
49124

50125
return ResponseEntity
51126
.status(SuccessBase.SUCCESS.getStatus())
52-
.body(BaseResponse.success(SuccessBase.SUCCESS, "."));
127+
.body(BaseResponse.success(SuccessBase.SUCCESS, toothDataAnalyzer));
53128
}
54129
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.example.server.model;
2+
3+
import java.util.List;
4+
5+
import lombok.Getter;
6+
import lombok.Setter;
7+
8+
@Getter
9+
@Setter
10+
public class ToothDataAnalyzer {
11+
private List<ToothReport> reports;
12+
private int seq = 0;
13+
14+
@Getter
15+
@Setter
16+
public static class ToothReport {
17+
private String name;
18+
private String percent;
19+
private String description;
20+
}
21+
}

docker-test-server/src/main/java/com/example/server/model/User.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.example.server.model;
22

3+
import java.time.LocalDate;
4+
import java.time.ZoneId;
5+
36
import javax.persistence.Column;
47
import javax.persistence.Entity;
58
import javax.persistence.Id;
@@ -27,4 +30,10 @@ public class User {
2730

2831
@Column(name = "random_id")
2932
private String randomId;
33+
34+
@Column(name = "tooth_seq")
35+
private Integer toothSeq = 0;
36+
37+
@Column(name = "tooth_date_renew")
38+
private LocalDate toothDateRenew = LocalDate.now(ZoneId.of("Asia/Seoul"));
3039
}

docker-test-server/src/main/java/com/example/server/repository/UserRepository.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.example.server.repository;
22

3+
import java.time.LocalDate;
4+
35
import org.springframework.data.jpa.repository.JpaRepository;
46
import org.springframework.data.jpa.repository.Query;
57
import org.springframework.data.jpa.repository.Modifying;
@@ -19,19 +21,24 @@ public interface UserRepository extends JpaRepository<User, Long> {
1921

2022
@Modifying
2123
@Transactional
22-
@Query(value = "INSERT INTO user_tb (id, pet_name, pet_weight, random_id) VALUES (:#{#user.id}, :#{#user.petName}, :#{#user.petWeight}, :#{#user.randomId})", nativeQuery = true)
24+
@Query(value = "INSERT INTO user_tb (id, pet_name, pet_weight, random_id, tooth_seq, tooth_date_renew) VALUES (:#{#user.id}, :#{#user.petName}, :#{#user.petWeight}, :#{#user.randomId}, :#{#user.toothSeq}, :#{#user.toothDateRenew})", nativeQuery = true)
2325
void insertUserInformation(@Param("user") User user);
2426

2527
@Query("SELECT u FROM User u WHERE u.id = :id")
26-
User getUserInformationByAccessToken(@Param("id") Long id);
28+
User getUserInformationById(@Param("id") Long id);
2729

2830
@Modifying
2931
@Transactional
3032
@Query(value = "UPDATE user_tb u SET u.pet_name=:petName, u.pet_weight=:petWeight WHERE u.id=:id", nativeQuery = true)
31-
void setUserPetInformationByAccessToken(@Param("id") Long id, @Param("petName") String petName, @Param("petWeight") Integer petWeight);
33+
void setUserPetInformationById(@Param("id") Long id, @Param("petName") String petName, @Param("petWeight") Integer petWeight);
3234

3335
@Modifying
3436
@Transactional
3537
@Query(value = "UPDATE user_tb u SET u.random_id=:randomId, u.random_id=:randomId WHERE u.id = :id", nativeQuery = true)
3638
void updateRandomIdByUserId(@Param("id") Long id, @Param("randomId") String randomId);
39+
40+
@Modifying
41+
@Transactional
42+
@Query(value = "UPDATE user_tb u SET u.tooth_seq=:toothSeq, u.tooth_date_renew=:toothDateRenew WHERE u.id = :id", nativeQuery = true)
43+
void updateSeqAndDateByUserId(@Param("id") Long id, @Param("toothSeq") int toothSeq, @Param("toothDateRenew") LocalDate toothDateRenew);
3744
}

docker-test-server/src/main/java/com/example/server/service/HomeService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ public class HomeService {
1313
private final UserRepository userRepository;
1414

1515
public User getPetInformation(Long userId) throws Exception{
16-
return userRepository.getUserInformationByAccessToken(userId);
16+
return userRepository.getUserInformationById(userId);
1717
}
1818
}

docker-test-server/src/main/java/com/example/server/service/RegisterService.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.example.server.service;
22

33

4+
import java.time.LocalDate;
5+
import java.time.ZoneId;
6+
47
import org.springframework.http.HttpEntity;
58
import org.springframework.http.HttpHeaders;
69
import org.springframework.http.MediaType;
@@ -58,7 +61,9 @@ public void createUserInfo(
5861
userId,
5962
PetName,
6063
PetWeight,
61-
RandomID
64+
RandomID,
65+
0,
66+
LocalDate.now(ZoneId.of("Asia/Seoul"))
6267
);
6368

6469
// Database에 Data 저장.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.example.server.service;
2+
3+
import java.time.LocalDate;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
import javax.annotation.PostConstruct;
8+
9+
import org.springframework.stereotype.Service;
10+
11+
import com.example.server.model.User;
12+
import com.example.server.repository.UserRepository;
13+
14+
import lombok.RequiredArgsConstructor;
15+
16+
@Service
17+
@RequiredArgsConstructor
18+
public class ToothService {
19+
private final UserRepository userRepository;
20+
21+
Map<String, String> toothPartEngToKor = new HashMap<>();
22+
23+
@PostConstruct
24+
private void init() {
25+
toothPartEngToKor.put("UNDER_FRONT", "아랫쪽 앞니");
26+
toothPartEngToKor.put("UP_FRONT", "윗쪽 앞니");
27+
toothPartEngToKor.put("UNDER_RIGHT_CANINE", "아래쪽 오른쪽 송곳니");
28+
toothPartEngToKor.put("UP_RIGHT_CANINE", "위쪽 오른쪽 송곳니");
29+
toothPartEngToKor.put("UNDER_RIGHT_MOLAR_OUTSIDE", "아랫쪽 오른쪽 어금니 바깥쪽");
30+
toothPartEngToKor.put("UP_RIGHT_MOLAR_OUTSIDE", "윗쪽 오른쪽 어금니 바깥쪽");
31+
toothPartEngToKor.put("UP_LEFT_MOLAR_CHEWING_SIDE", "윗쪽 왼쪽 어금니 씹는쪽");
32+
toothPartEngToKor.put("UP_RIGHT_MOLAR_CHEWING_SIDE", "윗쪽 오른쪽 어금니 씹는쪽");
33+
toothPartEngToKor.put("DOWN_RIGHT_MOLAR_CHEWING_SIDE", "아랫쪽 오른쪽 어금니 씹는쪽");
34+
toothPartEngToKor.put("DOWN_LEFT_MOLAR_CHEWING_SIDE", "아랫쪽 왼쪽 어금니 씹는쪽");
35+
toothPartEngToKor.put("UP_LEFT_MOLAR_OUTSIDE", "윗쪽 왼쪽 어금니 바깥쪽");
36+
toothPartEngToKor.put("UNDER_LEFT_MOLAR_OUTSIDE", "아랫쪽 왼쪽 어금니 바깥쪽");
37+
toothPartEngToKor.put("UNDER_LEFT_CANINE", "아랫쪽 왼쪽 송곳니");
38+
toothPartEngToKor.put("UP_LEFT_CANINE", "윗쪽 왼쪽 송곳니");
39+
}
40+
41+
public String toothPartEngToKorFunction(String englishPart) {
42+
return toothPartEngToKor.get(englishPart);
43+
}
44+
45+
public String evaluationPercentValue(Double percent) {
46+
if (100 == percent)
47+
return "적절해요.";
48+
else if (70 <= percent)
49+
return "주의해요.";
50+
return "미흡해요.";
51+
}
52+
53+
public User getToothSeqByUserId(Long userId){
54+
return userRepository.getUserInformationById(userId);
55+
}
56+
57+
public void setSeqAndDateByUserId(Long userId, int toothSeq, LocalDate toothDateRenew) {
58+
userRepository.updateSeqAndDateByUserId(userId, toothSeq, toothDateRenew);
59+
}
60+
}

docker-test-server/src/main/java/com/example/server/service/UserService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ public class UserService {
1515
private UserRepository userRepository;
1616

1717
public User getPetInformation(Long id) throws Exception{
18-
return userRepository.getUserInformationByAccessToken(id);
18+
return userRepository.getUserInformationById(id);
1919
}
2020

2121
public void setPetInformation(Long id, String PetName, Integer PetWeight) throws Exception {
22-
userRepository.setUserPetInformationByAccessToken(id, PetName, PetWeight);
22+
userRepository.setUserPetInformationById(id, PetName, PetWeight);
2323
}
2424
}

0 commit comments

Comments
 (0)