Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies {
runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-impl', version: '0.11.5'
runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-jackson', version: '0.11.5'

implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'

implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'mysql:mysql-connector-java:8.0.31'
Expand Down
30 changes: 0 additions & 30 deletions src/main/java/com/itsu/threedays/InitialUserLoader.java

This file was deleted.

28 changes: 28 additions & 0 deletions src/main/java/com/itsu/threedays/config/S3Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.itsu.threedays.config;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class S3Config {
@Value("${cloud.aws.credentials.access-key}")
private String accessKey;
@Value("${cloud.aws.credentials.secret-key}")
private String secretKey;
@Value("${cloud.aws.region.static}")
private String region;

@Bean
public AmazonS3Client amazonS3Client() {
BasicAWSCredentials awsCredentials= new BasicAWSCredentials(accessKey, secretKey);
return (AmazonS3Client) AmazonS3ClientBuilder.standard()
.withRegion(region)
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
.build();
}
}
20 changes: 0 additions & 20 deletions src/main/java/com/itsu/threedays/config/jwt/JwtConfig.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,12 @@ public Authentication getAuthentication(String token) {

return new UsernamePasswordAuthenticationToken(principal, token, authorities);
}

public String resolveToken(HttpServletRequest request){
String bearerToken = request.getHeader("authorization");
if(StringUtils.hasText(bearerToken) && bearerToken.startsWith("Bearer ")) {
return bearerToken.substring(7);
}
return null;
}
}
41 changes: 17 additions & 24 deletions src/main/java/com/itsu/threedays/controller/HabitController.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ ResponseEntity<List<HabitResponseDto>> getHabitList(@RequestParam("email") Strin
HabitResponseDto responseDto = new HabitResponseDto();
responseDto.setId(habit.getId());
responseDto.setTitle(habit.getTitle());
responseDto.setDuration(habit.getDuration());
responseDto.setVisible(habit.isVisible());
responseDto.setComboCount(habit.getComboCount());
responseDto.setAchievementRate(habit.getAchievementRate());
Expand All @@ -79,28 +80,6 @@ ResponseEntity<List<HabitResponseDto>> getHabitList(@RequestParam("email") Strin
return ResponseEntity.ok(habitResponseDtos);
}

@GetMapping("habits/edit-list") //편집시 습관목록조회(중지일 포함)
ResponseEntity<List<HabitEditResponseDto>> getHabitEditList(@RequestParam("email") String email) throws Exception{
List<HabitEntity> habits = habitService.findUndeletedAndAllHabits(email);
log.info("undeletedAndAllHabits: {}",habits);
List<HabitEditResponseDto> habitEditListDto = habits.stream()
.map(habitEntity -> {
HabitEditResponseDto editResponseDto = new HabitEditResponseDto();
editResponseDto.setId(habitEntity.getId());
editResponseDto.setTitle(habitEntity.getTitle());
editResponseDto.setDuration(habitEntity.getDuration());
editResponseDto.setVisible(habitEntity.isVisible());
editResponseDto.setComboCount(habitEntity.getComboCount());
editResponseDto.setAchievementRate(habitEntity.getAchievementRate());
editResponseDto.setAchievementCount(habitEntity.getAchievementCount());
editResponseDto.setStopDate(habitEntity.getStopDate()); //중지일
return editResponseDto;
})
.collect(Collectors.toList());
return ResponseEntity.ok(habitEditListDto)
}



@PutMapping("habits/{habitId}/edit") //습관수정(이름, 기간, 공개여부)
ResponseEntity<HabitResponseDto> updateHabit(@PathVariable("habitId") Long habitId,
Expand Down Expand Up @@ -134,12 +113,26 @@ ResponseEntity<String> stopHabit(@PathVariable("habitId") Long habitId){

}

//습관 편집시 목록

@GetMapping("habits/edit-list") //편집시 습관목록조회(중지일 포함)
ResponseEntity<List<HabitEditResponseDto>> getHabitEditList(@RequestParam("email") String email) throws Exception{
List<HabitEntity> habits = habitService.findUndeletedAndAllHabits(email);
log.info("undeletedAndAllHabits: {}",habits);
List<HabitEditResponseDto> habitEditListDto = habits.stream()
.map(habitEntity -> {
HabitEditResponseDto editResponseDto = new HabitEditResponseDto();
habitService.setCommonHabitFields(editResponseDto,habitEntity); //공통필드 설정
editResponseDto.setStopDate(habitEntity.getStopDate()); //중지일
return editResponseDto;
})
.collect(Collectors.toList());
return ResponseEntity.ok(habitEditListDto);
}

@GetMapping("habits/{habitId}/reset") //매주마다 달성횟수 리셋
ResponseEntity<String> resetAchievement(@PathVariable("habitId") Long habitId){
habitService.resetAchievement(habitId);
return ResponseEntity.ok("Resetting the achievement count was successful.");
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequiredArgsConstructor
@RequestMapping("api")
Expand Down Expand Up @@ -109,20 +111,19 @@ ResponseEntity<?> responseJwtToken(@RequestBody UserDto userDto) { //파베 토
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}

// @GetMapping("/test")
// public ResponseEntity<String> testApi(HttpServletRequest request) {
// log.info("!!");
// String jwt = tokenProvider.resolveToken(request);
// log.info("testApi - jwt: {}",jwt);
// if (jwt == null) {
// return new ResponseEntity<>("JWT not found!", HttpStatus.BAD_REQUEST);
// }
//
// if (tokenProvider.validateToken(jwt)) {
// return new ResponseEntity<>("JWT is valid", HttpStatus.OK);
// } else {
// return new ResponseEntity<>("JWT is invalid", HttpStatus.UNAUTHORIZED);
// }
// }
@GetMapping("/test")
public ResponseEntity<String> testApi(HttpServletRequest request) {
String jwt = tokenProvider.resolveToken(request);
log.info("testApi - jwt: {}",jwt);
if (jwt == null) {
return new ResponseEntity<>("JWT not found!", HttpStatus.BAD_REQUEST);
}

if (tokenProvider.validateToken(jwt)) {
return new ResponseEntity<>("JWT is valid", HttpStatus.OK);
} else {
return new ResponseEntity<>("JWT is invalid", HttpStatus.UNAUTHORIZED);
}
}

}
10 changes: 1 addition & 9 deletions src/main/java/com/itsu/threedays/dto/HabitEditResponseDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,8 @@
import java.time.LocalDateTime;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class HabitEditResponseDto {
Long id;
String title;
int duration;
boolean visible;
int comboCount;
int achievementRate;
int achievementCount;
public class HabitEditResponseDto extends HabitResponseDto{
LocalDateTime stopDate;
}
3 changes: 3 additions & 0 deletions src/main/java/com/itsu/threedays/dto/HabitResponseDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ public class HabitResponseDto {
int comboCount;
int achievementRate;
int achievementCount;

//추가할 것

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface HabitRepository extends JpaRepository<HabitEntity,Long> {
List<HabitEntity> findAllByUserId(UserEntity userId);
List<HabitEntity> findAllByUserIdAndDeleteYnAndStopDateIsNull(UserEntity userId, boolean deleteYn);

List<HabitEntity> findAllByUserIdAndDeleteYn(boolean deleteYn);
List<HabitEntity> findAllByDeleteYn(boolean deleteYn);

Optional<HabitEntity> findById(Long habitId);

Expand Down
37 changes: 26 additions & 11 deletions src/main/java/com/itsu/threedays/service/HabitService.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public List<HabitEntity> findUndeletedAndAllHabits(String email) throws Exceptio
if (byEmail.isPresent()){
UserEntity user = byEmail.get();
log.info("email로 user 찾기:{}",user.getEmail());
return habitRepository.findAllByUserIdAndDeleteYn(false);
return habitRepository.findAllByDeleteYn(false);
}
else {
throw new Exception("User NOT FOUND!");
Expand All @@ -73,13 +73,14 @@ public HabitResponseDto updateHabit(Long habitId, HabitUpdateRequestDto habitUpd
HabitEntity updatedHabit = habitRepository.save(habit);

HabitResponseDto habitResponseDto = new HabitResponseDto();
habitResponseDto.setId(updatedHabit.getId());
habitResponseDto.setTitle(updatedHabit.getTitle());
habitResponseDto.setDuration(updatedHabit.getDuration());
habitResponseDto.setVisible(updatedHabit.isVisible());
habitResponseDto.setComboCount(updatedHabit.getComboCount());
habitResponseDto.setAchievementRate(updatedHabit.getAchievementRate());
habitResponseDto.setAchievementCount(updatedHabit.getAchievementCount());
// habitResponseDto.setId(updatedHabit.getId());
// habitResponseDto.setTitle(updatedHabit.getTitle());
// habitResponseDto.setDuration(updatedHabit.getDuration());
// habitResponseDto.setVisible(updatedHabit.isVisible());
// habitResponseDto.setComboCount(updatedHabit.getComboCount());
// habitResponseDto.setAchievementRate(updatedHabit.getAchievementRate());
// habitResponseDto.setAchievementCount(updatedHabit.getAchievementCount());
setCommonHabitFields(habitResponseDto,updatedHabit);

return habitResponseDto;

Expand Down Expand Up @@ -119,22 +120,27 @@ public void updateAchievementAndCombo(Long habitId){

int newAchievementCount = habit.getAchievementCount() + 1;
habit.setAchievementCount(newAchievementCount); //달성횟수 +1
log.info("달성횟수: {}", newAchievementCount);

int newTotalAchievementCount = habit.getTotalAchievementCount() + 1;
habit.setTotalAchievementCount(newTotalAchievementCount); //누적달성횟수 +1
log.info("누적달성횟수: {}",newTotalAchievementCount);

LocalDate now = LocalDate.now();
LocalDate now = LocalDate.now(); //
LocalDate habitCreate = habit.getCreatedDate().toLocalDate();
long daysBetween = ChronoUnit.DAYS.between(now, habitCreate);
int weeks = (int) (daysBetween / 7) + 1;
log.info("현재주차: {}",weeks);

int newAchievementRate = (newAchievementCount / habit.getDuration() * weeks) * 100;
double i = ((double) newTotalAchievementCount / habit.getDuration() * weeks) * 100;
int newAchievementRate = (int) Math.round(i);
log.info("달성률: {}",newAchievementRate);
habit.setAchievementRate(newAchievementRate); //달성률 증가


if(habit.getDuration() == newAchievementCount) {
int newComboCount = habit.getComboCount() + 1;
habit.setComboCount(newComboCount); //콤보횟수 +1
log.info("콤보횟수: {}",newComboCount);
}

habitRepository.save(habit);
Expand Down Expand Up @@ -183,6 +189,15 @@ public void resetAchievement(Long habitId) {
}
}

public void setCommonHabitFields(HabitResponseDto habitDto, HabitEntity habitEntity ){
habitDto.setId(habitEntity.getId());
habitDto.setTitle(habitEntity.getTitle());
habitDto.setDuration(habitEntity.getDuration());
habitDto.setVisible(habitEntity.isVisible());
habitDto.setAchievementRate(habitEntity.getAchievementRate());
habitDto.setAchievementCount(habitEntity.getAchievementCount());
}



}
11 changes: 11 additions & 0 deletions src/main/resources/application-local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,16 @@ spring:
properties:
hibernate:
format_sql: true
cloud:
aws:
s3:
bucket: itsubucket
stack:
auto: false
region:
static: ap-northeast-2
credentials:
accessKey: AKIARREOKLWWNCOBGJHR
secretKey: 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
jwt:
secret: threedaysapitokensecretkey123149398230
47 changes: 29 additions & 18 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
#spring:
# datasource:
# url: jdbc:mysql://threedays.cnaor4agdclw.ap-northeast-2.rds.amazonaws.com :3306/threedays
# username: root
# password: threedays
# driver-class-name: com.mysql.cj.jdbc.Driver
# jpa:
# show-sql: true
# hibernate:
# ddl-auto: create
# database: mysql
# properties:
# hibernate:
# format_sql: true
## profiles:
## active: test
#jwt:
# secret: threedaysapitokensecretkey123149398230
spring:
datasource:
url: jdbc:mysql://threedays.cnaor4agdclw.ap-northeast-2.rds.amazonaws.com :3306/threedays
username: root
password: threedays
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
show-sql: true
hibernate:
ddl-auto: create
database: mysql
properties:
hibernate:
format_sql: true
# profiles:
# active: test
cloud:
aws:
s3:
bucket: itsubucket
stack:
auto: false
region:
static: ap-northeast-2
credentials:
accessKey: AKIARREOKLWWNCOBGJHR
secretKey: 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
jwt:
secret: threedaysapitokensecretkey123149398230
Loading