Skip to content

Commit

Permalink
🚀 Merge pull request #26 from local-mood/feat/24-team
Browse files Browse the repository at this point in the history
Feat: 팀 API 구현
  • Loading branch information
gmkim20713 authored Dec 21, 2023
2 parents 7f23223 + e2f31ba commit 5386732
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/ceos/vote/common/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/ceos/vote/domain/member/entity/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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());
}

@PatchMapping("/{teamId}")
public ResponseEntity<?> voteTeam(
@CurrentUser Member member,
@PathVariable("teamId") Long teamId
) {
return ResponseDto.ok(teamService.voteTeam(member, teamId));
}

}
25 changes: 25 additions & 0 deletions src/main/java/com/ceos/vote/domain/team/dto/TeamDto.java
Original file line number Diff line number Diff line change
@@ -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();
}

}
4 changes: 4 additions & 0 deletions src/main/java/com/ceos/vote/domain/team/entity/Team.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ public class Team {
private String description;
private Integer voteCnt;

public void patchVoteCnt(Integer voteCnt){
this.voteCnt = voteCnt+1;
}

}
Original file line number Diff line number Diff line change
@@ -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<Team, Long> {

List<Team> findAllByOrderByVoteCntDesc();

}
49 changes: 49 additions & 0 deletions src/main/java/com/ceos/vote/domain/team/service/TeamService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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
public class TeamService {

private final TeamRepository teamRepository;

public List<TeamDto> getTeamList(){

List<TeamDto> teamList = teamRepository.findAllByOrderByVoteCntDesc()
.stream()
.map(TeamDto::new)
.collect(Collectors.toList());

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);
}

}

}

0 comments on commit 5386732

Please sign in to comment.