From 00deeb806be16a3fde9da95c4e3037864c3e56b1 Mon Sep 17 00:00:00 2001 From: gkim Date: Thu, 21 Dec 2023 22:31:39 +0900 Subject: [PATCH 1/5] :sparkles: feat: Create TeamDto MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - TeamDto 생성 Related: #24 --- .../ceos/vote/domain/team/dto/TeamDto.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/main/java/com/ceos/vote/domain/team/dto/TeamDto.java diff --git a/src/main/java/com/ceos/vote/domain/team/dto/TeamDto.java b/src/main/java/com/ceos/vote/domain/team/dto/TeamDto.java new file mode 100644 index 0000000..6539feb --- /dev/null +++ b/src/main/java/com/ceos/vote/domain/team/dto/TeamDto.java @@ -0,0 +1,25 @@ +package com.ceos.vote.domain.team.dto; + +import com.ceos.vote.domain.team.entity.Team; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Builder; +import lombok.Data; + +@Data +@Schema(description = "팀 dto") +public class TeamDto { + + private Long id; + private String name; + private String description; + private Integer voteCnt; + + @Builder + public TeamDto(Team team) { + this.id = team.getId(); + this.name = team.getName(); + this.description = team.getDescription(); + this.voteCnt = team.getVoteCnt(); + } + +} From 521006386f04cbc29624567bb1f5f60ebd833ceb Mon Sep 17 00:00:00 2001 From: gkim Date: Thu, 21 Dec 2023 22:37:47 +0900 Subject: [PATCH 2/5] :sparkles: feat: Create getTeamList API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 팀 목록 조회 API 생성 (조회 시 득표 순 기준 정렬) Related: #24 --- .../team/controller/TeamController.java | 28 +++++++++++++++++++ .../team/repository/TeamRepository.java | 4 +++ .../vote/domain/team/service/TeamService.java | 26 +++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 src/main/java/com/ceos/vote/domain/team/controller/TeamController.java create mode 100644 src/main/java/com/ceos/vote/domain/team/service/TeamService.java diff --git a/src/main/java/com/ceos/vote/domain/team/controller/TeamController.java b/src/main/java/com/ceos/vote/domain/team/controller/TeamController.java new file mode 100644 index 0000000..b59fd97 --- /dev/null +++ b/src/main/java/com/ceos/vote/domain/team/controller/TeamController.java @@ -0,0 +1,28 @@ +package com.ceos.vote.domain.team.controller; + +import com.ceos.vote.auth.CurrentUser; +import com.ceos.vote.common.dto.ResponseDto; +import com.ceos.vote.domain.member.entity.Member; +import com.ceos.vote.domain.team.service.TeamService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PatchMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/app/team") +public class TeamController { + + private final TeamService teamService; + + @GetMapping("") + public ResponseEntity getTeamList() { + + return ResponseDto.ok(teamService.getTeamList()); + } + +} diff --git a/src/main/java/com/ceos/vote/domain/team/repository/TeamRepository.java b/src/main/java/com/ceos/vote/domain/team/repository/TeamRepository.java index faed165..c0b97de 100644 --- a/src/main/java/com/ceos/vote/domain/team/repository/TeamRepository.java +++ b/src/main/java/com/ceos/vote/domain/team/repository/TeamRepository.java @@ -1,9 +1,13 @@ package com.ceos.vote.domain.team.repository; import com.ceos.vote.domain.team.entity.Team; +import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface TeamRepository extends JpaRepository { + + List findAllByOrderByVoteCntDesc(); + } diff --git a/src/main/java/com/ceos/vote/domain/team/service/TeamService.java b/src/main/java/com/ceos/vote/domain/team/service/TeamService.java new file mode 100644 index 0000000..e6c6f60 --- /dev/null +++ b/src/main/java/com/ceos/vote/domain/team/service/TeamService.java @@ -0,0 +1,26 @@ +package com.ceos.vote.domain.team.service; + +import com.ceos.vote.domain.team.dto.TeamDto; +import com.ceos.vote.domain.team.repository.TeamRepository; +import java.util.List; +import java.util.stream.Collectors; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class TeamService { + + private final TeamRepository teamRepository; + + public List getTeamList(){ + + List teamList = teamRepository.findAllByOrderByVoteCntDesc() + .stream() + .map(TeamDto::new) + .collect(Collectors.toList()); + + return teamList; + } + +} From f93bd51bebee2d395d1a4eb5b04be366296e6e13 Mon Sep 17 00:00:00 2001 From: gkim Date: Thu, 21 Dec 2023 22:42:04 +0900 Subject: [PATCH 3/5] :sparkles: feat: Add new error code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 투표 완료 유저 에러 코드 추가 Related: #24 --- src/main/java/com/ceos/vote/common/exception/ErrorCode.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/ceos/vote/common/exception/ErrorCode.java b/src/main/java/com/ceos/vote/common/exception/ErrorCode.java index dc1438d..84619eb 100644 --- a/src/main/java/com/ceos/vote/common/exception/ErrorCode.java +++ b/src/main/java/com/ceos/vote/common/exception/ErrorCode.java @@ -12,7 +12,8 @@ public enum ErrorCode { ALREADY_MEMBER_EMAIL(HttpStatus.CONFLICT, "이미 존재하는 유저 정보입니다.", "다른 이메일로 가입해주세요."), ALREADY_MEMBER_ID(HttpStatus.CONFLICT, "이미 존재하는 유저 정보입니다.", "다른 ID로 가입해주세요."), INVALID_CREDENTIALS(HttpStatus.UNAUTHORIZED, "아이디 혹은 비밀번호가 틀렸습니다.", "다시 시도해주세요."), - INVALID_ACCESS_TOKEN(HttpStatus.UNAUTHORIZED, "유효하지 않은 액세스 토큰입니다.", "다시 로그인해주세요."); + INVALID_ACCESS_TOKEN(HttpStatus.UNAUTHORIZED, "유효하지 않은 액세스 토큰입니다.", "다시 로그인해주세요."), + ALREADY_VOTED_USER(HttpStatus.BAD_REQUEST, "이미 투표를 완료한 유저입니다.", "투표가 불가합니다."); private final HttpStatus httpStatus; private final String message; From 8cd24220110ad44e20bf4f720f2b7f62df0e7782 Mon Sep 17 00:00:00 2001 From: gkim Date: Thu, 21 Dec 2023 22:44:21 +0900 Subject: [PATCH 4/5] :sparkles: feat: Create voteTeam API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 팀 투표 API 생성 Related: #24 --- .../vote/domain/member/entity/Member.java | 5 ++++ .../team/controller/TeamController.java | 8 +++++++ .../ceos/vote/domain/team/entity/Team.java | 4 ++++ .../vote/domain/team/service/TeamService.java | 23 +++++++++++++++++++ 4 files changed, 40 insertions(+) diff --git a/src/main/java/com/ceos/vote/domain/member/entity/Member.java b/src/main/java/com/ceos/vote/domain/member/entity/Member.java index acf4e7f..839cfd2 100644 --- a/src/main/java/com/ceos/vote/domain/member/entity/Member.java +++ b/src/main/java/com/ceos/vote/domain/member/entity/Member.java @@ -52,4 +52,9 @@ public Member(Long id, String username, String userid, String email, String pass this.role = Role.ROLE_USER; } + + public void patchVoteFlagTeam(){ + this.voteFlagTeam = Boolean.TRUE; + } + } diff --git a/src/main/java/com/ceos/vote/domain/team/controller/TeamController.java b/src/main/java/com/ceos/vote/domain/team/controller/TeamController.java index b59fd97..9396672 100644 --- a/src/main/java/com/ceos/vote/domain/team/controller/TeamController.java +++ b/src/main/java/com/ceos/vote/domain/team/controller/TeamController.java @@ -25,4 +25,12 @@ public ResponseEntity getTeamList() { return ResponseDto.ok(teamService.getTeamList()); } + @PatchMapping("/{teamId}") + public ResponseEntity voteTeam( + @CurrentUser Member member, + @PathVariable("teamId") Long teamId + ) { + return ResponseDto.ok(teamService.voteTeam(member, teamId)); + } + } diff --git a/src/main/java/com/ceos/vote/domain/team/entity/Team.java b/src/main/java/com/ceos/vote/domain/team/entity/Team.java index ca99f6f..5080090 100644 --- a/src/main/java/com/ceos/vote/domain/team/entity/Team.java +++ b/src/main/java/com/ceos/vote/domain/team/entity/Team.java @@ -23,4 +23,8 @@ public class Team { private String description; private Integer voteCnt; + public void patchVoteCnt(Integer voteCnt){ + this.voteCnt = voteCnt+1; + } + } diff --git a/src/main/java/com/ceos/vote/domain/team/service/TeamService.java b/src/main/java/com/ceos/vote/domain/team/service/TeamService.java index e6c6f60..46cfb0c 100644 --- a/src/main/java/com/ceos/vote/domain/team/service/TeamService.java +++ b/src/main/java/com/ceos/vote/domain/team/service/TeamService.java @@ -1,11 +1,16 @@ package com.ceos.vote.domain.team.service; +import com.ceos.vote.common.exception.CeosException; +import com.ceos.vote.common.exception.ErrorCode; +import com.ceos.vote.domain.member.entity.Member; import com.ceos.vote.domain.team.dto.TeamDto; +import com.ceos.vote.domain.team.entity.Team; import com.ceos.vote.domain.team.repository.TeamRepository; import java.util.List; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; @Service @RequiredArgsConstructor @@ -23,4 +28,22 @@ public List getTeamList(){ return teamList; } + @Transactional + public TeamDto voteTeam(Member member, Long teamId) { + + Boolean voteFlag = member.getVoteFlagTeam(); + + if (!voteFlag) { + Team team = teamRepository.findById(teamId).orElseThrow(); + team.patchVoteCnt(team.getVoteCnt()); + member.patchVoteFlagTeam(); + + return TeamDto.builder().team(team).build(); + } + else { + throw new CeosException(ErrorCode.ALREADY_VOTED_USER); + } + + } + } From e2f31bada2d6782d3401d0386026877fd0488eac Mon Sep 17 00:00:00 2001 From: gkim Date: Thu, 21 Dec 2023 22:47:35 +0900 Subject: [PATCH 5/5] :sparkles: feat: Add team API permission MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 팀 투표 API 접근 권한 설정 - Config 파일 dir 정리 (Security Config 위치 이동) Related: #24 --- .../WebSecurityConfig.java | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) rename src/main/java/com/ceos/vote/common/{security => config}/WebSecurityConfig.java (74%) diff --git a/src/main/java/com/ceos/vote/common/security/WebSecurityConfig.java b/src/main/java/com/ceos/vote/common/config/WebSecurityConfig.java similarity index 74% rename from src/main/java/com/ceos/vote/common/security/WebSecurityConfig.java rename to src/main/java/com/ceos/vote/common/config/WebSecurityConfig.java index 17c88b5..ffb3c5c 100644 --- a/src/main/java/com/ceos/vote/common/security/WebSecurityConfig.java +++ b/src/main/java/com/ceos/vote/common/config/WebSecurityConfig.java @@ -1,4 +1,4 @@ -package com.ceos.vote.common.security; +package com.ceos.vote.common.config; import com.ceos.vote.auth.exception.JwtAuthenticationEntryPoint; import com.ceos.vote.auth.jwt.filter.JwtAuthenticationFilter; @@ -44,21 +44,21 @@ public AuthenticationManager authenticationManager(AuthenticationConfiguration a @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http.httpBasic(HttpBasicConfigurer::disable) - .csrf(CsrfConfigurer::disable) - .formLogin(FormLoginConfigurer::disable) - .addFilterBefore(new JwtAuthenticationFilter(jwtTokenProvider), UsernamePasswordAuthenticationFilter.class) - .authorizeRequests() // - .requestMatchers("/", - "/app/auth/signup", - "/app/auth/login/**", - "/app/auth/login").permitAll() - .anyRequest().authenticated() - .and() - .exceptionHandling((exceptionHandling) -> - exceptionHandling - .authenticationEntryPoint(jwtAuthenticationEntryPoint) - .accessDeniedHandler(jwtAccessDeniedHandler) - ); + .csrf(CsrfConfigurer::disable) + .formLogin(FormLoginConfigurer::disable) + .addFilterBefore(new JwtAuthenticationFilter(jwtTokenProvider), UsernamePasswordAuthenticationFilter.class) + .authorizeRequests() // + .requestMatchers("/", "/app/auth/signup", "/app/auth/login/**", "/app/auth/login") + .permitAll() + .requestMatchers("/app/team", "/app/team/**") + .permitAll() + .anyRequest().authenticated() + .and() + .exceptionHandling((exceptionHandling) -> + exceptionHandling + .authenticationEntryPoint(jwtAuthenticationEntryPoint) + .accessDeniedHandler(jwtAccessDeniedHandler) + ); return http.build(); }