Skip to content

Commit

Permalink
Merge pull request #38 from Anifriends/feat/#34
Browse files Browse the repository at this point in the history
feat: λ΄‰μ‚¬μž νšŒμ›κ°€μž… λ‘œμ§μ„ κ΅¬ν˜„ν•œλ‹€.
  • Loading branch information
bjo6300 authored Oct 31, 2023
2 parents e935a65 + 5726562 commit 25e86a6
Show file tree
Hide file tree
Showing 26 changed files with 784 additions and 5 deletions.
51 changes: 46 additions & 5 deletions src/main/java/com/clova/anifriends/domain/volunteer/Volunteer.java
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

0 comments on commit 25e86a6

Please sign in to comment.