Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
vydndrl committed Sep 4, 2024
2 parents 0d5c4d5 + 5fbdc35 commit d75ddf6
Show file tree
Hide file tree
Showing 15 changed files with 316 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public ResponseEntity<CommonResDto> deleteBlock(@AuthenticationPrincipal UserInf
return new ResponseEntity<>(commonResDto, HttpStatus.OK);
}

@GetMapping("/city/{stateId}")
@GetMapping("/city/stateId")
public ResponseEntity<CommonResDto> getBlocksByCity(@PathVariable Long stateId) {
List<BlockActiveListRsDto> blocks = blockService.getBlocksByState(stateId);
return new ResponseEntity<>(new CommonResDto(HttpStatus.OK, "Success", blocks), HttpStatus.OK);
Expand Down
24 changes: 18 additions & 6 deletions src/main/java/com/example/want/api/block/domain/Block.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.want.api.block.domain;

import com.example.want.api.block.dto.BlockDetailRsDto;
import com.example.want.api.location.domain.Location;
import com.example.want.api.project.domain.Project;
import com.example.want.common.BaseEntity;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -29,15 +30,20 @@ public class Block extends BaseEntity {
@Enumerated(EnumType.STRING)
private Category category;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "location_id")
private Location location;

private Double latitude;
private Double longitude;

private LocalDateTime startTime;
private LocalDateTime endTime;
private String isActivated;
private Long heartCount;
private Long popularCount;
private String isDeleted;

@Builder.Default
private boolean isHearted = false;

Expand All @@ -54,12 +60,13 @@ public BlockDetailRsDto toDetailDto() {
.content(this.content)
.placeName(this.placeName)
.category(this.category)
.latitude(this.latitude)
.longitude(this.longitude)
.latitude(this.getLatitude())
.longitude(this.getLongitude())
.startTime(this.startTime != null ? this.startTime.toString() : null)
.endTime(this.endTime != null ? this.endTime.toString() : null)
.isActivated(this.isActivated)
.heartCount(this.heartCount)
.popularCount(this.getPopularCount())
.isHearted(this.isHearted)
.projectId(this.project.getId())
.build();
Expand Down Expand Up @@ -99,9 +106,10 @@ public void changeIsActivated(String isActivated) {
this.isActivated = isActivated;
}

public void updatePoint(Double latitude, Double longitude) {
this.latitude = latitude;
this.longitude = longitude;
public void updatePoint(Double latitude, Double longitude, String placeName) {
this.latitude = latitude;
this.longitude = longitude;
this.placeName = placeName;
}


Expand All @@ -114,4 +122,8 @@ public void initializeFields() {
public void changeIsDelete() {
this.isDeleted = "Y";
}

public void updateLocation(Location location) {
this.location = location;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class BlockActiveListRsDto {
private String startTime;
private String endTime;
private Long heartCount;
private Long popularCount;
private Category category;
private String isActivated;
private Boolean isHearted;
Expand All @@ -38,6 +39,7 @@ public static BlockActiveListRsDto fromEntity(Block block) {
.startTime(block.getStartTime() != null ? block.getStartTime().toString() : null)
.endTime(block.getEndTime() != null ? block.getEndTime().toString() : null)
.heartCount(block.getHeartCount())
.popularCount(block.getPopularCount())
.isActivated(block.getIsActivated())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.want.api.block.dto;

import com.example.want.api.block.domain.Category;
import com.example.want.api.location.domain.Location;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -23,6 +24,7 @@ public class BlockDetailRsDto {
private String endTime;
private String isActivated;
private Long heartCount;
private Long popularCount;
private Boolean isHearted;
private Long projectId;
}
27 changes: 23 additions & 4 deletions src/main/java/com/example/want/api/block/service/BlockService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.example.want.api.block.repository.BlockRepository;
import com.example.want.api.heart.domain.Heart;
import com.example.want.api.heart.repository.HeartRepository;
import com.example.want.api.location.domain.Location;
import com.example.want.api.location.repository.LocationRepository;
import com.example.want.api.member.domain.Member;
import com.example.want.api.member.login.UserInfo;
import com.example.want.api.member.repository.MemberRepository;
Expand All @@ -17,7 +19,6 @@
import com.example.want.api.sse.NotificationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -48,12 +49,15 @@ public class BlockService {
private final ProjectRepository projectRepository;
private final ProjectMemberRepository projectMemberRepository;
private final StateRepository stateRepository;
private final LocationRepository locationRepository;
private final StringRedisTemplate stringRedisTemplate;
private final NotificationService notificationService;

@Qualifier("heart")
private final RedisTemplate<String, Object> heartRedisTemplate;

@Qualifier("popular")
private final RedisTemplate<String, Long> popularRedisTemplate;

@Transactional
public Block createBlock(CreateBlockRqDto request, UserInfo userInfo) {
Expand Down Expand Up @@ -182,7 +186,6 @@ private Long getLikesCountFromCache(String key, String hashKey) {
} else if (cachedValue instanceof Integer) {
return ((Integer) cachedValue).longValue();
}

return null;
}

Expand Down Expand Up @@ -324,7 +327,15 @@ public BlockDetailRsDto updateBlock(Long id, UpdateBlockRqDto updateBlockRqDto,
block.updatePlaceName(updateBlockRqDto.getPlaceName());
}
if (updateBlockRqDto.getLatitude() != null && updateBlockRqDto.getLongitude() != null) {
block.updatePoint(updateBlockRqDto.getLatitude(), updateBlockRqDto.getLongitude());
block.updatePoint(updateBlockRqDto.getLatitude(), updateBlockRqDto.getLongitude(), updateBlockRqDto.getPlaceName());
String redisKey = updateBlockRqDto.getLatitude() + ":" + updateBlockRqDto.getLongitude();

// Redis에서 값을 가져오거나 초기화
if (popularRedisTemplate.opsForValue().get(redisKey) == null) {
popularRedisTemplate.opsForValue().set(redisKey, 0L);
}
// 레디스에서 해당 위치의 popularCount +1
popularRedisTemplate.opsForValue().increment(redisKey, 1L);
}

if (updateBlockRqDto.getStartTime() != null && updateBlockRqDto.getEndTime() != null) {
Expand Down Expand Up @@ -393,7 +404,15 @@ public Block importBlock(UserInfo userInfo, ImportBlockRqDto importDto) {
Project project = validateProjectMember(importDto.getProjectId(), userInfo.getEmail());
Block findBlock = blockRepository.findById(importDto.getBlockId()).orElseThrow(() -> new EntityNotFoundException("해당 블록을 찾을 수 없습니다."));
Block block = importDto.toImport(findBlock, project);
System.out.println(block);

String redisKey = findBlock.getLatitude() + ":" + findBlock.getLongitude();

// Redis에서 값을 가져오거나 초기화
if (popularRedisTemplate.opsForValue().get(redisKey) == null) {
popularRedisTemplate.opsForValue().set(redisKey, 0L);
}
// 레디스에서 해당 위치의 popularCount +1
popularRedisTemplate.opsForValue().increment(redisKey, 1L);
return blockRepository.save(block);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.example.want.api.location.controller;

import com.example.want.api.location.domain.Location;
import com.example.want.api.location.dto.LocationResDto;
import com.example.want.api.location.repository.LocationRepository;
import com.example.want.api.location.service.LocationService;
import com.example.want.common.CommonResDto;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1")
public class LocationController {

private final LocationService locationService;
private final LocationRepository locationRepository;

// 추천 블럭 리스트에 사용할 코드
@GetMapping("/city/{stateId}")
public ResponseEntity<CommonResDto> getPopularLocations(@PathVariable Long stateId) {
List<LocationResDto> popularLocations = locationService.getPopularLocations(stateId);
CommonResDto commonResDto = new CommonResDto(HttpStatus.OK, "조회 완료", popularLocations);
return new ResponseEntity<>(commonResDto, HttpStatus.OK);
}

@GetMapping("/pop")
public ResponseEntity<CommonResDto> getPopularCountFromCache() {
locationService.getPopularCountFromCache();
List<Location> popularLocations = locationRepository.findAll();
CommonResDto commonResDto = new CommonResDto(HttpStatus.OK, "조회 완료", popularLocations);
return new ResponseEntity<>(commonResDto, HttpStatus.OK);
}

}
46 changes: 46 additions & 0 deletions src/main/java/com/example/want/api/location/domain/Location.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.example.want.api.location.domain;

import com.example.want.api.location.dto.LocationResDto;
import com.example.want.api.state.domain.State;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import javax.validation.constraints.NotNull;

@Entity
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Location {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private Double latitude;
private Double longitude;

private Long popularCount;
private String placeName;

@ManyToOne(fetch = FetchType.LAZY)
@NotNull
@JoinColumn(name = "state_id")
private State state;

public void updatePopularCount(Long popularCount) {
this.popularCount += popularCount;
}

public LocationResDto fromEntity(Location location) {
LocationResDto dto = LocationResDto.builder()
.latitude(location.getLatitude())
.longitude(location.getLongitude())
.popularCount(location.getPopularCount())
.placeName(location.getPlaceName())
.build();
return dto;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.example.want.api.location.dto;


import com.example.want.api.location.domain.Location;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;


@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class LocationReqDto {
private Double latitude;
private Double longitude;

public static LocationReqDto toEntity(Location location) {
LocationReqDto dto = LocationReqDto.builder()
.latitude(location.getLatitude())
.longitude(location.getLongitude())
.build();
return dto;
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.want.api.location.dto;

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

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class LocationResDto {
private Double latitude;
private Double longitude;
private Long popularCount;
private String placeName;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.want.api.location.repository;

import com.example.want.api.location.domain.Location;
import com.example.want.api.location.dto.LocationResDto;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface LocationRepository extends JpaRepository<Location, Long> {
Location findByLatitudeAndLongitude(Double latitude, Double longitude);
List<Location> findAllByStateIdOrderByPopularCountDesc(Long stateId);
}
Loading

0 comments on commit d75ddf6

Please sign in to comment.