Skip to content

Commit

Permalink
온도별 좋아요 순위
Browse files Browse the repository at this point in the history
  • Loading branch information
Johyunik committed Dec 5, 2023
1 parent 44ef150 commit 70871ae
Show file tree
Hide file tree
Showing 21 changed files with 101 additions and 46 deletions.
1 change: 0 additions & 1 deletion src/main/java/com/weatherfit/board/config/AwsS3Config.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.weatherfit.board.config;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3Client;
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/com/weatherfit/board/config/KafkaConfig.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
package com.weatherfit.board.config;


import org.apache.kafka.clients.admin.AdminClientConfig;
import org.apache.kafka.clients.admin.NewTopic;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.ListSerializer;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaAdmin;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;
import org.springframework.kafka.support.serializer.JsonSerializer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@EnableKafka
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.weatherfit.board.controller;


import com.amazonaws.services.kms.model.NotFoundException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
Expand All @@ -15,6 +14,10 @@
import com.weatherfit.board.service.LikeService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
Expand Down Expand Up @@ -165,4 +168,14 @@ public List<BoardSearchDTO> search(@RequestParam(required = false) List<String>
@RequestParam(required = false) List<String> hashtags) {
return boardService.search(categories, hashtags);
}

@GetMapping("/tops")
public Page<BoardListResponseDTO> getTop5BoardByAverageTemperatureAndLikes(
@RequestParam("temp_min") double minTemp,
@RequestParam("temp_max") double maxTemp,
@PageableDefault(size = 5, sort = "likeCount", direction = Sort.Direction.DESC) Pageable pageable) {
return boardService.getTop5Board(minTemp, maxTemp, pageable);
}


}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package com.weatherfit.board.controller;

import com.weatherfit.board.domain.BoardEntity;
import com.weatherfit.board.dto.MyLikeDTO;
import com.weatherfit.board.service.LikeService;
import org.springframework.web.bind.annotation.*;
Expand All @@ -18,7 +20,7 @@ public LikeController(LikeService likeService) {
}

@PostMapping("/like/{boardId}")
public boolean like (@RequestHeader("decodedToken") String nickName, @PathVariable("boardId") int boardId) throws UnsupportedEncodingException {
public boolean like(@RequestHeader("decodedToken") String nickName, @PathVariable("boardId") int boardId) throws UnsupportedEncodingException {
String decodedNickname = new String(Base64.getDecoder().decode(nickName), "UTF-8");
likeService.like(boardId, decodedNickname);
return true;
Expand All @@ -31,4 +33,7 @@ public List<MyLikeDTO> myLike(@RequestHeader("decodedToken") String nickName) th

return myLikeList;
}



}
1 change: 0 additions & 1 deletion src/main/java/com/weatherfit/board/domain/BoardEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public class BoardEntity extends BaseEntity {
@Column(name = "category", nullable = false)
private List<String> category = new ArrayList<>();


@ElementCollection
@Builder.Default
@Column(name = "hashTag")
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/weatherfit/board/domain/ImageEntity.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package com.weatherfit.board.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.weatherfit.board.dto.ImageDTO;
import jakarta.persistence.*;
Expand Down Expand Up @@ -36,6 +37,5 @@ public ImageDTO entityToDTO(ImageEntity imageEntity) {
.imageId(imageEntity.getImageId())
.imageUrl(imageEntity.getImageUrl())
.build();

}
}
33 changes: 33 additions & 0 deletions src/main/java/com/weatherfit/board/dto/BoardDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.weatherfit.board.dto;

import com.weatherfit.board.domain.BoardEntity;
import com.weatherfit.board.domain.LikeEntity;
import lombok.*;

import java.util.List;

