Skip to content

Commit

Permalink
BE: [feat] 크루 생성 CSID-DGU#83
Browse files Browse the repository at this point in the history
  • Loading branch information
Seoyoung2222 committed Nov 25, 2024
1 parent e71b245 commit af2bcb9
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 0 deletions.
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);
}
}
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("인증 사진을 입력해주세요.");
}
}
}
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> {

}
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> {
}
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);
}
}


0 comments on commit af2bcb9

Please sign in to comment.