Skip to content

Commit

Permalink
Merge pull request #46 from 9oormthon-univ/33-feature-implementation-…
Browse files Browse the repository at this point in the history
…of-api-for-leaving-messages-of-gratitude

33 feature implementation of api for leaving messages of gratitude
  • Loading branch information
himodu authored Nov 28, 2024
2 parents 6fe9203 + 75b6564 commit cecc717
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.mymoo.domain.donationusage.controller;

import com.example.mymoo.domain.donationusage.dto.request.DonationUsageCreateRequestDto;
import com.example.mymoo.domain.donationusage.dto.request.DonationUsageUpdateMessageRequestDto;
import com.example.mymoo.domain.donationusage.service.DonationUsageService;
import com.example.mymoo.global.security.CustomUserDetails;
import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -11,6 +12,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down Expand Up @@ -42,4 +44,24 @@ public ResponseEntity<Void> useDonation(
.status(HttpStatus.CREATED)
.build();
}

@Operation(
summary = "[아동] 감사의 말 작성",
description = "QR 코드 인식 후 아동 계정에서 감사 메시지 작성 시 해당 API를 호출합니다.",
responses = {
@ApiResponse(responseCode = "200", description = "작성 성공"),
}
)
@PatchMapping("/")
@PreAuthorize("hasAuthority('CHILD')")
public ResponseEntity<Void> updateMessage(
@AuthenticationPrincipal CustomUserDetails userDetails,
@Valid @RequestBody DonationUsageUpdateMessageRequestDto donationUsageUpdateMessageRequestDto
){
Long childAccountId = userDetails.getAccountId();
donationUsageService.updateMessage(childAccountId, donationUsageUpdateMessageRequestDto);
return ResponseEntity
.status(HttpStatus.OK)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.mymoo.domain.donationusage.dto.request;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;

public record DonationUsageUpdateMessageRequestDto(
@NotNull(message = "donationId는 필수 입력 항목입니다.")
Long donationId,
@NotBlank(message = "message는 필수 항목입니다")
@Size(min = 1, max = 255, message = "message는 최소 1자, 최대 255자입니다.")
String message
) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.example.mymoo.domain.child.entity.Child;
import com.example.mymoo.domain.donation.entity.Donation;
import com.example.mymoo.domain.donation.exception.DonationException;
import com.example.mymoo.domain.donation.exception.DonationExceptionDetails;
import com.example.mymoo.global.entity.BaseEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand Down Expand Up @@ -55,4 +57,11 @@ public DonationUsage(
this.donation = donation;
this.child = child;
}

public void setMessage(String message) {
if (message.isEmpty() || message.length() > 255) {
throw new IllegalArgumentException("message는 최소 1자 최대 255자여야 합니다.");
}
this.message = message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public enum DonationUsageExceptionDetails implements ExceptionDetails {
DONATION_USAGE_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 donationUsage 입니다."),
// 자신의 가게가 아닌 다른 가게의 후원을 사용하려 할 때
FORBIDDEN_ACCESS_TO_OTHER_STORE(HttpStatus.FORBIDDEN, "해당 후원을 사용할 권한이 없습니다."),
// 자신이 사용하지 않은 후원권에 대해 후원자에게 감사 메시지를 쓰려 할 때
FORBIDDEN_TO_WRITE_MESSAGE_TO_OTHER_DONATION(HttpStatus.FORBIDDEN, "해당 후원에 대해 메시지를 작성할 권한이 없습니다."),
;

private final HttpStatus status;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package com.example.mymoo.domain.donationusage.service;

import com.example.mymoo.domain.donationusage.dto.request.DonationUsageCreateRequestDto;
import com.example.mymoo.domain.donationusage.dto.request.DonationUsageUpdateMessageRequestDto;

public interface DonationUsageService {
void useDonation(
Long storeAccountId,
DonationUsageCreateRequestDto donationUsageCreateRequestDto
);

void updateMessage(
Long childAccountId,
DonationUsageUpdateMessageRequestDto donationUsageUpdateMessageRequestDto
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.example.mymoo.domain.donation.exception.DonationExceptionDetails;
import com.example.mymoo.domain.donation.repository.DonationRepository;
import com.example.mymoo.domain.donationusage.dto.request.DonationUsageCreateRequestDto;
import com.example.mymoo.domain.donationusage.dto.request.DonationUsageUpdateMessageRequestDto;
import com.example.mymoo.domain.donationusage.entity.DonationUsage;
import com.example.mymoo.domain.donationusage.exception.DonationUsageException;
import com.example.mymoo.domain.donationusage.exception.DonationUsageExceptionDetails;
Expand Down Expand Up @@ -70,4 +71,21 @@ public void useDonation(
.build()
);
}

@Override
public void updateMessage(
final Long childAccountId,
final DonationUsageUpdateMessageRequestDto donationUsageUpdateMessageRequestDto
) {
DonationUsage donationUsage = donationUsageRepository.findByDonation_id(donationUsageUpdateMessageRequestDto.donationId())
.orElseThrow(() -> new DonationUsageException(DonationUsageExceptionDetails.DONATION_USAGE_NOT_FOUND));

// 자신이 사용하지 않은 후원에 대해 감사 메시지 작성하려 할 때
if (!Objects.equals(donationUsage.getChild().getAccount().getId(), childAccountId)){
throw new DonationUsageException(DonationUsageExceptionDetails.FORBIDDEN_TO_WRITE_MESSAGE_TO_OTHER_DONATION);
}

// 메시지 업데이트
donationUsage.setMessage(donationUsageUpdateMessageRequestDto.message());
}
}

0 comments on commit cecc717

Please sign in to comment.