Skip to content

Commit

Permalink
Merge pull request #47 from aelimited/feature/#30-clubregister
Browse files Browse the repository at this point in the history
소모임 crud
  • Loading branch information
c0smosaur authored May 29, 2024
2 parents db6a10b + 081936b commit faaef34
Show file tree
Hide file tree
Showing 21 changed files with 344 additions and 149 deletions.
76 changes: 62 additions & 14 deletions src/main/java/com/core/linkup/club/controller/ClubController.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,82 @@
package com.core.linkup.club.controller;

import com.core.linkup.club.request.ClubRequest;
import com.core.linkup.club.response.ClubsResponse;
import com.core.linkup.club.requset.ClubCreateRequest;
import com.core.linkup.club.requset.ClubSearchRequest;
import com.core.linkup.club.requset.ClubUpdateRequest;
import com.core.linkup.club.response.ClubSearchResponse;
import com.core.linkup.club.service.ClubService;
import com.core.linkup.common.response.BaseResponse;
import com.core.linkup.security.MemberDetails;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/club")
public class ClubController {

private final ClubService clubService;

//소모임 개별조회
@GetMapping("/{clubId}")
public BaseResponse<ClubSearchResponse> findClub(
@AuthenticationPrincipal MemberDetails member,
@PathVariable Long clubId
) {
ClubSearchResponse response = clubService.findClub(clubId);
return BaseResponse.response(response);
}

//소모임 필터링으로 조회 (ClubType으로 조회 가능)
//TODO : OfficeBuilding으로 조회 가능 하도록 할 예정
@GetMapping("/search")
public BaseResponse<Page<ClubSearchResponse>> findClubs(
@PageableDefault(sort = "id", direction = Sort.Direction.ASC) Pageable pageable,
@ModelAttribute ClubSearchRequest request
) {
Page<ClubSearchResponse> searchResponse = clubService.findClubs(pageable, request);
return BaseResponse.response(searchResponse);
}

//소모임 등록
@PostMapping
public BaseResponse<List<ClubsResponse>> clubRegister(
public BaseResponse<ClubSearchResponse> create(
@AuthenticationPrincipal MemberDetails member,
@RequestBody ClubCreateRequest request
) {
ClubSearchResponse response = clubService.createClub(member, request);
return BaseResponse.response(response);
}

//소모임 수정
@PutMapping("/{club_id}")
public BaseResponse<ClubSearchResponse> update(
@AuthenticationPrincipal MemberDetails member,
@PathVariable("club_id") Long clubId,
@RequestBody ClubUpdateRequest updateRequest
) {
ClubSearchResponse updatedClub = clubService.updateClub(member, clubId, updateRequest);
return BaseResponse.response(updatedClub);
}

//소모임 삭제
@DeleteMapping("/{club_id}")
public BaseResponse<String> delete(
@AuthenticationPrincipal MemberDetails member,
@Valid @RequestBody ClubRequest clubRequest
@PathVariable("club_id") Long clubId
) {
List<ClubsResponse> clubsResponses = clubService.clubRegister(member, clubRequest);
return BaseResponse.response(clubsResponses);
clubService.delete(member, clubId);
return BaseResponse.response("OK");
}

// @GetMapping("/member/{memberId}")
// public ResponseEntity<List<ClubSearchResponse28>> getClubsByMemberId(@PathVariable Long memberId) {
// List<ClubSearchResponse28> response = clubService.getClubsByMemberId(memberId);
// return ResponseEntity.ok(response);
// }
}
66 changes: 66 additions & 0 deletions src/main/java/com/core/linkup/club/converter/ClubConverter.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,74 @@
package com.core.linkup.club.converter;

import com.core.linkup.club.entity.Club;
import com.core.linkup.club.requset.ClubCreateRequest;
import com.core.linkup.club.requset.ClubUpdateRequest;
import com.core.linkup.club.response.ClubSearchResponse;
import com.core.linkup.common.annotation.Converter;
import com.core.linkup.common.entity.enums.ClubType;
import com.core.linkup.common.exception.BaseException;
import com.core.linkup.common.response.BaseResponseStatus;
import com.core.linkup.member.entity.Member;

@Converter
public class ClubConverter {

public ClubSearchResponse toClubResponse(Club club) {
return ClubSearchResponse.builder()
.id(club.getId())
.title(club.getTitle())
.introduction(club.getIntroduction())
.clubType(club.getCategory())
.recruitCount(club.getRecruitCount())
.build();
}


public Club toClubEntity(ClubCreateRequest request, Long memberId) {
ClubType category;
try {
category = ClubType.fromKor(String.valueOf(request.clubType()));
} catch (IllegalArgumentException e) {
throw new BaseException(BaseResponseStatus.INVALID_REQUEST);
}
Club club = Club.builder()
.category(String.valueOf(category))
.clubAccessibility(request.clubAccessibility())
.title(request.title())
.introduction(request.introduction())
.recruitCount(request.recruitCount())
.detailedIntroduction(request.detailedIntroduction())
.applicationIntroduction(request.applicationIntroduction())
.clubThumbnail(request.clubThumbnail())
.member(
Member.builder().id(memberId).build())
.build();

//TODO : list로 question 받는거 해야 함, clubid르 null로 받아옴
// Optional.ofNullable(request.clubQuestions()).orElse(List.of()).forEach(q -> {
// ClubQuestion clubQuestion = ClubQuestion.builder()
// .question(q.getQuestion())
// .qorders(q.getQorders())
// .build();
// clubQuestion.setClub(club);
// club.getClubQuestions().add(clubQuestion);
// });

return club;
}

public Club updateClubEntity(Club updateClub, ClubUpdateRequest updateRequest) {
ClubType category = ClubType.fromKor(String.valueOf(updateRequest.clubType()));
return Club.builder()
.id(updateClub.getId())
.category(String.valueOf(category))
.title(updateRequest.title())
.introduction(updateRequest.introduction())
.recruitCount(updateRequest.recruitCount())
.detailedIntroduction(updateRequest.detailedIntroduction())
.applicationIntroduction(updateRequest.applicationIntroduction())
.clubThumbnail(updateRequest.clubThumbnail())
.build();
}

}
26 changes: 15 additions & 11 deletions src/main/java/com/core/linkup/club/entity/Club.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,27 @@
import com.core.linkup.common.entity.BaseEntity;
import com.core.linkup.member.entity.Member;
import com.core.linkup.office.entity.OfficeBuilding;
import jakarta.persistence.Entity;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import jakarta.persistence.*;
import lombok.*;
import lombok.experimental.SuperBuilder;

import java.util.ArrayList;
import java.util.List;

@Entity(name = "club")
@NoArgsConstructor
@AllArgsConstructor
@Getter
@SuperBuilder
public class Club extends BaseEntity {

@ManyToOne
@JoinColumn(name = "office_building_id")
private OfficeBuilding locationId;

// @ManyToOne
// @JoinColumn(name = "office_building_id")
// private OfficeBuilding officeBuilding;
//
@ManyToOne
@JoinColumn(name = "member_id")
private Member memberId;
private Member member;

private Boolean clubAccessibility;
private String category;
Expand All @@ -33,4 +34,7 @@ public class Club extends BaseEntity {
private String clubThumbnail;
private String applicationIntroduction;

@OneToMany
private List<ClubQuestion> clubQuestions = new ArrayList<>();

}
12 changes: 8 additions & 4 deletions src/main/java/com/core/linkup/club/entity/ClubQuestion.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@
import com.core.linkup.common.entity.BaseEntity;
import jakarta.persistence.Entity;
import jakarta.persistence.ManyToOne;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.*;
import lombok.experimental.SuperBuilder;

@Entity(name = "club_question")
@NoArgsConstructor
@AllArgsConstructor
@Getter
@SuperBuilder
public class ClubQuestion extends BaseEntity {

@Setter
@ManyToOne
private Club clubId;
private Club club;

private String question;
private Integer order;
private Integer qorders;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.core.linkup.club.repository;

import com.core.linkup.club.entity.Club;
import com.core.linkup.club.requset.ClubSearchRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;

public interface ClubCustomRepository {

Page<Club> findSearchClubs(ClubSearchRequest request, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.core.linkup.club.repository;

import com.core.linkup.club.entity.Club;
import com.core.linkup.club.entity.QClub;
import com.core.linkup.club.requset.ClubSearchRequest;
import com.core.linkup.common.entity.enums.ClubType;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@RequiredArgsConstructor
public class ClubCustomRepositoryImpl implements ClubCustomRepository {

private final JPAQueryFactory queryFactory;

@Override
public Page<Club> findSearchClubs(ClubSearchRequest request, Pageable pageable) {
QClub club = QClub.club;

BooleanBuilder booleanBuilder = new BooleanBuilder();

// if (request.getOfficeBuildingId() != null) {
// booleanBuilder.and(club.officeBuilding.id.eq(request.getOfficeBuildingId()));
// }
if (request.getClubType() != null && !request.getClubType().isEmpty()) {
ClubType clubType = ClubType.fromKor(request.getClubType());
booleanBuilder.and(club.category.eq(String.valueOf(clubType)));
}

List<Club> clubs = queryFactory.selectFrom(club)
.where(booleanBuilder)
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();

long total = queryFactory.selectFrom(club)
.where(booleanBuilder)
.fetchCount();

return new PageImpl<>(clubs, pageable, total);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
import org.springframework.stereotype.Repository;

@Repository
public interface ClubRepository extends JpaRepository<Club, Long> {
public interface ClubRepository extends JpaRepository<Club, Long>, ClubCustomRepository {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.core.linkup.club.request;

import lombok.Builder;

@Builder
public record ClubQuestionRequest(
String question,
Integer orders
) {
}
20 changes: 0 additions & 20 deletions src/main/java/com/core/linkup/club/request/ClubRequest.java

This file was deleted.

19 changes: 19 additions & 0 deletions src/main/java/com/core/linkup/club/requset/ClubCreateRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.core.linkup.club.requset;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Builder;

@Builder
public record ClubCreateRequest(
String title,
String introduction,
@JsonProperty("category")
String clubType,
Integer recruitCount,
Boolean clubAccessibility,
String detailedIntroduction,
String clubThumbnail,
String applicationIntroduction
// List<ClubQuestion> clubQuestions
) {
}
14 changes: 14 additions & 0 deletions src/main/java/com/core/linkup/club/requset/ClubSearchRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.core.linkup.club.requset;

import lombok.*;

@Getter
@RequiredArgsConstructor
@AllArgsConstructor
@Setter
public class ClubSearchRequest {

private Long officeBuildingId;
private String clubType;

}
Loading

0 comments on commit faaef34

Please sign in to comment.