Skip to content

Commit cfc6cc8

Browse files
authored
Merge pull request #51 from GTable/feat/#47_관리자페이지주문조회
feat(user): 슈퍼계정 생성
2 parents dda0a30 + e8351c0 commit cfc6cc8

64 files changed

Lines changed: 2436 additions & 9 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
name: 백엔드 이슈
3+
about: 백엔드와 관련된 이슈
4+
title: "[백엔드] "
5+
labels: Backend
6+
assignees: ''
7+
8+
---
9+
10+
# 이슈 내용
11+
12+
13+
# 작업 목록
14+
- [ ]
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.example.apiuser;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.autoconfigure.domain.EntityScan;
6+
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
7+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
8+
9+
@EnableJpaAuditing
10+
@SpringBootApplication(scanBasePackages = {
11+
"com.example.apiuser",
12+
"com.nowait.auth"
13+
})
14+
@EntityScan(basePackages = {
15+
"com.example.menu.entity", // domain-menu
16+
"com.example.domainstore.entity", // domain-store
17+
"com.example.domaintoken.entity",
18+
"com.nowaiting.user.entity",
19+
"com.nowait.domainbookmark.entity",
20+
"com.nowait.domainreservation.entity",
21+
"com.nowait.domainorder.entity",
22+
"com.nowait.domainorder.entity"
23+
})
24+
@EnableJpaRepositories(basePackages = {
25+
"com.example.menu.repository",
26+
"com.nowaiting.user.repository",
27+
"com.example.domainstore.repository",
28+
"com.example.domaintoken.repository",
29+
"com.nowait.domainbookmark.repository",
30+
"com.nowait.domainorder.repository",
31+
"com.nowait.domainreservation.repository"
32+
})
33+
public class ApiUserApplication {
34+
35+
public static void main(String[] args) {
36+
SpringApplication.run(ApiUserApplication.class, args);
37+
}
38+
39+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.example.apiuser.bookmark.controller;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
6+
import org.springframework.web.bind.annotation.DeleteMapping;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.PathVariable;
9+
import org.springframework.web.bind.annotation.PostMapping;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.bind.annotation.RestController;
12+
13+
import com.example.apiuser.bookmark.dto.BookmarkCreateResponse;
14+
import com.example.apiuser.bookmark.service.BookmarkService;
15+
import com.nowait.auth.dto.CustomOAuth2User;
16+
import com.nowaiting.common.api.ApiUtils;
17+
18+
import io.swagger.v3.oas.annotations.Operation;
19+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
20+
import io.swagger.v3.oas.annotations.tags.Tag;
21+
import lombok.RequiredArgsConstructor;
22+
@Tag(name = "Bookmark API", description = "북마크 API")
23+
@RestController
24+
@RequestMapping("/bookmarks")
25+
@RequiredArgsConstructor
26+
public class BookmarkController {
27+
private final BookmarkService bookmarkService;
28+
29+
@PostMapping("/{storeId}")
30+
@Operation(summary = "북마크 생성", description = "특정 주점에 대한 북마크 생성")
31+
@ApiResponse(responseCode = "201", description = "북마크 생성")
32+
public ResponseEntity<?> createBookmark(@PathVariable Long storeId,@AuthenticationPrincipal CustomOAuth2User customOAuth2User) {
33+
BookmarkCreateResponse response = bookmarkService.createBookmark(storeId,customOAuth2User);
34+
35+
return ResponseEntity
36+
.status(HttpStatus.CREATED)
37+
.body(
38+
ApiUtils.success(
39+
response
40+
)
41+
);
42+
}
43+
@GetMapping
44+
@Operation(summary = "북마크 조회", description = "내가 북마크한 주점 조회")
45+
@ApiResponse(responseCode = "200", description = "북마크 조회")
46+
public ResponseEntity<?> getAllBookmarks(@AuthenticationPrincipal CustomOAuth2User customOAuth2User) {
47+
return ResponseEntity
48+
.ok()
49+
.body(
50+
ApiUtils.success(
51+
bookmarkService.getBookmarks(customOAuth2User)
52+
)
53+
);
54+
}
55+
@DeleteMapping("/{bookmarkId}")
56+
@Operation(summary = "북마크 삭제", description = "특정 주점에 대한 북마크 삭제")
57+
@ApiResponse(responseCode = "200", description = "북마크 삭제")
58+
public ResponseEntity<?> deleteBookmark(@PathVariable Long bookmarkId, @AuthenticationPrincipal CustomOAuth2User customOAuth2User) {
59+
return ResponseEntity
60+
.ok()
61+
.body(
62+
ApiUtils.success(
63+
bookmarkService.deleteBookmark(bookmarkId,customOAuth2User)
64+
)
65+
);
66+
}
67+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.example.apiuser.bookmark.dto;
2+
3+
import com.nowait.domainbookmark.entity.Bookmark;
4+
5+
import lombok.AllArgsConstructor;
6+
import lombok.Builder;
7+
import lombok.Getter;
8+
9+
@Getter
10+
@AllArgsConstructor
11+
@Builder
12+
public class BookmarkCreateResponse {
13+
private Long bookmarkId;
14+
private Long userId;
15+
private Long storeId;
16+
17+
public static BookmarkCreateResponse fromEntity(Bookmark bookmark) {
18+
return BookmarkCreateResponse.builder()
19+
.bookmarkId(bookmark.getId())
20+
.userId(bookmark.getUser().getId())
21+
.storeId(bookmark.getStore().getStoreId())
22+
.build();
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.example.apiuser.bookmark.dto;
2+
3+
import com.nowait.domainbookmark.entity.Bookmark;
4+
5+
import lombok.AllArgsConstructor;
6+
import lombok.Builder;
7+
import lombok.Getter;
8+
9+
@Getter
10+
@AllArgsConstructor
11+
@Builder
12+
public class BookmarkGetResponse {
13+
private Long bookmarkId;
14+
private Long userId;
15+
private Long storeId;
16+
17+
public static BookmarkGetResponse fromEntity(Bookmark bookmark) {
18+
return BookmarkGetResponse.builder()
19+
.bookmarkId(bookmark.getId())
20+
.userId(bookmark.getUser().getId())
21+
.storeId(bookmark.getStore().getStoreId())
22+
.build();
23+
}
24+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.example.apiuser.bookmark.service;
2+
3+
import java.util.List;
4+
5+
import org.springframework.stereotype.Service;
6+
import org.springframework.transaction.annotation.Transactional;
7+
8+
import com.example.apiuser.bookmark.dto.BookmarkCreateResponse;
9+
import com.example.apiuser.bookmark.dto.BookmarkGetResponse;
10+
import com.example.domainstore.entity.Store;
11+
import com.example.domainstore.repository.StoreRepository;
12+
import com.nowait.auth.dto.CustomOAuth2User;
13+
import com.nowait.domainbookmark.entity.Bookmark;
14+
import com.nowait.domainbookmark.repository.BookmarkRepository;
15+
import com.nowaiting.user.entity.User;
16+
import com.nowaiting.user.repository.UserRepository;
17+
18+
import jakarta.persistence.EntityNotFoundException;
19+
import lombok.RequiredArgsConstructor;
20+
21+
@Service
22+
@RequiredArgsConstructor
23+
public class BookmarkService {
24+
private final BookmarkRepository bookmarkRepository;
25+
private final StoreRepository storeRepository;
26+
private final UserRepository userRepository;
27+
@Transactional
28+
public BookmarkCreateResponse createBookmark(Long storeId, CustomOAuth2User customOAuth2User) {
29+
parameterValidation(storeId, customOAuth2User);
30+
Store store = storeRepository.findById(storeId)
31+
.orElseThrow(() -> new EntityNotFoundException(storeId + " store not found."));
32+
User user = userRepository.findById(customOAuth2User.getUserId())
33+
.orElseThrow(() -> new EntityNotFoundException("User not found"));
34+
35+
if (bookmarkRepository.existsByUserAndStore(user, store)) {
36+
throw new IllegalArgumentException("already bookmarked");
37+
}
38+
39+
Bookmark bookmark = Bookmark.builder()
40+
.store(store)
41+
.user(user)
42+
.build();
43+
44+
return BookmarkCreateResponse.fromEntity(bookmarkRepository.save(bookmark));
45+
}
46+
47+
@Transactional(readOnly = true)
48+
public List<BookmarkGetResponse> getBookmarks(CustomOAuth2User customOAuth2User) {
49+
User user = userRepository.findById(customOAuth2User.getUserId())
50+
.orElseThrow(() -> new EntityNotFoundException("User not found"));
51+
return bookmarkRepository.findAllByUser(user)
52+
.stream()
53+
.map(BookmarkGetResponse::fromEntity)
54+
.toList();
55+
}
56+
57+
@Transactional
58+
public String deleteBookmark(Long bookmarkId, CustomOAuth2User customOAuth2User) {
59+
parameterValidation(bookmarkId, customOAuth2User);
60+
Bookmark bookmark = bookmarkRepository.findById(bookmarkId)
61+
.orElseThrow(() -> new EntityNotFoundException(bookmarkId + " bookmark not found."));
62+
if (bookmark.getUser().getId() != customOAuth2User.getUserId()) {
63+
throw new IllegalArgumentException("you can only delete your own bookmark");
64+
}
65+
bookmarkRepository.delete(bookmark);
66+
return "Bookmark ID " + bookmarkId + " deleted.";
67+
}
68+
69+
private static void parameterValidation(Long storeId, CustomOAuth2User customOAuth2User) {
70+
// 파라미터 유효성 검사
71+
if (storeId == null || storeId < 0) {
72+
throw new IllegalArgumentException("storeId must be a positive number");
73+
}
74+
if (customOAuth2User == null || customOAuth2User.getUserId() == null) {
75+
throw new IllegalArgumentException("UserInfo is required");
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)