@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class BoardDTO {
private int boardId;
private String nickName;
private int likeCount;
private double temperature;
private ImageDTO images;
private List<String> category;
private List<String> hashTag;
private String weatherIcon;
private List<LikeEntity> likelist;

public BoardDTO(BoardEntity boardEntity) {
this.boardId = boardEntity.getBoardId();
this.nickName = boardEntity.getNickName();
this.temperature = boardEntity.getTemperature();
this.category = boardEntity.getCategory();
this.hashTag = boardEntity.getHashTag();
this.weatherIcon = boardEntity.getWeatherIcon();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.weatherfit.board.domain.LikeEntity;
import lombok.*;

import java.util.List;

@Getter
Expand All @@ -23,5 +24,4 @@ public class BoardDetailResponseDTO {
private List<LikeEntity> likelist;



}
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package com.weatherfit.board.dto;


import com.weatherfit.board.domain.LikeEntity;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import java.util.List;


@Getter
@Builder
@Setter
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/weatherfit/board/dto/BoardSearchDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import java.util.List;


@Getter
@Setter
@Builder
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/weatherfit/board/dto/BoardWriteDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public class CommentResponseDTO {
private int boardId;
private String nickname;
private String content;

private String createdDate;
private String createdTime;
private int status;
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/com/weatherfit/board/dto/ImageDTO.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package com.weatherfit.board.dto;

import com.weatherfit.board.domain.BoardEntity;
import jakarta.persistence.*;
import lombok.*;

import java.util.List;


@Getter
@Builder
public class ImageDTO {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/weatherfit/board/dto/LikeDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.weatherfit.board.feignclient;

import com.weatherfit.board.dto.CommentResponseDTO;
import lombok.Getter;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
import java.util.ArrayList;
import java.util.List;

public class BoardCustomRepositoryImpl implements BoardCustomRepository{
public class BoardCustomRepositoryImpl implements BoardCustomRepository {

@PersistenceContext
private EntityManager em;

@Override
public List<BoardEntity> findBoardEntitiesWithCategoriesAndHashtags(List<String> categories, List<String> hashtags) {
CriteriaBuilder cb = em.getCriteriaBuilder();
Expand Down Expand Up @@ -45,5 +46,4 @@ public List<BoardEntity> findBoardEntitiesWithCategoriesAndHashtags(List<String>
}



}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.weatherfit.board.repository;

import com.weatherfit.board.domain.BoardEntity;
import feign.Param;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.awt.print.Pageable;
import java.util.List;

public interface BoardRepository extends JpaRepository<BoardEntity, Integer>, BoardCustomRepository {
Expand All @@ -13,11 +16,14 @@ public interface BoardRepository extends JpaRepository<BoardEntity, Integer>, Bo
List<BoardEntity> findAll();

List<BoardEntity> findAllByOrderByCreateDateDesc();

@Query("SELECT b FROM BoardEntity b LEFT JOIN LikeEntity l ON b.boardId = l.boardId.boardId GROUP BY b.boardId ORDER BY COUNT(l) DESC")
List<BoardEntity> findAllByOrderByLikeCountDesc();

BoardEntity findById(int id);

List<BoardEntity> findByNickName(String nickName);

@Query("SELECT b FROM BoardEntity b WHERE b.temperature >= :minTemp AND b.temperature <= :maxTemp ORDER BY size(b.likelist) DESC")
List<BoardEntity> findBoardsByTemperatureRange(@Param("minTemp") double minTemp, @Param("maxTemp") double maxTemp);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
package com.weatherfit.board.repository;

import com.weatherfit.board.domain.BoardEntity;
import com.weatherfit.board.domain.ImageEntity;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ImageRepository extends JpaRepository<ImageEntity, Integer> {

void deleteByBoardId(BoardEntity boardId);

ImageEntity findByImageUrl(String imageUrl);

boolean existsByImageUrl(String fileUrl);
}
33 changes: 33 additions & 0 deletions src/main/java/com/weatherfit/board/service/BoardService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
import com.weatherfit.board.dto.BoardListResponseDTO;
import com.weatherfit.board.dto.BoardSearchDTO;
import com.weatherfit.board.dto.BoardUpdateDTO;
import com.weatherfit.board.dto.ImageDTO;
import com.weatherfit.board.repository.BoardRepository;
import com.weatherfit.board.repository.ImageRepository;
import com.weatherfit.board.repository.LikeRepository;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
Expand All @@ -26,6 +29,7 @@ public class BoardService {

private final KafkaTemplate<String, String> kafkaTemplate;
private final BoardRepository boardRepository;
private final LikeRepository likeRepository;
private final ImageRepository imageRepository;
private final ImageService imageService;
@Autowired
Expand Down Expand Up @@ -250,4 +254,33 @@ public List<BoardSearchDTO> search(List<String> categories, List<String> hashTag
return result;
}


// 온도 별 좋아요 Top 5 게시글
public Page<BoardListResponseDTO> getTop5Board(double minTemp, double maxTemp, Pageable pageable) {
List<BoardEntity> boards = boardRepository.findBoardsByTemperatureRange(minTemp, maxTemp);

int start = (int) pageable.getOffset();
int end = Math.min((start + pageable.getPageSize()), boards.size());

List<BoardListResponseDTO> dtos = new ArrayList<>();

for (BoardEntity board : boards.subList(start, end)) {
BoardListResponseDTO dto = BoardListResponseDTO.builder()
.boardId(board.getBoardId())
.nickName(board.getNickName())
.temperature(board.getTemperature())
.likeCount(likeService.countLikes(board.getBoardId()))
.images(board.entityToDTO(board.getImages().get(0)))
.category(board.getCategory())
.hashTag(board.getHashTag())
.weatherIcon(board.getWeatherIcon())
.likelist(board.getLikelist())
.build();

dtos.add(dto);
}

return new PageImpl<BoardListResponseDTO>(dtos, pageable, boards.size());
}

}
12 changes: 0 additions & 12 deletions src/main/java/com/weatherfit/board/service/ImageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.weatherfit.board.domain.ImageEntity;
import com.weatherfit.board.repository.ImageRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

Expand All @@ -20,11 +18,9 @@ public class ImageService {
ImageRepository imageRepository;
private final AmazonS3Client amazonS3Client;


@Value("${cloud.aws.s3.bucket}")
private String bucketName;


public String saveImage(MultipartFile file) {
try {
String originalFilename = file.getOriginalFilename();
Expand All @@ -48,12 +44,4 @@ public String saveImage(MultipartFile file) {
throw new RuntimeException("Failed to upload image to S3", e);
}
}


// public void deleteImage(ImageEntity imageEntity) {
// String imageUrl = imageEntity.getImageUrl();
//// String fileName = imageUrl.substring(imageUrl.lastIndexOf("/") + 1);
// amazonS3Client.deleteObject(bucketName, imageUrl);
// }

}
Loading

0 comments on commit 70871ae

Please sign in to comment.