Skip to content

Commit fd447f3

Browse files
authored
Merge pull request #62 from 9oormthon-univ/feat/partner-update
Feat: 파트너 정보 수정 구현
2 parents 87db6ed + a055ab4 commit fd447f3

File tree

5 files changed

+159
-0
lines changed

5 files changed

+159
-0
lines changed

src/main/java/com/trashheroesbe/feature/partner/api/PartnerController.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44

55
import com.trashheroesbe.feature.partner.application.PartnerService;
66
import com.trashheroesbe.feature.partner.dto.request.RegisterPartnerRequest;
7+
import com.trashheroesbe.feature.partner.dto.request.UpdatePartnerRequest;
78
import com.trashheroesbe.feature.partner.dto.response.RegisterPartnerResponse;
9+
import com.trashheroesbe.global.auth.security.CustomerDetails;
810
import com.trashheroesbe.global.response.ApiResponse;
911
import jakarta.validation.Valid;
1012
import lombok.RequiredArgsConstructor;
1113
import org.springframework.http.MediaType;
14+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
15+
import org.springframework.web.bind.annotation.PatchMapping;
1216
import org.springframework.web.bind.annotation.PostMapping;
17+
import org.springframework.web.bind.annotation.RequestBody;
1318
import org.springframework.web.bind.annotation.RequestMapping;
1419
import org.springframework.web.bind.annotation.RequestPart;
1520
import org.springframework.web.bind.annotation.RestController;
@@ -31,4 +36,15 @@ public ApiResponse<RegisterPartnerResponse> registerPartner(
3136
RegisterPartnerResponse response = partnerService.registerPartner(request, image);
3237
return ApiResponse.success(OK, response);
3338
}
39+
40+
@Override
41+
@PatchMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
42+
public ApiResponse<Void> updatePartner(
43+
@RequestPart(value = "metadata") @Valid UpdatePartnerRequest request,
44+
@RequestPart(value = "image", required = false) MultipartFile image,
45+
@AuthenticationPrincipal CustomerDetails customerDetails
46+
) {
47+
partnerService.updatePartner(request, image, customerDetails.getUser());
48+
return ApiResponse.success(OK);
49+
}
3450
}

src/main/java/com/trashheroesbe/feature/partner/api/PartnerControllerApi.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.trashheroesbe.feature.partner.api;
22

33
import com.trashheroesbe.feature.partner.dto.request.RegisterPartnerRequest;
4+
import com.trashheroesbe.feature.partner.dto.request.UpdatePartnerRequest;
45
import com.trashheroesbe.feature.partner.dto.response.RegisterPartnerResponse;
6+
import com.trashheroesbe.global.auth.security.CustomerDetails;
57
import com.trashheroesbe.global.response.ApiResponse;
68
import io.swagger.v3.oas.annotations.Operation;
79
import io.swagger.v3.oas.annotations.Parameter;
@@ -22,4 +24,18 @@ ApiResponse<RegisterPartnerResponse> registerPartner(
2224
@RequestPart(value = "image", required = false)
2325
MultipartFile image
2426
);
27+
28+
@Operation(summary = "파트너 정보 수정", description = "파트너 정보를 수정합니다.")
29+
ApiResponse<Void> updatePartner(
30+
@RequestPart(value = "metadata")
31+
UpdatePartnerRequest request,
32+
33+
@Parameter(description = "업로드할 이미지 파일")
34+
@RequestPart(value = "image", required = false)
35+
MultipartFile image,
36+
37+
@Parameter(hidden = true)
38+
CustomerDetails customerDetails
39+
40+
);
2541
}

src/main/java/com/trashheroesbe/feature/partner/application/PartnerService.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
package com.trashheroesbe.feature.partner.application;
22

3+
import static com.trashheroesbe.global.response.type.ErrorCode.ENTITY_NOT_FOUND;
34
import static com.trashheroesbe.global.response.type.ErrorCode.EXISTS_EMAIL;
45
import static com.trashheroesbe.global.response.type.ErrorCode.S3_UPLOAD_FAIL;
56

