Skip to content

Commit

Permalink
Feat: 번개 조회 API 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
vividswan committed Nov 24, 2021
1 parent 6cbb9a6 commit 3b6d7cf
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package com.server.wupitch.impromptu;

import com.server.wupitch.club.dto.ClubIdRes;
import com.server.wupitch.club.dto.CreateClubReq;
import com.server.wupitch.club.dto.ClubListRes;
import com.server.wupitch.configure.response.DataResponse;
import com.server.wupitch.configure.response.ResponseService;
import com.server.wupitch.configure.security.authentication.CustomUserDetails;
import com.server.wupitch.impromptu.dto.CreateImpromptuReq;
import com.server.wupitch.impromptu.dto.ImpromptuIdRes;
import com.server.wupitch.impromptu.dto.ImpromptuListRes;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
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.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = {"Impromptu API"})
@RestController
Expand All @@ -37,4 +37,25 @@ public DataResponse<ImpromptuIdRes> createImpromptu(@RequestBody CreateImpromptu
return responseService.getDataResponse(new ImpromptuIdRes(impromptuId));
}

@Operation(summary = "번개 조회 API", description = "page, size, sortBy, isAsc, RequestParam 설정")
@GetMapping(value = "/impromptus")
public DataResponse<Page<ImpromptuListRes>> getAllImpromptuList(
@RequestParam(name = "page", required = false) Integer page,
@RequestParam(name = "size", required = false) Integer size,
@RequestParam(name = "sortBy", required = false) String sortBy,
@RequestParam(name = "isAsc", required = false) Boolean isAsc,
@RequestParam(name = "areaId", required = false) Long areaId,
@RequestParam(name = "scheduleIndex", required = false) Integer scheduleIndex,
@RequestParam(name = "days", required = false) List<Integer> days,
@RequestParam(name = "memberCountIndex", required = false) Integer memberCountIndex
) {
if (page == null) page = 1;
page = page - 1;
if (size == null) size = 10;
if (isAsc == null) isAsc = true;
if (sortBy == null) sortBy = "updatedAt";
Page<ImpromptuListRes> result = impromptuService.getAllImpromptuList(page, size, sortBy, isAsc, areaId, scheduleIndex, days, memberCountIndex);
return responseService.getDataResponse(result);
}

}
34 changes: 34 additions & 0 deletions src/main/java/com/server/wupitch/impromptu/ImpromptuService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,28 @@
import com.server.wupitch.account.entity.Account;
import com.server.wupitch.area.Area;
import com.server.wupitch.area.AreaRepository;
import com.server.wupitch.club.Club;
import com.server.wupitch.club.dto.ClubListRes;
import com.server.wupitch.configure.response.exception.CustomException;
import com.server.wupitch.configure.response.exception.CustomExceptionStatus;
import com.server.wupitch.configure.security.authentication.CustomUserDetails;
import com.server.wupitch.impromptu.dto.CreateImpromptuReq;
import com.server.wupitch.impromptu.dto.ImpromptuListRes;
import com.server.wupitch.impromptu.entity.Impromptu;
import com.server.wupitch.impromptu.repository.ImpromptuRepository;
import com.server.wupitch.impromptu.repository.ImpromptuRepositoryCustom;
import com.server.wupitch.sports.entity.Sports;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

import static com.server.wupitch.configure.entity.Status.VALID;

