Skip to content

Commit

Permalink
Merge pull request #242 from Gongjakso/feat/apply
Browse files Browse the repository at this point in the history
feat: QA사항 반영
  • Loading branch information
yumzen authored Sep 23, 2024
2 parents 0173906 + 58e5ae6 commit 912ceb5
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public record ApplyReq(
@Schema(description = "포트폴리오 공개 설정", example = "false")
Boolean isPrivate,

@Size(max = 500)
@Size(max = 1000)
@Schema(description = "지원 이유", example = "저는 데이터 사이언스 과목을 수강하며 데이터에 관한 기본적인 내용들을 배우며 이런 데이터를 잘 활용하고, 이용하는 것이 중요한 역량이 될 것 같다고 판단했습니다. 그래서 관련된 역량을 쌓고자 공모전에 출품하고 싶다는 생각을 가지게 되었고, 공공데이터 공모전이 적합하다고 생각했습니다!")
String body,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public record ApplyRes(

Boolean isPrivate,

@Size(max = 500)
@Size(max = 1000)
String body,

@Size(max = 20)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.gongjakso.server.domain.apply.repository;

import com.gongjakso.server.domain.apply.entity.Apply;
import com.gongjakso.server.domain.apply.enumerate.ApplyStatus;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
Expand All @@ -9,7 +10,7 @@
public interface ApplyRepository extends JpaRepository<Apply, Long>, ApplyRepositoryCustom {
Boolean existsByMemberIdAndTeamIdAndDeletedAtIsNull(Long memberId, Long teamId);
Optional<Apply> findByIdAndDeletedAtIsNull(Long applyId);
Optional<Apply> findByTeamIdAndDeletedAtIsNull(Long teamId);
Optional<Apply> findByTeamIdAndMemberIdAndDeletedAtIsNull(Long teamId, Long memberId);
List<Apply> findAllByTeamIdAndDeletedAtIsNull(Long teamId);
int countByTeamIdAndStatusAndDeletedAtIsNull(Long teamId, ApplyStatus status);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gongjakso.server.domain.apply.service;

import com.gongjakso.server.domain.apply.enumerate.ApplyStatus;
import com.gongjakso.server.domain.member.entity.Member;
import com.gongjakso.server.domain.portfolio.entity.Portfolio;
import com.gongjakso.server.domain.portfolio.repository.PortfolioRepository;
Expand All @@ -16,19 +17,22 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.chrono.ChronoLocalDate;

import static java.time.LocalDateTime.now;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class ApplyService {

private final ApplyRepository applyRepository;
private final PortfolioRepository portfolioRepository;
private final TeamRepository teamRepository;

@Transactional
public ApplyRes apply(Member member, Long teamId, ApplyReq applyReq) {
//Validation: teamId, 지원 가능 기간인지, 리더가 지원하는지, 이미 지원했는지, 본인의 포트폴리오인지 유효성 검사
Team team = teamRepository.findTeamById(teamId)
Expand Down Expand Up @@ -73,6 +77,7 @@ public Page<ApplyRes> getMyApplies(Member member, Pageable pageable) {
return applyPage;
}

@Transactional
public ApplyRes getApply(Member member, Long applyId) {
//Validation: Apply가 유효하지 않거나, 리더가 아니거나, 자신이 아닌 경우 예외 처리
Apply apply = applyRepository.findApplyDetails(applyId)
Expand All @@ -92,6 +97,7 @@ public ApplyRes getApply(Member member, Long applyId) {
return ApplyRes.of(apply);
}

@Transactional
public ApplyRes selectApply(Member member, Long applyId, StatusReq req){
//Validation: Apply가 유효하지 않거나, 리더가 아닌 경우 예외 처리
Apply apply = applyRepository.findApplyDetails(applyId).orElseThrow(() -> new ApplicationException(ErrorCode.APPLY_NOT_FOUND_EXCEPTION));
Expand All @@ -103,6 +109,7 @@ public ApplyRes selectApply(Member member, Long applyId, StatusReq req){
//Business Logic
if (req != null) {
apply.select(req.status());
updatePassCount(apply.getTeam());
}

applyRepository.save(apply);
Expand All @@ -111,6 +118,7 @@ public ApplyRes selectApply(Member member, Long applyId, StatusReq req){
return ApplyRes.of(apply);
}

@Transactional
public void cancelApply(Member member, Long applyId) {
//Validation: Apply가 유효하지 않거나, 지원자 아닌 경우 예외 처리
Apply apply = applyRepository.findByIdAndDeletedAtIsNull(applyId)
Expand All @@ -122,5 +130,12 @@ public void cancelApply(Member member, Long applyId) {

//Business Logic
applyRepository.delete(apply);

updatePassCount(apply.getTeam());
}

public void updatePassCount(Team team) {
int passCount = applyRepository.countByTeamIdAndStatusAndDeletedAtIsNull(team.getId(), ApplyStatus.ACCEPTED);
team.updatePassCount(passCount);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
Expand Down Expand Up @@ -86,8 +88,10 @@ public ApplicationResponse<Void> deleteTeam(@AuthenticationPrincipal PrincipalDe
@GetMapping("/contest/{contest_id}/team/{team_id}")
public ApplicationResponse<TeamRes> getTeam(@AuthenticationPrincipal PrincipalDetails principalDetails,
@PathVariable(value = "contest_id") Long contestId,
@PathVariable(value = "team_id") Long teamId) {
return ApplicationResponse.ok(teamService.getTeam(principalDetails == null ? null : principalDetails.getMember(), contestId, teamId));
@PathVariable(value = "team_id") Long teamId,
HttpServletRequest request,
HttpServletResponse response) {
return ApplicationResponse.ok(teamService.getTeam(principalDetails == null ? null : principalDetails.getMember(), contestId, teamId, request, response));
}

@Operation(summary = "팀 리스트 조회 API",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public static RecruitPartRes of(RecruitPart recruitPart) {

}

public static TeamRes of(Team team, String teamRole, Apply apply) {
public static TeamResBuilder teamResBuilder(Team team){
List<RecruitPartRes> recruitPartRes = (team.getRecruitPart() != null) ? team.getRecruitPart().stream()
.map(RecruitPartRes::of)
.toList() : null;
Expand All @@ -127,7 +127,7 @@ public static TeamRes of(Team team, String teamRole, Apply apply) {
.contestId(team.getContest().getId())
.contestTitle(team.getContest().getTitle())
.title(team.getTitle())
.body(team.getTitle())
.body(team.getBody())
.status(team.getStatus().getDescription())
.totalCount(team.getTotalCount())
.passCount(team.getPassCount())
Expand All @@ -142,6 +142,17 @@ public static TeamRes of(Team team, String teamRole, Apply apply) {
.channelLink(team.getChannelLink())
.contestLink(team.getContest().getContestLink())
.scrapCount(team.getScrapCount())
.viewCount(team.getViewCount());
}

public static TeamRes of(Team team, String teamRole){
return teamResBuilder(team)
.teamRole(teamRole)
.build();
}

public static TeamRes of(Team team, String teamRole, Apply apply){
return teamResBuilder(team)
.teamRole(teamRole)
.applyStatus(apply != null ? apply.getStatus().getDescription() : null)
.applyId(apply != null ? apply.getId() : null)
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/gongjakso/server/domain/team/entity/Team.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public class Team extends BaseTimeEntity {
private int scrapCount;

@Column(name = "view_count", nullable = false, columnDefinition = "int")
private int viewCount;
private Integer viewCount;

public void update(TeamReq teamReq) {
this.title = (teamReq.title() != null) ? teamReq.title() : this.title;
Expand All @@ -113,6 +113,14 @@ public void updateScrapCount(int scrapCount) {
this.scrapCount = scrapCount;
}

public void updateViewCount(Team team){
this.viewCount = team.getViewCount() + 1;
}

public void updatePassCount(int passCount) {
this.passCount = passCount;
}

@Builder
public Team(Member member,
Contest contest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.gongjakso.server.domain.apply.dto.response.SimpleApplyRes;
import com.gongjakso.server.domain.apply.entity.Apply;
import com.gongjakso.server.domain.apply.enumerate.ApplyStatus;
import com.gongjakso.server.domain.apply.repository.ApplyRepository;
import com.gongjakso.server.domain.contest.entity.Contest;
import com.gongjakso.server.domain.contest.repository.ContestRepository;
Expand All @@ -17,6 +18,9 @@
import com.gongjakso.server.domain.team.repository.TeamRepository;
import com.gongjakso.server.global.exception.ApplicationException;
import com.gongjakso.server.global.exception.ErrorCode;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -47,7 +51,7 @@ public TeamRes createTeam(Member member, Long contestId, TeamReq teamReq) {
Team savedTeam = teamRepository.save(team);

// Response
return TeamRes.of(savedTeam, "LEADER", null);
return TeamRes.of(savedTeam, "LEADER");
}

@Transactional
Expand All @@ -69,7 +73,7 @@ public TeamRes updateTeam(Member member, Long contestId, Long teamId, TeamReq te
Team updatedTeam = teamRepository.save(team);

// Response
return TeamRes.of(updatedTeam, "LEADER", null);
return TeamRes.of(updatedTeam, "LEADER");
}

@Transactional
Expand All @@ -92,7 +96,7 @@ public TeamRes extendRecruit(Member member, Long contestId, Long teamId, LocalDa
Team updatedTeam = teamRepository.save(team);

// Response
return TeamRes.of(updatedTeam, "LEADER", null);
return TeamRes.of(updatedTeam, "LEADER");
}

@Transactional
Expand All @@ -114,7 +118,7 @@ public TeamRes closeRecruit(Member member, Long contestId, Long teamId) {
Team updatedTeam = teamRepository.save(team);

// Response
return TeamRes.of(updatedTeam, "LEADER", null);
return TeamRes.of(updatedTeam, "LEADER");
}

@Transactional
Expand All @@ -136,7 +140,7 @@ public TeamRes cancelRecruit(Member member, Long contestId, Long teamId) {
Team updatedTeam = teamRepository.save(team);

// Response
return TeamRes.of(updatedTeam, "LEADER", null);
return TeamRes.of(updatedTeam, "LEADER");
}

@Transactional
Expand All @@ -157,8 +161,8 @@ public void deleteTeam(Member member, Long contestId, Long teamId) {
teamRepository.delete(team);
}

// TODO: 조회수 관련 로직 도입 및 @Transactional 도입 필요
public TeamRes getTeam(Member member, Long contestId, Long teamId) {
@Transactional
public TeamRes getTeam(Member member, Long contestId, Long teamId, HttpServletRequest request, HttpServletResponse response) {
// Validation
contestRepository.findByIdAndDeletedAtIsNull(contestId)
.orElseThrow(() -> new ApplicationException(ErrorCode.CONTEST_NOT_FOUND_EXCEPTION));
Expand All @@ -168,19 +172,18 @@ public TeamRes getTeam(Member member, Long contestId, Long teamId) {
throw new ApplicationException(ErrorCode.TEAM_NOT_FOUND_EXCEPTION);
}

String teamRole = "GENERAL";
Apply apply = null;

if(member != null && team.getMember().getId().equals(member.getId())){
teamRole = "LEADER";
return TeamRes.of(team, "LEADER");
}else if(member != null && applyRepository.findByTeamIdAndMemberIdAndDeletedAtIsNull(teamId, member.getId()).isPresent()){
teamRole = "APPLIER";
apply = applyRepository.findByTeamIdAndMemberIdAndDeletedAtIsNull(teamId, member.getId())
Apply apply = applyRepository.findByTeamIdAndMemberIdAndDeletedAtIsNull(teamId, member.getId())
.orElseThrow(() -> new ApplicationException(ErrorCode.APPLY_NOT_FOUND_EXCEPTION));
return TeamRes.of(team, "APPLIER", apply);
}

updateView(team, request, response);

// Business Logic
return TeamRes.of(team, teamRole, apply);
return TeamRes.of(team, "GENERAL");
}

public Page<SimpleTeamRes> getTeamListWithContest(Long contestId, String province, String district, Pageable pageable) {
Expand Down Expand Up @@ -315,4 +318,28 @@ public List<SimpleApplyRes> getApplyList(Member member, Long teamId) {
.map(SimpleApplyRes::of)
.toList();
}

public void updateView(Team team, HttpServletRequest request, HttpServletResponse response) {
boolean hasViewed = false;
Cookie[] cookies = request.getCookies();

String COOKIE_NAME = "team_view";
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals(COOKIE_NAME)) {
hasViewed = true;
break;
}
}
}

if (!hasViewed) {
team.updateViewCount(team);
// 세션 쿠키 설정
Cookie newCookie = new Cookie(COOKIE_NAME, "viewed");
newCookie.setMaxAge(-1); // 브라우저 세션이 끝날 때까지 유효
newCookie.setPath("/");
response.addCookie(newCookie);
}
}
}

0 comments on commit 912ceb5

Please sign in to comment.