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

[BE] issue227: 스터디원을 위한 링크공유 CRUD #237

Merged
merged 26 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
41616fc
test: 링크 공유글 생성 관련 인수 테스트 추가
sc0116 Aug 8, 2022
8c4c255
test: 링크 공유글 생성 관련 인수 테스트 수정
sc0116 Aug 9, 2022
65dd9ac
Merge branch 'develop' into feat/227-link-sharing-room-crud
sc0116 Aug 9, 2022
5bf09a6
feat: 링크 공유 생성 기능 구현
sc0116 Aug 9, 2022
8273634
test: 링크 공유글 수정 관련 인수 테스트 추가
sc0116 Aug 9, 2022
e42828c
feat: 존재하지 않는 링크 공유글인 경우 예외 처리 구현
sc0116 Aug 9, 2022
4813280
feat: 내가 작성한 링크 공유글이 아닌 경우 예외 처리 및 기능 구현
sc0116 Aug 9, 2022
1cc0e1c
test: 링크 공유글 삭제 관련 인수 테스트 추가
sc0116 Aug 9, 2022
bab05b0
feat: 존재하지 않는 링크 공유글 삭제 시 예외 처리 구현
sc0116 Aug 9, 2022
c4b9db7
feat: 내가 작성하지 않은 링크 공유글 삭제 시 예외 처리 및 기능 구현
sc0116 Aug 9, 2022
6a4e60a
test: 링크 공유 전체 조회 관련 인수 테스트 추가
sc0116 Aug 10, 2022
ebba66e
feat: 링크 전체 조회 쿼리 작성
sc0116 Aug 10, 2022
1ec20ff
feat: 링크 전체 조회 기능 구현
sc0116 Aug 10, 2022
ce02344
test: 깨지는 테스트 수정
sc0116 Aug 10, 2022
6c50b2a
refactor: 링크 공유 리팩토링
sc0116 Aug 10, 2022
2a747ea
feat: 링크 공유글 수정, 삭제 시 해당 스터디에 속해있는지 검증
sc0116 Aug 10, 2022
2a05ac3
feat: 링크 공유글 수정, 삭제 시 작성자가 해당 스터디에 참여하고 있는지 검증
sc0116 Aug 10, 2022
82c6ea0
test: 링크 공유글 수정, 삭제 인수 테스트 수정
sc0116 Aug 11, 2022
23be7fa
Merge branch 'develop' into feat/227-link-sharing-room-crud
sc0116 Aug 11, 2022
996d8e1
refactor: 링크 공유글 설명 최대 글자수 50자에서 25자로 변경
sc0116 Aug 11, 2022
0cb7384
refactor: 피드백 반영
sc0116 Aug 12, 2022
0446320
refactor: REST Docs 전체 조회 response-fields 추가
sc0116 Aug 12, 2022
a8fbfc5
refactor: BaseEntity LocalDate -> LocalDateTime으로 변경
sc0116 Aug 12, 2022
115c9dc
refactor: 피드백 반영
sc0116 Aug 12, 2022
5d1dc73
chore: Merge 충돌 해결
sc0116 Aug 12, 2022
817ab07
refactor: 커뮤니티 관련 REST Docs 설정 추가
sc0116 Aug 12, 2022
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
15 changes: 15 additions & 0 deletions backend/src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ operation::studies/details[snippets='http-request,http-response']
=== 참여한 스터디 목록 조회
operation::studies/myStudy[snippets='http-request,http-response']

[[Study-Room]]
== 스터디룸

=== 링크 공유글 생성
operation::reference-room/create[snippets='http-request,request-headers,http-response']

=== 링크 공유글 전체 조회
operation::reference-room/list[snippets='http-request,request-headers,http-response']

=== 링크 공유글 수정
operation::reference-room/update[snippets='http-request,request-headers,http-response']

=== 링크 공유글 삭제
operation::reference-room/delete[snippets='http-request,request-headers,http-response']

Copy link
Collaborator

Choose a reason for hiding this comment

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

Docs 추가 좋습니다!

