Skip to content

Commit

Permalink
Merge pull request #43 from team-REDDI/dev
Browse files Browse the repository at this point in the history
dev to main
  • Loading branch information
itsme-shawn authored Jan 31, 2024
2 parents eb86769 + b352888 commit be695ad
Show file tree
Hide file tree
Showing 17 changed files with 225 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class GoogleOAuth {
private final String GOOGLE_TOKEN_REQUEST_URL = "https://oauth2.googleapis.com/token";
private final String GOOGLE_USERINFO_REQUEST_URL = "https://www.googleapis.com/oauth2/v3/userinfo";

@Value("${spring.security.oauth2.client.registration.google.redirectUri}")
private String REDIRECT_URI;
// @Value("${spring.security.oauth2.client.registration.google.redirectUri}")
// private String REDIRECT_URI;

@Value("${spring.security.oauth2.client.registration.google.client-id}")
private String CLIENT_ID;
Expand All @@ -31,12 +31,12 @@ public class GoogleOAuth {

private final WebClient webClient;

public GoogleTokenResponseDto requestToken(String code) {
public GoogleTokenResponseDto requestToken(String code, String redirectUri) {
Map<String, Object> params = Map.of(
"code", code,
"client_id", CLIENT_ID,
"client_secret", CLIENT_SECRET,
"redirect_uri", REDIRECT_URI,
"redirect_uri", redirectUri,
"grant_type", "authorization_code"
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public class OAuthService {
private final JwtTokenProvider jwtTokenProvider;

@Transactional
public LoginResponseDto login(String code) {
GoogleTokenResponseDto googleToken = googleOAuth.requestToken(code);
public LoginResponseDto login(String code, String redirectUri) {
GoogleTokenResponseDto googleToken = googleOAuth.requestToken(code, redirectUri);
System.out.println("googleToken = " + googleToken);

// 구글토큰으로 구글 유저 정보 조회
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
// .formLogin(formLogin -> formLogin.disable())
.authorizeHttpRequests((authorize) -> authorize
.requestMatchers("/static/**", "/resources/**", "/css/**", "/js/**", "/images/**").authenticated()
.requestMatchers(HttpMethod.GET, "/**").permitAll()
.requestMatchers("/**").permitAll()
.requestMatchers(HttpMethod.GET, "/swagger-ui/**").permitAll()
.requestMatchers(HttpMethod.GET, "/v3/api-docs/**").permitAll()
.anyRequest().authenticated()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.example.reddiserver.auth.service.OAuthService;
import com.example.reddiserver.service.MemberService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
Expand All @@ -27,19 +28,22 @@ public class AuthController {


@Operation(summary = "구글 로그인 링크")
@Parameter(name = "redirectUri", description = "http://localhost:3000/auth/google/callback , https://reddi.kr/auth/google/callback 중 하나", required = true)
@GetMapping("/google-auth-url")
public Map<String, String> getGoogleAuthUrl() throws Exception {
public Map<String, String> getGoogleAuthUrl(@RequestParam String redirectUri) throws Exception {

Map<String, String> urlMap = new HashMap<>();
urlMap.put("url", googleOAuth.getGOOGLE_LOGIN_URL() + "?redirect_uri=" + googleOAuth.getREDIRECT_URI() + "&response_type=code&client_id=" + googleOAuth.getCLIENT_ID() + "&scope=profile email");
// urlMap.put("url", googleOAuth.getGOOGLE_LOGIN_URL() + "?redirect_uri=" + googleOAuth.getREDIRECT_URI() + "&response_type=code&client_id=" + googleOAuth.getCLIENT_ID() + "&scope=profile email");
urlMap.put("url", googleOAuth.getGOOGLE_LOGIN_URL() + "?redirect_uri=" + redirectUri + "&response_type=code&client_id=" + googleOAuth.getCLIENT_ID() + "&scope=profile email");
return urlMap;
}

// code 를 받아서 Google 로 부터 access token 을 발급 받고, 이를 이용해 회원가입 및 로그인을 진행
@PostMapping("/google/login")
public ApiResponse<LoginResponseDto> googleLogin(@RequestBody GoogleLoginRequestDto googleLoginRequestDto) throws Exception {
String code = googleLoginRequestDto.getCode();
LoginResponseDto loginResponse = oAuthService.login(code);
String redirectUri = googleLoginRequestDto.getRedirectUri();
LoginResponseDto loginResponse = oAuthService.login(code, redirectUri);

return ApiResponse.successResponse(loginResponse);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package com.example.reddiserver.controller;

import com.example.reddiserver.auth.service.OAuthService;
import com.example.reddiserver.common.ApiResponse;
import com.example.reddiserver.dto.brand.response.BrandContentsResponseDto;
import com.example.reddiserver.dto.brand.response.BrandResponseDto;
import com.example.reddiserver.repository.BrandRepository;
import com.example.reddiserver.service.BrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
Expand All @@ -24,7 +23,7 @@
public class BrandController {

private final BrandService brandService;
private final BrandRepository brandRepository;
private final OAuthService oAuthService;

@Operation(summary = "브랜드 리스트 조회")
@GetMapping("/")
Expand Down Expand Up @@ -59,4 +58,31 @@ public ApiResponse<List<BrandResponseDto>> getTopNBrands(@RequestParam(defaultVa
List<BrandResponseDto> topNBrands = brandService.getTopNBrands(n);
return ApiResponse.successResponse(topNBrands);
}

@SecurityRequirement(name = "Authorization") // 인증 필요한 엔드포인트에 설정
@Operation(summary = "브랜드 북마크 토글 (북마크 추가/삭제)")
@PutMapping("/bookmark/toggle")
public ApiResponse<HashMap<String, Boolean>> toggleBookmarkBrand(@RequestParam Long brandId) {

Long memberId = oAuthService.getUserId();

boolean isBookmarked = brandService.toggleBookmarkBrand(memberId, brandId);

HashMap<String, Boolean> response = new HashMap<>();
response.put("is_bookmarked", isBookmarked);

return ApiResponse.successResponse(response);
}

@SecurityRequirement(name = "Authorization") // 인증 필요한 엔드포인트에 설정
@Operation(summary = "유저의 브랜드 북마크 조회")
@GetMapping("/bookmark")
public ApiResponse<List<BrandResponseDto>> getBookmarkBrands() {

Long memberId = oAuthService.getUserId();

List<BrandResponseDto> bookmarkBrands = brandService.getBookmarkBrandList(memberId);

return ApiResponse.successResponse(bookmarkBrands);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.reddiserver.controller;

import com.example.reddiserver.auth.service.OAuthService;
import com.example.reddiserver.common.ApiResponse;
import com.example.reddiserver.dto.brand.response.BrandResponseDto;
import com.example.reddiserver.dto.post.response.HomeCuratingPostDto;
Expand All @@ -10,15 +11,13 @@
import com.example.reddiserver.service.PostService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -32,6 +31,7 @@
public class PostController {
private final PostService postService;
private final NotionService notionService;
private final OAuthService oAuthService;

@Operation(summary = "포스트(마케팅) 리스트 조회")
@GetMapping("/")
Expand Down Expand Up @@ -100,4 +100,31 @@ public ApiResponse<List<PostResponseDto>> getTopNPosts(@RequestParam(defaultValu
List<PostResponseDto> topNPosts = postService.getTopNPosts(n);
return ApiResponse.successResponse(topNPosts);
}

@SecurityRequirement(name = "Authorization") // 인증 필요한 엔드포인트에 설정
@Operation(summary = "마케팅(포스트) 북마크 토글 (북마크 추가/삭제)")
@PutMapping("/bookmark/toggle")
public ApiResponse<HashMap<String, Boolean>> toggleBookmarkPost(@RequestParam Long postId) {

Long memberId = oAuthService.getUserId();

boolean isBookmarked = postService.toggleBookmarkPost(memberId, postId);

HashMap<String, Boolean> response = new HashMap<>();
response.put("is_bookmarked", isBookmarked);

return ApiResponse.successResponse(response);
}

@SecurityRequirement(name = "Authorization") // 인증 필요한 엔드포인트에 설정
@Operation(summary = "유저의 마케팅(포스트) 북마크 조회")
@GetMapping("/bookmark")
public ApiResponse<List<PostResponseDto>> getBookmarkBrands() {

Long memberId = oAuthService.getUserId();

List<PostResponseDto> bookmarkPosts = postService.getBookmarkPostList(memberId);

return ApiResponse.successResponse(bookmarkPosts);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
@Setter
public class GoogleLoginRequestDto {
private String code;
private String redirectUri;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,25 @@
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "bookmarks")
@Table(name = "bookmark_brands")
@DynamicInsert
@DynamicUpdate
public class Bookmark extends BaseTimeEntity {
public class BookmarkBrand extends BaseTimeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@OneToMany(mappedBy = "bookmark", cascade = CascadeType.ALL)
private List<BookmarkPost> bookmarkPosts = new ArrayList<>();

@ManyToOne(fetch = FetchType.LAZY) // 연관관계 주인
@JoinColumn(name = "member_id")
private Member member;

@Column(nullable = false)
private String title;
@ManyToOne(fetch = FetchType.LAZY) // 연관관계 주인
@JoinColumn(name = "brand_id")
private Brand brand;

@Builder
public Bookmark(Member member, String title) {
public BookmarkBrand(Member member, Brand brand) {
this.member = member;
this.title = title;
this.brand = brand;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ public class BookmarkPost extends BaseTimeEntity {
private Long id;

@ManyToOne(fetch = FetchType.LAZY) // 연관관계 주인
@JoinColumn(name = "bookmark_id")
private Bookmark bookmark;
@JoinColumn(name = "member_id")
private Member member;

@ManyToOne(fetch = FetchType.LAZY) // 연관관계 주인
@JoinColumn(name = "post_id")
private Post post;

@Builder
public BookmarkPost(Bookmark bookmark, Post post) {
this.bookmark = bookmark;
public BookmarkPost(Member member, Post post) {
this.member = member;
this.post = post;
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/example/reddiserver/entity/Keyword.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.reddiserver.entity;

import com.example.reddiserver.entity.base.BaseTimeEntity;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
Expand All @@ -10,7 +11,7 @@
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "keywords")
public class Keyword {
public class Keyword extends BaseTimeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/example/reddiserver/entity/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ public class Member extends BaseTimeEntity {
private Long id;

@OneToMany(mappedBy = "member", cascade = CascadeType.ALL)
private List<Bookmark> bookmarks = new ArrayList<>();
private List<BookmarkBrand> bookmarkBrands = new ArrayList<>();

@OneToMany(mappedBy = "member", cascade = CascadeType.ALL)
private List<BookmarkPost> bookmarkPosts = new ArrayList<>();

@Column(nullable = false, unique = true)
private String providerId;
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/com/example/reddiserver/entity/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ public class Post extends BaseTimeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@OneToMany(mappedBy = "post", cascade = CascadeType.ALL)
private List<BookmarkPost> bookmarkPosts = new ArrayList<>();


@OneToMany(mappedBy = "post", cascade = CascadeType.ALL)
private List<PostTag> postTags = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.reddiserver.repository;

import com.example.reddiserver.entity.BookmarkBrand;
import com.example.reddiserver.entity.Brand;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface BookmarkBrandRepository extends JpaRepository<BookmarkBrand, Long> {

List<BookmarkBrand> findByMemberId(Long memberId);

BookmarkBrand findByMemberIdAndBrandId(Long memberId, Long brandId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.reddiserver.repository;

import com.example.reddiserver.entity.BookmarkBrand;
import com.example.reddiserver.entity.BookmarkPost;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface BookmarkPostRepository extends JpaRepository<BookmarkPost, Long> {

List<BookmarkPost> findByMemberId(Long memberId);

BookmarkPost findByMemberIdAndPostId(Long memberId, Long postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ public interface BrandRepository extends JpaRepository<Brand, Long> {

@Query("SELECT b FROM Brand b WHERE b.name like :keyword OR b.content like :keyword")
Page<Brand> findByNameContainingOrContentContaining(String keyword, Pageable pageable);

}
Loading

0 comments on commit be695ad

Please sign in to comment.