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

Dev backend #123

Merged
merged 12 commits into from
May 15, 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
4 changes: 2 additions & 2 deletions backend/core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ dependencies {
annotationProcessor 'org.projectlombok:lombok'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.5.Final'

testCompileOnly 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'
testAnnotationProcessor "org.mapstruct:mapstruct-processor:1.5.5.Final"
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
testCompileOnly 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'
}

tasks.named('bootBuildImage') {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.rollthedice.backend.domain.bookmark.api;

import com.rollthedice.backend.domain.bookmark.dto.response.BookmarkResponse;
import com.rollthedice.backend.domain.news.dto.response.NewsResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
Expand All @@ -12,24 +13,40 @@

public interface BookmarkApi {
@Operation(
summary = "๋ถ๋งˆํฌ ์กฐํšŒ",
summary = "๋ถ๋งˆํฌ ์ „์ฒด ์กฐํšŒ",
description = "ํšŒ์›์ด ๋ถ๋งˆํฌํ•œ ๋‰ด์Šค๋ฅผ ํŽ˜์ด์ง€๋กœ ๋‚˜๋ˆ„์–ด ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.",
security = {@SecurityRequirement(name = "access_token")},
tags = {"bookmark"}
tags = {"๋ถ๋งˆํฌ"}
)
@ApiResponse(
responseCode = "200",
description = "OK"
)
List<NewsResponse> getBookmarked(
List<NewsResponse> getAllBookmarkedNews(
Pageable pageable
);

@Operation(
summary = "๋‰ด์Šค ๋ถ๋งˆํฌ ์—ฌ๋ถ€ ์กฐํšŒ",
description = "๋กœ๊ทธ์ธํ•œ ํšŒ์›์ด ํ•ด๋‹น ๋‰ด์Šค๋ฅผ ๋ถ๋งˆํฌ ํ–ˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.",
security = {@SecurityRequirement(name = "access_token")},
tags = {"๋ถ๋งˆํฌ"}
)
@ApiResponse(
responseCode = "200",
description = "OK"
)
BookmarkResponse getIsBookmarked(
@Parameter(in = ParameterIn.PATH, description = "๋‰ด์Šค ID", required = true)
Long newsId
);


@Operation(
summary = "๋ถ๋งˆํฌ ์ €์žฅ",
description = "๋‰ด์Šค์— ๋Œ€ํ•˜์—ฌ ๋ถ๋งˆํฌ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.",
security = {@SecurityRequirement(name = "access_token")},
tags = {"bookmark"}
tags = {"๋ถ๋งˆํฌ"}
)
@ApiResponse(
responseCode = "201",
Expand All @@ -44,7 +61,7 @@ void saveBookmark(
summary = "๋ถ๋งˆํฌ ์‚ญ์ œ",
description = "์ €์žฅ๋œ ๋ถ๋งˆํฌ๋ฅผ ํ•ด์ œํ•ฉ๋‹ˆ๋‹ค.",
security = {@SecurityRequirement(name = "access_token")},
tags = {"bookmark"}
tags = {"๋ถ๋งˆํฌ"}
)
@ApiResponse(
responseCode = "204",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.rollthedice.backend.domain.bookmark.api;

import com.rollthedice.backend.domain.bookmark.dto.response.BookmarkResponse;
import com.rollthedice.backend.domain.bookmark.service.BookmarkService;
import com.rollthedice.backend.domain.news.dto.response.NewsResponse;
import lombok.RequiredArgsConstructor;
Expand All @@ -18,8 +19,15 @@ public class BookmarkController implements BookmarkApi {
@ResponseStatus(HttpStatus.OK)
@GetMapping("")
@Override
public List<NewsResponse> getBookmarked(final Pageable pageable) {
return bookmarkService.getBookmarkedNews(pageable);
public List<NewsResponse> getAllBookmarkedNews(final Pageable pageable) {
return bookmarkService.getAllBookmarkedNews(pageable);
}

@ResponseStatus(HttpStatus.OK)
@GetMapping("/{newsId}")
@Override
public BookmarkResponse getIsBookmarked(@PathVariable final Long newsId) {
return bookmarkService.getIsBookmarked(newsId);
}

@ResponseStatus(HttpStatus.CREATED)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.rollthedice.backend.domain.bookmark.dto.response;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class BookmarkResponse {
private Long id;
private Boolean isBookmarked;

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
@Repository
public interface BookmarkRepository extends JpaRepository<Bookmark, Long> {
Boolean existsBookmarkByMemberAndNews(Member member, News news);
Boolean existsBookmarkByMemberAndNewsId(Member member, Long newsId);

List<Bookmark> findAllByMemberOrderByCreatedAt(Member member, Pageable pageable);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.rollthedice.backend.domain.bookmark.service;

import com.rollthedice.backend.domain.bookmark.dto.response.BookmarkResponse;
import com.rollthedice.backend.domain.bookmark.entity.Bookmark;
import com.rollthedice.backend.domain.bookmark.repository.BookmarkRepository;
import com.rollthedice.backend.domain.member.entity.Member;
Expand All @@ -11,14 +12,15 @@
import com.rollthedice.backend.domain.news.repository.NewsRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
@RequiredArgsConstructor
public class BookmarkService {
private final AuthService authService;
private final BookmarkRepository bookmarkRepository;
Expand All @@ -31,7 +33,7 @@ public boolean isBookmarked(Member member, News news) {
}

@Transactional(readOnly = true)
public List<NewsResponse> getBookmarkedNews(Pageable pageable) {
public List<NewsResponse> getAllBookmarkedNews(Pageable pageable) {
Member member = authService.getMember();
return bookmarkRepository.findAllByMemberOrderByCreatedAt(member, pageable).stream()
.map(bookmark -> newsMapper.toResponse(bookmark.getNews(), true))
Expand All @@ -53,5 +55,14 @@ public void saveBookmark(Long newsId) {
public void deleteBookmark(Long newsId) {
bookmarkRepository.deleteByNewsId(newsId);
}

public BookmarkResponse getIsBookmarked(Long newsId) {
Member member = authService.getMember();
Boolean isBookmarked = bookmarkRepository.existsBookmarkByMemberAndNewsId(member, newsId);
return BookmarkResponse.builder()
.id(newsId)
.isBookmarked(isBookmarked)
.build();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@ public class NewsDetailResponse {
private String content;
private String thumbnailUrl;
private String postDate;
private Boolean isBookmarked;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
public class NewsResponse {
private Long id;
private String title;
private String content;
private String thumbnail;
private String postDate;
private Boolean isBookmarked;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ public interface NewsMapper {

NewsResponse toResponse(final News news, boolean isBookmarked);

NewsDetailResponse toDetailResponse(final News news, boolean isBookmarked);
NewsDetailResponse toDetailResponse(final News news);
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ public List<NewsResponse> getNews(final Pageable pageable) {
}

public NewsDetailResponse getDetailNews(Long newsId) {
Member member = authService.getMember();
final News news = newsRepository.findById(newsId).orElseThrow(NewsNotFoundException::new);
return newsMapper.toDetailResponse(news, bookmarkService.isBookmarked(member, news));
return newsMapper.toDetailResponse(news);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
@Configuration
public class SwaggerConfig {
private final String JWT = "JWT";
private final String BEARER = "Bearer";
private final String BEARER = "bearer";
private final String AUTHORIZATION = "Authorization";
private final String ACCESS_TOKEN = "access_token";


@Bean
public OpenAPI openAPI() {
Expand All @@ -28,21 +31,22 @@ public OpenAPI openAPI() {

private List<Server> getServers() {
return List.of(new Server()
.url("/api")
.description("๋ฐฑ์—”๋“œ api ์„œ๋ฒ„")
.url("/")
.description("๋ฐฑ์—”๋“œ api ์„œ๋ฒ„")
);
}

private Info getInfo() {
return new Info()
.title("Roll The Dice API")
.description("์š”์•ฝ๋œ ๋‰ด์Šค๋ฅผ AR๋กœ ์žฌ๋ฏธ์žˆ๊ฒŒ ์ฆ๊ธฐ๋Š” ์„œ๋น„์Šค")
.title("Scoop API")
.description("ChatGPT๊ธฐ๋ฐ˜, ์ƒ์„ฑํ˜• AI๋กœ ์š”์•ฝํ•œ ๋‰ด์Šค ํ•™์Šต ์–ดํ”Œ")
.version("demo");
}

private Components getComponents() {
return new Components().addSecuritySchemes(JWT, new SecurityScheme()
.name(JWT)
return new Components().addSecuritySchemes(ACCESS_TOKEN, new SecurityScheme()
.name(AUTHORIZATION)
.in(SecurityScheme.In.HEADER)
.type(SecurityScheme.Type.HTTP)
.scheme(BEARER)
.bearerFormat(JWT)
Expand Down
Loading