-
Notifications
You must be signed in to change notification settings - Fork 3
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 #91 from Seoyoung2222/backend/feature/crew
BE: [feat] 크루 생성 기능 추가 #83
- Loading branch information
Showing
5 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
...ckend/src/main/java/RunningMachines/R2R/domain/crew/common/controller/CrewController.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,27 @@ | ||
package RunningMachines.R2R.domain.crew.common.controller; | ||
|
||
import RunningMachines.R2R.domain.crew.common.dto.CrewCreateCommandDto; | ||
import RunningMachines.R2R.domain.crew.common.service.CrewCommandService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
@RestController | ||
@RequestMapping("/crew") | ||
@RequiredArgsConstructor | ||
public class CrewController { | ||
private final CrewCommandService crewCommandService; | ||
|
||
@PostMapping("/create") | ||
public ResponseEntity<Integer> createCrew(@RequestParam String title, @RequestPart MultipartFile certificationImage) { | ||
CrewCreateCommandDto crewCreateCommandDto = CrewCreateCommandDto.builder() | ||
.title(title) | ||
.certificationImage(certificationImage) | ||
.build(); | ||
|
||
Integer crewPasscode = crewCommandService.createCrew(crewCreateCommandDto); | ||
|
||
return ResponseEntity.ok(crewPasscode); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...ackend/src/main/java/RunningMachines/R2R/domain/crew/common/dto/CrewCreateCommandDto.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,22 @@ | ||
package RunningMachines.R2R.domain.crew.common.dto; | ||
|
||
import lombok.*; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
@Getter | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class CrewCreateCommandDto { | ||
private String title; | ||
private MultipartFile certificationImage; | ||
|
||
public void validate() { | ||
if (title==null || title.isEmpty()) { | ||
throw new IllegalArgumentException("크루명을 입력해주세요"); | ||
} | ||
if (certificationImage == null || certificationImage.isEmpty()) { | ||
throw new IllegalArgumentException("인증 사진을 입력해주세요."); | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...ckend/src/main/java/RunningMachines/R2R/domain/crew/common/repository/CrewRepository.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,8 @@ | ||
package RunningMachines.R2R.domain.crew.common.repository; | ||
|
||
import RunningMachines.R2R.domain.crew.common.entity.Crew; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface CrewRepository extends JpaRepository<Crew, Long> { | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
...d/src/main/java/RunningMachines/R2R/domain/crew/common/repository/CrewUserRepository.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,7 @@ | ||
package RunningMachines.R2R.domain.crew.common.repository; | ||
|
||
import RunningMachines.R2R.domain.crew.common.entity.CrewUser; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface CrewUserRepository extends JpaRepository<CrewUser, Integer> { | ||
} |
58 changes: 58 additions & 0 deletions
58
...kend/src/main/java/RunningMachines/R2R/domain/crew/common/service/CrewCommandService.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,58 @@ | ||
package RunningMachines.R2R.domain.crew.common.service; | ||
|
||
import RunningMachines.R2R.domain.crew.common.dto.CrewCreateCommandDto; | ||
import RunningMachines.R2R.domain.crew.common.entity.Crew; | ||
import RunningMachines.R2R.domain.crew.common.entity.CrewRole; | ||
import RunningMachines.R2R.domain.crew.common.entity.CrewUser; | ||
import RunningMachines.R2R.domain.crew.common.repository.CrewRepository; | ||
import RunningMachines.R2R.domain.crew.common.repository.CrewUserRepository; | ||
import RunningMachines.R2R.domain.user.entity.User; | ||
import RunningMachines.R2R.domain.user.service.AuthCommandService; | ||
import RunningMachines.R2R.global.s3.S3Provider; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.Random; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CrewCommandService { | ||
private final CrewRepository crewRepository; | ||
private final S3Provider s3Provider; | ||
private final AuthCommandService authCommandService; | ||
private final CrewUserRepository crewUserRepository; | ||
|
||
@Transactional | ||
public int createCrew(CrewCreateCommandDto crewCreateCommandDto) { | ||
// TODO - 관리자의 크루 생성 허용 | ||
crewCreateCommandDto.validate(); | ||
|
||
User currentUser = authCommandService.getCurrentUser(); | ||
int passcode = generateRandomPasscode(); | ||
|
||
// Crew 생성 | ||
Crew crew = Crew.builder() | ||
.title(crewCreateCommandDto.getTitle()) | ||
.passcode(passcode) | ||
.build(); | ||
crewRepository.save(crew); | ||
|
||
// 생성자 크루원 가입 | ||
CrewUser crewUser = CrewUser.builder() | ||
.role(CrewRole.LEADER) | ||
.user(currentUser) | ||
.crew(crew) | ||
.build(); | ||
crewUserRepository.save(crewUser); | ||
|
||
return crew.getPasscode(); | ||
} | ||
|
||
private int generateRandomPasscode() { | ||
Random random = new Random(); | ||
return 1000 + random.nextInt(9000); | ||
} | ||
} | ||
|
||
|