-
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.
- Loading branch information
Showing
40 changed files
with
1,002 additions
and
13 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -39,4 +39,4 @@ dependencies { | |
|
||
tasks.named('test') { | ||
useJUnitPlatform() | ||
} | ||
} |
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); | ||
} |
47 changes: 47 additions & 0 deletions
47
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,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); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/umc/hackaton/snapspot/like/controller/LikeController.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,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)); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/umc/hackaton/snapspot/like/converter/LikeConverter.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,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
21
src/main/java/com/umc/hackaton/snapspot/like/dto/LikeDto.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,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
34
src/main/java/com/umc/hackaton/snapspot/like/entity/Like.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,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; | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/umc/hackaton/snapspot/like/repository/LikeRepository.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,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); | ||
} |
Oops, something went wrong.