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: 클라이언트 정보 수정 및 삭제 #109

Merged
merged 1 commit into from
Nov 23, 2024
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
@@ -0,0 +1,16 @@
package the_monitor.application.dto.request;

import jakarta.validation.constraints.NotBlank;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class ClientUpdateRequest {

@NotBlank(message = "고객사명은 필수입니다.")
private String name;

@NotBlank(message = "담당자명은 필수입니다.")
private String managerName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.springframework.web.multipart.MultipartFile;
import the_monitor.application.dto.request.ClientRequest;
import the_monitor.application.dto.request.ClientUpdateRequest;
import the_monitor.application.dto.response.ClientResponse;
import the_monitor.domain.model.Client;

Expand All @@ -16,4 +17,8 @@ public interface ClientService {
Client findClientById(Long clientId);

ClientResponse getClient(Long clientId);

String deleteClientById(Long clientId);

String updateClient(Long clientId, ClientUpdateRequest request, MultipartFile logo);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.context.SecurityContextHolder;
import the_monitor.application.dto.request.ClientRequest;
import the_monitor.application.dto.request.ClientUpdateRequest;
import the_monitor.application.dto.response.ClientResponse;
import the_monitor.application.dto.response.ReportListResponse;
import the_monitor.application.service.CategoryService;
Expand Down Expand Up @@ -130,6 +131,7 @@ public Client findClientById(Long clientId) {
return clientRepository.findById(clientId)
.orElseThrow(() -> new ApiException(ErrorStatus._CLIENT_NOT_FOUND));
}

@Override
public ClientResponse getClient(Long clientId){

Expand Down Expand Up @@ -168,4 +170,40 @@ private Long getAccountIdFromJwt() {
return jwtProvider.getAccountId(token);
}

@Override
@Transactional
public String deleteClientById(Long clientId) {
Long accountId = getAccountIdFromJwt(); // JWT에서 accountId 추출 (유틸리티 메서드)

// 1. Client 존재 여부 및 권한 확인
Client client = clientRepository.findById(clientId)
.orElseThrow(() -> new ApiException(ErrorStatus._CLIENT_NOT_FOUND));

if (!client.getAccount().getId().equals(accountId)) {
throw new ApiException(ErrorStatus._CLIENT_FORBIDDEN);
}

// 2. Client 삭제
clientRepository.delete(client);

// 3. 성공 메시지 반환
return "고객사 정보가 성공적으로 삭제되었습니다.";
}

@Override
@Transactional
public String updateClient(Long clientId, ClientUpdateRequest request, MultipartFile logo) {
// 1. 고객사 조회
Client client = clientRepository.findById(clientId)
.orElseThrow(() -> new IllegalArgumentException("Client not found with id: " + clientId));

String logoPath;
logoPath = (logo != null) ? s3Service.uploadFile(logo) : defaultLogoUrl;

// 2. 수정 사항 적용
client.updateClientInfo(request.getName(), request.getManagerName(), logoPath);

return "고객사 정보가 성공적으로 수정되었습니다.";
}

}
1 change: 1 addition & 0 deletions src/main/java/the_monitor/common/ErrorStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public enum ErrorStatus implements BaseErrorCode {

// Client
_CLIENT_NOT_FOUND(HttpStatus.NOT_FOUND, "CLIENT404", "해당 클라이언트를 찾을 수 없습니다."),
_CLIENT_FORBIDDEN(HttpStatus.FORBIDDEN, "CLIENT403", "해당 클라이언트에 접속할 수 없습니다,"),

// Report
_REPORT_NOT_FOUND(HttpStatus.NOT_FOUND, "REPORT404", "해당 리포트를 찾을 수 없습니다."),
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/the_monitor/domain/model/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,15 @@ public void setClientMailCCs(List<ClientMailCC> clientMailCCs) {
this.clientMailCCs = clientMailCCs;
}

public void updateClientInfo(String name, String managerName, String logoUrl) {
if (name != null && !name.isBlank()) {
this.name = name;
}
if (managerName != null && !managerName.isBlank()) {
this.managerName = managerName;
}
if (logoUrl != null) {
this.logo = logoUrl;
}
}
}
18 changes: 18 additions & 0 deletions src/main/java/the_monitor/presentation/ClientController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.springframework.http.MediaType;
import org.springframework.web.multipart.MultipartFile;
import the_monitor.application.dto.request.ClientRequest;
import the_monitor.application.dto.request.ClientUpdateRequest;
import the_monitor.application.dto.request.ReportUpdateTitleRequest;
import the_monitor.application.dto.response.ClientResponse;
import the_monitor.application.service.ClientService;
import the_monitor.common.ApiResponse;
Expand Down Expand Up @@ -40,10 +42,26 @@ public ApiResponse<List<ClientResponse>> getClient() {
return ApiResponse.onSuccessData("클라이언트 조회 성공", clientResponses);
}

@Operation(summary = "고객사 정보 삭제", description = "고객사 정보를 삭제합니다.")
@DeleteMapping()
public ApiResponse<String> deleteClient(@RequestParam("clientId") Long clientId) {
return ApiResponse.onSuccess(clientService.deleteClientById(clientId));

}

@Operation(summary = "clietId로 고객사 정보 반환", description = "clientId로 고객사 정보를 조회합니다.")
@GetMapping("/info")
public ApiResponse<ClientResponse> getClientInfo(@RequestParam("clientId") Long clientId){
return ApiResponse.onSuccessData("클라이언트 정보 조회 성공", clientService.getClient(clientId));
}

@Operation(summary = "고객사 정보 수정", description = "고객사 정보를 수정합니다.")
@PutMapping(value = "/update", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ApiResponse<String> UpdateClient(@RequestParam("clientId") Long clientId,
@RequestPart("clientData") @Valid ClientUpdateRequest request,
@RequestPart(name = "logo", required = false) MultipartFile logo) {
return ApiResponse.onSuccess(clientService.updateClient(clientId, request, logo));

}

}