Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 봉사자 회원가입 로직을 구현한다. #38

Merged
merged 13 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.clova.anifriends.domain.volunteer;

import com.clova.anifriends.domain.common.BaseTimeEntity;
import com.clova.anifriends.domain.volunteer.exception.VolunteerBadRequestException;
import com.clova.anifriends.domain.volunteer.wrapper.VolunteerEmail;
import com.clova.anifriends.domain.volunteer.wrapper.VolunteerGender;
import com.clova.anifriends.domain.volunteer.wrapper.VolunteerName;
Expand All @@ -17,6 +18,7 @@
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import java.time.LocalDate;
import java.time.format.DateTimeParseException;

@Entity
@Table(name = "volunteer")
Expand Down Expand Up @@ -55,18 +57,57 @@ protected Volunteer() {
public Volunteer(
String email,
String password,
LocalDate birthDate,
String birthDate,
String phoneNumber,
String gender,
int temperature,
String name
) {
this.email = new VolunteerEmail(email);
this.password = new VolunteerPassword(password);
this.birthDate = birthDate;
this.birthDate = validateBirthDate(birthDate);
this.phoneNumber = new VolunteerPhoneNumber(phoneNumber);
this.gender = VolunteerGender.valueOf(gender);
this.temperature = new VolunteerTemperature(temperature);
this.gender = VolunteerGender.from(gender);
this.temperature = new VolunteerTemperature(36);
this.name = new VolunteerName(name);
}

private LocalDate validateBirthDate(String birthDate) {
try {
return LocalDate.parse(birthDate);
} catch (DateTimeParseException e) {
throw new VolunteerBadRequestException("생년월일 형식이 맞지 않습니다.");
}
}

public Long getVolunteerId() {
return volunteerId;
}

public String getEmail() {
return this.email.getEmail();
}

public String getPassword() {
return this.password.getPassword();
}

public LocalDate getBirthDate() {
return birthDate;
}

public String getPhoneNumber() {
return this.phoneNumber.getPhoneNumber();
}

public String getGender() {
return this.gender.getName();
}

public Integer getTemperature() {
return this.temperature.getTemperature();
}

public String getName() {
return this.name.getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.Getter;

@Entity
@Getter
@Table(name = "voulunteer_image")
public class VolunteerImage extends BaseTimeEntity {

Expand All @@ -27,4 +29,7 @@ public class VolunteerImage extends BaseTimeEntity {
@Column(name = "image_url")
private String imageUrl;

protected VolunteerImage() {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.clova.anifriends.domain.volunteer.controller;

import com.clova.anifriends.domain.volunteer.dto.request.RegisterVolunteerRequest;
import com.clova.anifriends.domain.volunteer.service.VolunteerService;
import jakarta.validation.Valid;
import java.net.URI;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/volunteers")
public class VolunteerController {

private final VolunteerService volunteerService;
private static final String BASE_URI = "/api/volunteers/";

@PostMapping
public ResponseEntity<Void> registerVolunteer(
@RequestBody @Valid RegisterVolunteerRequest registerVolunteerRequest
) {
Long registeredVolunteerID = volunteerService.registerVolunteer(registerVolunteerRequest);
URI location = URI.create(BASE_URI + registeredVolunteerID);
return ResponseEntity.created(location).build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.clova.anifriends.domain.volunteer.dto.request;

import jakarta.validation.constraints.NotBlank;

public record RegisterVolunteerRequest(
@NotBlank(message = "이메일은 필수 항목입니다.") String email,
@NotBlank(message = "비밀번호는 필수 항목입니다.") String password,
@NotBlank(message = "이름은 필수 항목입니다.") String name,
@NotBlank(message = "생년월일은 필수 항목입니다.") String birthDate,
@NotBlank(message = "전화번호는 필수 항목입니다.") String phoneNumber,
@NotBlank(message = "성별은 필수 항목입니다.") String gender
) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.clova.anifriends.domain.volunteer.exception;

import com.clova.anifriends.global.exception.NotFoundException;

public class NotFoundVolunteerGenderException extends NotFoundException {

public NotFoundVolunteerGenderException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.clova.anifriends.domain.volunteer.exception;

import com.clova.anifriends.global.exception.BadRequestException;

public class VolunteerBadRequestException extends BadRequestException {

public VolunteerBadRequestException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.clova.anifriends.domain.volunteer.repository;

import com.clova.anifriends.domain.volunteer.VolunteerImage;
import org.springframework.data.jpa.repository.JpaRepository;

public interface VolunteerImageRepository extends JpaRepository<VolunteerImage, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.clova.anifriends.domain.volunteer.service;

import com.clova.anifriends.domain.volunteer.Volunteer;
import com.clova.anifriends.domain.volunteer.dto.request.RegisterVolunteerRequest;
import com.clova.anifriends.domain.volunteer.repository.VolunteerRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class VolunteerService {

private final VolunteerRepository volunteerRepository;

@Transactional
public Long registerVolunteer(RegisterVolunteerRequest registerVolunteerRequest) {
Volunteer volunteer = new Volunteer(
registerVolunteerRequest.email(),
registerVolunteerRequest.password(),
registerVolunteerRequest.birthDate(),
registerVolunteerRequest.phoneNumber(),
registerVolunteerRequest.gender(),
registerVolunteerRequest.name()
);

volunteerRepository.save(volunteer);
return volunteer.getVolunteerId();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.clova.anifriends.domain.volunteer.wrapper;

import com.clova.anifriends.domain.volunteer.exception.VolunteerBadRequestException;
import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import lombok.Getter;

@Getter
@Embeddable
public class VolunteerEmail {

Expand All @@ -13,7 +18,23 @@ protected VolunteerEmail() {
}

public VolunteerEmail(String value) {
validateVolunteerEmail(value);
this.email = value;
}

private void validateVolunteerEmail(String email) {
if (email == null || email.isBlank()) {
throw new VolunteerBadRequestException("이메일은 필수 항목입니다.");
}

String pattern = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";

Pattern emailPattern = Pattern.compile(pattern);
Matcher matcher = emailPattern.matcher(email);

if (!matcher.matches()) {
throw new VolunteerBadRequestException("이메일 형식에 맞지 않습니다.");
}
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.clova.anifriends.domain.volunteer.wrapper;

import com.clova.anifriends.EnumType;
import com.clova.anifriends.domain.volunteer.exception.NotFoundVolunteerGenderException;
import java.util.Arrays;

public enum VolunteerGender implements EnumType {

Expand All @@ -17,4 +19,11 @@ public String getName() {
public String getValue() {
return this.name();
}

public static VolunteerGender from(String gender) {
return Arrays.stream(VolunteerGender.values())
.filter(volunteerGender -> volunteerGender.name().equalsIgnoreCase(gender))
.findAny()
.orElseThrow(() -> new NotFoundVolunteerGenderException("존재하지 않는 성별입니다."));
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
package com.clova.anifriends.domain.volunteer.wrapper;

import com.clova.anifriends.domain.volunteer.exception.VolunteerBadRequestException;
import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import lombok.Getter;

@Getter
@Embeddable
public class VolunteerName {

private static final int MAX_VOLUNTEER_NAME_LENGTH = 10;

@Column(name = "name")
private String name;

protected VolunteerName() {
}

public VolunteerName(String value) {
validateVolunteerName(value);
this.name = value;
}

private void validateVolunteerName(String name) {
if (name == null || name.isBlank()) {
throw new VolunteerBadRequestException("이름은 필수 항목입니다.");
}

if (name.length() > MAX_VOLUNTEER_NAME_LENGTH) {
throw new VolunteerBadRequestException("이름은 최대 10자입니다.");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
package com.clova.anifriends.domain.volunteer.wrapper;

import com.clova.anifriends.domain.volunteer.exception.VolunteerBadRequestException;
import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import lombok.Getter;

@Getter
@Embeddable
public class VolunteerPassword {

private static final int MIN_PASSWORD_LENGTH = 6;
private static final int MAX_PASSWORD_LENGTH = 16;

@Column(name = "password")
private String password;

protected VolunteerPassword() {
}

public VolunteerPassword(String value) {
validateVolunteerPassword(value);
this.password = value;
}

private void validateVolunteerPassword(String password) {
if (password.length() < MIN_PASSWORD_LENGTH || password.length() > MAX_PASSWORD_LENGTH) {
throw new VolunteerBadRequestException("비밀번호는 최소 6자, 최대 16자입니다.");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
package com.clova.anifriends.domain.volunteer.wrapper;

import com.clova.anifriends.domain.volunteer.exception.VolunteerBadRequestException;
import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import lombok.Getter;

@Getter
@Embeddable
public class VolunteerPhoneNumber {

private static final int MIN_PHONE_NUMBER_LENGTH = 9;
private static final int MAX_PHONE_NUMBER_LENGTH = 11;

@Column(name = "phone_number")
private String phoneNumber;

protected VolunteerPhoneNumber() {
}

public VolunteerPhoneNumber(String value) {
validateVolunteerPhoneNumber(value);
this.phoneNumber = value;
}

private void validateVolunteerPhoneNumber(String phoneNumber) {
if (phoneNumber.length() < MIN_PHONE_NUMBER_LENGTH
|| phoneNumber.length() > MAX_PHONE_NUMBER_LENGTH) {
throw new VolunteerBadRequestException("전화번호는 최소 9자, 최대 11자입니다.");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
package com.clova.anifriends.domain.volunteer.wrapper;

import com.clova.anifriends.domain.volunteer.exception.VolunteerBadRequestException;
import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import lombok.Getter;

@Getter
@Embeddable
public class VolunteerTemperature {

private static final int MAX_VOLUNTEER_TEMPERATURE = 99;

@Column(name = "temperature")
private Integer temperature;

protected VolunteerTemperature() {
}

public VolunteerTemperature(int value) {
validateVolunteerTemperature(value);
this.temperature = value;
}

private void validateVolunteerTemperature(int temperature) {
if (temperature > MAX_VOLUNTEER_TEMPERATURE) {
throw new VolunteerBadRequestException("봉사자 체온은 99도 이하입니다.");
}
}
}
Loading
Loading