-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from KwakDooChul/feat/10-create-apply
[Feat] 수강신청 기능 구현
- Loading branch information
Showing
7 changed files
with
148 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
doochul/src/main/java/org/doochul/application/MemberShipService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.doochul.application; | ||
|
||
import java.time.Duration; | ||
import lombok.RequiredArgsConstructor; | ||
import org.doochul.domain.membership.MemberShip; | ||
import org.doochul.domain.membership.MemberShipRepository; | ||
import org.doochul.domain.product.Product; | ||
import org.doochul.domain.product.ProductRepository; | ||
import org.doochul.domain.user.User; | ||
import org.doochul.domain.user.UserRepository; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class MemberShipService { | ||
|
||
private final MemberShipRepository memberShipRepository; | ||
private final ProductRepository productRepository; | ||
private final UserRepository userRepository; | ||
private final RedisService redisService; | ||
|
||
public Long save(final Long userId, final Long productId) { | ||
final Product product = productRepository.findById(productId).orElseThrow(); | ||
final User user = userRepository.findById(userId).orElseThrow(); | ||
|
||
final String key = Long.toString(userId); | ||
if (redisService.setNX(key, "apply", Duration.ofSeconds(5))) { | ||
final Long id = memberShipRepository.save(MemberShip.of(user, product, product.getCount())).getId(); | ||
redisService.delete(key); | ||
return id; | ||
} | ||
throw new IllegalArgumentException(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
doochul/src/main/java/org/doochul/ui/MemberShipController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,25 @@ | ||
package org.doochul.ui; | ||
|
||
import java.net.URI; | ||
import lombok.RequiredArgsConstructor; | ||
import org.doochul.application.MemberShipService; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/memberShip") | ||
public class MemberShipController { | ||
|
||
private final MemberShipService memberShipService; | ||
|
||
@PostMapping("/apply/{productId}") | ||
public ResponseEntity<Void> apply(@AuthenticationPrincipal final Long userId, @PathVariable final Long productId) { | ||
Long memberShipId = memberShipService.save(userId, productId); | ||
return ResponseEntity.created(URI.create("/memberShips" + memberShipId)).build(); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
doochul/src/test/java/org/doochul/application/MemberShipServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package org.doochul.application; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.BDDMockito.then; | ||
import static org.mockito.Mockito.times; | ||
|
||
import java.time.Duration; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import org.doochul.domain.membership.MemberShip; | ||
import org.doochul.domain.membership.MemberShipRepository; | ||
import org.doochul.domain.product.Product; | ||
import org.doochul.domain.product.ProductRepository; | ||
import org.doochul.domain.product.ProductType; | ||
import org.doochul.domain.user.Gender; | ||
import org.doochul.domain.user.Identity; | ||
import org.doochul.domain.user.User; | ||
import org.doochul.domain.user.UserRepository; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
|
||
@SpringBootTest | ||
public class MemberShipServiceTest { | ||
|
||
@MockBean | ||
private MemberShipRepository memberShipRepository; | ||
|
||
@Autowired | ||
private ProductRepository productRepository; | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
@Autowired | ||
private RedisService redisService; | ||
|
||
@Test | ||
void memberShip_save() throws InterruptedException { | ||
// Given | ||
User user = new User(2L, "JEON", "deviceToken1234", "password1234", Gender.MEN, Identity.GENERAL); | ||
User teacher = new User(1L, "Faker", "deviceToken123", "password123", Gender.MEN, Identity.TEACHER); | ||
|
||
userRepository.save(user); | ||
userRepository.save(teacher); | ||
|
||
Product product = new Product(1L, "페이커", ProductType.LOL, teacher, 10); | ||
productRepository.save(product); | ||
|
||
ExecutorService executorService = Executors.newFixedThreadPool(5); | ||
CountDownLatch latch = new CountDownLatch(5); | ||
|
||
given(memberShipRepository.save(any())).willReturn(new MemberShip(1L, user, product, 10)); | ||
|
||
for (int i = 0; i < 5; i++) { | ||
executorService.submit(() -> { | ||
try { | ||
final String key = Long.toString(user.getId()); | ||
if (redisService.setNX(key, "apply", Duration.ofSeconds(5))) { | ||
memberShipRepository.save(MemberShip.of(user, product, product.getCount())); | ||
redisService.delete(key); | ||
} | ||
} finally { | ||
latch.countDown(); | ||
} | ||
}); | ||
} | ||
|
||
latch.await(); | ||
executorService.shutdown(); | ||
|
||
then(memberShipRepository).should(times(1)).save(any()); | ||
} | ||
} |