Skip to content

Commit

Permalink
Merge pull request #77 from whatever-mentoring/hotfix/karmapol/fixret…
Browse files Browse the repository at this point in the history
…urnvalue

Hotfix/karmapol/fixreturnvalue
  • Loading branch information
KarmaPol authored Sep 19, 2023
2 parents 3394ef6 + 13a7178 commit 529459f
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import dopamine.backend.global.s3.service.ImageService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequiredArgsConstructor
Expand All @@ -25,7 +27,7 @@ public class ChallengeController {
* 챌린지 생성
* @param challengeRequestDTO
*/
@PostMapping("/challenges")
@PostMapping(value = "/challenges", consumes={MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE})
public void createChallenge(@Valid @RequestPart(value = "request") ChallengeRequestDTO challengeRequestDTO,
@RequestPart(value = "image", required = false) MultipartFile file){
if(file != null){
Expand Down Expand Up @@ -68,7 +70,7 @@ public ChallengeResponseDTO getChallenge(@PathVariable("challengeId") Long chall
* @param challengeId
* @param challengeEditDTO
*/
@PutMapping("/challenges/{challengeId}")
@PutMapping(value = "/challenges/{challengeId}", consumes={MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE})
public void editChallenge(@PathVariable Long challengeId, @RequestPart(value = "request") ChallengeEditDTO challengeEditDTO,
@RequestPart(value = "image", required = false) MultipartFile file){
if(file != null){
Expand All @@ -83,7 +85,7 @@ public void editChallenge(@PathVariable Long challengeId, @RequestPart(value = "
* @param userId
*/
@GetMapping("/challenges/today-challenge/{userId}")
public void todayChallenge(@PathVariable Long userId){
challengeService.todayChallenge(userId);
public List<ChallengeResponseDTO> todayChallenge(@PathVariable Long userId){
return challengeService.todayChallenge(userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ public class ChallengeResponseDTO {
private String image;
private String challengeGuide;
private ChallengeLevel challengeLevel;
private Boolean certificationYn;

public void setCertificationYn(Boolean certificationYn) {
this.certificationYn = certificationYn;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,14 @@ public List<ChallengeResponseDTO> todayChallenge(Long userId) {
// 기존에 챌린지 받은 적 없음
LocalDateTime existRefreshDate = member.getChallengeRefreshDate();
if(existRefreshDate == null){

List<Challenge> todayChallenges = challengeCustomRepository.getTodayChallenges(null);
challengeResponseList = getChallengeResponseList(todayChallenges);

for (int i = 0; i < challengeResponseList.size(); i++) {
challengeResponseList.get(i).setCertificationYn(false);
}

todayChallenges.stream().forEach((challenge) -> new ChallengeMember(member, challenge));
}

Expand All @@ -120,27 +125,36 @@ public List<ChallengeResponseDTO> todayChallenge(Long userId) {

List<ChallengeMember> challengeMembers = member.getChallengeMembers();
List<Challenge> exitChallenge = challengeMembers.stream().map(challengeMember -> challengeMember.getChallenge()).collect(Collectors.toList());
List<Boolean> certificationYnList = challengeMembers.stream().map(ChallengeMember::getCertificationYn).collect(Collectors.toList());

// 갱신일자가 오늘이 아닐 경우, 새로운 챌린지 발급
if(!todayInfo.equals(existInfo)){
if (!todayInfo.equals(existInfo)) {
List<Challenge> todayChallenges = challengeCustomRepository.getTodayChallenges(exitChallenge);

// 기존 연관관계 삭제
for(int i = 0; i < challengeMembers.size(); i++){
for (int i = 0; i < challengeMembers.size(); i++) {
challengeMembers.get(i).deleteChallengeMember();
}
challengeMemberRepository.deleteAllInBatch(challengeMembers);

// 오늘의 챌린지 생성
challengeResponseList = getChallengeResponseList(todayChallenges);

for (int i = 0; i < challengeResponseList.size(); i++) {
challengeResponseList.get(i).setCertificationYn(false);
}

todayChallenges.stream().forEach((challenge) -> new ChallengeMember(member, challenge));
}
// 조회
else {
challengeResponseList = getChallengeResponseList(exitChallenge);
for (int i = 0; i < challengeResponseList.size(); i++) {
challengeResponseList.get(i).setCertificationYn(certificationYnList.get(i));
}
}
}

member.setChallengeRefreshDate(LocalDateTime.now());

return challengeResponseList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package dopamine.backend.domain.challengemember.repository;

import dopamine.backend.domain.challenge.entity.Challenge;
import dopamine.backend.domain.challengemember.entity.ChallengeMember;
import dopamine.backend.domain.member.entity.Member;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface ChallengeMemberRepository extends JpaRepository<ChallengeMember, Long> {

Optional<ChallengeMember> findChallengeMemberByChallengeAndMember(Challenge challenge, Member member);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import dopamine.backend.global.jwt.service.JwtService;
import dopamine.backend.global.s3.service.ImageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
Expand Down Expand Up @@ -103,9 +105,11 @@ public List<FeedResponseDTO> getFeedsByMemberAndMonth(@RequestHeader("Authorizat
*
* @param feedRequestDTO
*/
@PostMapping("/feeds")
@ApiOperation(value = "인증글 작성", notes = "요청값으로 form 데이터에 \n 1. Key:request, Value:문서 하단 Models-FeedRequestDTO를 json \n 2. Key:images, Value:image 여러 개를 한번에")
@PostMapping(value = "/feeds", consumes={MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE})
public void postFeed(@RequestPart(value = "request") FeedRequestDTO feedRequestDTO,
@RequestPart(value = "images") List<MultipartFile> files) {
@RequestPart(value = "images") List<MultipartFile> files,
@RequestHeader("Authorization") String accessToken) {

int index = 0;

Expand All @@ -126,7 +130,9 @@ else if (index == 2)
index++;
}

feedService.postFeed(feedRequestDTO);
Member member = jwtService.getMemberFromAccessToken(accessToken); // member 찾기

feedService.postFeed(member, feedRequestDTO);
}

/**
Expand All @@ -135,7 +141,8 @@ else if (index == 2)
* @param feedId
* @param feedEditDTO
*/
@PutMapping("/feeds/{feedId}")
@ApiOperation(value = "인증글 수정", notes = "요청값으로 url에 feedId \n form 데이터에 \n 1. Key:request, Value:문서 하단 Models-FeedEditDTO를 json \n 2. Key:images Value:image 여러 개를 한번에")
@PutMapping(value = "/feeds/{feedId}", consumes={MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE})
public void editFeed(@PathVariable Long feedId,
@RequestPart(value = "request") FeedEditDTO feedEditDTO,
@RequestPart(value = "images") List<MultipartFile> files) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ public class FeedRequestDTO {

private String image3Url;

private Long memberId;

private Long challengeId;

public void setImage1Url(String image1Url) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import dopamine.backend.domain.challenge.repository.ChallengeRepository;
import dopamine.backend.domain.challenge.response.ChallengeResponseDTO;
import dopamine.backend.domain.challenge.service.ChallengeService;
import dopamine.backend.domain.challengemember.entity.ChallengeMember;
import dopamine.backend.domain.challengemember.repository.ChallengeMemberRepository;
import dopamine.backend.domain.feed.entity.Feed;
import dopamine.backend.domain.feed.mapper.FeedMapper;
import dopamine.backend.domain.feed.repository.FeedCustomRepository;
Expand Down Expand Up @@ -34,6 +36,7 @@ public class FeedService {

private final FeedRepository feedRepository;
private final ChallengeRepository challengeRepository;
private final ChallengeMemberRepository challengeMemberRepository;

private final FeedCustomRepository feedCustomRepository;

Expand Down Expand Up @@ -67,23 +70,32 @@ public FeedResponseDTO getFeed(Long feedId) {
*
* @param feedRequestDTO
*/
public void postFeed(FeedRequestDTO feedRequestDTO) {
public void postFeed(Member member, FeedRequestDTO feedRequestDTO) {
Challenge challenge = challengeRepository.findById(feedRequestDTO.getChallengeId()).orElseThrow(() -> new RuntimeException("존재하지 않는 챌린지입니다."));

Member member = memberService.verifiedMember(feedRequestDTO.getMemberId());

Feed feed = feedMapper.feedRequestDtoToFeed(feedRequestDTO);
feed.setChallenge(challenge);
feed.setMember(member);

// member의 exp추가 & level 반영 => ChallengeLevel Enum 값에 경험치 넣어주었습니다
setCertification(member, challenge);

int exp = feed.getChallenge().getChallengeLevel().getExp();

memberService.plusMemberExp(member, exp);

feedRepository.save(feed);
}

/**
* 인증 여부 갱신
* @param member
* @param challenge
*/
private void setCertification(Member member, Challenge challenge) {
ChallengeMember challengeMember = challengeMemberRepository.findChallengeMemberByChallengeAndMember(challenge, member).orElseThrow(() -> new RuntimeException("유효하지 않은 챌린지입니다."));
challengeMember.setCertificationYn(true);
}

/**
* 피드 수정
*
Expand Down Expand Up @@ -181,6 +193,12 @@ public List<FeedResponseDTO> feedListByMember(Long memberId, Integer page) {
return getFeedResponseDTOS(feedListByMemberOrderByDate);
}

/**
* 피드 리스트 조회 - 월 필터
* @param member
* @param month
* @return
*/
public List<FeedResponseDTO> feedListByMemberAndMonth(Member member, String month) {

LocalDate date = LocalDate.parse(month + "-01");
Expand Down

0 comments on commit 529459f

Please sign in to comment.