Notice
Recent Posts
Recent Comments
Link
반응형
250x250
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- javaspring
- 자바스크립트 기초
- 전자정부 서버세팅
- 처음만나는자바스크립트
- spring
- java
- 스프링부트
- 마이바티스
- 기초코딩
- 웹앱
- 구글캘린더api
- 구글 oauth
- mybatis
- HTML
- 코딩
- 웹
- 자바스크립트기초문법
- js
- 리액트세팅
- 리액트초기세팅
- 자바스크립트
- CSS
- Spring Boot
- Javascript
- 자바스크립트기초
- 자바
- 리액트프로젝트세팅
- react
- 기초 코딩
- springboot
Archives
- Today
- Total
인생 디벨로퍼
[1단계] 기업 회원가입 본문
728x90
반응형
1. 컨트롤러
@RestController
@RequiredArgsConstructor
@RequestMapping("/api")
public class CompanyController {
private final CompanyService companyService;
@PostMapping("/joinCompany")
public ResponseEntity<?> joinCompany(@RequestBody CompanyRequest.JoinInDTO joinDTO) {
CompanyResponse.JoinDTO data = companyService.회원가입(joinDTO);
ResponseDto<?> responseDTO = new ResponseDto<>().data(data);
return ResponseEntity.ok().body(responseDTO);
}
}
2. Service
@Transactional
public CompanyResponse.JoinDTO 회원가입(CompanyRequest.JoinInDTO joinDTO) {
Optional<User> emailCheck = userRepository.findByEmail(joinDTO.getEmail());
if (emailCheck.isPresent()) {
throw new Exception400("동일한 email이 있습니다");
}
if (joinDTO.getPassword() == joinDTO.getPasswordCon()) {
throw new Exception400("비밀번호가 일치하지 않습니다");
}
String rawPassword = joinDTO.getPassword();
String encPassword = passwordEncoder.encode(rawPassword); // 60Byte
joinDTO.setPassword(encPassword);
joinDTO.setRole(RoleType.COMPANY.toString());
User userPS = userRepository.save(joinDTO.toEntity());
return new CompanyResponse.JoinDTO(userPS);
}
- 패스워드 encode
- role 적용
- status 디폴트
3. DTO
public class CompanyResponse {
@Getter
@Setter
@EqualsAndHashCode
@AllArgsConstructor
@NoArgsConstructor
public static class JoinDTO {
private String nickname;
private String email;
private String role;
private String createdAt; // 포맷해서 던짐 (util)
public JoinDTO(User user) {
this.nickname = user.getNickname();
this.email = user.getEmail();
this.role = user.getRole();
this.createdAt = MyDateUtils.toStringFormat(user.getCreatedAt());
}
}
}
4. Test
@WebMvcTest(CompanyController.class)
@MockBean(JpaMetamodelMappingContext.class)
public class CompanyTest {
@Autowired
MockMvc mvc;
@MockBean
private CompanyService companyService;
private final ObjectMapper objectMapper = new ObjectMapper();
@Test
@DisplayName("company 회원가입")
@WithMockUser(username = "ssar", roles = { "USER" })
void joinCompany() throws Exception {
// given
CompanyRequest.JoinInDTO joinInDTO = new CompanyRequest.JoinInDTO();
CompanyResponse.JoinDTO joinOutDTO = new CompanyResponse.JoinDTO("1234","1234","sdif@sdflk.dsf","컴퍼니");
given(this.companyService.회원가입(joinInDTO)).willReturn(joinOutDTO);
// When
ResultActions perform = this.mvc.perform(
post("/api/joinCompany")
.with(csrf())
.content(objectMapper.writeValueAsString(joinInDTO))
.accept(MediaType.APPLICATION_JSON_VALUE)
.contentType(MediaType.APPLICATION_JSON_VALUE));
String responseBody = perform.andReturn().getResponse().getContentAsString();
// Then
perform
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.email").value("1234"));
}
}
5. 결과 확인
{
"password":"1234",
"passwordCon":"1234",
"email":"jointest@naver.com"
}
{
"status": 200,
"msg": "성공",
"data": {
"nickname": null,
"email": "jointest@naver.com",
"role": "COMPANY",
"createdAt": "2023-04-30 15:27:30"
}
}
728x90
반응형
'Project > Final Project - Sporting (매칭)' 카테고리의 다른 글
[2단계] Admin page Court view (0) | 2023.05.01 |
---|---|
[1단계] Stadium Detail (0) | 2023.05.01 |
[1단계] Company Update Form (0) | 2023.05.01 |
[3단계] sentry.io 적용 (0) | 2023.05.01 |
[0단계] JPA Repository Test (0) | 2023.04.26 |