-
Notifications
You must be signed in to change notification settings - Fork 5
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
Changes from 20 commits
41616fc
8c4c255
65dd9ac
5bf09a6
8273634
e42828c
4813280
1cc0e1c
bab05b0
c4b9db7
6a4e60a
ebba66e
1ec20ff
ce02344
6c50b2a
2a747ea
2a05ac3
82c6ea0
23be7fa
996d8e1
0cb7384
0446320
a8fbfc5
115c9dc
5d1dc73
817ab07
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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+") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아마 스터디 커뮤니티쪽도 추가되게 되면 굉장히 많아지겠네요.. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
@@ -16,9 +16,9 @@ public class BaseEntity { | |
|
||
@CreatedDate | ||
@Column(updatable = false, nullable = false) | ||
private Date createdDate; | ||
private LocalDate createdDate; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 린론, 루스와 얘기해보니 |
||
|
||
@LastModifiedDate | ||
@Column(nullable = false) | ||
private Date lastModifiedDate; | ||
private LocalDate lastModifiedDate; | ||
} |
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); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 조회랑 명령 분리해준거 좋네요!!👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 필드로 바로 접근보다 getter 쓰는게 어떨까요??ㅇㅅㅇ |
||
|
||
linkUrl = updatedLink.linkUrl; | ||
description = updatedLink.description; | ||
} | ||
|
||
public void delete(final AssociatedStudy associatedStudy, final Author author) { | ||
validateBelongToStudy(associatedStudy); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 5개 모두 Getter가 필요하다면 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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,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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 어 여기 공백 두 칸이네요!! There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docs 추가 좋습니다!