Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/hive 하이브 카테고리 별 조회기능 구현 #100

Merged
merged 3 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.HHive.hhive.domain.category.data;

import java.util.Arrays;

public enum MajorCategory {

GAME("게임"),
Expand All @@ -14,4 +16,8 @@ public enum MajorCategory {
public String getTitle() {
return title;
}

public static MajorCategory findByStringName(String categoryName) {
return Arrays.stream(MajorCategory.values()).filter(majorCategory -> majorCategory.name().equals(categoryName)).findFirst().orElse(null);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.HHive.hhive.domain.category.data;

import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import java.util.Arrays;

public enum SubCategory {

//GAME
Expand All @@ -11,6 +15,7 @@ public enum SubCategory {
SOCCER(MajorCategory.SPORTS, "축구"),
BASEBALL(MajorCategory.SPORTS, "야구");

@Enumerated(EnumType.STRING)
private final MajorCategory majorCategory;
private final String title;

Expand All @@ -26,4 +31,8 @@ public MajorCategory getMajorCategory() {
public String getTitle() {
return title;
}

public static SubCategory findByStringName(String categoryName) {
return Arrays.stream(SubCategory.values()).filter(subCategory -> subCategory.name().equals(categoryName)).findFirst().orElse(null);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package com.HHive.hhive.domain.hive.controller;

import com.HHive.hhive.domain.category.data.MajorCategory;
import com.HHive.hhive.domain.category.data.SubCategory;
import com.HHive.hhive.domain.hive.dto.CreateHiveRequestDTO;
import com.HHive.hhive.domain.hive.dto.HiveResponseDTO;
import com.HHive.hhive.domain.hive.dto.UpdateHiveRequestDTO;
import com.HHive.hhive.domain.hive.service.HiveService;
import com.HHive.hhive.domain.relationship.hiveuser.dto.HiveUserInviteRequestDTO;
import com.HHive.hhive.domain.relationship.hiveuser.service.HiveUserService;
import com.HHive.hhive.domain.user.UserDetailsImpl;
import com.HHive.hhive.domain.user.dto.UserInfoResponseDTO;
import com.HHive.hhive.global.common.CommonResponse;
import jakarta.validation.Valid;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.DeleteMapping;
Expand All @@ -32,92 +34,105 @@
public class HiveController {

private final HiveService hiveService;
private final HiveUserService hiveUserService;

@PostMapping
public ResponseEntity<CommonResponse> createHive(
public ResponseEntity<CommonResponse<HiveResponseDTO>> createHive(
@AuthenticationPrincipal UserDetailsImpl userDetails,
@RequestBody @Valid CreateHiveRequestDTO createHiveRequestDTO) {
HiveResponseDTO response = hiveService.createHive(userDetails.getUser(),
createHiveRequestDTO);
return ResponseEntity.ok()
.body(new CommonResponse<>(200, "하이브가 작성되었습니다.", response));
.body(CommonResponse.of(HttpStatus.OK.value(), "하이브가 작성되었습니다.", response));
}

@PatchMapping("{hive_id}/update")
public ResponseEntity<CommonResponse> updateHive(
public ResponseEntity<CommonResponse<HiveResponseDTO>> updateHive(
@AuthenticationPrincipal UserDetailsImpl userDetails, @PathVariable Long hive_id,
@RequestBody @Valid UpdateHiveRequestDTO updateRequest) {
HiveResponseDTO response = hiveService.updateHive(userDetails.getUser(), hive_id,
updateRequest);
return ResponseEntity.ok()
.body(new CommonResponse<>(200, "하이브가 수정되었습니다.", response));
.body(CommonResponse.of(HttpStatus.OK.value(), "하이브가 수정되었습니다.", response));
}

@GetMapping("/{hive_id}")
public ResponseEntity<CommonResponse> getHive(
public ResponseEntity<CommonResponse<HiveResponseDTO>> getHive(
@AuthenticationPrincipal UserDetailsImpl userDetails,
@PathVariable Long hive_id) {
HiveResponseDTO response = hiveService.getHive(userDetails.getUser(), hive_id);
return ResponseEntity.ok()
.body(new CommonResponse<>(200, "하이브가 조회되었습니다.", response));
.body(CommonResponse.of(HttpStatus.OK.value(), "하이브가 조회되었습니다.", response));
}

@GetMapping("")
public ResponseEntity<CommonResponse> gethives() {
public ResponseEntity<CommonResponse<List<HiveResponseDTO>>> gethives() {
List<HiveResponseDTO> responses = hiveService.getHives();
return ResponseEntity.ok()
.body(new CommonResponse<>(200, "하이브들이 조회되었습니다.", responses));
.body(CommonResponse.of(HttpStatus.OK.value(), "하이브들이 조회되었습니다.", responses));
}

@PatchMapping("{hive_id}")
public ResponseEntity<CommonResponse> deleteHive(
@GetMapping("/search")
public ResponseEntity<CommonResponse<List<HiveResponseDTO>>> getHivesByCategory(
@RequestParam(required = false) String majorCategory,
@RequestParam(required = false) String subCategory) {
List<HiveResponseDTO> responses = hiveService.getHivesByCategory(
MajorCategory.findByStringName(majorCategory),
SubCategory.findByStringName(subCategory));
return ResponseEntity.ok()
.body(CommonResponse.of(HttpStatus.OK.value(), "하이브들이 조회되었습니다.", responses));
}


@DeleteMapping("{hive_id}")
public ResponseEntity<CommonResponse<String>> deleteHive(
@AuthenticationPrincipal UserDetailsImpl userDetails,
@PathVariable Long hive_id) {
hiveService.deleteHive(userDetails.getUser(), hive_id);
return ResponseEntity.ok()
.body(new CommonResponse<>(200, "하이브가 삭제되었습니다.", null));
.body(CommonResponse.of(HttpStatus.OK.value(), "하이브가 삭제되었습니다.", null));
}

@PostMapping("/{hive_id}")
public ResponseEntity<CommonResponse> inviteNewUser(
public ResponseEntity<CommonResponse<String>> inviteNewUser(
@AuthenticationPrincipal UserDetailsImpl userDetails,
@PathVariable Long hive_id,
@RequestBody @Valid HiveUserInviteRequestDTO requestDTO) {
hiveService.inviteNewUser(userDetails.getUser(), hive_id, requestDTO);
return ResponseEntity.ok()
.body(new CommonResponse<>(200, "하이브에 참여하였습니다.", null));
.body(CommonResponse.of(HttpStatus.OK.value(), "하이브에 참여하였습니다.", null));
}

@GetMapping("/{hive_id}/hiveUsers")
public ResponseEntity<CommonResponse> getHiveUsersInHive(
public ResponseEntity<CommonResponse<List<UserInfoResponseDTO>>> getHiveUsersInHive(
@AuthenticationPrincipal UserDetailsImpl userDetails,
@PathVariable Long hive_id) {
List<UserInfoResponseDTO> hiveUserResponses = hiveService.searchUsersInHive(
userDetails.getUser(), hive_id);
return ResponseEntity.ok()
.body(new CommonResponse<>(200, "하이브 유저들이 조회되었습니다.", hiveUserResponses));
.body(CommonResponse.of(HttpStatus.OK.value(), "하이브 유저들이 조회되었습니다.",
hiveUserResponses));
}

@GetMapping("/{hive_id}/hiveUsers/search")
public ResponseEntity<CommonResponse> getHiveUserInHive(
public ResponseEntity<CommonResponse<UserInfoResponseDTO>> getHiveUserInHive(
@AuthenticationPrincipal UserDetailsImpl userDetails,
@PathVariable Long hive_id,
@RequestParam String username) {
UserInfoResponseDTO hiveUserResponse = hiveService.searchUserInHive(userDetails.getUser(),
hive_id, username);
return ResponseEntity.ok()
.body(new CommonResponse<>(200, "하이브 유저가 조회되었습니다.", hiveUserResponse));
.body(CommonResponse.of(HttpStatus.OK.value(), "하이브 유저가 조회되었습니다.",
hiveUserResponse));
}

@DeleteMapping("{hive_id}/hiveUsers")
public ResponseEntity<CommonResponse> deleteHiveUser(
public ResponseEntity<CommonResponse<String>> deleteHiveUser(
@AuthenticationPrincipal UserDetailsImpl userDetails,
@PathVariable Long hive_id,
@RequestParam String username) {
hiveService.deleteHiveUser(userDetails.getUser(), hive_id, username);
return ResponseEntity.ok()
.body(new CommonResponse<>(200, "하이브 유저가 탈퇴되었습니다.", null));
.body(CommonResponse.of(HttpStatus.OK.value(), "하이브 유저가 탈퇴되었습니다.", null));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.HHive.hhive.domain.hive.dto;

import com.HHive.hhive.domain.category.data.MajorCategory;
import com.HHive.hhive.domain.category.data.SubCategory;
import com.HHive.hhive.domain.hive.entity.Hive;
import com.HHive.hhive.domain.user.entity.User;
import jakarta.validation.constraints.NotBlank;
Expand All @@ -17,9 +19,15 @@ public class CreateHiveRequestDTO {
@NotBlank
private String title;

private MajorCategory majorCategory;

private SubCategory subCategory;

public Hive toEntity(User createdBy) {
return Hive.builder()
.title(title)
.majorCategory(majorCategory)
.subCategory(subCategory)
.creatorId(createdBy.getId())
.introduction("내용을 넣어주세요")
.user(createdBy)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.HHive.hhive.domain.hive.dto;

import com.HHive.hhive.domain.hive.entity.Hive;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.time.LocalDateTime;
import java.util.List;
import lombok.AllArgsConstructor;
Expand All @@ -18,6 +19,10 @@ public class HiveResponseDTO {

private String title;

private String majorCategory;

private String subCategory;

private String introduction;

private Long hostId;
Expand All @@ -26,14 +31,22 @@ public class HiveResponseDTO {

private List<String> hivePlayers;

@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
private LocalDateTime createdAt;

@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
private LocalDateTime modifiedAt;

public static HiveResponseDTO of(Hive hive) {
String majorName =
hive.getMajorCategory() == null ? "" : hive.getMajorCategory().getTitle();

String subName = hive.getSubCategory() == null ? "" : hive.getSubCategory().getTitle();

return HiveResponseDTO.builder()
.id(hive.getId())
.title(hive.getTitle())
.majorCategory(majorName)
.subCategory(subName)
.introduction(hive.getIntroduction())
.hostId(hive.getId())
.hostName(hive.getUser().getEmail())
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/HHive/hhive/domain/hive/entity/Hive.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.HHive.hhive.domain.hive.entity;


import com.HHive.hhive.domain.category.data.MajorCategory;
import com.HHive.hhive.domain.category.data.SubCategory;
import com.HHive.hhive.domain.hive.dto.UpdateHiveRequestDTO;
import com.HHive.hhive.domain.user.entity.User;
import com.HHive.hhive.global.auditing.BaseTimeEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
Expand Down Expand Up @@ -35,6 +39,14 @@ public class Hive extends BaseTimeEntity {
@Column(nullable = false, unique = true)
private String title;

@Column
@Enumerated(EnumType.STRING)
private MajorCategory majorCategory;

@Column
@Enumerated(EnumType.STRING)
private SubCategory subCategory;

@Column
private String introduction;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.HHive.hhive.domain.hive.repository;

import com.HHive.hhive.domain.category.data.MajorCategory;
import com.HHive.hhive.domain.category.data.SubCategory;
import com.HHive.hhive.domain.hive.entity.Hive;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
Expand All @@ -17,4 +20,10 @@ public interface HiveRepository extends JpaRepository<Hive, Long> {
@Query("SELECT h FROM Hive h WHERE h.isDeleted = false")
List<Hive> findAllHiveNotDeleted();

@Query("SELECT h FROM Hive h WHERE (:majorCategory is null OR h.majorCategory = :majorCategory) "
+ "AND (:subCategory is null OR h.subCategory = :subCategory) AND (:majorCategory is not null OR :subCategory is not null)")
List<Hive> findAllByMajorCategoryAndSubCategoryContaining(
@Param("majorCategory") MajorCategory majorCategory,
@Param("subCategory")SubCategory subCategory
);
}
15 changes: 15 additions & 0 deletions src/main/java/com/HHive/hhive/domain/hive/service/HiveService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.HHive.hhive.domain.hive.service;

import com.HHive.hhive.domain.category.data.MajorCategory;
import com.HHive.hhive.domain.category.data.SubCategory;
import com.HHive.hhive.domain.hive.dto.CreateHiveRequestDTO;
import com.HHive.hhive.domain.hive.dto.HiveResponseDTO;
import com.HHive.hhive.domain.hive.dto.UpdateHiveRequestDTO;
Expand Down Expand Up @@ -65,6 +67,18 @@ public List<HiveResponseDTO> getHives() {
return hives.stream().map(HiveResponseDTO::of).toList();
}

public List<HiveResponseDTO> getHivesByCategory(MajorCategory majorCategory,
SubCategory subCategory) {
List<Hive> hives = hiveRepository.findAllByMajorCategoryAndSubCategoryContaining(
majorCategory, subCategory);

if (hives.isEmpty()) {
throw new NotFoundHiveException();
}

return hives.stream().map(HiveResponseDTO::of).toList();
}

@Transactional
public void deleteHive(User user, Long hiveId) {
Hive hive = getHiveAndCheckAuth(user, hiveId);
Expand Down Expand Up @@ -120,4 +134,5 @@ public Hive findHiveById(Long hiveId) {
NotFoundHiveException::new);
}


}
Loading