Skip to content

Commit

Permalink
feat: 비콘 엔티티 생성, 알림 생성 요청 api (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
julia-heo committed May 23, 2024
1 parent 6e45ea3 commit 1019abf
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ewha.lux.once.domain.home.controller;

import ewha.lux.once.domain.card.dto.SearchStoresRequestDto;
import ewha.lux.once.domain.home.dto.AnnounceFavoriteRequestDto;
import ewha.lux.once.domain.home.dto.BeaconRequestDto;
import ewha.lux.once.domain.home.service.HomeService;
import ewha.lux.once.global.common.CommonResponse;
import ewha.lux.once.global.common.CustomException;
Expand Down Expand Up @@ -80,5 +82,27 @@ public CommonResponse<?> nearFavorite(@AuthenticationPrincipal UserAccount user,
return new CommonResponse<>(e.getStatus());
}
}
// [Post] 알림 생성 요청
@PostMapping("/announcement")
@ResponseBody
public CommonResponse<?> announceFavorite(@AuthenticationPrincipal UserAccount user, @RequestBody AnnounceFavoriteRequestDto announceFavoriteRequestDto){
try {
homeService.postAnnounceFavorite(announceFavoriteRequestDto, user.getUsers());
return new CommonResponse<>(ResponseCode.SUCCESS);
} catch (CustomException e){
return new CommonResponse<>(e.getStatus());
}
}
// [Post] 비콘 알림 생성 요청
@PostMapping("/beacon")
@ResponseBody
public CommonResponse<?> beaconAnnouncement(@AuthenticationPrincipal UserAccount user, @RequestBody BeaconRequestDto beaconRequestDto){
try {
homeService.postBeaconAnnouncement(beaconRequestDto, user.getUsers());
return new CommonResponse<>(ResponseCode.SUCCESS);
} catch (CustomException e){
return new CommonResponse<>(e.getStatus());
}
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ewha.lux.once.domain.home.dto;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class AnnounceFavoriteRequestDto {
private String store;
private String storeName;
private double latitude;
private double longitude;
}
17 changes: 17 additions & 0 deletions src/main/java/ewha/lux/once/domain/home/dto/BeaconRequestDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ewha.lux.once.domain.home.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;


@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class BeaconRequestDto {
private String proximityUUID;
private Integer major;
private Integer minor;
}
40 changes: 40 additions & 0 deletions src/main/java/ewha/lux/once/domain/home/entity/Beacon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package ewha.lux.once.domain.home.entity;


import jakarta.persistence.*;
import lombok.*;

@Entity
@Table(name="Beacon")
@Getter
@Setter
@Builder
@NoArgsConstructor(access= AccessLevel.PROTECTED)
@AllArgsConstructor
public class Beacon {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "beaconId")
private Long id;

@Column(name = "proximityUUID", nullable = false)
private String proximityUUID;

@Column(name = "major")
private Integer major;

@Column(name = "minor")
private Integer minor;

@Column(name = "name")
private String name;

@Column(name = "store")
private String store;

@Column(name = "latitude")
private String latitude;

@Column(name = "longitude")
private String longitude;
}
102 changes: 94 additions & 8 deletions src/main/java/ewha/lux/once/domain/home/service/HomeService.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package ewha.lux.once.domain.home.service;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import ewha.lux.once.domain.card.dto.GoogleMapPlaceResponseDto;
import ewha.lux.once.domain.card.dto.SearchStoresRequestDto;
import ewha.lux.once.domain.card.dto.Place;
import ewha.lux.once.domain.card.dto.SearchStoresRequestDto;
import ewha.lux.once.domain.card.dto.SearchStoresResponseDto;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import ewha.lux.once.domain.card.entity.Card;
import ewha.lux.once.domain.card.entity.OwnedCard;
import ewha.lux.once.domain.home.dto.*;
import ewha.lux.once.domain.home.entity.Announcement;
import ewha.lux.once.domain.home.entity.ChatHistory;
import ewha.lux.once.domain.home.entity.Favorite;
import ewha.lux.once.domain.home.entity.*;
import ewha.lux.once.domain.user.entity.Users;
import ewha.lux.once.global.common.CustomException;
import ewha.lux.once.global.common.ResponseCode;
import ewha.lux.once.global.repository.*;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
Expand All @@ -35,13 +34,18 @@ public class HomeService {
@Value("${google-map.api-key}")
private String apiKey;
private final RestTemplate restTemplate;
private final FavoriteRepository favoriteRepository;
private final CardRepository cardRepository;
private final OwnedCardRepository ownedCardRepository;
private final ChatHistoryRepository chatHistoryRepository;
private final AnnouncementRepository announcementRepository;
private final FavoriteRepository favoriteRepository;

private final GeminiService geminiService;
private final OpenaiService openaiService;
private final FCMTokenRepository fcmTokenRepository;
private final FirebaseCloudMessageService firebaseCloudMessageService;
private final BeaconRepository beaconRepository;
private final CODEFAsyncService codefAsyncService;

// 챗봇 카드 추천
public ChatDto getHomeChat(Users nowUser, String keyword, int paymentAmount) throws CustomException {
Expand Down Expand Up @@ -311,4 +315,86 @@ public List<SearchStoresResponseDto> searchStores(SearchStoresRequestDto dto, Us
}
return resultList;
}
public void postAnnounceFavorite(AnnounceFavoriteRequestDto dto, Users nowUser) throws CustomException {
List<FCMToken> fcmTokens = fcmTokenRepository.findAllByUsers(nowUser);
String keyword = dto.getStore();
int paymentAmount=10000;
String response = openaiService.cardRecommend(nowUser, keyword, paymentAmount);
ObjectMapper objectMapper = new ObjectMapper();
Integer cardId;
Card card;
String benefit;
Integer discount;
try {
Map<String, Object> map = objectMapper.readValue(response, Map.class);
cardId = (Integer) map.get("카드번호");
card = cardRepository.findById(Long.valueOf(cardId)).orElse(null);
benefit = (String) map.get("혜택 정보");
discount = (Integer) map.get("할인 금액");

} catch ( JsonProcessingException e) {
throw new CustomException(ResponseCode.FAILED_TO_OPENAI_RECOMMEND);
}

String contents = dto.getStoreName()+" 근처시군요.\n"+card.getName()+" 사용해 보세요!";
String title = dto.getStoreName()+" 근처시군요";
String content = card.getName()+" 사용해 보세요!";
String moreInfo = dto.getLatitude()+", "+dto.getLongitude();
Announcement announcement = Announcement.builder()
.users(nowUser)
.type(0)
.content(contents)
.moreInfo(moreInfo)
.hasCheck(false)
.build();
announcementRepository.save(announcement);
for ( FCMToken fcmToken : fcmTokens){

String token = fcmToken.getToken();
firebaseCloudMessageService.sendNotification(new AnnouncementRequestDto(token,title,content));
}
}
public void postBeaconAnnouncement(BeaconRequestDto dto, Users nowUser)throws CustomException {
List<FCMToken> fcmTokens = fcmTokenRepository.findAllByUsers(nowUser);

Beacon beacon = beaconRepository.findAllByProximityUUIDAndMajorAndMinor(dto.getProximityUUID(),dto.getMajor(),dto.getMinor());

String keyword = beacon.getName();
int paymentAmount=10000;
String response = openaiService.cardRecommend(nowUser, keyword, paymentAmount);
ObjectMapper objectMapper = new ObjectMapper();
Integer cardId;
Card card;
String benefit;
Integer discount;
try {
Map<String, Object> map = objectMapper.readValue(response, Map.class);
cardId = (Integer) map.get("카드번호");
card = cardRepository.findById(Long.valueOf(cardId)).orElse(null);
benefit = (String) map.get("혜택 정보");
discount = (Integer) map.get("할인 금액");

} catch ( JsonProcessingException e) {
throw new CustomException(ResponseCode.FAILED_TO_OPENAI_RECOMMEND);
}

String title = beacon.getStore()+" 근처시군요";
String content = card.getName()+" 사용해 보세요!";
String contents = beacon.getStore()+" 근처시군요.\n"+card.getName()+" 사용해 보세요!";

String moreInfo = beacon.getLatitude()+", "+beacon.getLongitude();
Announcement announcement = Announcement.builder()
.users(nowUser)
.type(0)
.content(contents)
.moreInfo(moreInfo)
.hasCheck(false)
.build();
announcementRepository.save(announcement);
for ( FCMToken fcmToken : fcmTokens){
String token = fcmToken.getToken();
firebaseCloudMessageService.sendNotification(new AnnouncementRequestDto(token,title,content));
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ewha.lux.once.global.repository;

import ewha.lux.once.domain.home.entity.Beacon;
import org.springframework.data.jpa.repository.JpaRepository;
public interface BeaconRepository extends JpaRepository<Beacon, Long> {
Beacon findAllByProximityUUIDAndMajorAndMinor(String uuid, int major, int minor);
}

0 comments on commit 1019abf

Please sign in to comment.