From a82bb5304d20fc76a487bcc7635b97e6c49b9c12 Mon Sep 17 00:00:00 2001 From: Lunawood Date: Fri, 6 Dec 2024 02:22:57 +0900 Subject: [PATCH] feat: Add ToothController Function, it works tooth data report. --- docker-test-server/sql/init.sql | 4 +- .../server/controller/RegisterController.java | 2 +- .../server/controller/ToothController.java | 81 ++++++++++++++++++- .../server/model/ToothDataAnalyzer.java | 21 +++++ .../java/com/example/server/model/User.java | 9 +++ .../server/repository/UserRepository.java | 13 ++- .../example/server/service/HomeService.java | 2 +- .../server/service/RegisterService.java | 7 +- .../example/server/service/ToothService.java | 60 ++++++++++++++ .../example/server/service/UserService.java | 4 +- 10 files changed, 191 insertions(+), 12 deletions(-) create mode 100644 docker-test-server/src/main/java/com/example/server/model/ToothDataAnalyzer.java create mode 100644 docker-test-server/src/main/java/com/example/server/service/ToothService.java diff --git a/docker-test-server/sql/init.sql b/docker-test-server/sql/init.sql index a6ff12e..57872c7 100644 --- a/docker-test-server/sql/init.sql +++ b/docker-test-server/sql/init.sql @@ -5,5 +5,7 @@ CREATE TABLE user_tb ( id BIGINT PRIMARY KEY, pet_name VARCHAR(50) NOT NULL, pet_weight INT NOT NULL, - random_id VARCHAR(255) NOT NULL + random_id VARCHAR(255) NOT NULL, + tooth_seq INT NOT NULL DEFAULT 0, + tooth_date_renew Date NOT NULL, ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; \ No newline at end of file diff --git a/docker-test-server/src/main/java/com/example/server/controller/RegisterController.java b/docker-test-server/src/main/java/com/example/server/controller/RegisterController.java index 35fdb55..e41f1cc 100644 --- a/docker-test-server/src/main/java/com/example/server/controller/RegisterController.java +++ b/docker-test-server/src/main/java/com/example/server/controller/RegisterController.java @@ -69,7 +69,7 @@ public ResponseEntity register( // 4. 모든 정보 저장. try { - registerService.createUserInfo(userId, randomId, userPet.getPetName(), userPet.getPetWeight());; + registerService.createUserInfo(userId, randomId, userPet.getPetName(), userPet.getPetWeight()); } catch(Exception e) { throw new CException(ErrorBase.INTERNAL_SERVER_ERROR); } diff --git a/docker-test-server/src/main/java/com/example/server/controller/ToothController.java b/docker-test-server/src/main/java/com/example/server/controller/ToothController.java index e25f8fd..caa1abf 100644 --- a/docker-test-server/src/main/java/com/example/server/controller/ToothController.java +++ b/docker-test-server/src/main/java/com/example/server/controller/ToothController.java @@ -1,7 +1,14 @@ package com.example.server.controller; +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -11,7 +18,11 @@ import com.example.server.Response.ErrorBase; import com.example.server.Response.SuccessBase; import com.example.server.jwt.JwtTokenService; +import com.example.server.model.ToothDataAnalyzer; +import com.example.server.model.User; +import com.example.server.model.ToothDataAnalyzer.ToothReport; import com.example.server.service.InvalidTokenService; +import com.example.server.service.ToothService; import lombok.RequiredArgsConstructor; @@ -21,9 +32,15 @@ public class ToothController { private final JwtTokenService jwtTokenService; private final InvalidTokenService invalidTokenService; + private final ToothService toothService; + private final Double totalCnt = 500.0; @GetMapping("") - public ResponseEntity mypageGetinfo(@RequestHeader("AccessToken") String AccessToken) { + public ResponseEntity mypageGetinfo + ( + @RequestHeader("AccessToken") String AccessToken, + @RequestBody List Instructions + ) { // 1. RefreshToken Valid? try { if(jwtTokenService.validateAccessToken(AccessToken) == false) { @@ -44,11 +61,69 @@ public ResponseEntity mypageGetinfo(@RequestHeader("AccessToken") String Acce throw new CException(ErrorBase.INVALID_TOKEN); } - // 3. Create New Data + // 3. Count Frequency + Map frequency = new HashMap<>(); + + for (String instruction : Instructions) { + frequency.put(instruction, frequency.getOrDefault(instruction, 0) + 1); + } + + List toothReports = new ArrayList<>(); + + for (Map.Entry entry : frequency.entrySet()) { + ToothReport toothReport = new ToothReport(); + System.out.println(entry.getKey() + ": " + entry.getValue()); + String koreanPart = toothService.toothPartEngToKorFunction(entry.getKey()); + if(koreanPart == null) { + continue; + } + String percentStr = String.format("%.2f", entry.getValue() / totalCnt); + if(100 < Double.parseDouble(percentStr)) + percentStr = "100.00"; + toothReport.setName(koreanPart); + toothReport.setPercent(percentStr); + toothReport.setDescription(toothService.evaluationPercentValue(Double.parseDouble(percentStr))); + toothReports.add(toothReport); + } + + // 4. Get Sequence Data + User user = null; + try { + user = toothService.getToothSeqByUserId(userId); + } catch (Exception e) { + throw new CException(ErrorBase.INTERNAL_SERVER_ERROR); + } + + // 5. Check Date if Date yesterday or after, change value + LocalDate userDateRenew = user.getToothDateRenew(); + LocalDate nowDate = LocalDate.now(); + if(!userDateRenew.isEqual(nowDate)) { + if(userDateRenew.plusDays(1).isEqual(nowDate)) { + // Seq += 1 + user.setToothSeq(user.getToothSeq() + 1); + } + else if (userDateRenew.plusDays(1).isAfter(nowDate)) { + // init Seq = 0 + user.setToothSeq(0); + } + + // 6. 모든 값 저장 + try { + toothService.setSeqAndDateByUserId(userId, user.getToothSeq(), nowDate); + } catch(Exception e) { + throw new CException(ErrorBase.INTERNAL_SERVER_ERROR); + } + } + + + + ToothDataAnalyzer toothDataAnalyzer = new ToothDataAnalyzer(); + toothDataAnalyzer.setReports(toothReports); + toothDataAnalyzer.setSeq(user.getToothSeq()); return ResponseEntity .status(SuccessBase.SUCCESS.getStatus()) - .body(BaseResponse.success(SuccessBase.SUCCESS, ".")); + .body(BaseResponse.success(SuccessBase.SUCCESS, toothDataAnalyzer)); } } diff --git a/docker-test-server/src/main/java/com/example/server/model/ToothDataAnalyzer.java b/docker-test-server/src/main/java/com/example/server/model/ToothDataAnalyzer.java new file mode 100644 index 0000000..96966f4 --- /dev/null +++ b/docker-test-server/src/main/java/com/example/server/model/ToothDataAnalyzer.java @@ -0,0 +1,21 @@ +package com.example.server.model; + +import java.util.List; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class ToothDataAnalyzer { + private List reports; + private int seq = 0; + + @Getter + @Setter + public static class ToothReport { + private String name; + private String percent; + private String description; + } +} diff --git a/docker-test-server/src/main/java/com/example/server/model/User.java b/docker-test-server/src/main/java/com/example/server/model/User.java index 8a48df2..7f75b50 100644 --- a/docker-test-server/src/main/java/com/example/server/model/User.java +++ b/docker-test-server/src/main/java/com/example/server/model/User.java @@ -1,5 +1,8 @@ package com.example.server.model; +import java.time.LocalDate; +import java.time.ZoneId; + import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; @@ -27,4 +30,10 @@ public class User { @Column(name = "random_id") private String randomId; + + @Column(name = "tooth_seq") + private Integer toothSeq = 0; + + @Column(name = "tooth_date_renew") + private LocalDate toothDateRenew = LocalDate.now(ZoneId.of("Asia/Seoul")); } \ No newline at end of file diff --git a/docker-test-server/src/main/java/com/example/server/repository/UserRepository.java b/docker-test-server/src/main/java/com/example/server/repository/UserRepository.java index ec5abb0..268dee7 100644 --- a/docker-test-server/src/main/java/com/example/server/repository/UserRepository.java +++ b/docker-test-server/src/main/java/com/example/server/repository/UserRepository.java @@ -1,5 +1,7 @@ package com.example.server.repository; +import java.time.LocalDate; + import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Modifying; @@ -19,19 +21,24 @@ public interface UserRepository extends JpaRepository { @Modifying @Transactional - @Query(value = "INSERT INTO user_tb (id, pet_name, pet_weight, random_id) VALUES (:#{#user.id}, :#{#user.petName}, :#{#user.petWeight}, :#{#user.randomId})", nativeQuery = true) + @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) void insertUserInformation(@Param("user") User user); @Query("SELECT u FROM User u WHERE u.id = :id") - User getUserInformationByAccessToken(@Param("id") Long id); + User getUserInformationById(@Param("id") Long id); @Modifying @Transactional @Query(value = "UPDATE user_tb u SET u.pet_name=:petName, u.pet_weight=:petWeight WHERE u.id=:id", nativeQuery = true) - void setUserPetInformationByAccessToken(@Param("id") Long id, @Param("petName") String petName, @Param("petWeight") Integer petWeight); + void setUserPetInformationById(@Param("id") Long id, @Param("petName") String petName, @Param("petWeight") Integer petWeight); @Modifying @Transactional @Query(value = "UPDATE user_tb u SET u.random_id=:randomId, u.random_id=:randomId WHERE u.id = :id", nativeQuery = true) void updateRandomIdByUserId(@Param("id") Long id, @Param("randomId") String randomId); + + @Modifying + @Transactional + @Query(value = "UPDATE user_tb u SET u.tooth_seq=:toothSeq, u.tooth_date_renew=:toothDateRenew WHERE u.id = :id", nativeQuery = true) + void updateSeqAndDateByUserId(@Param("id") Long id, @Param("toothSeq") int toothSeq, @Param("toothDateRenew") LocalDate toothDateRenew); } \ No newline at end of file diff --git a/docker-test-server/src/main/java/com/example/server/service/HomeService.java b/docker-test-server/src/main/java/com/example/server/service/HomeService.java index e365432..df9ee71 100644 --- a/docker-test-server/src/main/java/com/example/server/service/HomeService.java +++ b/docker-test-server/src/main/java/com/example/server/service/HomeService.java @@ -13,6 +13,6 @@ public class HomeService { private final UserRepository userRepository; public User getPetInformation(Long userId) throws Exception{ - return userRepository.getUserInformationByAccessToken(userId); + return userRepository.getUserInformationById(userId); } } \ No newline at end of file diff --git a/docker-test-server/src/main/java/com/example/server/service/RegisterService.java b/docker-test-server/src/main/java/com/example/server/service/RegisterService.java index aab81dc..3aa6daf 100644 --- a/docker-test-server/src/main/java/com/example/server/service/RegisterService.java +++ b/docker-test-server/src/main/java/com/example/server/service/RegisterService.java @@ -1,6 +1,9 @@ package com.example.server.service; +import java.time.LocalDate; +import java.time.ZoneId; + import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; @@ -58,7 +61,9 @@ public void createUserInfo( userId, PetName, PetWeight, - RandomID + RandomID, + 0, + LocalDate.now(ZoneId.of("Asia/Seoul")) ); // Database에 Data 저장. diff --git a/docker-test-server/src/main/java/com/example/server/service/ToothService.java b/docker-test-server/src/main/java/com/example/server/service/ToothService.java new file mode 100644 index 0000000..440bfc3 --- /dev/null +++ b/docker-test-server/src/main/java/com/example/server/service/ToothService.java @@ -0,0 +1,60 @@ +package com.example.server.service; + +import java.time.LocalDate; +import java.util.HashMap; +import java.util.Map; + +import javax.annotation.PostConstruct; + +import org.springframework.stereotype.Service; + +import com.example.server.model.User; +import com.example.server.repository.UserRepository; + +import lombok.RequiredArgsConstructor; + +@Service +@RequiredArgsConstructor +public class ToothService { + private final UserRepository userRepository; + + Map toothPartEngToKor = new HashMap<>(); + + @PostConstruct + private void init() { + toothPartEngToKor.put("UNDER_FRONT", "아랫쪽 앞니"); + toothPartEngToKor.put("UP_FRONT", "윗쪽 앞니"); + toothPartEngToKor.put("UNDER_RIGHT_CANINE", "아래쪽 오른쪽 송곳니"); + toothPartEngToKor.put("UP_RIGHT_CANINE", "위쪽 오른쪽 송곳니"); + toothPartEngToKor.put("UNDER_RIGHT_MOLAR_OUTSIDE", "아랫쪽 오른쪽 어금니 바깥쪽"); + toothPartEngToKor.put("UP_RIGHT_MOLAR_OUTSIDE", "윗쪽 오른쪽 어금니 바깥쪽"); + toothPartEngToKor.put("UP_LEFT_MOLAR_CHEWING_SIDE", "윗쪽 왼쪽 어금니 씹는쪽"); + toothPartEngToKor.put("UP_RIGHT_MOLAR_CHEWING_SIDE", "윗쪽 오른쪽 어금니 씹는쪽"); + toothPartEngToKor.put("DOWN_RIGHT_MOLAR_CHEWING_SIDE", "아랫쪽 오른쪽 어금니 씹는쪽"); + toothPartEngToKor.put("DOWN_LEFT_MOLAR_CHEWING_SIDE", "아랫쪽 왼쪽 어금니 씹는쪽"); + toothPartEngToKor.put("UP_LEFT_MOLAR_OUTSIDE", "윗쪽 왼쪽 어금니 바깥쪽"); + toothPartEngToKor.put("UNDER_LEFT_MOLAR_OUTSIDE", "아랫쪽 왼쪽 어금니 바깥쪽"); + toothPartEngToKor.put("UNDER_LEFT_CANINE", "아랫쪽 왼쪽 송곳니"); + toothPartEngToKor.put("UP_LEFT_CANINE", "윗쪽 왼쪽 송곳니"); + } + + public String toothPartEngToKorFunction(String englishPart) { + return toothPartEngToKor.get(englishPart); + } + + public String evaluationPercentValue(Double percent) { + if (100 == percent) + return "적절해요."; + else if (70 <= percent) + return "주의해요."; + return "미흡해요."; + } + + public User getToothSeqByUserId(Long userId){ + return userRepository.getUserInformationById(userId); + } + + public void setSeqAndDateByUserId(Long userId, int toothSeq, LocalDate toothDateRenew) { + userRepository.updateSeqAndDateByUserId(userId, toothSeq, toothDateRenew); + } +} diff --git a/docker-test-server/src/main/java/com/example/server/service/UserService.java b/docker-test-server/src/main/java/com/example/server/service/UserService.java index fa7a2d5..a70f098 100644 --- a/docker-test-server/src/main/java/com/example/server/service/UserService.java +++ b/docker-test-server/src/main/java/com/example/server/service/UserService.java @@ -15,10 +15,10 @@ public class UserService { private UserRepository userRepository; public User getPetInformation(Long id) throws Exception{ - return userRepository.getUserInformationByAccessToken(id); + return userRepository.getUserInformationById(id); } public void setPetInformation(Long id, String PetName, Integer PetWeight) throws Exception { - userRepository.setUserPetInformationByAccessToken(id, PetName, PetWeight); + userRepository.setUserPetInformationById(id, PetName, PetWeight); } }