Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
redhean committed Dec 4, 2024
2 parents bc39dd6 + c87899f commit 604dadf
Show file tree
Hide file tree
Showing 70 changed files with 2,159 additions and 568 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.hallyugo.hallyugo.content.controller;

import com.hallyugo.hallyugo.content.domain.ContentResponseDto;
import com.hallyugo.hallyugo.content.domain.response.ContentForMapResponseDto;
import com.hallyugo.hallyugo.content.domain.response.ContentResponseDto;
import com.hallyugo.hallyugo.content.service.ContentService;
import java.util.List;
import java.util.Map;
Expand All @@ -12,26 +13,50 @@
import org.springframework.web.bind.annotation.RestController;

@RequiredArgsConstructor
@RequestMapping("/api/v1/content")
@RequestMapping("/api/v1/")
@RestController
public class ContentController {
private final ContentService contentService;

@GetMapping("/initial")
@GetMapping("/content/initial")
public ResponseEntity<Map<String, List<ContentResponseDto>>> getRandomContents() {
Map<String, List<ContentResponseDto>> result = contentService.getRandomContents();
return ResponseEntity.ok(result);
}

@GetMapping(params = "category")
@GetMapping(value = "/content", params = "category")
public ResponseEntity<List<ContentResponseDto>> getContentsByCategory(@RequestParam String category) {
List<ContentResponseDto> result = contentService.getContentsByCategory(category);
return ResponseEntity.ok(result);
}

@GetMapping(params = "keyword")
@GetMapping(value = "/content", params = "keyword")
public ResponseEntity<List<ContentResponseDto>> getContentsByKeyword(@RequestParam String keyword) {
List<ContentResponseDto> result = contentService.getContentsByKeyword(keyword);
return ResponseEntity.ok(result);
}

@GetMapping(value = "/location", params = "content_id")
public ResponseEntity<ContentForMapResponseDto> getContentWithLocationsAndImages(
@RequestParam(name = "content_id") Long contentId
) {
ContentForMapResponseDto result = contentService.getContentWithLocationsAndImages(contentId);
return ResponseEntity.ok(result);
}

@GetMapping(value = "/location", params = "category")
public ResponseEntity<List<ContentForMapResponseDto>> getContentsWithLocationsAndImagesByCategory(
@RequestParam(name = "category") String category
) {
List<ContentForMapResponseDto> result = contentService.getContentsWithLocationsAndImagesByCategory(category);
return ResponseEntity.ok(result);
}

@GetMapping(value = "/location", params = "keyword")
public ResponseEntity<List<ContentForMapResponseDto>> searchContentsWithLocationsAndImagesByKeyword(
@RequestParam(name = "keyword") String keyword
) {
List<ContentForMapResponseDto> result = contentService.getContentsWithLocationsAndImagesByKeyword(keyword);
return ResponseEntity.ok(result);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.hallyugo.hallyugo.content.domain;

import com.hallyugo.hallyugo.location.domain.Location;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
Expand All @@ -10,11 +8,8 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
Expand Down Expand Up @@ -55,9 +50,6 @@ public class Content {
@LastModifiedDate
private LocalDateTime updatedAt;

@OneToMany(mappedBy = "content", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Location> locations = new ArrayList<>();

public Content(Category category, String title, String description, String contentImageUrl, String hashtag) {
this.category = category;
this.title = title;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.hallyugo.hallyugo.content.domain.response;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.hallyugo.hallyugo.content.domain.Category;
import com.hallyugo.hallyugo.content.domain.Content;
import com.hallyugo.hallyugo.location.domain.response.LocationWithImagesResponseDto;
import java.util.List;
import lombok.Getter;

public class ContentForMapResponseDto {

@JsonProperty
private Long id;

@JsonProperty
private Category category;

@JsonProperty
private String title;

@JsonProperty
private String hashtag;

@JsonProperty
@Getter
private List<LocationWithImagesResponseDto> locations;

private ContentForMapResponseDto(Content content, List<LocationWithImagesResponseDto> locations) {
this.id = content.getId();
this.category = content.getCategory();
this.title = content.getTitle();
this.hashtag = content.getHashtag();
this.locations = locations;
}

public static ContentForMapResponseDto toDto(Content content,
List<LocationWithImagesResponseDto> locationWithImages) {
return new ContentForMapResponseDto(content, locationWithImages);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.hallyugo.hallyugo.content.domain;
package com.hallyugo.hallyugo.content.domain.response;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.hallyugo.hallyugo.content.domain.Category;
import com.hallyugo.hallyugo.content.domain.Content;

public class ContentResponseDto {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package com.hallyugo.hallyugo.content.service;

import com.hallyugo.hallyugo.common.exception.EntityNotFoundException;
import com.hallyugo.hallyugo.common.exception.ExceptionCode;
import com.hallyugo.hallyugo.content.domain.Category;
import com.hallyugo.hallyugo.content.domain.Content;
import com.hallyugo.hallyugo.content.domain.ContentResponseDto;
import com.hallyugo.hallyugo.content.domain.response.ContentForMapResponseDto;
import com.hallyugo.hallyugo.content.domain.response.ContentResponseDto;
import com.hallyugo.hallyugo.content.repository.ContentRepository;
import com.hallyugo.hallyugo.location.domain.response.LocationWithImagesResponseDto;
import com.hallyugo.hallyugo.location.service.LocationService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
Expand All @@ -18,6 +24,7 @@ public class ContentService {
private static final int PAGE_NUMBER = 0;
private static final int INITIAL_CONTENTS_SIZE_PER_CATEGORY = 2;
private final ContentRepository contentRepository;
private final LocationService locationService;

public Map<String, List<ContentResponseDto>> getRandomContents() {
Map<String, List<ContentResponseDto>> result = new HashMap<>();
Expand Down Expand Up @@ -45,4 +52,34 @@ public List<ContentResponseDto> getContentsByKeyword(String keyword) {
List<Content> contents = contentRepository.findByTitleContainingIgnoreCase(keyword);
return contents.stream().map(ContentResponseDto::toDto).toList();
}

public ContentForMapResponseDto getContentWithLocationsAndImages(Long contentId) {
Content content = contentRepository.findById(contentId)
.orElseThrow(() -> new EntityNotFoundException(ExceptionCode.ENTITY_NOT_FOUND));

List<LocationWithImagesResponseDto> locationsWithImages =
locationService.getLocationsWithImagesByContentId(contentId);

return ContentForMapResponseDto.toDto(content, locationsWithImages);
}

public List<ContentForMapResponseDto> getContentsWithLocationsAndImagesByCategory(String category) {
List<Content> contents = contentRepository.findByCategory(Category.valueOf(category));

List<ContentForMapResponseDto> contentDtos = contents.stream()
.map(content -> getContentWithLocationsAndImages(content.getId())).collect(Collectors.toList());

return contentDtos;
}

public List<ContentForMapResponseDto> getContentsWithLocationsAndImagesByKeyword(String keyword) {
List<Content> contents = contentRepository.findByTitleContainingIgnoreCase(keyword);

return contents.stream().map(content -> {
List<LocationWithImagesResponseDto> locationsWithImages =
locationService.getLocationsWithImagesByContentId(content.getId());
return ContentForMapResponseDto.toDto(content, locationsWithImages);
}).toList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
import com.hallyugo.hallyugo.user.domain.User;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
Expand All @@ -23,4 +29,22 @@ public ResponseEntity<FavoriteResponseDto> getUserFavorite(
FavoriteResponseDto result = favoriteService.getFavoritesByUser(user, limit);
return ResponseEntity.ok(result);
}

@PostMapping("/favorite/on?location_id={locationId}")
public ResponseEntity<Void> increaseFavoriteCount(
@AuthUser User user,
@PathVariable Long locationId
) {
favoriteService.increaseFavoriteCountAndSave(user, locationId);
return ResponseEntity.ok().build();
}

@DeleteMapping("/favorite/off?location_id={locationId}")
public ResponseEntity<Void> decreaseFavoriteCount(
@AuthUser User user,
@PathVariable Long locationId
) {
favoriteService.decreaseFavoriteCountAndDelete(user, locationId);
return ResponseEntity.ok().build();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.hallyugo.hallyugo.favorite.domain.response;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.hallyugo.hallyugo.favorite.domain.EntityType;
import java.time.LocalDateTime;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Data
@NoArgsConstructor
public class FavoriteResponseItem {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.hallyugo.hallyugo.favorite.repository;

import com.hallyugo.hallyugo.favorite.domain.Favorite;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

public interface FavoriteRepository extends JpaRepository<Favorite, Long> {
List<Favorite> findByUserId(Long userId);

boolean existsByUserIdAndLocationId(Long userId, Long locationId);

void deleteByUserIdAndLocationId(Long userId, Long locationId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import com.hallyugo.hallyugo.location.domain.Location;
import com.hallyugo.hallyugo.location.repository.LocationRepository;
import com.hallyugo.hallyugo.user.domain.User;
import jakarta.transaction.Transactional;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class FavoriteService {
Expand Down Expand Up @@ -63,4 +63,41 @@ private FavoriteResponseItem createFavoriteResponseItem(Favorite favorite) {
}
return item;
}

@Transactional
public void increaseFavoriteCountAndSave(User user, Long locationId) {
// 전달된 locationId를 이용해 Location 객체 조회
Location location = locationRepository.findById(locationId)
.orElseThrow(() -> new EntityNotFoundException(ExceptionCode.ENTITY_NOT_FOUND));

if (!favoriteRepository.existsByUserIdAndLocationId(user.getId(), locationId)) {
// 해당 객체의 favoriteCount 1 증가
location.increaseFavoriteCount();

// favoriteCount가 갱신된 Location 객체 저장
locationRepository.save(location);

// Favorite 객체 생성 후 저장
Favorite favorite = new Favorite(user, location);
favoriteRepository.save(favorite);
}
}

@Transactional
public void decreaseFavoriteCountAndDelete(User user, Long locationId) {
// 전달된 locationId를 이용해 Location 객체 조회
Location location = locationRepository.findById(locationId)
.orElseThrow(() -> new EntityNotFoundException(ExceptionCode.ENTITY_NOT_FOUND));

if (favoriteRepository.existsByUserIdAndLocationId(user.getId(), locationId)) {
// 해당 객체의 favoriteCount 1 감소
location.decreaseFavoriteCount();

// favoriteCount가 갱신된 Location 객체 저장
locationRepository.save(location);

// Favorite 객체 삭제
favoriteRepository.deleteByUserIdAndLocationId(user.getId(), locationId);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.hallyugo.hallyugo.image.domain.response;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.hallyugo.hallyugo.image.domain.Image;
import java.time.LocalDateTime;
import java.util.Objects;

public class ImageResponseDto {

@JsonProperty
private Long id;

@JsonProperty("image_url")
private String imageUrl;

@JsonProperty
private String description;

@JsonProperty("created_at")
private LocalDateTime createdAt;

private ImageResponseDto(Image image) {
this.id = image.getId();
this.imageUrl = image.getImageUrl();
this.description = image.getDescription();
this.createdAt = image.getCreatedAt();
}

public static ImageResponseDto toDto(Image image) {
return new ImageResponseDto(image);
}

@Override
public int hashCode() {
return Objects.hash(id);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}

ImageResponseDto that = (ImageResponseDto) obj;

return Objects.equals(id, that.id);
}

}
Loading

0 comments on commit 604dadf

Please sign in to comment.