λ°μν
νμ κ°μ νλ©΄μΌλ‘ λΆν° λμ΄μ€λ κ°μ μ 보λ₯Ό λ΄μ DTO κ°μ²΄
@Setter
@NoArgsConstructor
public class MemberFormDto {
@NotBlank(message = "μ΄λ¦μ νμ μ
λ ₯ κ°μ
λλ€.")
private String name;
@NotEmpty(message = "μμ΄λλ νμ μ
λ ₯ κ°μ
λλ€.")
private String memberId;
@NotEmpty(message = "λΉλ°λ²νΈλ νμ μ
λ ₯ κ°μ
λλ€.")
@Length(min = 8, max = 16, message = "λΉλ°λ²νΈλ 8μ μ΄μ, 16μ μ΄νλ‘ μ
λ ₯ν΄μ£ΌμΈμ.")
private String password;
@NotEmpty(message = "μ£Όμλ νμ μ
λ ₯ κ°μ
λλ€.")
private int postCode;
private String address;
private String additionalAddress;
@Builder
public Member toEntity() {
return Member.builder()
.name(name)
.memberId(memberId)
.password(password)
.postCode(postCode)
.address(address)
.addtionalAddress(additionalAddress)
.build();
}
}
λΉλ ν¨ν΄μ μ¬μ©νκΈ° μν΄ Memberμλ μμ±μμ @Builder
μ μ©ν΄μ£ΌκΈ°
MemberService
@RequiredArgsConstructor
@Service
@Transactional
public class MemberService {
private final MemberRepository memberRepository;
public Long save(MemberFormDto memberFormDto) {
return memberRepository.save(memberFormDto.toEntity()).getId();
}
public Optional<Member> findById(Long id) {
return memberRepository.findById(id);
}
}
@RequiredArgsConstructor
λ‘ memberRepository DI
νμ κ°μ ν μ€νΈ
@SpringBootTest
@Transactional
@TestPropertySource(locations = "classpath:application-test.properties")
class MemberServiceTest {
@Autowired
MemberService memberService;
public MemberFormDto createMember() {
MemberFormDto memberFormDto = new MemberFormDto();
memberFormDto.setMemberId("test");
memberFormDto.setName("ν
μ€νΈ");
memberFormDto.setPassword("password");
memberFormDto.setPostCode(2222);
memberFormDto.setAddress("ν
μ€νΈ");
memberFormDto.setAdditionalAddress("ν
μ€νΈ");
return memberFormDto;
}
@Test
void νμ_κ°μ
_ν
μ€νΈ() {
MemberFormDto memberFormDto = createMember();
Long id = memberService.save(memberFormDto);
Optional<Member> savedMember = memberService.findById(id);
assertEquals(savedMember.get().getMemberId(), memberFormDto.getMemberId());
}
}
MemberController
- GET μμ²μΌλ‘ νμ κ°μ νμ΄μ§ λ겨μ€
- POST μμ³₯μΌλ‘ λμ΄μ¨ μ 보λ₯Ό MemberFormDto κ°μ²΄λ‘ λ°μ
μ λ ₯ν μμ΄λ μ€λ³΅ 체ν¬
join.js
function checkDuplicated() {
var memberId = $('.idInput').val();
var data = {memberId: memberId}
if(!memberId){
alert("μμ΄λλ₯Ό μ
λ ₯νμΈμ");
return;
}
$.ajax({
type: "post",
url: "/members/memberIdCheck",
data: data,
success: function (result) {
if (result != 'fail') {
$('.id_input_re_1').css("display", "inline-block");
$('.id_input_re_2').css("display", "none");
} else {
$('.id_input_re_1').css("display", "none");
$('.id_input_re_2').css("display", "inline-block");
}
},
});
}
MemberController
@RequestMapping(value = "/memberIdCheck", method = RequestMethod.POST)
@ResponseBody
public String memberIdCheck(String memberId) {
int result = memberService.memberIdCheck(memberId);
if(result != 0)
return "fail";
return "success";
}
πβοΈ μμ΄λ μ λ ₯ κ°μ λ겨 μ΄λ―Έ κ°μ μ¬λΆ μ‘°ν
μ£Όμ κ²μ
<script src="//t1.daumcdn.net/mapjsapi/bundle/postcode/prod/postcode.v2.js"></script>
function findAddress() {
new daum.Postcode({
oncomplete: function (data) {
var addr = ''; // μ£Όμ λ³μ
var extraAddr = ''; // μ°Έκ³ νλͺ© λ³μ
//μ¬μ©μκ° μ νν μ£Όμ νμ
μ λ°λΌ ν΄λΉ μ£Όμ κ°μ κ°μ Έμ΄
if (data.userSelectedType === 'R') {
addr = data.roadAddress;
} else {
addr = data.jibunAddress;
}
// μ¬μ©μκ° μ νν μ£Όμκ° λλ‘λͺ
νμ
μΌλ μ°Έκ³ νλͺ©μ μ‘°ν©νλ€.
if (data.userSelectedType === 'R') {
// λ²μ λλͺ
μ΄ μμ κ²½μ° μΆκ°νλ€. (λ²μ 리λ μ μΈ)
// λ²μ λμ κ²½μ° λ§μ§λ§ λ¬Έμκ° "λ/λ‘/κ°"λ‘ λλλ€.
if (data.bname !== '' && /[λ|λ‘|κ°]$/g.test(data.bname)) {
extraAddr += data.bname;
}
// 건물λͺ
μ΄ μκ³ , 곡λμ£ΌνμΌ κ²½μ° μΆκ°νλ€.
if (data.buildingName !== '' && data.apartment === 'Y') {
extraAddr += (extraAddr !== '' ? ', ' + data.buildingName : data.buildingName);
}
// νμν μ°Έκ³ νλͺ©μ΄ μμ κ²½μ°, κ΄νΈκΉμ§ μΆκ°ν μ΅μ’
λ¬Έμμ΄μ λ§λ λ€.
if (extraAddr !== '') {
extraAddr = ' (' + extraAddr + ')';
}
addr += extraAddr;
} else {
addr += ' ';
}
$(".address_input_1").val(data.zonecode);
$(".address_input_2").val(addr);
$(".address_input_3").attr("readonly", false);
$(".address_input_3").focus();
}
}).open();
}
π λ€μμ API μ¬μ©
κ°μ μλ£
function join() {
var data = {
name: $('.nameInput').val(),
memberId: $('.idInput').val(),
password: $('.passwordInput').val(),
postCode: $('.address_input_1').val(),
address: $('.address_input_2').val(),
additionalAddress: $('.address_input_3').val()
};
if (!data.name || !data.memberId || !data.password || !data.postCode || !data.address || !data.additionalAddress) {
alert('λͺ¨λ νλͺ©μ μ±μμΌν©λλ€.');
return;
}
if (data.password.length < 8 || 16 < data.password.length) {
alert('λΉλ° λ²νΈλ 8μ μ΄μ, 16μ μ΄νμ¬μΌ ν©λλ€.');
return;
}
if (!isDuplicatedCheck) {
$.ajax({
type: "post",
url: "/members/new",
dataType: 'json',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(data)
}).done(function () {
console.log(data);
alert('νμκ°μ
μ΄ μλ£λμμ΅λλ€.');
window.location.href = '/';
}).fail(function (error) {
console.log(data);
alert(JSON.stringify(error));
});
} else {
alert("μ€λ³΅λ μμ΄λκ° μ‘΄μ¬ν©λλ€.");
}
}
π λΉ μΉΈμ΄ μκ±°λ, μμ΄λκ° μ€λ³΅μ΄κ±°λ, λΉλ°λ²νΈκ° 8μμ΄μ 16μμ΄νκ° μλλΌλ©΄, κ°μ λμ§ μμ!
728x90
λ°μν
'μ€νλ§ > μΌνλͺ° νλ‘μ νΈ' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
νμ΄μ§ κΆν μ€μ νκΈ° (0) | 2022.11.24 |
---|---|
μν μ€κ³ λ° λ±λ‘νκΈ° (0) | 2022.11.24 |
λΉλ ν¨ν΄ (0) | 2022.11.21 |
μ€νλ§ μν리ν°λ₯Ό μ΄μ©ν λ‘κ·ΈμΈ/λ‘κ·Έμμ (0) | 2022.11.21 |
Thymeleaf - κ³΅ν΅ λ μ΄μμ (0) | 2022.11.21 |