Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix]:Main Papago 번역 main에 적용 #151

Merged
merged 2 commits into from
May 23, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import com.kusitms29.backendH.api.sync.service.SyncService;
import com.kusitms29.backendH.api.sync.service.dto.request.SyncInfoRequestDto;
import com.kusitms29.backendH.api.sync.service.dto.response.SyncAssociateInfoResponse;
import com.kusitms29.backendH.api.sync.service.dto.response.SyncAssociateInfoResponseDto;
import com.kusitms29.backendH.api.sync.service.dto.response.SyncInfoResponse;
import com.kusitms29.backendH.api.sync.service.dto.response.SyncInfoResponseDto;
import com.kusitms29.backendH.domain.user.ip.IpService;
import com.kusitms29.backendH.global.common.SuccessResponse;
import com.kusitms29.backendH.infra.config.auth.UserId;
import com.kusitms29.backendH.infra.external.clova.map.GeoLocationService;
import com.kusitms29.backendH.infra.utils.TranslateUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
Expand All @@ -25,26 +28,31 @@ public class SyncMainController {
private final SyncService syncManageService;
private final IpService ipService;
private final GeoLocationService geoLocationService;
private final TranslateUtil translateUtil;
@GetMapping("/recommend")
public ResponseEntity<SuccessResponse<?>> recommendSync(@UserId Long userId, HttpServletRequest request) throws NoSuchAlgorithmException, InvalidKeyException, IOException {
public ResponseEntity<SuccessResponse<?>> recommendSync(@UserId Long userId,@RequestParam(name = "language", defaultValue = "한국어") String language, HttpServletRequest request) throws NoSuchAlgorithmException, InvalidKeyException, IOException {
String clientIp = ipService.getClientIpAddress(request);
// GeoLocation geoLocation = geoLocationService.getGeoLocation(clientIp);
List<SyncInfoResponseDto> syncInfoResponseDtos = syncManageService.recommendSync(userId, clientIp);
if (language.equals("영어"))syncInfoResponseDtos=syncInfoResponseDtos.stream().map(syncInfoResponseDto -> translateUtil.translateObject(syncInfoResponseDto)).toList();
return SuccessResponse.ok(syncInfoResponseDtos);
}
@PostMapping("/friend")
public ResponseEntity<SuccessResponse<?>> friendSync(@UserId Long userId, @RequestBody SyncInfoRequestDto syncInfoRequestDto) {
List<SyncInfoResponseDto> syncInfoResponseDtos = syncManageService.friendSync(userId, syncInfoRequestDto);
List<SyncInfoResponse> syncInfoResponseDtos = syncManageService.friendSync(userId, syncInfoRequestDto);
if (syncInfoRequestDto.language().equals("영어"))syncInfoResponseDtos=syncInfoResponseDtos.stream().map(syncInfoResponseDto -> translateUtil.translateObject(syncInfoResponseDto)).toList();
return SuccessResponse.ok(syncInfoResponseDtos);
}
@PostMapping("/search")
public ResponseEntity<SuccessResponse<?>> searchSync(@UserId Long userId, @RequestBody SyncInfoRequestDto syncInfoRequestDto) {
List<SyncInfoResponseDto> syncInfoResponseDtos = syncManageService.searchSync(userId, syncInfoRequestDto);
List<SyncInfoResponse> syncInfoResponseDtos = syncManageService.searchSync(userId, syncInfoRequestDto);
if (syncInfoRequestDto.language().equals("영어"))syncInfoResponseDtos=syncInfoResponseDtos.stream().map(syncInfoResponseDto -> translateUtil.translateObject(syncInfoResponseDto)).toList();
return SuccessResponse.ok(syncInfoResponseDtos);
}
@PostMapping("/associate")
public ResponseEntity<SuccessResponse<?>> associateSync(@UserId Long userId, @RequestBody SyncInfoRequestDto syncInfoRequestDto) {
List<SyncAssociateInfoResponseDto> syncInfoResponseDtos = syncManageService.associateSync(userId, syncInfoRequestDto);
List<SyncAssociateInfoResponse> syncInfoResponseDtos = syncManageService.associateSync(userId, syncInfoRequestDto);
if (syncInfoRequestDto.language().equals("영어"))syncInfoResponseDtos=syncInfoResponseDtos.stream().map(syncInfoResponseDto -> translateUtil.translateObject(syncInfoResponseDto)).toList();
return SuccessResponse.ok(syncInfoResponseDtos);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ public List<SyncInfoResponseDto> recommendSync(Long userId, String clientIp){
favoriteSyncManager.existsByUserIdAndSyncId(userId, sync.getId())
)).toList();
}
public List<SyncInfoResponseDto> friendSync(Long userId, SyncInfoRequestDto syncInfoRequestDto){
public List<SyncInfoResponse> friendSync(Long userId, SyncInfoRequestDto syncInfoRequestDto){
List<Sync> syncList = syncReader.findAllBySyncTypeAndType(FROM_FRIEND, getEnumTypeFromStringType(syncInfoRequestDto.type()));
List<SyncInfoResponseDto> syncInfoResponseDtos = syncList.stream()
List<SyncInfoResponse> syncInfoResponseDtos = syncList.stream()
//음 이거보다 위에서 if문써서 하는게 더 가독성 있는듯
// .filter(sync -> type == null || sync.getType().name().equals(type))
.map( sync -> SyncInfoResponseDto.of(
.map( sync -> SyncInfoResponse.of(
sync.getId(),
sync.getSyncType(),
sync.getType(),
Expand All @@ -93,9 +93,9 @@ public List<SyncInfoResponseDto> friendSync(Long userId, SyncInfoRequestDto sync
)).toList();
return listUtils.getListByTake(syncInfoResponseDtos, syncInfoRequestDto.take());
}
public List<SyncAssociateInfoResponseDto> associateSync(Long userId, SyncInfoRequestDto syncInfoRequestDto){
public List<SyncAssociateInfoResponse> associateSync(Long userId, SyncInfoRequestDto syncInfoRequestDto){
List<Sync> syncList = syncReader.findAllByAssociateIsExist(getEnumFROMStringSyncType(syncInfoRequestDto.syncType()), getEnumTypeFromStringType(syncInfoRequestDto.type()));
List<SyncAssociateInfoResponseDto> syncAssociateInfoResponseDtos = syncList.stream().map( sync -> SyncAssociateInfoResponseDto.of(
List<SyncAssociateInfoResponse> syncAssociateInfoResponseDtos = syncList.stream().map( sync -> SyncAssociateInfoResponse.of(
sync.getId(),
sync.getSyncType(),
sync.getType(),
Expand All @@ -110,10 +110,10 @@ public List<SyncAssociateInfoResponseDto> associateSync(Long userId, SyncInfoReq
)).toList();
return listUtils.getListByTake(syncAssociateInfoResponseDtos, syncInfoRequestDto.take());
}
public List<SyncInfoResponseDto> searchSync(Long userId, SyncInfoRequestDto syncInfoRequestDto){
public List<SyncInfoResponse> searchSync(Long userId, SyncInfoRequestDto syncInfoRequestDto){
List<Sync> syncList = syncReader.findAllBySyncTypeAndType(getEnumFROMStringSyncType(syncInfoRequestDto.syncType()), getEnumTypeFromStringType(syncInfoRequestDto.type()));

List<SyncInfoResponseDto> syncInfoResponseDtos = syncList.stream().map( sync -> SyncInfoResponseDto.of(
List<SyncInfoResponse> syncInfoResponseDtos = syncList.stream().map( sync -> SyncInfoResponse.of(
sync.getId(),
sync.getSyncType(),
sync.getType(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
public record SyncInfoRequestDto(
int take,
String syncType,
String type
String type,
String language
) {
public SyncInfoRequestDto {
if (take < 0) {
take = 0;
}
if (language == null || language.isBlank()) {
language = "한국어";
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.kusitms29.backendH.api.sync.service.dto.response;

import com.kusitms29.backendH.domain.category.entity.Type;
import com.kusitms29.backendH.domain.sync.entity.SyncType;
import lombok.Builder;
import lombok.Getter;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DecimalStyle;
import java.util.Locale;

@Getter
@Builder
public class SyncAssociateInfoResponse {
private Long syncId;
private String syncType;
private String type;
private String image;
private int userCnt;
private int totalCnt;
private String syncName;
private String location;
private String date;
private String associate;
private Boolean isMarked;
private static final DateTimeFormatter DATE_TIME_FORMATTER =
DateTimeFormatter.ofPattern("M/d(EEE) a h시")
.withLocale(Locale.KOREAN)
.withDecimalStyle(DecimalStyle.of(Locale.KOREAN));

public SyncAssociateInfoResponse(){

}
public SyncAssociateInfoResponse(
Long syncId,
String syncType,
String type,
String image,
int userCnt,
int totalCnt,
String syncName,
String location,
String date,
String associate,
Boolean isMarked) {
this.syncId = syncId;
this.syncType = syncType;
this.type = type;
this.image = image;
this.userCnt = userCnt;
this.totalCnt = totalCnt;
this.syncName = syncName;
this.location = location;
this.date = date;
this.associate = associate;
this.isMarked = isMarked;
}
public static SyncAssociateInfoResponse of(
Long syncId,
SyncType syncType,
Type type,
String image,
int userCnt,
int totalCnt,
String syncName,
String location,
LocalDateTime date,
String associate,
Boolean isMarked) {
String dateString = date != null ? date.format(DATE_TIME_FORMATTER) : null;
return new SyncAssociateInfoResponse(
syncId,
syncType.getStringSyncType(),
type.getStringType(),
image,
userCnt,
totalCnt,
syncName,
location,
dateString,
associate,
isMarked
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.kusitms29.backendH.api.sync.service.dto.response;

import com.kusitms29.backendH.domain.category.entity.Type;
import com.kusitms29.backendH.domain.sync.entity.SyncType;
import lombok.Builder;
import lombok.Getter;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DecimalStyle;
import java.util.Locale;
@Getter
@Builder
public class SyncInfoResponse {
private Long syncId;
private String syncType;
private String type;
private String image;
private int userCnt;
private int totalCnt;
private String syncName;
private String location;
private String date;
private Boolean isMarked;

private static final DateTimeFormatter DATE_TIME_FORMATTER =
DateTimeFormatter.ofPattern("M/d(EEE) a h시")
.withLocale(Locale.KOREAN)
.withDecimalStyle(DecimalStyle.of(Locale.KOREAN));

public SyncInfoResponse(){

}
public SyncInfoResponse(
Long syncId,
String syncType,
String type,
String image,
int userCnt,
int totalCnt,
String syncName,
String location,
String date,
Boolean isMarked) {
this.syncId = syncId;
this.syncType = syncType;
this.type = type;
this.image = image;
this.userCnt = userCnt;
this.totalCnt = totalCnt;
this.syncName = syncName;
this.location = location;
this.date = date;
this.isMarked = isMarked;
}

public static SyncInfoResponse of(
Long syncId,
SyncType syncType,
Type type,
String image,
int userCnt,
int totalCnt,
String syncName,
String location,
LocalDateTime date,
Boolean isMarked) {
String dateString = date != null ? date.format(DATE_TIME_FORMATTER) : null;
return new SyncInfoResponse(
syncId,
syncType.getStringSyncType(),
type.getStringType(),
image,
userCnt,
totalCnt,
syncName,
location,
dateString,
isMarked
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public static ChatContent createChatContent(String userName, String content, Roo
.content(content)
.time(LocalDateTime.now())
.build();
if(room==null)
return chatContent;
room.addChatContent(chatContent);
return chatContent;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ public static Room createRoom(List<ChatUser> users, List<ChatContent> contents,
}
return room;
}
public static Room createNewRoom(String roomName) {
public static Room chat(String roomName,ChatContent chatContent) {
Room room = Room.builder()
.roomName(roomName)
.build();
room.addChatContent(chatContent);
return room;
}
public void addChatContent(ChatContent content) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
import com.kusitms29.backendH.domain.chat.entity.Room;
import org.springframework.data.mongodb.repository.MongoRepository;

import java.util.Optional;

public interface RoomRepository extends MongoRepository<Room, String> {
Optional<Room> findByRoomName(String roomName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.kusitms29.backendH.global.common;

public record ChatReq(
String roomName,
String content
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,37 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kusitms29.backendH.domain.chat.entity.ChatContent;
import com.kusitms29.backendH.domain.chat.entity.ChatUser;
import com.kusitms29.backendH.domain.chat.entity.Room;
import com.kusitms29.backendH.domain.chat.repository.RoomRepository;
import com.kusitms29.backendH.domain.user.entity.User;
import com.kusitms29.backendH.domain.user.service.UserReader;
import com.kusitms29.backendH.infra.config.auth.UserId;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api")
public class HealthCheckApiController {
private final MongoTemplate mongoTemplate;
private final RoomRepository roomRepository;
private final UserReader userReader;
private static final String GOOGLE_AUTH_ENDPOINT = "https://accounts.google.com/o/oauth2/v2/auth";
private static final String GOOGLE_TOKEN_ENDPOINT = "https://oauth2.googleapis.com/token";
private static final String KAKAO_AUTH_ENDPOINT = "https://kauth.kakao.com/oauth/authorize";
Expand Down Expand Up @@ -137,5 +150,16 @@ public ResponseEntity<String> kakaoOauthCallback(@RequestParam(name = "code") St
public Long MeetUpServer(@UserId Long userId) {
return userId;
}
@PostMapping("/chat")
public void chat(@UserId Long userId, @RequestBody ChatReq chatReq){
User user = userReader.findByUserId(userId);
Query query = new Query();
query.addCriteria(Criteria.where("roomName").is(chatReq.roomName()));
Room room = mongoTemplate.findOne(query, Room.class);

ChatContent chatContent = ChatContent.createChatContent(user.getUserName(), chatReq.content(), room);
// room.addChatContent(chatContent);
roomRepository.save(room);

}
}
Loading