인생 디벨로퍼

[1단계] 기업 회원가입 본문

Project/Final Project - Sporting (매칭)

[1단계] 기업 회원가입

뫄뫙뫄 2023. 4. 26. 15:29
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