[[Tag]]
== 태그

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ public class AuthRequestMatchConfig {
@Bean
public AuthenticationRequestMatcher authenticationRequestMatcher() {
return new AuthenticationRequestMatcherBuilder()
.addUpAuthenticationPath(HttpMethod.POST, "/api/studies", "/api/studies/\\d+/reviews", "/api/studies/\\d+/reviews/\\d+")
.addUpAuthenticationPath(HttpMethod.GET, "/api/my/studies", "/api/members/me", "/api/members/me/role")
.addUpAuthenticationPath(HttpMethod.PUT, "/api/studies/\\d+/reviews/\\d+")
.addUpAuthenticationPath(HttpMethod.DELETE, "/api/studies/\\d+/reviews/\\d+")
.addUpAuthenticationPath(HttpMethod.POST, "/api/studies", "/api/studies/\\d+/reviews",
"/api/studies/\\d+/reviews/\\d+", "/api/studies/\\d+/reference-room/links")
.addUpAuthenticationPath(HttpMethod.GET, "/api/my/studies", "/api/members/me", "/api/members/me/role",
"/api/studies/\\d+/reference-room/links")
.addUpAuthenticationPath(HttpMethod.PUT, "/api/studies/\\d+/reviews/\\d+",
"/api/studies/\\d+/reference-room/links/\\d+")
.addUpAuthenticationPath(HttpMethod.DELETE, "/api/studies/\\d+/reviews/\\d+",
"/api/studies/\\d+/reference-room/links/\\d+")
Copy link
Collaborator

Choose a reason for hiding this comment

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

아마 스터디 커뮤니티쪽도 추가되게 되면 굉장히 많아지겠네요..
슬슬...개선방안을 고민해봐야할 듯 ㅠ.ㅠ

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

맞아요 더 많아질텐데 개선해봐야겠어요

.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.woowacourse.moamoa.common.entity;

import java.util.Date;
import java.time.LocalDate;
import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
Expand All @@ -16,9 +16,9 @@ public class BaseEntity {

@CreatedDate
@Column(updatable = false, nullable = false)
private Date createdDate;
private LocalDate createdDate;
Copy link
Collaborator

Choose a reason for hiding this comment

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

LocalDateTime 으로 수정 부탁드립니다!!
밑에 lastModifiedDate 도!!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

린론, 루스와 얘기해보니 LocalDate로 하는 것 같아요!


@LastModifiedDate
@Column(nullable = false)
private Date lastModifiedDate;
private LocalDate lastModifiedDate;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
public class NotParticipatedMemberException extends UnauthorizedException {

public NotParticipatedMemberException() {
super("스터디에 참여한 회원만 후기를 작성할 수 있습니다.");
super("스터디에 참여한 회원만 작성할 수 있습니다.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.woowacourse.moamoa.referenceroom.controller;

import com.woowacourse.moamoa.auth.config.AuthenticationPrincipal;
import com.woowacourse.moamoa.referenceroom.service.ReferenceRoomService;
import com.woowacourse.moamoa.referenceroom.service.request.CreatingLinkRequest;
import com.woowacourse.moamoa.referenceroom.service.request.EditingLinkRequest;
import java.net.URI;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/studies/{study-id}/reference-room/links")
@RequiredArgsConstructor
public class ReferenceRoomController {

private final ReferenceRoomService referenceRoomService;

@PostMapping
public ResponseEntity<Void> createLink(
@AuthenticationPrincipal final Long githubId,
@PathVariable("study-id") final Long studyId,
@Valid @RequestBody final CreatingLinkRequest creatingLinkRequest
) {
final Long id = referenceRoomService.createLink(githubId, studyId, creatingLinkRequest).getId();
return ResponseEntity.created(URI.create("/api/studies/" + studyId + "/reference-room/links/" + id)).build();
}

@PutMapping("/{link-id}")
public ResponseEntity<Void> updateLink(
@AuthenticationPrincipal final Long githubId,
@PathVariable("study-id") final Long studyId,
@PathVariable("link-id") final Long linkId,
@Valid @RequestBody final EditingLinkRequest editingLinkRequest
) {
referenceRoomService.updateLink(githubId, studyId, linkId, editingLinkRequest);
return ResponseEntity.noContent().build();
}

@DeleteMapping("/{link-id}")
public ResponseEntity<Void> deleteLink(
@AuthenticationPrincipal final Long githubId,
@PathVariable("study-id") final Long studyId,
@PathVariable("link-id") final Long linkId
) {
referenceRoomService.deleteLink(githubId, studyId, linkId);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.woowacourse.moamoa.referenceroom.controller;

import com.woowacourse.moamoa.auth.config.AuthenticationPrincipal;
import com.woowacourse.moamoa.referenceroom.service.SearchingReferenceRoomService;
import com.woowacourse.moamoa.referenceroom.service.response.LinksResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/studies/{study-id}/reference-room/links")
@RequiredArgsConstructor
public class SearchingReferenceRoomController {

private final SearchingReferenceRoomService searchingReferenceRoomService;

@GetMapping
public ResponseEntity<LinksResponse> getLinks(
@AuthenticationPrincipal final Long githubId,
@PathVariable("study-id") final Long studyId,
@PageableDefault(size = 5) final Pageable pageable
) {
final LinksResponse linksResponse = searchingReferenceRoomService.getLinks(githubId, studyId, pageable);
return ResponseEntity.ok().body(linksResponse);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

조회랑 명령 분리해준거 좋네요!!👍
defalut size는 5로 프론트와 이야기를 한 건지 궁금합니다!!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

디폴트가 9로 바꼈네요 ㅎㅎ

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.woowacourse.moamoa.referenceroom.domain;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

@Embeddable
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@EqualsAndHashCode
public class Author {

@Column(name = "member_id", nullable = false)
private Long memberId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.woowacourse.moamoa.referenceroom.domain;

import com.woowacourse.moamoa.common.entity.BaseEntity;
import com.woowacourse.moamoa.referenceroom.service.exception.NotLinkAuthorException;
import com.woowacourse.moamoa.referenceroom.service.exception.NotRelatedLinkException;
import com.woowacourse.moamoa.review.domain.AssociatedStudy;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Where;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Where(clause = "deleted = false")
public class Link extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Embedded
private AssociatedStudy associatedStudy;

@Embedded
private Author author;

@Column(nullable = false)
private String linkUrl;

private String description;

@Column(nullable = false)
private boolean deleted;

public Link(
final AssociatedStudy associatedStudy, final Author author, final String linkUrl, final String description
) {
this(null, associatedStudy, author, linkUrl, description, false);
}
Comment on lines +42 to +46
Copy link
Collaborator

Choose a reason for hiding this comment

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

생성자 체이닝 👍


public void update(final Link updatedLink) {
validateBelongToStudy(updatedLink.associatedStudy);
validateAuthor(updatedLink.author);
Comment on lines +49 to +50
Copy link
Collaborator

Choose a reason for hiding this comment

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

필드로 바로 접근보다 getter 쓰는게 어떨까요??ㅇㅅㅇ
Link 클래스내의 로직이라 접근이 가능한 것 같은데 parameter로 넘어온 객체는 현재 로직이 실행되는 객체랑은 달라서!


linkUrl = updatedLink.linkUrl;
description = updatedLink.description;
}

public void delete(final AssociatedStudy associatedStudy, final Author author) {
validateBelongToStudy(associatedStudy);
Copy link
Collaborator

Choose a reason for hiding this comment

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

꼼꼼한 검증 👍

validateAuthor(author);

deleted = true;
}

private void validateBelongToStudy(final AssociatedStudy associatedStudy) {
if (!this.associatedStudy.equals(associatedStudy)) {
throw new NotRelatedLinkException();
}
}

private void validateAuthor(final Author author) {
if (!this.author.equals(author)) {
throw new NotLinkAuthorException();
}
}

public Long getId() {
return id;
}

public AssociatedStudy getAssociatedStudy() {
return associatedStudy;
}

public String getLinkUrl() {
return linkUrl;
}

public String getDescription() {
return description;
}

public boolean isDeleted() {
return deleted;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

5개 모두 Getter가 필요하다면 @Getter 어노테이션도 괜찮을 것 같아요!! (꼭 반영하지 않으셔도 됩니다~_~)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

getAuthor()가 현재는 필요하지 않고, 롬복쓰면 간결해지지만 어디에 쓰는지 모르겠더라구여..!

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.woowacourse.moamoa.referenceroom.domain.repository;

import com.woowacourse.moamoa.referenceroom.domain.Link;
import org.springframework.data.jpa.repository.JpaRepository;

public interface JpaLinkRepository extends JpaRepository<Link, Long>, LinkRepository {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.woowacourse.moamoa.referenceroom.domain.repository;

import com.woowacourse.moamoa.referenceroom.domain.Link;
import java.util.Optional;

public interface LinkRepository {

Link save(Link link);

Optional<Link> findById(Long id);
}
Comment on lines +6 to +11
Copy link
Collaborator

Choose a reason for hiding this comment

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

사용하는 메소드만 👍👍

Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.woowacourse.moamoa.referenceroom.query;

import com.woowacourse.moamoa.member.query.data.MemberData;
import com.woowacourse.moamoa.referenceroom.query.data.LinkData;
import java.time.LocalDate;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.SliceImpl;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
@RequiredArgsConstructor
public class LinkDao {

private final NamedParameterJdbcTemplate namedParameterJdbcTemplate;

public Slice<LinkData> findAllByStudyId(final Long studyId, final Pageable pageable) {
final String sql = "SELECT link.id, link.link_url, link.description, link.created_date, link.last_modified_date, "
+ " member.github_id, member.username, member.image_url, member.profile_url "
Copy link
Collaborator

Choose a reason for hiding this comment

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

어 여기 공백 두 칸이네요!!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

헉 그러네요 한 칸으로 줄였습니다

+ "FROM link "
+ "JOIN member ON link.member_id = member.id "
+ "WHERE link.deleted = false "
+ "AND link.study_id = :studyId "
+ "ORDER BY link.created_date DESC";
Copy link
Collaborator

Choose a reason for hiding this comment

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

link.id로 정렬하는 부분도 추가하면 좋을 것 같아요
시간이 아예 같은 경우 순서가 뒤죽박죽이 되더라고요! 🥲

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

넵 id로 정렬하는 부분 추가하겠습니다!

final MapSqlParameterSource params = new MapSqlParameterSource("studyId", studyId);

final List<LinkData> linkData = namedParameterJdbcTemplate.query(sql, params, rowMapper());
return new SliceImpl<>(getCurrentPageLinks(linkData, pageable), pageable, hasNext(linkData, pageable));
}

private List<LinkData> getCurrentPageLinks(final List<LinkData> linkData, final Pageable pageable) {
if (hasNext(linkData, pageable)) {
return linkData.subList(0, linkData.size() - 1);
}
return linkData;
}

private boolean hasNext(final List<LinkData> linkData, final Pageable pageable) {
return linkData.size() > pageable.getPageSize();
}

private RowMapper<LinkData> rowMapper() {
return (rs, rn) -> {
final Long id = rs.getLong("id");
final String linkUrl = rs.getString("link_url");
final String description = rs.getString("description");
final LocalDate createdDate = rs.getObject("created_date", LocalDate.class);
final LocalDate lastModifiedDate = rs.getObject("last_modified_date", LocalDate.class);

final Long githubId = rs.getLong("github_id");
final String username = rs.getString("username");
final String imageUrl = rs.getString("image_url");
final String profileUrl = rs.getString("profile_url");
final MemberData memberData = new MemberData(githubId, username, imageUrl, profileUrl);

return new LinkData(id, memberData, linkUrl, description, createdDate, lastModifiedDate);
};
}
Comment on lines +47 to +63
Copy link
Collaborator

Choose a reason for hiding this comment

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

굿굿!! 편리함을 느꼈다니 기쁘네요~_~😁

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.woowacourse.moamoa.referenceroom.query.data;

import com.woowacourse.moamoa.member.query.data.MemberData;
import java.time.LocalDate;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Getter
@EqualsAndHashCode
public class LinkData {

private Long id;
private MemberData memberData;
private String linkUrl;
private String description;
private LocalDate createdDate;
private LocalDate lastModifiedDate;
}
Loading