Skip to content

Commit

Permalink
feat : 이벤트 응모 API 작성 (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
wonjunYou authored Feb 27, 2024
1 parent a3d4992 commit 77a4287
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ public enum ErrorMessage {
NO_SUCH_CLUB("해당 동아리가 존재하지 않습니다."),
NO_SUCH_NOTICE("해당 공지사항이 존재하지 않습니다."),
NO_SUCH_ACTIVITY_REPORT("해당 활동보고서가 존재하지 않습니다."),
NO_SUCH_QR_STAMP_HISTORY("이벤트 참여 내역이 존재하지 않습니다."),
INVALID_CLUB_SCORE_VALUE("동아리 점수는 0 ~ 999점 입니다."),
INVALID_PASSWORD("잘못된 비밀번호입니다."),
INVALID_STAMP_COUNT_FOR_APPLY("스탬프를 모두 모아야 이벤트에 참여할 수 있어요!"),
ACCESS_DENIED("접근권한이 없습니다."),
UNREGISTER_ID("등록되지 않은 ID입니다."),
NO_SUCH_BANNER("해당 배너가 존재하지 않습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

@RestController
@RequiredArgsConstructor
@RequestMapping("/server/event")
@RequestMapping("/server/events")
public class EventController {

private final EventQrGenerator eventQrGenerator;
Expand All @@ -22,4 +22,5 @@ public EventQrResponse generateQrCode(
) {
return eventQrGenerator.generateQrCodeUri(studentName, studentNumber);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ddingdong.ddingdongBE.domain.event.controller.dto.request;

import javax.validation.constraints.NotBlank;

public record ApplyEventRequest(
@NotBlank(message = "학번은 빈 값이 될 수 없습니다.")
String studentNumber,

@NotBlank(message = "전화번호는 빈 값이 될 수 없습니다.")
String telephone
) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public enum FileDomainCategory {
NOTICE("notice/"),
BANNER("banner/"),
ACTIVITY_REPORT("activity-report/"),
FIX_ZONE("fix/");
FIX_ZONE("fix/"),
EVENT("event/");

private final String fileDomain;
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
package ddingdong.ddingdongBE.domain.qrstamp.controller;

import static ddingdong.ddingdongBE.domain.fileinformation.entity.FileDomainCategory.EVENT;
import static ddingdong.ddingdongBE.domain.fileinformation.entity.FileTypeCategory.IMAGE;

import ddingdong.ddingdongBE.domain.event.controller.dto.request.ApplyEventRequest;
import ddingdong.ddingdongBE.domain.fileinformation.service.FileInformationService;
import ddingdong.ddingdongBE.domain.qrstamp.controller.dto.request.CollectStampRequest;
import ddingdong.ddingdongBE.domain.qrstamp.controller.dto.response.CollectionResultResponse;
import ddingdong.ddingdongBE.domain.qrstamp.service.QrStampService;
import ddingdong.ddingdongBE.file.service.FileService;
import java.time.LocalDateTime;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
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;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/server/events")
@RequiredArgsConstructor
public class QrStampController {

private final QrStampService qrStampService;
private final FileService fileService;
private final FileInformationService fileInformationService;

@PostMapping("/stamps")
public String collectStamp(@RequestBody CollectStampRequest request) {
Expand All @@ -30,4 +43,18 @@ public CollectionResultResponse getCollectionResult(@RequestParam String student
return qrStampService.getCollectionResult(studentName, studentNumber);
}

@PatchMapping("/apply")
public void applyEvent(
@ModelAttribute ApplyEventRequest request,
@RequestPart(name = "certificationImage", required = false) MultipartFile image
) {
Long stampHistoryId = qrStampService.findByStudentNumber(request.studentNumber());

fileService.uploadFile(stampHistoryId, List.of(image), IMAGE, EVENT);

List<String> imageUrls = fileInformationService.getImageUrls(IMAGE.getFileType() + EVENT.getFileDomain() + stampHistoryId);

qrStampService.applyEvent(request, imageUrls);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,19 @@ public class StampHistory extends BaseEntity {

private String department;

private String telephone;

private String certificationImageUrl;

@Builder

private StampHistory(Long id, String studentName, String department, String studentNumber,
LocalDateTime completedAt, String certificationImageUrl) {
String telephone, LocalDateTime completedAt, String certificationImageUrl) {
this.id = id;
this.studentName = studentName;
this.department = department;
this.studentNumber = studentNumber;
this.telephone = telephone;
this.completedAt = completedAt;
this.certificationImageUrl = certificationImageUrl;
}
Expand All @@ -69,4 +72,10 @@ public boolean isCompleted() {
return this.collectedStamps.size() >= 10;
}


public void apply(String telephone, String certificationImageUrl) {
this.telephone = telephone;
this.certificationImageUrl = certificationImageUrl;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
@Repository
public interface StampHistoryRepository extends JpaRepository<StampHistory, Long> {

Optional<StampHistory> findStampHistoryByStudentNumber(String studentNumber);

Optional<StampHistory> findStampHistoryByStudentNameAndStudentNumber(String studentName, String studentNumber);

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package ddingdong.ddingdongBE.domain.qrstamp.service;

import static ddingdong.ddingdongBE.common.exception.ErrorMessage.INVALID_STAMP_COUNT_FOR_APPLY;
import static ddingdong.ddingdongBE.common.exception.ErrorMessage.NO_SUCH_QR_STAMP_HISTORY;

import ddingdong.ddingdongBE.domain.event.controller.dto.request.ApplyEventRequest;
import ddingdong.ddingdongBE.domain.qrstamp.controller.dto.request.CollectStampRequest;
import ddingdong.ddingdongBE.domain.qrstamp.controller.dto.response.CollectedStampsResponse;
import ddingdong.ddingdongBE.domain.qrstamp.controller.dto.response.CollectionResultResponse;
Expand All @@ -9,7 +12,9 @@
import ddingdong.ddingdongBE.domain.qrstamp.repository.StampHistoryRepository;
import java.time.LocalDateTime;
import java.util.List;
import java.util.NoSuchElementException;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -49,4 +54,30 @@ public CollectionResultResponse getCollectionResult(String studentNumber, String
.toList();
return CollectionResultResponse.of(stampHistory.isCompleted(), collectedStampsResponse);
}

@Transactional
public void applyEvent(ApplyEventRequest request, List<String> imageUrls) {
StampHistory stampHistory = stampHistoryRepository.findStampHistoryByStudentNumber(
request.studentNumber()
)
.orElseThrow(() -> new NoSuchElementException(NO_SUCH_QR_STAMP_HISTORY.getText()));

validateEventIsCompleted(stampHistory);

stampHistory.apply(request.telephone(), imageUrls.get(0));
}

public Long findByStudentNumber(String studentNumber) {
StampHistory stampHistory = stampHistoryRepository.findStampHistoryByStudentNumber(studentNumber)
.orElseThrow(() -> new NoSuchElementException(NO_SUCH_QR_STAMP_HISTORY.getText()));

return stampHistory.getId();
}

private void validateEventIsCompleted(StampHistory stampHistory) {
if (!stampHistory.isCompleted()) {
throw new IllegalArgumentException(INVALID_STAMP_COUNT_FOR_APPLY.getText());
}
}

}

0 comments on commit 77a4287

Please sign in to comment.