-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from kakao-tech-campus-2nd-step3/feature/63-fcm
feat: FCM ํธ์ฌ ์๋ฆผ ๊ตฌํ
- Loading branch information
Showing
19 changed files
with
457 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ build/ | |
!**/src/test/**/build/ | ||
|
||
application-dev.yml | ||
everymoment.json | ||
|
||
### STS ### | ||
.apt_generated | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/com/potatocake/everymoment/config/FcmConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.potatocake.everymoment.config; | ||
|
||
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.firebase.FirebaseApp; | ||
import com.google.firebase.FirebaseOptions; | ||
import com.google.firebase.messaging.FirebaseMessaging; | ||
import java.io.IOException; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.io.ClassPathResource; | ||
|
||
@Configuration | ||
public class FcmConfig { | ||
|
||
@Bean | ||
public FirebaseMessaging firebaseMessaging() throws IOException { | ||
GoogleCredentials googleCredentials = GoogleCredentials | ||
.fromStream(new ClassPathResource("everymoment.json").getInputStream()); | ||
|
||
FirebaseOptions firebaseOptions = FirebaseOptions.builder() | ||
.setCredentials(googleCredentials) | ||
.build(); | ||
|
||
FirebaseApp app = FirebaseApp.initializeApp(firebaseOptions); | ||
|
||
return FirebaseMessaging.getInstance(app); | ||
} | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/com/potatocake/everymoment/controller/FcmController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.potatocake.everymoment.controller; | ||
|
||
import com.potatocake.everymoment.dto.SuccessResponse; | ||
import com.potatocake.everymoment.dto.request.FcmTokenRequest; | ||
import com.potatocake.everymoment.security.MemberDetails; | ||
import com.potatocake.everymoment.service.FcmService; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.Parameter; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@Tag(name = "FCM", description = "FCM ํ ํฐ ๊ด๋ฆฌ API") | ||
@RestController | ||
@RequestMapping("/api/fcm") | ||
@RequiredArgsConstructor | ||
public class FcmController { | ||
|
||
private final FcmService fcmService; | ||
|
||
@Operation(summary = "FCM ํ ํฐ ๋ฑ๋ก/๊ฐฑ์ ", description = "๋๋ฐ์ด์ค์ FCM ํ ํฐ์ ๋ฑ๋กํ๊ฑฐ๋ ๊ฐฑ์ ํฉ๋๋ค.") | ||
@ApiResponse(responseCode = "200", description = "ํ ํฐ ๋ฑ๋ก/๊ฐฑ์ ์ฑ๊ณต") | ||
@PostMapping("/token") | ||
public ResponseEntity<SuccessResponse> registerToken( | ||
@Parameter(description = "์ธ์ฆ๋ ์ฌ์ฉ์ ์ ๋ณด", hidden = true) | ||
@AuthenticationPrincipal MemberDetails memberDetails, | ||
@Parameter(description = "FCM ํ ํฐ ์ ๋ณด", required = true) | ||
@RequestBody @Valid FcmTokenRequest request) { | ||
|
||
fcmService.registerToken(memberDetails.getId(), request.getDeviceId(), request.getFcmToken()); | ||
|
||
return ResponseEntity.ok() | ||
.body(SuccessResponse.ok()); | ||
} | ||
|
||
@Operation(summary = "FCM ํ ํฐ ์ญ์ ", description = "๋๋ฐ์ด์ค์ FCM ํ ํฐ์ ์ญ์ ํฉ๋๋ค.") | ||
@ApiResponse(responseCode = "200", description = "ํ ํฐ ์ญ์ ์ฑ๊ณต") | ||
@DeleteMapping("/token") | ||
public ResponseEntity<SuccessResponse> removeToken( | ||
@Parameter(description = "์ธ์ฆ๋ ์ฌ์ฉ์ ์ ๋ณด", hidden = true) | ||
@AuthenticationPrincipal MemberDetails memberDetails, | ||
@Parameter(description = "๋๋ฐ์ด์ค ID", required = true) | ||
@RequestParam String deviceId) { | ||
|
||
fcmService.removeToken(memberDetails.getId(), deviceId); | ||
|
||
return ResponseEntity.ok() | ||
.body(SuccessResponse.ok()); | ||
} | ||
|
||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/potatocake/everymoment/dto/request/CommentRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
package com.potatocake.everymoment.dto.request; | ||
|
||
import jakarta.validation.constraints.NotEmpty; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class CommentRequest { | ||
|
||
@NotEmpty(message = "๋๊ธ ๋ด์ฉ์ ์ ๋ ฅํด ์ฃผ์ธ์.") | ||
private String content; | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/potatocake/everymoment/dto/request/DiaryManualCreateRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,30 @@ | ||
package com.potatocake.everymoment.dto.request; | ||
|
||
import com.potatocake.everymoment.dto.LocationPoint; | ||
import jakarta.validation.constraints.Size; | ||
import java.util.List; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class DiaryManualCreateRequest { | ||
|
||
private List<CategoryRequest> categories; | ||
|
||
private LocationPoint locationPoint; | ||
|
||
@Size(max = 50, message = "์ฅ์๋ช ์ 50์๋ฅผ ์ด๊ณผํ ์ ์์ต๋๋ค") | ||
private String locationName; | ||
|
||
@Size(max = 200, message = "์ฃผ์๋ 200์๋ฅผ ์ด๊ณผํ ์ ์์ต๋๋ค") | ||
private String address; | ||
|
||
private boolean isBookmark; | ||
private boolean isPublic; | ||
|
||
@Size(max = 10, message = "์ด๋ชจ์ง๋ 10์๋ฅผ ์ด๊ณผํ ์ ์์ต๋๋ค") | ||
private String emoji; | ||
|
||
@Size(max = 5000, message = "์ผ๊ธฐ ๋ด์ฉ์ 5000์๋ฅผ ์ด๊ณผํ ์ ์์ต๋๋ค") | ||
private String content; | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/potatocake/everymoment/dto/request/FcmNotificationRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.potatocake.everymoment.dto.request; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@Builder | ||
public class FcmNotificationRequest { | ||
|
||
private String title; | ||
private String body; | ||
private String type; | ||
private Long targetId; | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/potatocake/everymoment/dto/request/FcmTokenRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.potatocake.everymoment.dto.request; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class FcmTokenRequest { | ||
|
||
@NotBlank(message = "FCM ํ ํฐ์ ํ์์ ๋๋ค.") | ||
private String fcmToken; | ||
|
||
@NotBlank(message = "๋๋ฐ์ด์ค ID๋ ํ์์ ๋๋ค.") | ||
private String deviceId; | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/com/potatocake/everymoment/entity/DeviceToken.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.potatocake.everymoment.entity; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.Lob; | ||
import jakarta.persistence.ManyToOne; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class DeviceToken extends BaseTimeEntity { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(nullable = false) | ||
private Member member; | ||
|
||
@Column(nullable = false) | ||
private String fcmToken; | ||
|
||
@Column(nullable = false) | ||
@Lob | ||
private String deviceId; | ||
|
||
@Builder | ||
public DeviceToken(Member member, String fcmToken, String deviceId) { | ||
this.member = member; | ||
this.fcmToken = fcmToken; | ||
this.deviceId = deviceId; | ||
} | ||
|
||
public void updateToken(String fcmToken) { | ||
this.fcmToken = fcmToken; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/java/com/potatocake/everymoment/repository/DeviceTokenRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.potatocake.everymoment.repository; | ||
|
||
import com.potatocake.everymoment.entity.DeviceToken; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface DeviceTokenRepository extends JpaRepository<DeviceToken, Long> { | ||
|
||
List<DeviceToken> findAllByMemberId(Long memberId); | ||
|
||
Optional<DeviceToken> findByMemberIdAndDeviceId(Long memberId, String deviceId); | ||
|
||
void deleteByMemberIdAndDeviceId(Long memberId, String deviceId); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.