Skip to content

Commit

Permalink
Merge branch 'feat/jeonghoon/#26' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
shb03323 committed Apr 3, 2023
2 parents 08411b6 + fe1683c commit 7b1638a
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 19 deletions.
3 changes: 2 additions & 1 deletion workit/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ out/
### VS Code ###
.vscode/

src/main/resources/application.yml
src/main/resources/application.yml
src/main/resources/firebase-adminsdk.json
4 changes: 4 additions & 0 deletions workit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ dependencies {
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.5'
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.11.5'

// firebase
implementation 'com.google.firebase:firebase-admin:9.1.1'
implementation 'com.squareup.okhttp3:okhttp:4.10.0'

implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign:3.1.5'
implementation 'com.google.code.gson:gson:2.10'
Expand Down
18 changes: 18 additions & 0 deletions workit/src/main/java/workit/auth/ScheduledConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package workit.auth;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

@Configuration
@EnableScheduling
public class ScheduledConfig {

public TaskScheduler scheduler() {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
int poolSize = 10;
scheduler.setPoolSize(poolSize);
return scheduler;
}
}
30 changes: 30 additions & 0 deletions workit/src/main/java/workit/dto/firebase/FcmMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package workit.dto.firebase;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Builder
@AllArgsConstructor
@Getter
public class FcmMessage {
private boolean validateOnly;
private Message message;

@Builder
@AllArgsConstructor
@Getter
public static class Message {
private Notification notification;
private String token;
}

@Builder
@AllArgsConstructor
@Getter
public static class Notification {
private String title;
private String body;
private String image;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class WorkCreateRequestDto {
@NotNull
private Date date;
@NotNull
private String projectTitle;
private Long projectId;
@NotNull
private String workTitle;
@NotNull
Expand Down
16 changes: 14 additions & 2 deletions workit/src/main/java/workit/dto/work/WorkDetailResponseDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.Data;
import workit.dto.ability.AbilityInfo;
import workit.entity.Project;
import workit.entity.Work;

import java.util.Date;
Expand All @@ -11,20 +12,31 @@
@Data
public class WorkDetailResponseDto {
private Long workId;
private String projectTitle;
private Project project;
private String workTitle;
private String description;
private Date date;
private List<AbilityInfo> abilities;

public WorkDetailResponseDto(Work work) {
this.workId = work.getId();
this.projectTitle = work.getProject().getTitle();
this.project = new Project(work.getProject());
this.workTitle = work.getTitle();
this.description = work.getDescription();
this.date = work.getDate();
this.abilities = work.getWorkAbilities().stream()
.map(workAbility -> new AbilityInfo(workAbility.getAbility()))
.collect(Collectors.toList());
}

@Data
private static class Project {
private Long projectId;
private String projectTitle;

public Project(final workit.entity.Project project) {
this.projectId = project.getId();
this.projectTitle = project.getTitle();
}
}
}
76 changes: 76 additions & 0 deletions workit/src/main/java/workit/service/PushAlarmService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package workit.service;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingException;
import com.google.firebase.messaging.Message;
import com.google.firebase.messaging.Notification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.Arrays;

@Slf4j
@Service
public class PushAlarmService {

@Value("${fcm.key.path}")
private String FCM_PRIVATE_KEY_PATH;

// 메시징만 권한 설정
@Value("${fcm.key.scope}")
private String FIREBASE_SCOPE;

@Value("${fcm.key.url}")
private String API_URL;

private FirebaseMessaging instance;

@PostConstruct
public void firebaseSetting() throws IOException {
GoogleCredentials googleCredentials = GoogleCredentials
.fromStream(new ClassPathResource(FCM_PRIVATE_KEY_PATH).getInputStream())
.createScoped((Arrays.asList(FIREBASE_SCOPE)));
FirebaseOptions secondaryAppConfig = FirebaseOptions.builder()
.setCredentials(googleCredentials)
.build();
FirebaseApp app = FirebaseApp.initializeApp(secondaryAppConfig);
this.instance = FirebaseMessaging.getInstance(app);
}

@Scheduled(cron = "0 0 09 * * ?")
public void pushMorningDietAlarm() throws FirebaseMessagingException {
log.info("아침 식사 알림");
pushAlarm("워킷 메시지", "오늘도 화이팅!");
}

private void pushAlarm(String title, String body) throws FirebaseMessagingException {
Message message = getMessage(title, body);
sendMessage(message);
}

private Message getMessage(String title, String body) {
Notification notification = Notification
.builder()
.setTitle(title)
.setBody(body)
.build();
Message.Builder builder = Message.builder();
Message message = builder
.setTopic(API_URL)
.setNotification(notification)
.build();
return message;
}

public String sendMessage(Message message) throws FirebaseMessagingException {
return this.instance.send(message);
}
}
22 changes: 7 additions & 15 deletions workit/src/main/java/workit/service/WorkService.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public AllWorkResponseDto getWorksByDateFilter(Long userId, String start, String
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = simpleDateFormat.parse(start);
Date endDate = simpleDateFormat.parse(end);

Calendar calendar = Calendar.getInstance();
calendar.setTime(endDate);
calendar.add(Calendar.DATE, 1);
Expand All @@ -47,8 +46,8 @@ public AllWorkResponseDto getWorksByDateFilter(Long userId, String start, String
List<WorkResponseDto> workResponseDtos = new ArrayList<>();
projects.forEach(project -> {
workRepository.findByProject(project).stream()
.filter(work -> work.getDate().equals(startDate) || work.getDate().after(startDate))
.filter(work -> work.getDate().equals(finalEndDate) || work.getDate().before(finalEndDate))
.filter(work -> !work.getDate().before(startDate))
.filter(work -> !work.getDate().after(finalEndDate))
.forEach(work -> {
WorkResponseDto workResponseDto = new WorkResponseDto(work);
workResponseDtos.add(workResponseDto);
Expand Down Expand Up @@ -95,15 +94,13 @@ public WorkDetailResponseDto createWork(WorkCreateRequestDto request, Long userI
() -> new CustomException(ResponseCode.USER_NOT_FOUND)
);

Project project = getProject(user, request.getProjectTitle());
Project project = getProject(user, request.getProjectId());

if (request.getAbilities().isEmpty()) {
throw new CustomException(ResponseCode.NO_ABILITIES);
}

validateWorkDescriptionLength(request.getDescription());

validateProjectTitleLength(request.getProjectTitle());
validateWorkTitleLength(request.getWorkTitle());

Work work = new Work();
Expand All @@ -130,13 +127,12 @@ public WorkDetailResponseDto modifyWork(WorkCreateRequestDto request, Long workI
);
validateUsersWork(work, user);

Project project = getProject(user, request.getProjectTitle());
Project project = getProject(user, request.getProjectId());

if (request.getAbilities().isEmpty()) {
throw new CustomException(ResponseCode.NO_ABILITIES);
}

validateProjectTitleLength(request.getProjectTitle());
validateWorkTitleLength(request.getWorkTitle());

WorkRequestDto workRequestDto = new WorkRequestDto(
Expand Down Expand Up @@ -165,13 +161,9 @@ public void deleteWork(Long workId, Long userId) {
workRepository.delete(work);
}

private Project getProject(User user, String projectTitle) {
return projectRepository.findByUserAndTitle(user, projectTitle)
.orElseGet(() -> {
Project proj = new Project(user, projectTitle);
projectRepository.save(proj);
return proj;
});
private Project getProject(User user, Long projectId) {
return projectRepository.findByUserIdAndId(user.getId(), projectId)
.orElseThrow(() -> new CustomException(ResponseCode.PROJECT_NOT_FOUND));
}

private List<WorkAbility> makeWorkAbilities(Work work, List<Ability> abilities) {
Expand Down

0 comments on commit 7b1638a

Please sign in to comment.