Skip to content

Commit

Permalink
fix: confilct 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
mclub4 committed May 16, 2024
2 parents 480339d + f2512cb commit ff0d348
Show file tree
Hide file tree
Showing 39 changed files with 472 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public RouteLocator gatewayRoutes(RouteLocatorBuilder builder,
.uri("http://ruby:3000"))
.route("nonJwt-spring", r -> r.path("/api/user/signin", "/api/user/test", "/api/user/signup",
"/api/announcement/**", "/api/menu/**", "/api/speech/**", "/api/question/read", "/api/question/list",
"/api/answer/list", "/api/faq/**", "/api/help/read", "/api/help/list", "/api/auth/**", "/api/swagger-ui/**"
, "/api/api-docs/**", "/api/auth/logout")
"/api/faq/**", "/api/help/read", "/api/help/list", "/api/auth/**", "/api/swagger-ui/**", "/api/api-docs/**",
"/api/auth/logout")
.uri("http://spring:8080"))
.route("spring", r -> r.path("/api/**")
.filters(f->f
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.example.capstone.domain.like.entity;

import static com.querydsl.core.types.PathMetadataFactory.*;

import com.querydsl.core.types.dsl.*;

import com.querydsl.core.types.PathMetadata;
import javax.annotation.processing.Generated;
import com.querydsl.core.types.Path;


/**
* QLike is a Querydsl query type for Like
*/
@Generated("com.querydsl.codegen.DefaultEntitySerializer")
public class QLike extends EntityPathBase<Like> {

private static final long serialVersionUID = -1705593941L;

public static final QLike like = new QLike("like1");

public final NumberPath<Long> answerId = createNumber("answerId", Long.class);

public final NumberPath<Long> id = createNumber("id", Long.class);

public final BooleanPath isClick = createBoolean("isClick");

public final StringPath uuid = createString("uuid");

public QLike(String variable) {
super(Like.class, forVariable(variable));
}

public QLike(Path<? extends Like> path) {
super(path.getType(), path.getMetadata());
}

public QLike(PathMetadata metadata) {
super(Like.class, metadata);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class AnnouncementCrawlService {

private final AnnouncementRepository announcementRepository;

private final List<String> languages = List.of("KO", "EN-US");
private final List<String> languages = List.of("KO", "EN-US", "ZH");

@Async
@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
public class HelpController {

private final HelpService helpService;
private final JwtTokenProvider jwtTokenProvider;

@PostMapping(value = "/create")
@Operation(summary = "헬퍼글 생성", description = "request 정보를 기반으로 헬퍼글을 생성합니다.")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.capstone.domain.help.exception;

import com.example.capstone.global.error.exception.EntityNotFoundException;

public class HelpNotFoundException extends EntityNotFoundException {
public HelpNotFoundException(Long id) {
super(id + " is not found help");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.capstone.domain.help.service;

import com.example.capstone.domain.help.dto.*;
import com.example.capstone.domain.help.exception.HelpNotFoundException;
import com.example.capstone.domain.help.repository.HelpListRepository;
import com.example.capstone.domain.help.repository.HelpRepository;
import com.example.capstone.domain.help.entity.Help;
Expand Down Expand Up @@ -28,14 +29,17 @@ public HelpResponse createHelp(String userId, HelpPostRequest request) {
}

public HelpResponse getHelp(Long id) {
Help help = helpRepository.findById(id).get();
Help help = helpRepository.findById(id).orElseThrow(() ->
new HelpNotFoundException(id)
);
return help.toDTO();
}

@Transactional
public void updateHelp(String userId, HelpPutRequest request) {
LocalDateTime current = LocalDateTime.now();
Help help = helpRepository.findById(request.id()).get();
Help help = helpRepository.findById(request.id()).orElseThrow(() ->
new HelpNotFoundException(request.id()));
if(help.getUuid().equals(userId)){
help.update(request.title(), request.context(), current);
}
Expand All @@ -56,7 +60,9 @@ public HelpSliceResponse getHelpList(HelpListRequest request) {

@Transactional
public void doneHelp(String userId, Long id) {
Help help = helpRepository.findById(id).get();
Help help = helpRepository.findById(id).orElseThrow(() ->
new HelpNotFoundException(id)
);
if(help.getUuid().equals(userId)){
help.done();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.example.capstone.domain.like.entity;

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

@Entity
@Table(name = "likes")
@Getter
@Builder
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Like {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;

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

@Column(name = "answer_id", nullable = false)
private Long answerId;

@Column(name = "is_click", nullable = false)
private Boolean isClick;

public void updateClick(Boolean isClick) {
this.isClick = isClick;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.capstone.domain.like.repository;

import com.example.capstone.domain.like.entity.Like;

import java.util.List;
import java.util.Optional;

public interface LikeCustomRepository {
Optional<Like> findByAnswerIdAndUuid(Long answerId, String uuid);

void deleteByAnswerIdAndUuid(Long answerId, String uuid);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.capstone.domain.like.repository;

import com.example.capstone.domain.like.entity.Like;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface LikeRepository extends JpaRepository<Like, Long>, LikeCustomRepository {
Like save(Like like);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.example.capstone.domain.like.repository;

import com.example.capstone.domain.like.entity.QLike;
import com.example.capstone.domain.like.entity.Like;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;

import java.util.List;
import java.util.Optional;

@RequiredArgsConstructor
public class LikeRepositoryImpl implements LikeCustomRepository {
private final JPAQueryFactory jpaQueryFactory;
private final QLike like = QLike.like;

@Override
public Optional<Like> findByAnswerIdAndUuid(Long answerId, String uuid) {
return Optional.ofNullable(jpaQueryFactory
.selectFrom(like)
.where(like.answerId.eq(answerId),
like.uuid.eq(uuid))
.fetchOne());
}

@Override
public void deleteByAnswerIdAndUuid(Long answerId, String uuid) {
jpaQueryFactory
.delete(like)
.where(like.answerId.eq(answerId),
like.uuid.eq(uuid))
.execute();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.example.capstone.domain.like.service;

import com.example.capstone.domain.like.entity.Like;
import com.example.capstone.domain.like.repository.LikeRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
public class LikeService {
private final LikeRepository likeRepository;

@Transactional
public void likeAnswer(String userId, Long answerId) {
likeRepository.findByAnswerIdAndUuid(answerId, userId).ifPresentOrElse(
l -> {
l.updateClick(true);
}, () -> {
likeRepository.save(Like.builder().uuid(userId).answerId(answerId).isClick(true).build());
});
}

@Transactional
public void unlikeAnswer(String userId, Long answerId) {
likeRepository.findByAnswerIdAndUuid(answerId, userId).ifPresent(
l -> {
l.updateClick(false);
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

@Controller
Expand All @@ -20,8 +22,8 @@
public class MenuController {
private final MenuCrawlingService menuCrawlingService;
private final MenuSearchService menuSearchService;
private final MenuUpdateService menuUpdateService;

@ResponseBody
@GetMapping("/daily")
public ResponseEntity<ApiResult<List<Object>>> getMenuByDate(@RequestParam LocalDate date, @RequestParam String language){
List<Object> menu = menuSearchService.findMenuByDate(date, language);
Expand All @@ -31,10 +33,11 @@ public ResponseEntity<ApiResult<List<Object>>> getMenuByDate(@RequestParam Local

@PostMapping("/test")
@Operation(summary = "학식 파싱", description = "[주의 : 테스트용] 강제로 학생 저장을 시킴 (DB 중복해서 들어가니깐 물어보고 쓰세요!!)")
public ResponseEntity<?> testMenu(@RequestParam(value = "key") String key){
public ResponseEntity<?> testMenu(@RequestParam(value = "key") String key, @RequestParam LocalDate date){
menuCrawlingService.testKeyCheck(key);
menuCrawlingService.crawlingMenus();
return ResponseEntity.ok("");
menuUpdateService.updateMenus(date);
return ResponseEntity
.ok(new ApiResult<>("Successfully update menu", 200));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;

import static com.example.capstone.global.error.exception.ErrorCode.TEST_KEY_NOT_VALID;
Expand All @@ -26,7 +27,7 @@ public boolean testKeyCheck(String key){

@Scheduled(cron = "0 0 4 * * MON")
public void crawlingMenus(){
LocalDateTime startDay = LocalDateTime.now();
LocalDate startDay = LocalDate.now();

for(int i=0; i<7; i++){
menuUpdateService.updateMenus(startDay.plusDays(i));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
Expand All @@ -27,35 +29,45 @@ public class MenuUpdateService {

@Value("${deepl.api.key}")
private String authKey;
List<String> languages = List.of("KO", "EN-US", "ZH");

@Async
public void updateMenus(LocalDateTime startTime) {
@Transactional
public void updateMenus(LocalDate startTime) {
RestTemplate restTemplate = new RestTemplateBuilder().build();
String sdate = startTime.format(DateTimeFormatter.ISO_LOCAL_DATE);
String sdate = startTime.toString();
String url = "https://kmucoop.kookmin.ac.kr/menu/menujson.php?callback=jQuery112401919322099601417_1711424604017";

url += "&sdate=" + sdate + "&edate=" + sdate + "&today=" + sdate + "&_=1711424604018";
String escapeString = restTemplate.getForObject(url, String.class);
CompletableFuture<String> decode = decodeUnicodeService.decodeUnicode(escapeString);

Translator translator = new Translator(authKey);
List<String> languages = List.of("KO", "EN-US");

try {
String response = decode.get();

System.out.println(response);

for(String language : languages) {
String json = response.substring(response.indexOf("(") + 1, response.lastIndexOf(")"));
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> allMap = mapper.readValue(json, Map.class);

for (Map.Entry<String, Object> cafeEntry : allMap.entrySet()) {
if( (cafeEntry.getValue() instanceof Map) == false ) continue;

String cafeteria = translator.translateText(cafeEntry.getKey(), null, language).getText();

for (Map.Entry<String, Object> dateEntry : ((Map<String, Object>) cafeEntry.getValue()).entrySet()) {
if( (dateEntry.getValue() instanceof Map) == false ) continue;

String dateString = dateEntry.getKey();
LocalDateTime date = LocalDate.parse(dateString, DateTimeFormatter.ISO_LOCAL_DATE).atStartOfDay();

for (Map.Entry<String, Object> sectionEntry : ((Map<String, Object>) dateEntry.getValue()).entrySet()) {
if( (sectionEntry.getValue() instanceof Map) == false ) continue;

String section = translator.translateText(sectionEntry.getKey(), null, language).getText();
String name = "";
Long price = 0L;
Expand Down
Loading

0 comments on commit ff0d348

Please sign in to comment.