67
import com.trashheroesbe.feature.partner.domain.entity.Partner;
78
import com.trashheroesbe.feature.partner.dto.request.RegisterPartnerRequest;
9+
import com.trashheroesbe.feature.partner.dto.request.UpdatePartnerRequest;
810
import com.trashheroesbe.feature.partner.dto.response.RegisterPartnerResponse;
911
import com.trashheroesbe.feature.partner.infrastructure.PartnerRepository;
1012
import com.trashheroesbe.feature.user.domain.entity.User;
1113
import com.trashheroesbe.feature.user.infrastructure.UserRepository;
1214
import com.trashheroesbe.global.exception.BusinessException;
1315
import com.trashheroesbe.global.util.FileUtils;
1416
import com.trashheroesbe.infrastructure.port.s3.FileStoragePort;
17+
import jakarta.validation.Valid;
1518
import java.util.Objects;
1619
import java.util.UUID;
1720
import lombok.RequiredArgsConstructor;
@@ -68,4 +71,63 @@ public RegisterPartnerResponse registerPartner(
6871

6972
return RegisterPartnerResponse.from(request.partnerName(), request.email(), rawPassword);
7073
}
74+
75+
@Transactional
76+
public void updatePartner(
77+
@Valid UpdatePartnerRequest request,
78+
MultipartFile image,
79+
User user
80+
) {
81+
Partner partner = user.getPartner();
82+
if (partner == null) {
83+
throw new BusinessException(ENTITY_NOT_FOUND);
84+
}
85+
86+
if (request.partnerName() != null) {
87+
partner.updatePartnerName(request.partnerName());
88+
user.updateNickname(request.partnerName());
89+
}
90+
91+
if (request.email() != null && !request.email().equals(partner.getEmail())) {
92+
if (partnerRepository.existsByEmail(request.email())) {
93+
throw new BusinessException(EXISTS_EMAIL);
94+
}
95+
partner.updateEmail(request.email());
96+
}
97+
98+
if (request.password() != null) {
99+
String encodedPassword = passwordEncoder.encode(request.password());
100+
partner.updatePassword(encodedPassword);
101+
}
102+
103+
if (request.address() != null) {
104+
partner.updateAddress(request.address());
105+
}
106+
107+
if (request.description() != null) {
108+
partner.updateDescription(request.description());
109+
}
110+
111+
if (image != null && !image.isEmpty()) {
112+
try {
113+
String storedKey = FileUtils.generateStoredKey(
114+
Objects.requireNonNull(image.getOriginalFilename()),
115+
S3_PARTNER_PREFIX
116+
);
117+
118+
String imageUrl = fileStoragePort.uploadFile(
119+
storedKey,
120+
image.getContentType(),
121+
image.getBytes()
122+
);
123+
124+
if (partner.getImageUrl() != null && !partner.getImageUrl().isEmpty()) {
125+
fileStoragePort.deleteFileByUrl(partner.getImageUrl());
126+
}
127+
partner.updateImageUrl(imageUrl);
128+
} catch (Exception e) {
129+
throw new BusinessException(S3_UPLOAD_FAIL, e);
130+
}
131+
}
132+
}
71133
}

src/main/java/com/trashheroesbe/feature/partner/domain/entity/Partner.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,40 @@ public class Partner {
3838

3939
@Column
4040
private String imageUrl;
41+
42+
public void updatePartnerName(String partnerName) {
43+
if (partnerName != null && !partnerName.isEmpty()) {
44+
this.partnerName = partnerName;
45+
}
46+
}
47+
48+
public void updateEmail(String email) {
49+
if (email != null && !email.isEmpty()) {
50+
this.email = email;
51+
}
52+
}
53+
54+
public void updatePassword(String encodedPassword) {
55+
if (encodedPassword != null && !encodedPassword.isEmpty()) {
56+
this.password = encodedPassword;
57+
}
58+
}
59+
60+
public void updateAddress(String address) {
61+
if (address != null && !address.isEmpty()) {
62+
this.address = address;
63+
}
64+
}
65+
66+
public void updateDescription(String description) {
67+
if (description != null && !description.isEmpty()) {
68+
this.description = description;
69+
}
70+
}
71+
72+
public void updateImageUrl(String imageUrl) {
73+
if (imageUrl != null && !imageUrl.isEmpty()) {
74+
this.imageUrl = imageUrl;
75+
}
76+
}
4177
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.trashheroesbe.feature.partner.dto.request;
2+
3+
import io.swagger.v3.oas.annotations.media.Schema;
4+
import jakarta.validation.constraints.Email;
5+
import jakarta.validation.constraints.Pattern;
6+
7+
public record UpdatePartnerRequest(
8+
@Schema(description = "파트너 이름", example = "어스어스2")
9+
String partnerName,
10+
11+
@Schema(description = "이메일", example = "usus2@gmail.com")
12+
@Email
13+
String email,
14+
15+
@Schema(description = "비밀번호", example = "123123qwe!")
16+
@Pattern(
17+
regexp = "^(?=.*[a-z])(?=.*\\d)(?=.*[@$!%*?&#^()_+=\\-\\[\\]{}|;:'\",.<>/?])[a-z\\d@$!%*?&#^()_+=\\-\\[\\]{}|;:'\",.<>/?]{8,}$",
18+
message = "비밀번호는 소문자, 숫자, 특수문자를 포함한 8자 이상이어야 합니다."
19+
)
20+
String password,
21+
22+
@Schema(description = "주소", example = "서울 서대문구 창전동")
23+
String address,
24+
25+
@Schema(description = "설명", example = "우리는 어스어스입니다!!")
26+
String description
27+
) {
28+
29+
}

0 commit comments

Comments
 (0)