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

보호자 마이페이지 관련 API 구현 #88

Merged
merged 1 commit into from
Nov 9, 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,70 @@
package dbdr.domain.guardian.controller;

import dbdr.domain.guardian.dto.request.GuardianRequest;
import dbdr.domain.guardian.dto.response.GuardianResponse;
import dbdr.domain.guardian.service.GuardianService;
import dbdr.global.util.api.ApiUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "[관리자] 보호자 (Guardian)", description = "보호자 정보 조회, 추가, 수정, 삭제")
@RestController
@RequestMapping("/${spring.app.version}/admin/guardian")
@RequiredArgsConstructor
public class GuardianAdminController {

private final GuardianService guardianService;

@Operation(summary = "전체 보호자 정보 조회")
@GetMapping
public ResponseEntity<ApiUtils.ApiResult<List<GuardianResponse>>> showAllGuardian() {
List<GuardianResponse> guardianResponseList = guardianService.getAllGuardian();
return ResponseEntity.ok(ApiUtils.success(guardianResponseList));
}

@Operation(summary = "보호자 한 사람의 정보 조회")
@GetMapping("/{guardianId}")
public ResponseEntity<ApiUtils.ApiResult<GuardianResponse>> showOneGuardian(
@PathVariable("guardianId") Long guardianId) {
GuardianResponse guardianResponse = guardianService.getGuardianById(guardianId);
return ResponseEntity.ok(ApiUtils.success(guardianResponse));
}

@Operation(summary = "보호자 추가")
@PostMapping
public ResponseEntity<ApiUtils.ApiResult<GuardianResponse>> addGuardian(
@Valid @RequestBody GuardianRequest guardianRequest) {
GuardianResponse guardianResponse = guardianService.addGuardian(guardianRequest);
return ResponseEntity.status(HttpStatus.CREATED).body(ApiUtils.success(guardianResponse));
}

@Operation(summary = "보호자 정보 수정")
@PutMapping("/{guardianId}")
public ResponseEntity<ApiUtils.ApiResult<GuardianResponse>> updateGuardian(
@PathVariable("guardianId") Long guardianId,
@Valid @RequestBody GuardianRequest guardianRequest) {
GuardianResponse guardianResponse = guardianService.updateGuardianById(guardianId,
guardianRequest);
return ResponseEntity.ok(ApiUtils.success(guardianResponse));
}

@Operation(summary = "보호자 삭제")
@DeleteMapping("/{guardianId}")
public ResponseEntity<ApiUtils.ApiResult<String>> deleteGuardian(@PathVariable("guardianId") Long guardianId) {
guardianService.deleteGuardianById(guardianId);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package dbdr.domain.guardian.controller;

import dbdr.domain.guardian.dto.request.GuardianRequest;
import dbdr.domain.guardian.dto.response.GuardianResponse;
import dbdr.domain.guardian.dto.request.GuardianAlertTimeRequest;
import dbdr.domain.guardian.dto.response.GuardianMyPageResponse;
import dbdr.domain.guardian.entity.Guardian;
import dbdr.domain.guardian.service.GuardianService;
import dbdr.global.util.api.ApiUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
Expand All @@ -30,20 +31,21 @@ public class GuardianController {

@Operation(summary = "보호자 본인의 정보 조회", security = @SecurityRequirement(name = "JWT"))
@GetMapping
public ResponseEntity<GuardianResponse> showGuardianInfo(
public ResponseEntity<ApiUtils.ApiResult<GuardianMyPageResponse>> showGuardianInfo(
@Parameter(hidden = true) @LoginGuardian Guardian guardian) {
log.info("guardianId: {}", guardian.getName());
GuardianResponse guardianResponse = guardianService.getGuardianById(guardian.getId());
return ResponseEntity.ok(guardianResponse);
GuardianMyPageResponse guardianMyPageResponse = guardianService.getMyPageGuardianInfo(
guardian.getId());
return ResponseEntity.ok(ApiUtils.success(guardianMyPageResponse));
}

@Operation(summary = "보호자 본인의 정보 수정", security = @SecurityRequirement(name = "JWT"))
@PutMapping
public ResponseEntity<GuardianResponse> updateGuardianInfo(
@Parameter(hidden = true) @Valid @RequestBody GuardianRequest guardianRequest,
public ResponseEntity<ApiUtils.ApiResult<GuardianMyPageResponse>> updateGuardianInfo(
@Parameter(hidden = true) @Valid @RequestBody GuardianAlertTimeRequest guardianAlertTimeRequest,
@LoginGuardian Guardian guardian) {
GuardianResponse guardianResponse = guardianService.updateGuardianById(guardian.getId(),
guardianRequest);
return ResponseEntity.ok(guardianResponse);
GuardianMyPageResponse guardianMyPageResponse = guardianService.updateAlertTime(guardian.getId(),
guardianAlertTimeRequest);
return ResponseEntity.ok(ApiUtils.success(guardianMyPageResponse));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dbdr.domain.guardian.dto.request.GuardianRequest;
import dbdr.domain.guardian.dto.response.GuardianResponse;
import dbdr.domain.guardian.service.GuardianService;
import dbdr.global.util.api.ApiUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
Expand All @@ -19,7 +20,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "[관리자] 보호자 (Guardian)", description = "보호자 정보 조회, 추가, 수정, 삭제")
@Tag(name = "[요양원관리자] 보호자 (Guardian)", description = "보호자 정보 조회, 추가, 수정, 삭제")
@RestController
@RequestMapping("/${spring.app.version}/institution/guardian")
@RequiredArgsConstructor
Expand All @@ -29,40 +30,40 @@ public class GuardianInstitutionController {

@Operation(summary = "전체 보호자 정보 조회")
@GetMapping
public ResponseEntity<List<GuardianResponse>> showAllGuardian() {
public ResponseEntity<ApiUtils.ApiResult<List<GuardianResponse>>> showAllGuardian() {
List<GuardianResponse> guardianResponseList = guardianService.getAllGuardian();
return ResponseEntity.ok(guardianResponseList);
return ResponseEntity.ok(ApiUtils.success(guardianResponseList));
}

@Operation(summary = "보호자 한 사람의 정보 조회")
@GetMapping("/{guardianId}")
public ResponseEntity<GuardianResponse> showOneGuardian(
public ResponseEntity<ApiUtils.ApiResult<GuardianResponse>> showOneGuardian(
@PathVariable("guardianId") Long guardianId) {
GuardianResponse guardianResponse = guardianService.getGuardianById(guardianId);
return ResponseEntity.ok(guardianResponse);
return ResponseEntity.ok(ApiUtils.success(guardianResponse));
}

@Operation(summary = "보호자 추가")
@PostMapping
public ResponseEntity<GuardianResponse> addGuardian(
public ResponseEntity<ApiUtils.ApiResult<GuardianResponse>> addGuardian(
@Valid @RequestBody GuardianRequest guardianRequest) {
GuardianResponse guardianResponse = guardianService.addGuardian(guardianRequest);
return ResponseEntity.status(HttpStatus.CREATED).body(guardianResponse);
return ResponseEntity.status(HttpStatus.CREATED).body(ApiUtils.success(guardianResponse));
}

@Operation(summary = "보호자 정보 수정")
@PutMapping("/{guardianId}")
public ResponseEntity<GuardianResponse> updateGuardian(
public ResponseEntity<ApiUtils.ApiResult<GuardianResponse>> updateGuardian(
@PathVariable("guardianId") Long guardianId,
@Valid @RequestBody GuardianRequest guardianRequest) {
GuardianResponse guardianResponse = guardianService.updateGuardianById(guardianId,
guardianRequest);
return ResponseEntity.ok(guardianResponse);
return ResponseEntity.ok(ApiUtils.success(guardianResponse));
}

@Operation(summary = "보호자 삭제")
@DeleteMapping("/{guardianId}")
public ResponseEntity<Void> deleteGuardian(@PathVariable("guardianId") Long guardianId) {
public ResponseEntity<ApiUtils.ApiResult<String>> deleteGuardian(@PathVariable("guardianId") Long guardianId) {
guardianService.deleteGuardianById(guardianId);
return ResponseEntity.noContent().build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dbdr.domain.guardian.dto.request;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import java.time.LocalTime;

public record GuardianAlertTimeRequest(@Schema(description = "보호자의 이름", example = "박준협")
@NotBlank(message = "이름은 필수 항목입니다.")
String name,
@Schema(description = "보호자의 휴대폰 번호", example = "01012341234")
@NotBlank(message = "휴대폰 번호는 필수 항목입니다.")
@Pattern(regexp = "010\\d{8}", message = "010XXXXXXXX형식으로 입력해주세요.")
String phone,
String loginId, LocalTime alertTime) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dbdr.domain.guardian.dto.response;

import java.time.LocalTime;

public record GuardianMyPageResponse(String name, String phone,
String loginId, LocalTime alertTime) {

}
8 changes: 7 additions & 1 deletion src/main/java/dbdr/domain/guardian/entity/Guardian.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,20 @@ public class Guardian extends BaseEntity {
public Guardian(String phone, String name) {
this.phone = phone;
this.name = name;
this.alertTime = LocalTime.of(9, 0); // 오전 9시로 초기화
this.alertTime = LocalTime.of(18, 0); // 오후 6시로 초기화
}

public void updateGuardian(String phone, String name) {
this.phone = phone;
this.name = name;
}

public void updateAlertTime(String name, String phone, LocalTime alertTime) {
this.phone = phone;
this.name = name;
this.alertTime = alertTime;
}

public void updateLineUserId(String lineUserId) {
this.lineUserId = lineUserId;
}
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/dbdr/domain/guardian/service/GuardianService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dbdr.domain.guardian.service;

import dbdr.domain.guardian.dto.request.GuardianAlertTimeRequest;
import dbdr.domain.guardian.dto.response.GuardianMyPageResponse;
import dbdr.domain.guardian.entity.Guardian;
import dbdr.domain.guardian.dto.request.GuardianRequest;
import dbdr.domain.guardian.dto.response.GuardianResponse;
Expand All @@ -22,6 +24,22 @@ public GuardianResponse getGuardianById(Long guardianId) {
return new GuardianResponse(guardian.getPhone(), guardian.getName(), guardian.isActive());
}

public GuardianMyPageResponse getMyPageGuardianInfo(Long guardianId) {
Guardian guardian = findGuardianById(guardianId);
return new GuardianMyPageResponse(guardian.getName(), guardian.getPhone(),
guardian.getLoginId(), guardian.getAlertTime());
}

public GuardianMyPageResponse updateAlertTime(Long guardianId,
GuardianAlertTimeRequest request) {
ensureUniquePhone(request.phone());
Guardian guardian = findGuardianById(guardianId);
guardian.updateAlertTime(request.name(), request.phone(), request.alertTime());
guardianRepository.save(guardian);
return new GuardianMyPageResponse(guardian.getName(), guardian.getPhone(),
guardian.getLoginId(), guardian.getAlertTime());
}

public GuardianResponse updateGuardianById(Long guardianId,
GuardianRequest guardianRequest) {
ensureUniquePhone(guardianRequest.phone());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dbdr.domain.institution.dto.request.InstitutionRequest;
import dbdr.domain.institution.dto.response.InstitutionResponse;
import dbdr.domain.institution.service.InstitutionService;
import dbdr.global.util.api.ApiUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
Expand All @@ -29,39 +30,39 @@ public class InstitutionAdminController {

@Operation(summary = "전체 요양원 정보 조회")
@GetMapping
public ResponseEntity<List<InstitutionResponse>> showAllInstitution() {
public ResponseEntity<ApiUtils.ApiResult<List<InstitutionResponse>>> showAllInstitution() {
List<InstitutionResponse> institutionResponseList = institutionService.getAllInstitution();
return ResponseEntity.ok(institutionResponseList);
return ResponseEntity.ok(ApiUtils.success(institutionResponseList));
}

@Operation(summary = "요양원 하나의 정보 조회")
@GetMapping("/{institutionId}")
public ResponseEntity<InstitutionResponse> showOneInstitution(@PathVariable("institutionId") Long institutionId) {
public ResponseEntity<ApiUtils.ApiResult<InstitutionResponse>> showOneInstitution(@PathVariable("institutionId") Long institutionId) {
InstitutionResponse institutionResponse = institutionService.getInstitutionResponseById(institutionId);
return ResponseEntity.ok(institutionResponse);
return ResponseEntity.ok(ApiUtils.success(institutionResponse));
}

@Operation(summary = "요양원 추가")
@PostMapping
public ResponseEntity<InstitutionResponse> addInstitution(
public ResponseEntity<ApiUtils.ApiResult<InstitutionResponse>> addInstitution(
@Valid @RequestBody InstitutionRequest institutionRequest) {
InstitutionResponse institutionResponse = institutionService.addInstitution(
institutionRequest);
return ResponseEntity.status(HttpStatus.CREATED).body(institutionResponse);
return ResponseEntity.status(HttpStatus.CREATED).body(ApiUtils.success(institutionResponse));
}

@Operation(summary = "요양원 정보 수정")
@PutMapping("/{institutionId}")
public ResponseEntity<InstitutionResponse> updateInstitution(@PathVariable("institutionId") Long institutionId,
public ResponseEntity<ApiUtils.ApiResult<InstitutionResponse>> updateInstitution(@PathVariable("institutionId") Long institutionId,
@Valid @RequestBody InstitutionRequest institutionRequest) {
InstitutionResponse institutionResponse = institutionService.updateInstitution(institutionId,
institutionRequest);
return ResponseEntity.ok(institutionResponse);
return ResponseEntity.ok(ApiUtils.success(institutionResponse));
}

@Operation(summary = "요양원 삭제")
@DeleteMapping("/{institutionId}")
public ResponseEntity<Void> deleteInstitution(@PathVariable("institutionId") Long institutionId) {
public ResponseEntity<ApiUtils.ApiResult<String>> deleteInstitution(@PathVariable("institutionId") Long institutionId) {
institutionService.deleteInstitutionById(institutionId);
return ResponseEntity.noContent().build();
}
Expand Down