Skip to content

Commit

Permalink
feat: Add ToothController Function, it works tooth data report.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lunawood committed Dec 5, 2024
1 parent 04c498d commit a82bb53
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 12 deletions.
4 changes: 3 additions & 1 deletion docker-test-server/sql/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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<String> Instructions
) {
// 1. RefreshToken Valid?
try {
if(jwtTokenService.validateAccessToken(AccessToken) == false) {
Expand All @@ -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<String, Integer> frequency = new HashMap<>();

for (String instruction : Instructions) {
frequency.put(instruction, frequency.getOrDefault(instruction, 0) + 1);
}

List<ToothReport> toothReports = new ArrayList<>();

for (Map.Entry<String, Integer> 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));
}
}
Original file line number Diff line number Diff line change
@@ -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<ToothReport> reports;
private int seq = 0;

@Getter
@Setter
public static class ToothReport {
private String name;
private String percent;
private String description;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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"));
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,19 +21,24 @@ public interface UserRepository extends JpaRepository<User, Long> {

@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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -58,7 +61,9 @@ public void createUserInfo(
userId,
PetName,
PetWeight,
RandomID
RandomID,
0,
LocalDate.now(ZoneId.of("Asia/Seoul"))
);

// Database에 Data 저장.
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit a82bb53

Please sign in to comment.