Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[스프린트 4] 강수민 - 팀 공지사항 업데이트 기능 API 구현 #48

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package PNUMEAT.Backend.domain.team.controller;

import PNUMEAT.Backend.domain.auth.entity.Member;
import PNUMEAT.Backend.domain.team.dto.request.TeamAnnouncementRequest;
import PNUMEAT.Backend.domain.team.dto.request.TeamJoinRequest;
import PNUMEAT.Backend.domain.team.dto.request.TeamRequest;
import PNUMEAT.Backend.domain.team.dto.response.MyTeamResponse;
Expand Down Expand Up @@ -106,6 +107,17 @@ public ResponseEntity<ApiResponse<?>> joinTeam(@PathVariable("teamId") Long team
.contentType(MediaType.APPLICATION_JSON)
.body(ApiResponse.successResponseWithMessage(TEAM_JOIN_SUCCESS.getMessage()));
}

@PatchMapping("/{teamId}/announcement")
public ResponseEntity<ApiResponse<?>> updateTeamAnnouncement(@PathVariable("teamId") Long teamId,
@RequestBody @Valid TeamAnnouncementRequest teamAnnouncementRequest,
@LoginMember Member member) {
teamService.updateTeamAnnouncement(member, teamAnnouncementRequest.teamAnnouncement(), teamId);

return ResponseEntity.status(TEAM_ANNOUNCEMENT_UPDATE_SUCCESS.getStatusCode())
.contentType(MediaType.APPLICATION_JSON)
.body(ApiResponse.successResponseWithMessage(TEAM_ANNOUNCEMENT_UPDATE_SUCCESS.getMessage()));
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package PNUMEAT.Backend.domain.team.dto.request;

import PNUMEAT.Backend.global.validation.annotation.NotNullOrBlank;
import jakarta.validation.constraints.Size;

public record TeamAnnouncementRequest(
@NotNullOrBlank
@Size(
max = 255,
message = "글자 수를 초과했습니다."
)
String teamAnnouncement) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public record MyTeamResponse(Long teamId,
int memberCount,
int streakDays,
int successMemberCount,
String teamAnnouncement,
LocalDate createdAt,
List<MemberProfileResponse> members) {

Expand All @@ -30,6 +31,7 @@ public static MyTeamResponse of(Team team){
team.getTeamMembers().size(),
team.getStreakDays(),
0, // 게시물 엔티티 없는 관계로 0으로 설정
team.getTeamAnnouncement(),
team.getCreatedDate().toLocalDate(),
team.getTeamMembers().stream()
.map(tm -> MemberProfileResponse.of(tm.getMember()))
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/PNUMEAT/Backend/domain/team/entity/Team.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class Team extends TimeStamp {

private int streakDays = 0;

private String teamAnnouncement="";

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member teamManager;
Expand All @@ -58,4 +60,6 @@ public Team(String teamName, Topic teamTopic, String teamExplain, int maxPartici
public void updateTeamIconUrl(String teamIconUrl){
this.teamIconUrl = teamIconUrl;
}

public void updateTeamAnnouncement(String teamAnnouncement) { this.teamAnnouncement = teamAnnouncement; }
}
18 changes: 15 additions & 3 deletions src/main/java/PNUMEAT/Backend/domain/team/service/TeamService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import PNUMEAT.Backend.domain.team.repository.TeamRepository;
import PNUMEAT.Backend.domain.teamMember.entity.TeamMember;
import PNUMEAT.Backend.domain.teamMember.repository.TeamMemberRepository;
import PNUMEAT.Backend.global.error.ComonException;
import PNUMEAT.Backend.global.error.Team.TeamAlreadyJoinException;
import PNUMEAT.Backend.global.error.Team.TeamManagerInvalidException;
import PNUMEAT.Backend.global.error.Team.TeamNotFoundException;
import PNUMEAT.Backend.global.error.Team.TeamPasswordInvalidException;
import PNUMEAT.Backend.global.images.ImageService;
Expand All @@ -23,8 +23,6 @@
import java.util.Collections;
import java.util.List;

import static PNUMEAT.Backend.global.error.ErrorCode.*;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
Expand Down Expand Up @@ -83,6 +81,20 @@ public TeamMember joinTeam(Member member, String password, Long teamId){
return teamMemberRepository.save(new TeamMember(team, member));
}

@Transactional
public Team updateTeamAnnouncement(Member member, String teamAnnouncement, Long teamId){
Team team = teamRepository.findById(teamId)
.orElseThrow(TeamNotFoundException::new);

if(!member.getUuid().equals(team.getTeamManager().getUuid())) {
throw new TeamManagerInvalidException();
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

객체 내부에 검증 관련 method가 있으면 좋을 것 같습니다.
그리고 주체는 Team 이 되면 좋을 것 같습니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 부분 수정했습니다!
pr #57


team.updateTeamAnnouncement(teamAnnouncement);

return team;
}

private void validatePassword(String password, Team team) {
if (!password.equals(team.getTeamPassword())) {
throw new TeamPasswordInvalidException();
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/PNUMEAT/Backend/global/error/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ public enum ErrorCode {

//ARTICLE
ARTICLE_FORBIDDEN_ERROR(HttpStatus.FORBIDDEN, "게시글 권한이 없습니다."),
ARTICLE_NOT_FOUND_ERROR(HttpStatus.NOT_FOUND, "존재 하지 않는 게시글 입니다."),

//TEAM
TOPIC_INVALID_ERROR(HttpStatus.BAD_REQUEST, "주제가 올바른 형식이 아닙니다."),
TEAM_NOT_FOUND_ERROR(HttpStatus.NOT_FOUND, "팀이 존재하지 않습니다."),
TEAM_PASSWORD_INVALID(HttpStatus.BAD_REQUEST, "팀 비밀번호가 옳지 않습니다."),
TEAM_ALREADY_JOIN(HttpStatus.CONFLICT, "이미 팀에 가입했습니다."),

// ARTICLE
ARTICLE_NOT_FOUND_ERROR(HttpStatus.NOT_FOUND, "존재 하지 않는 게시글 입니다."),
TEAM_MANAGER_INVALID_ERROR(HttpStatus.BAD_REQUEST, "팀의 매니저가 옳지 않습니다."),

//IMAGE
IMAGE_FILE_UPLOAD_ERROR(HttpStatus.BAD_REQUEST,"이미지 파일 업로드 에러발생."),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package PNUMEAT.Backend.global.error.Team;

import PNUMEAT.Backend.global.error.ComonException;

import static PNUMEAT.Backend.global.error.ErrorCode.TEAM_MANAGER_INVALID_ERROR;

public class TeamManagerInvalidException extends ComonException {
public TeamManagerInvalidException(){
super(TEAM_MANAGER_INVALID_ERROR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public enum ResponseMessageEnum {
TEAM_TOTAL_DETAILS_SUCCESS("전체 팀을 성공적으로 조회했습니다.",200),
MY_TEAM_DETAILS_SUCCESS("나의 팀을 성공적으로 조회했습니다.",200),
TEAM_JOIN_SUCCESS("팀에 성공적으로 가입했습니다.", 200),
TEAM_ANNOUNCEMENT_UPDATE_SUCCESS("팀 공지가 성공적으로 업데이트되었습니다.", 200),

//Article
ARTICLE_CREATE_SUCCESS("게시글이 성공적으로 생성되었습니다.",201),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,52 @@ private Team createTeam(String teamName, String teamExplain, int maxParticipant,
.teamManager(teamManager)
.build();
}

@Test
@DisplayName("팀 공지 업데이트 하기 - 정상 업데이트")
void updateTeamAnnouncement_정상_업데이트() {
// given
Long teamId = team2.getTeamId();
String teamAnnouncement = "팀 공지 입니다.";

given(teamRepository.findById(teamId)).willReturn(Optional.of(team2));

// when
Team team = teamService.updateTeamAnnouncement(member, teamAnnouncement, teamId);

// then
assertThat(team.getTeamId()).isEqualTo(teamId);
assertThat(team.getTeamManager().getUuid()).isEqualTo(member.getUuid());
assertThat(team.getTeamAnnouncement()).isEqualTo(teamAnnouncement);

}

@Test
@DisplayName("팀 공지 올리기 - 팀이 존재하지 않을 때")
void updateTeamAnnouncement_팀이_존재하지_않을_때() {
// given
Long teamId = team2.getTeamId();
String teamAnnouncement = "팀 공지 입니다.";
given(teamRepository.findById(teamId)).willReturn(Optional.empty());

// expected
assertThatThrownBy(() -> teamService.updateTeamAnnouncement(member, teamAnnouncement, teamId))
.isInstanceOf(ComonException.class)
.hasMessage(TEAM_NOT_FOUND_ERROR.getMessage());
}

@Test
@DisplayName("팀 공지 올리기 - 팀의 매니저가 아닐 때")
void updateTeamAnnouncement_팀의_매니저가_아닐_때() {
// given
Long teamId = team2.getTeamId();
String teamAnnouncement = "팀 공지 입니다.";
Member invalidMember = new Member("INVALID", "INVALID", "ROLE_USER");
given(teamRepository.findById(teamId)).willReturn(Optional.of(team2));

// expected
assertThatThrownBy(() -> teamService.updateTeamAnnouncement(invalidMember, teamAnnouncement, teamId))
.isInstanceOf(ComonException.class)
.hasMessage(TEAM_MANAGER_INVALID_ERROR.getMessage());
}
}