Skip to content

Commit

Permalink
Merge pull request #133 from kookmin-sw/backend/develop/v3
Browse files Browse the repository at this point in the history
Backend/develop/v3
  • Loading branch information
J-Yong99 authored May 7, 2024
2 parents 0d49ff8 + e3affa0 commit c8f339c
Show file tree
Hide file tree
Showing 15 changed files with 313 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public void save(User user) {
}
public void registerUserToCoreServer(User user) {
try{
log.info("Core 서버에 유저 등록을 시도합니다.");
log.info("user : {}", user.getId());
coreClient.registerUser(UserRequestDTO.registerUser
.builder()
.email(user.getEmail())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,15 @@ public ResponseEntity<APIResponse> updateTrip(
tripService.update(userId, updateTrip);
return ResponseEntity.ok(APIResponse.of(SuccessCode.UPDATE_SUCCESS));
}

// 여행 조회
@GetMapping("/{tripId}")
public ResponseEntity<APIResponse<TripResponseDTO.GetTripSpec>> getTrip(
@RequestHeader Long userId,
@PathVariable Long tripId
) {
userService.validateUserWithTrip(userId, tripId);
TripResponseDTO.GetTripSpec trip = tripService.getTrip(tripId);
return ResponseEntity.ok(APIResponse.of(SuccessCode.SELECT_SUCCESS, trip));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import com.moment.core.common.APIResponse;
import com.moment.core.common.code.SuccessCode;
import com.moment.core.domain.trip.Trip;
import com.moment.core.domain.user.User;
import com.moment.core.dto.request.UserRequestDTO;
import com.moment.core.service.S3Service;
import com.moment.core.service.TripService;
import com.moment.core.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
Expand All @@ -25,6 +27,7 @@
public class UserController {
private final UserService userService;
private final S3Service s3Service;
private final TripService tripService;

// 유저 등록
@PostMapping("/register")
Expand All @@ -37,7 +40,17 @@ public ResponseEntity<APIResponse> registerUser(
@RequestBody UserRequestDTO.registerUser request
) {
// s3Service.createFolder(request.getId().toString());
userService.save(request);
User user = userService.save(request);
log.info("user : {}", user.getId());
tripService.save(Trip.builder()
.user(user)
.analyzingCount(0)
.startDate(null)
.endDate(null)
.tripName("untitled trip")
.isNotTitled(true)
.build()
);
return ResponseEntity.ok(APIResponse.of(SuccessCode.INSERT_SUCCESS));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ public class CardView extends BaseEntity {
private String temperature;

// STT
@Column(name = "stt")
@Lob
@Column(name = "stt", columnDefinition = "TEXT")
private String stt;

// happy
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
package com.moment.core.domain.user;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.time.LocalDateTime;

public interface UserRepository extends JpaRepository<User, Long> {
Boolean existsByEmail(String email);


@Query(value = "INSERT INTO user (id, createdAt, updatedAt, email, notification, data_usage, firebase_token) VALUES (:id, :createdAt, :updatedAt, :email, :notification, :dataUsage, :firebaseToken)", nativeQuery = true)
@Modifying
void saveN(Long id, LocalDateTime createdAt, LocalDateTime updatedAt, String email, boolean notification, boolean dataUsage, String firebaseToken);

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,49 @@ public static GetTrip fromEntity(Trip trip) {
public static class GetAllTrip {
private final List<GetTrip> trips;
}

@Getter
@Builder
public static class GetTripSpec {
private final Long id;

private final String email;

private final LocalDate startDate;

private final LocalDate endDate;

private final Integer analyzingCount;

private final String tripName;

private final int numOfCard;

private final Float happy;

private final Float sad;

private final Float angry;

private final Float neutral;

private final Float disgust;

public static GetTripSpec fromEntity(Trip trip, int numOfCard, Float happy, Float sad, Float angry, Float neutral, Float disgust) {
return GetTripSpec.builder()
.id(trip.getId())
.email(trip.getUser().getEmail())
.startDate(trip.getStartDate())
.endDate(trip.getEndDate())
.analyzingCount(trip.getAnalyzingCount())
.tripName(trip.getTripName())
.numOfCard(numOfCard)
.happy(happy)
.sad(sad)
.angry(angry)
.neutral(neutral)
.disgust(disgust)
.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,15 @@ public void createReceipt(Long userId, ReceiptRequestDTO.createReceipt createRec
float disgust = map.get("disgust");
float total = happy + sad + angry + neutral + disgust;

happy = (Float.isNaN(happy / total)) ? (float) 0.0 : happy / total;
sad = (Float.isNaN(sad / total)) ? (float) 0.0 : sad / total;
angry = (Float.isNaN(angry / total)) ? (float) 0.0 : angry / total;
neutral = (Float.isNaN(neutral / total)) ? (float) 0.0 : neutral / total;
disgust = (Float.isNaN(disgust / total)) ? (float) 0.0 : disgust / total;

// 영수증 생성
Receipt receipt = Receipt.builder()
.trip(trip)
.mainDeparture(createReceipt.getMainDeparture())
.subDeparture(createReceipt.getSubDeparture())
.mainDestination(createReceipt.getMainDestination())
Expand All @@ -160,11 +167,11 @@ public void createReceipt(Long userId, ReceiptRequestDTO.createReceipt createRec
.stDate(trip.getStartDate())
.edDate(trip.getEndDate())
.tripName(trip.getTripName())
.happy(happy / total)
.sad(sad / total)
.angry(angry / total)
.neutral(neutral / total)
.disgust(disgust / total)
.happy(happy)
.sad(sad)
.angry(angry)
.neutral(neutral)
.disgust(disgust)
.receiptThemeType(createReceipt.getReceiptThemeType())
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
Expand All @@ -28,6 +29,7 @@ public class TripService {
private final AlreadyBookedDateService alreadyBookedDateService;
private final TripFileService tripFileService;
private final EntityManager em;
private final ReceiptService receiptService;

public void save(Trip trip) {
tripRepository.save(trip);
Expand Down Expand Up @@ -138,4 +140,33 @@ public TripResponseDTO.GetAllTrip getAllTrip(Long userId) {
}
return TripResponseDTO.GetAllTrip.builder().trips(rtnList).build();
}

public TripResponseDTO.GetTripSpec getTrip(Long tripId) {
Trip trip = tripRepository.findById(tripId).orElseThrow(() -> new RuntimeException("존재하지 않는 여행입니다."));
// 해당 여행이 끝났는지, 해당 여행에 분석중인 카드뷰가 남아있는지 확인
if (!trip.getEndDate().isBefore(LocalDate.now())) {
throw new IllegalArgumentException("여행이 끝나지 않았습니다.");
}
if (trip.getAnalyzingCount() != 0) {
throw new IllegalArgumentException("분석중인 카드뷰가 남아있습니다.");
}
Map<String, Float> map = receiptService.getCardViewCount(trip);
int cardViewCount = map.get("total").intValue();

// 감정 별 통계
float happy = map.get("happy");
float sad = map.get("sad");
float angry = map.get("angry");
float neutral = map.get("neutral");
float disgust = map.get("disgust");
float total = happy + sad + angry + neutral + disgust;

happy = (Float.isNaN(happy / total)) ? (float) 0.0 : happy / total;
sad = (Float.isNaN(sad / total)) ? (float) 0.0 : sad / total;
angry = (Float.isNaN(angry / total)) ? (float) 0.0 : angry / total;
neutral = (Float.isNaN(neutral / total)) ? (float) 0.0 : neutral / total;
disgust = (Float.isNaN(disgust / total)) ? (float) 0.0 : disgust / total;

return TripResponseDTO.GetTripSpec.fromEntity(trip, cardViewCount, happy, sad, angry, neutral, disgust);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@
import com.moment.core.exception.UserNotValidException;
import jakarta.persistence.EntityManager;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
@AllArgsConstructor
@Slf4j
public class UserService {
private final UserRepository userRepository;
private final TripService tripService;
Expand All @@ -32,25 +36,19 @@ public User save(UserRequestDTO.registerUser request) {
if (isAlreadyExist) {
throw new UserAlreadyExistException("이미 존재하는 아이디입니다.");
}
User user = User.builder()
.id(request.getId())
.email(request.getEmail())
.notification(request.isNotification())
.dataUsage(request.isDataUsage())
.firebaseToken(request.getFirebaseToken())
.build();
// em.persist(user);
User managedUser = em.merge(user);
tripService.save(Trip.builder()
.user(managedUser)
.analyzingCount(0)
.startDate(null)
.endDate(null)
.tripName("untitled trip")
.isNotTitled(true)
.build()
);
return userRepository.save(managedUser);
log.info("유저 등록을 시도합니다.");
log.info("user : {}", request.getId());
// User user = User.builder()
// .id(request.getId())
// .email(request.getEmail())
// .notification(request.isNotification())
// .dataUsage(request.isDataUsage())
// .firebaseToken(request.getFirebaseToken())
// .build();
// log.info("user : {}", user.getId());
// User mu = userRepository.save(user);
userRepository.saveN(request.getId(), LocalDateTime.now(), LocalDateTime.now(), request.getEmail(), request.isNotification(), request.isDataUsage(), request.getFirebaseToken());
return userRepository.findById(request.getId()).orElseThrow(() -> new IllegalArgumentException("존재하지 않는 유저입니다."));
}

public void validateUserWithTrip(Long userId, Long tripId) {
Expand Down
Loading

0 comments on commit c8f339c

Please sign in to comment.