-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚀 Merge pull request #26 from local-mood/feat/24-team
Feat: 팀 API 구현
- Loading branch information
Showing
8 changed files
with
141 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/java/com/ceos/vote/domain/team/controller/TeamController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/main/java/com/ceos/vote/domain/team/repository/TeamRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
49
src/main/java/com/ceos/vote/domain/team/service/TeamService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
|
||
} |