@Transactional(readOnly = true)
Expand All @@ -23,6 +36,7 @@ public class ImpromptuService {
private final AccountRepository accountRepository;
private final AreaRepository areaRepository;
private final ImpromptuRepository impromptuRepository;
private final ImpromptuRepositoryCustom impromptuRepositoryCustom;

@Transactional
public Long createImpromptu(CreateImpromptuReq dto, CustomUserDetails customUserDetails) {
Expand All @@ -40,4 +54,24 @@ public Long createImpromptu(CreateImpromptuReq dto, CustomUserDetails customUser
return save.getImpromptuId();

}

public Page<ImpromptuListRes> getAllImpromptuList
(Integer page, Integer size, String sortBy, Boolean isAsc, Long areaId, Integer scheduleIndex, List<Integer> days, Integer memberCountIndex)
{
Sort.Direction direction = isAsc ? Sort.Direction.ASC : Sort.Direction.DESC;
Sort sort = Sort.by(direction, sortBy);

Pageable pageable = PageRequest.of(page, size, sort);

Area area = null;
if(areaId != null){
Optional<Area> optionalArea = areaRepository.findByAreaIdAndStatus(areaId, VALID);
if(optionalArea.isPresent()) area = optionalArea.get();
}

Page<Impromptu> allImpromptu = impromptuRepositoryCustom.getAllImpromptuList(pageable, area,scheduleIndex ,days, memberCountIndex);

return allImpromptu.map(ImpromptuListRes::new);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.Setter;

import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.ChronoUnit;

@Getter
Expand Down Expand Up @@ -44,11 +45,10 @@ public class ImpromptuListRes {
public ImpromptuListRes(Impromptu impromptu) {
this.impromptuId = impromptu.getImpromptuId();
this.impromptuImage = impromptu.getImpromptuImage();
LocalDate validDateTime = impromptu.getDate();
LocalDate validDate = impromptu.getDate();
LocalDate now = LocalDate.now();
long diffTime = validDateTime.until(now, ChronoUnit.SECONDS);
diffTime = diffTime / 24;
this.dDay = (int)diffTime;
Period between = Period.between(now, validDate);
this.dDay = between.getDays();

this.title = impromptu.getTitle();
this.date = impromptu.getDate();
Expand All @@ -59,6 +59,13 @@ public ImpromptuListRes(Impromptu impromptu) {
this.introduction = impromptu.getIntroduction();
this.materials = impromptu.getMaterials();
this.inquiries = impromptu.getInquiries();
if(impromptu.getDayIdx() == 1) day = "월요일";
else if(impromptu.getDayIdx() == 2) day = "화요일";
else if(impromptu.getDayIdx() == 3) day = "수요일";
else if(impromptu.getDayIdx() == 4) day = "목요일";
else if(impromptu.getDayIdx() == 5) day = "금요일";
else if(impromptu.getDayIdx() == 6) day = "토요일";
else day = "일요일";


}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.server.wupitch.account.entity.Account;
import com.server.wupitch.area.Area;
import com.server.wupitch.configure.entity.BaseTimeEntity;
import com.server.wupitch.configure.entity.Status;
import com.server.wupitch.impromptu.dto.CreateImpromptuReq;
import lombok.AllArgsConstructor;
Expand All @@ -21,7 +22,7 @@
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class Impromptu {
public class Impromptu extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.server.wupitch.impromptu.repository;

import com.querydsl.core.QueryResults;
import com.querydsl.core.types.Order;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.core.types.dsl.SimplePath;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.server.wupitch.area.Area;
import com.server.wupitch.area.QArea;
import com.server.wupitch.impromptu.entity.Impromptu;
import com.server.wupitch.impromptu.entity.QImpromptu;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;

import static com.server.wupitch.configure.entity.Status.VALID;

@RequiredArgsConstructor
@Repository
public class ImpromptuQueryRepository implements ImpromptuRepositoryCustom{

private final JPAQueryFactory queryFactory;

private OrderSpecifier<?>[] getSortedColumn(Sort sorts) {
return sorts.toList().stream().map(x -> {
Order order = x.getDirection().name() == "ASC" ? Order.ASC : Order.DESC;
SimplePath<Object> filedPath = Expressions.path(Object.class, QImpromptu.impromptu, x.getProperty());
return new OrderSpecifier(order, filedPath);
}).toArray(OrderSpecifier[]::new);
}

@Override
public Page<Impromptu> getAllImpromptuList(Pageable pageable, Area area,
Integer scheduleIndex, List<Integer> days, Integer memberCountIndex) {
QImpromptu qImpromptu = QImpromptu.impromptu;
QArea qArea = QArea.area;

QueryResults<Impromptu> result = queryFactory
.select(qImpromptu)
.from(qImpromptu)
// .leftJoin(qArea).on(qImpromptu.area.eq(qArea).and(qArea.status.eq(VALID)))
.where(
afterBoolean(qImpromptu),
areaEq(qImpromptu, area),
scheduleIndexEq(qImpromptu, scheduleIndex),
daysEq(qImpromptu, days),
memberCountIndexEq(qImpromptu, memberCountIndex)
)
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.orderBy(getSortedColumn(pageable.getSort()))
.fetchResults();

return new PageImpl<>(result.getResults(), pageable, result.getTotal());
}

private BooleanExpression afterBoolean(QImpromptu qImpromptu) {
return qImpromptu.date.after(LocalDate.now());
}

private BooleanExpression areaEq(QImpromptu qImpromptu, Area area) {
if (area == null) return null;
return qImpromptu.area.eq(area);
}

private BooleanExpression daysEq(QImpromptu qImpromptu, List<Integer> days) {
if(days == null) return null;
return qImpromptu.dayIdx.in(days);
}

private BooleanExpression scheduleIndexEq(QImpromptu qImpromptu, Integer scheduleIndex) {
if(scheduleIndex == null) return null;
LocalDate now = LocalDate.now();
final int WEEK = 7;
return qImpromptu.date.before(now.plusDays(WEEK * scheduleIndex +1));
}

private BooleanExpression memberCountIndexEq(QImpromptu qImpromptu, Integer memberCountIndex) {
if(memberCountIndex == null) return null;
if(memberCountIndex == 1) return qImpromptu.recruitmentCount.loe(5);
else if(memberCountIndex == 2) return qImpromptu.recruitmentCount.loe(10).and(qImpromptu.recruitmentCount.gt(5));
else if(memberCountIndex == 3) return qImpromptu.recruitmentCount.loe(15).and(qImpromptu.recruitmentCount.gt(10));
else return qImpromptu.recruitmentCount.loe(20).and(qImpromptu.recruitmentCount.gt(15));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.server.wupitch.impromptu.repository;

import com.server.wupitch.area.Area;
import com.server.wupitch.impromptu.entity.Impromptu;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;

public interface ImpromptuRepositoryCustom {
Page<Impromptu> getAllImpromptuList(Pageable pageable, Area area, Integer scheduleIndex, List<Integer> days, Integer memberCountIndex);
}

0 comments on commit 3b6d7cf

Please sign in to comment.