-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
271 additions
and
6 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/main/java/com/umc/hackaton/snapspot/category/controller/CategoryController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("카테고리별 스팟 리스트 조회에 실패하였습니다."); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/umc/hackaton/snapspot/category/dto/CategoryRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/umc/hackaton/snapspot/category/entity/Category.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<>(); | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/umc/hackaton/snapspot/category/entity/CategorySpot.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/umc/hackaton/snapspot/category/repository/CategoryRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/umc/hackaton/snapspot/category/repository/CategorySpotRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/com/umc/hackaton/snapspot/category/service/CategoryService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
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.dto.SpotDto; | ||
import com.umc.hackaton.snapspot.spot.entity.Spot; | ||
import com.umc.hackaton.snapspot.user.entity.User; | ||
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); | ||
} | ||
// if (category != null) { | ||
// List<CategorySpot> categorySpots = categorySpotRepository.findAllByCategory(category); | ||
// for (CategorySpot categorySpot : categorySpots) { | ||
// SpotDto spotDto = new SpotDto(); | ||
// User user = categorySpot.getSpot().getUser(); | ||
// spotDto.setUser(user); | ||
// spotDto.setLatitude(categorySpot.getSpot().getLatitude()); | ||
// spotDto.setLongitude(categorySpot.getSpot().getLongitude()); | ||
// spotDto.setTitle(categorySpot.getSpot().getTitle()); | ||
// spotDto.setDescription(categorySpot.getSpot().getDescription()); | ||
// spotDto.setImgUrl(categorySpot.getSpot().getImgUrl()); | ||
// | ||
// Spot spot = spotDto.toEntity(); | ||
// spots.add(spot); | ||
// } | ||
// } | ||
} | ||
return new ArrayList<>(spots); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters