Skip to content

Commit

Permalink
feat: 스팟 상세조회
Browse files Browse the repository at this point in the history
  • Loading branch information
do-dop committed Jul 4, 2024
2 parents b61d555 + 00cddd8 commit 76a87ab
Show file tree
Hide file tree
Showing 40 changed files with 1,002 additions and 13 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ dependencies {

tasks.named('test') {
useJUnitPlatform()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.umc.hackaton.snapspot.category.controller;

import com.umc.hackaton.snapspot.category.dto.CategoryRequestDto;
import com.umc.hackaton.snapspot.category.entity.Category;
import com.umc.hackaton.snapspot.category.service.CategoryService;
import com.umc.hackaton.snapspot.savespot.dto.UserFolderRequestDto;
import com.umc.hackaton.snapspot.spot.entity.Spot;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/api/v1/categories")
public class CategoryController {
private final CategoryService categoryService;

@PostMapping("/")
public ResponseEntity<?> createCategory(@RequestBody CategoryRequestDto category) {
try {
categoryService.save(category);
return ResponseEntity.ok().body(category);
} catch (Exception e){
log.info("카테고리 생성에 실패하였습니다.", e);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("카테고리 생성에 실패하였습니다.");
}
}


@GetMapping("/")
public ResponseEntity<?> showAllCategories() {
try {
List<Category> categories = categoryService.showAll();
return ResponseEntity.ok().body(categories);
} catch (Exception e){
log.info("카테고리 리스트 조회에 실패하였습니다.", e);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("카테고리 리스트 조회에 실패하였습니다.");
}
}

@GetMapping("/spots")
public ResponseEntity<?> showSpotsByCategoryId(
@RequestParam List<Long> categoryIds
) {
try {
List<Spot> spots = categoryService.showSpotsByCategoryIds(categoryIds);
return ResponseEntity.ok().body(spots);
} catch (Exception e){
log.info("카테고리별 스팟 리스트 조회에 실패하였습니다.", e);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("카테고리별 스팟 리스트 조회에 실패하였습니다.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.umc.hackaton.snapspot.category.dto;

import com.umc.hackaton.snapspot.category.entity.Category;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Getter;

@Getter
public class CategoryRequestDto {
@NotNull private String categoryName;

@Builder
public Category toEntity() {
return Category.builder()
.categoryName(categoryName)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.umc.hackaton.snapspot.category.entity;

import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.umc.hackaton.snapspot.config.entity.BaseEntity;
import jakarta.persistence.*;
import lombok.*;

import java.util.LinkedHashSet;
import java.util.Set;

@Entity
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Category extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;

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

@ToString.Exclude
@JsonManagedReference
@OrderBy("id")
@OneToMany(mappedBy = "category")
private final Set<CategorySpot> categorySpots = new LinkedHashSet<>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.umc.hackaton.snapspot.category.entity;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.umc.hackaton.snapspot.config.entity.BaseEntity;
import com.umc.hackaton.snapspot.spot.entity.Spot;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class CategorySpot extends BaseEntity{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "spot_id")
@JsonBackReference
private Spot spot;

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id")
@JsonBackReference
private Category category;


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.umc.hackaton.snapspot.category.repository;

import com.umc.hackaton.snapspot.category.entity.Category;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface CategoryRepository extends JpaRepository<Category, Long> {
@Query("SELECT c.id, c.categoryName FROM Category c")
List<Object[]> findAllCategories();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.umc.hackaton.snapspot.category.repository;

import com.umc.hackaton.snapspot.category.entity.Category;
import com.umc.hackaton.snapspot.category.entity.CategorySpot;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface CategorySpotRepository extends JpaRepository<CategorySpot, Long> {
List<CategorySpot> findAllByCategory(Category category);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.umc.hackaton.snapspot.category.service;

import com.umc.hackaton.snapspot.category.dto.CategoryRequestDto;
import com.umc.hackaton.snapspot.category.entity.Category;
import com.umc.hackaton.snapspot.category.entity.CategorySpot;
import com.umc.hackaton.snapspot.category.repository.CategoryRepository;
import com.umc.hackaton.snapspot.category.repository.CategorySpotRepository;
import com.umc.hackaton.snapspot.spot.entity.Spot;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class CategoryService {
private final CategoryRepository categoryRepository;
private final CategorySpotRepository categorySpotRepository;


public void save(CategoryRequestDto category) {
categoryRepository.save(category.toEntity());
}

public List<Category> showAll() {
List<Object[]> objects = categoryRepository.findAllCategories();
return objects.stream()
.map(object -> new Category((Long) object[0], (String) object[1]))
.collect(Collectors.toList());
}

public List<Spot> showSpotsByCategoryIds(List<Long> categoryIds) {
Set<Spot> spots = new HashSet<>();
for (Long categoryId : categoryIds) {
Category category = categoryRepository.findById(categoryId).orElse(null);
if (category != null) {
List<CategorySpot> categorySpots = categorySpotRepository.findAllByCategory(category);
List<Spot> categorySpotsMapped = categorySpots.stream()
.map(CategorySpot::getSpot)
.collect(Collectors.toList());
spots.addAll(categorySpotsMapped);
}
}
return new ArrayList<>(spots);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.umc.hackaton.snapspot.like.controller;

import com.umc.hackaton.snapspot.like.dto.LikeDto;
import com.umc.hackaton.snapspot.like.service.LikeService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/spots/like")
@RequiredArgsConstructor
public class LikeController {

private final LikeService likeService;

@PostMapping("")
public ResponseEntity<LikeDto.ResponseDto> registerLike(@RequestBody LikeDto.RequestDto likeDto){
return ResponseEntity.ok(likeService.registerLike(likeDto));
}


@DeleteMapping("")
public ResponseEntity<LikeDto.ResponseDto> deleteLike(@RequestBody LikeDto.RequestDto likeDto){
return ResponseEntity.ok(likeService.deleteLike(likeDto));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.umc.hackaton.snapspot.like.converter;

import com.umc.hackaton.snapspot.like.entity.Like;
import com.umc.hackaton.snapspot.spot.entity.Spot;
import com.umc.hackaton.snapspot.user.entity.User;

public class LikeConverter {
public static Like toEntity(User user, Spot spot){
return Like.builder()
.spot(spot)
.user(user)
.build();
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/umc/hackaton/snapspot/like/dto/LikeDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.umc.hackaton.snapspot.like.dto;

import lombok.Builder;
import lombok.Getter;

public class LikeDto {
@Builder
@Getter
public static class RequestDto{
Long userId;
Long spotId;

}

@Builder
@Getter
public static class ResponseDto{
String message;

}
}
34 changes: 34 additions & 0 deletions src/main/java/com/umc/hackaton/snapspot/like/entity/Like.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.umc.hackaton.snapspot.like.entity;

import com.umc.hackaton.snapspot.config.entity.BaseEntity;
import com.umc.hackaton.snapspot.spot.entity.Spot;
import com.umc.hackaton.snapspot.user.entity.User;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;

@Entity
@Table(name = "`like`")
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Like extends BaseEntity {


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


@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
private User user;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "spot_id", nullable = false)
private Spot spot;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.umc.hackaton.snapspot.like.repository;

import com.umc.hackaton.snapspot.like.entity.Like;
import com.umc.hackaton.snapspot.spot.entity.Spot;
import com.umc.hackaton.snapspot.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface LikeRepository extends JpaRepository<Like, Long> {

Like findByUserAndSpot(User user, Spot spot);
}
Loading

0 comments on commit 76a87ab

Please sign in to comment.