Skip to content

Commit

Permalink
sprint/2-5 (#18)
Browse files Browse the repository at this point in the history
* fix: fix thumbs up bug

* refactor: change ordering

* fix: add worker id to param

* fix: fix test code

* refactor: extract method

* test: add access key for s3

* test: add access key for s3

* test: add access key for s3

* refactor: modify response of api

* fix: delete thumps-up when deleting a photo feed

* fix: modify ordering
  • Loading branch information
aiaiaiai1 authored Dec 30, 2024
1 parent 52433c0 commit 357afd9
Show file tree
Hide file tree
Showing 14 changed files with 104 additions and 30 deletions.
10 changes: 10 additions & 0 deletions src/main/java/gymmi/global/S3Config.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package gymmi.global;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
Expand All @@ -10,10 +12,18 @@
@Configuration
public class S3Config {

@Value("${cloud.aws.s3.access-key}")
private String accessKey;

@Value("${cloud.aws.s3.secret-key}")
private String secretKey;

@Bean
public AmazonS3 amazonS3() {
BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(accessKey, secretKey);
return AmazonS3ClientBuilder.standard()
.withRegion(Regions.AP_NORTHEAST_2)
.withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

import gymmi.photoboard.domain.entity.ThumbsUp;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.Optional;

public interface ThumbsUpRepository extends JpaRepository<ThumbsUp, Long> {


@Query("select t from ThumbsUp t where t.user.id =:userId")
Optional<ThumbsUp> findByUserId(Long userId);
@Query("select t from ThumbsUp t where t.user.id =:userId and t.photoFeed.id =:photoFeedId")
Optional<ThumbsUp> findByUserIdAndPhotoFeedId(Long userId, Long photoFeedId);

@Modifying(flushAutomatically = true, clearAutomatically = true)
@Query("delete from ThumbsUp t where t.photoFeed.id =:photoFeedId")
void deleteByPhotoFeedId(Long photoFeedId);
}
6 changes: 4 additions & 2 deletions src/main/java/gymmi/photoboard/service/PhotoFeedService.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public PhotoFeedDetailResponse getPhotoFeed(User loginedUser, Long photoFeedId)
PhotoFeed photoFeed = photoFeedRepository.getByPhotoFeedId(photoFeedId);
PhotoFeedImage photoFeedImage = photoFeedImageRepository.getByPhotoFeedId(photoFeedId);
String photoImagePresignedUrl = s3Service.getPresignedUrl(PhotoFeedImage.IMAGE_USE, photoFeedImage.getFilename());
if (thumbsUpRepository.findByUserId(loginedUser.getId()).isEmpty()) {
if (thumbsUpRepository.findByUserIdAndPhotoFeedId(loginedUser.getId(), photoFeed.getId()).isEmpty()) {
return new PhotoFeedDetailResponse(photoFeed, photoImagePresignedUrl, photoFeed.isWriter(loginedUser), false);
}
return new PhotoFeedDetailResponse(photoFeed, photoImagePresignedUrl, photoFeed.isWriter(loginedUser), true);
Expand All @@ -56,7 +56,7 @@ public PhotoFeedDetailResponse getPhotoFeed(User loginedUser, Long photoFeedId)
@Transactional
public void likePhotoFeed(User loginedUser, Long photoFeedId) {
PhotoFeed photoFeed = photoFeedRepository.getByPhotoFeedId(photoFeedId);
thumbsUpRepository.findByUserId(loginedUser.getId())
thumbsUpRepository.findByUserIdAndPhotoFeedId(loginedUser.getId(), photoFeed.getId())
.ifPresentOrElse(
(thumbsUp) -> {
photoFeed.decrease();
Expand All @@ -78,7 +78,9 @@ public void delete(User loginedUser, Long photoFeedId) {

PhotoFeedImage photoFeedImage = photoFeedImageRepository.getByPhotoFeedId(photoFeedId);
photoFeedImageRepository.delete(photoFeedImage);
thumbsUpRepository.deleteByPhotoFeedId(photoFeed.getId());
photoFeedRepository.delete(photoFeed);
// event?
s3Service.delete(PhotoFeedImage.IMAGE_USE, photoFeedImage.getFilename());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,13 @@ public ResponseEntity<List<FavoriteMissionResponse>> seeFavoriteMissions(
}

@GetMapping("/workspaces/{workspaceId}/workout-confirmations")
public ResponseEntity<List<WorkoutConfirmationOrObjectionResponse>> seeWorkoutConfirmations(
public ResponseEntity<WorkoutConfirmationResponse> seeWorkoutConfirmations(
@Logined User user,
@PathVariable Long workspaceId,
@RequestParam int pageNumber
) {
List<WorkoutConfirmationOrObjectionResponse> responses = workspaceQueryService.getWorkoutConfirmations(user, workspaceId, pageNumber);
return ResponseEntity.ok().body(responses);
WorkoutConfirmationResponse response = workspaceQueryService.getWorkoutConfirmations(user, workspaceId, pageNumber);
return ResponseEntity.ok().body(response);
}

@GetMapping("/workspaces/{workspaceId}/workout-confirmations/{workoutConfirmationId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public interface WorkoutHistoryCustomRepository {
List<WorkoutHistory> getAllByWorkspaceId(Long workspaceId, Pageable pageable);

List<WorkoutHistory> getAllByDate(LocalDate localDate);
List<WorkoutHistory> getAllByDate(Long workerId, LocalDate localDate);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package gymmi.workspace.repository.custom;

import com.querydsl.core.Tuple;
import com.querydsl.jpa.impl.JPAQueryFactory;
import gymmi.workspace.domain.entity.Objection;
import gymmi.workspace.domain.entity.WorkoutHistory;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
Expand All @@ -11,7 +9,6 @@
import java.time.LocalDateTime;
import java.util.List;

import static gymmi.workspace.domain.entity.QObjection.objection;
import static gymmi.workspace.domain.entity.QWorker.worker;
import static gymmi.workspace.domain.entity.QWorkoutConfirmation.workoutConfirmation;
import static gymmi.workspace.domain.entity.QWorkoutHistory.workoutHistory;
Expand Down Expand Up @@ -46,4 +43,16 @@ public List<WorkoutHistory> getAllByDate(LocalDate now) {
.fetch();
}

@Override
public List<WorkoutHistory> getAllByDate(Long workerId, LocalDate now) {
LocalDateTime startDay = now.atStartOfDay();
LocalDateTime endDay = now.plusDays(1).atStartOfDay();
return jpaQueryFactory.select(workoutHistory)
.from(workoutHistory)
.where(workoutHistory.createdAt.goe(startDay).and(workoutHistory.createdAt.before(endDay)
.and(workoutHistory.worker.id.eq(workerId))
))
.fetch();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package gymmi.workspace.response;

import lombok.Getter;

import java.util.List;

@Getter
public class WorkoutConfirmationResponse {

private final List<WorkoutConfirmationOrObjectionResponse> data;
private final Integer voteIncompletionCount;

public WorkoutConfirmationResponse(List<WorkoutConfirmationOrObjectionResponse> data, Integer voteIncompletionCount) {
this.data = data;
this.voteIncompletionCount = voteIncompletionCount;
}
}
20 changes: 14 additions & 6 deletions src/main/java/gymmi/workspace/service/WorkspaceCommandService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -113,10 +116,9 @@ public Integer workMissionsInWorkspace(
Worker worker = workerRepository.getByUserIdAndWorkspaceId(loginedUser.getId(), workspace.getId());
List<Mission> missions = missionRepository.getAllByWorkspaceId(workspace.getId());
Map<Mission, Integer> workouts = getWorkouts(workoutRequest.getMissions());
validateDailyWorkoutHistoryCount();
validateDailyWorkoutHistoryCount(worker.getId());

WorkspaceProgressManager workspaceProgressManager = new WorkspaceProgressManager(workspace, missions);

WorkoutHistory workoutHistory = workspaceProgressManager.doWorkout(
worker,
workouts,
Expand All @@ -129,15 +131,19 @@ public Integer workMissionsInWorkspace(
int achievementScore = workspaceRepository.getAchievementScore(workspaceId);

workspaceProgressManager.completeWhenGoalScoreIsAchieved(achievementScore);
linkToPhotoBoardIfRequested(loginedUser, workoutRequest);
return workoutHistory.getSum();
}

private void linkToPhotoBoardIfRequested(User loginedUser, WorkoutRequest workoutRequest) {
if (workoutRequest.getWillLink()) {
String filename = s3Service.copy(WorkoutConfirmation.IMAGE_USE, workoutRequest.getImageUrl(), PhotoFeedImage.IMAGE_USE);
photoFeedService.createPhotoFeed(loginedUser, new CreatePhotoFeedRequest(filename, workoutRequest.getComment()));
}
return workoutHistory.getSum();
}

private void validateDailyWorkoutHistoryCount() {
List<WorkoutHistory> workoutHistories = workoutHistoryRepository.getAllByDate(LocalDate.now());
private void validateDailyWorkoutHistoryCount(Long workerId) {
List<WorkoutHistory> workoutHistories = workoutHistoryRepository.getAllByDate(workerId, LocalDate.now());
if (workoutHistories.size() >= 3) {
throw new InvalidStateException(ErrorCode.EXCEED_MAX_DAILY_WORKOUT_HISTORY_COUNT);
}
Expand Down Expand Up @@ -215,6 +221,7 @@ public void objectToWorkoutConfirmation(User loginedUser, Long workspaceId, Long
.workoutConfirmation(workoutHistory.getWorkoutConfirmation())
.build();
objectionRepository.save(objection);
//리펙터링
}

public void voteToObjection(User loginedUser, Long workspaceId, Long objectionId, VoteRequest request) {
Expand All @@ -239,6 +246,7 @@ private void rejectWorkoutHistory(ObjectionManager objectionManager, WorkoutHist
if (objectionManager.isApproved()) {
workoutHistory.cancel();
}

}


Expand Down
13 changes: 10 additions & 3 deletions src/main/java/gymmi/workspace/service/WorkspaceQueryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -166,11 +167,13 @@ public List<FavoriteMissionResponse> getFavoriteMissions(User loginedUser, Long
.toList();
}

public List<WorkoutConfirmationOrObjectionResponse> getWorkoutConfirmations(User loginedUser, Long workspaceId, int page) {
public WorkoutConfirmationResponse getWorkoutConfirmations(User loginedUser, Long workspaceId, int page) {
Workspace workspace = workspaceRepository.getWorkspaceById(workspaceId);
validateIfWorkerIsInWorkspace(loginedUser.getId(), workspace.getId());
Worker worker = validateIfWorkerIsInWorkspace(loginedUser.getId(), workspace.getId());
List<WorkoutConfirmationOrObjectionProjection> dtos = workoutHistoryRepository.getWorkoutConfirmationAndObjectionDto(workspace.getId(), page);
dtos.sort(Comparator.comparing(WorkoutConfirmationOrObjectionProjection::getCreatedAt));

int voteIncompletionCount = 0;
List<WorkoutConfirmationOrObjectionResponse> responses = new ArrayList<>();
for (WorkoutConfirmationOrObjectionProjection dto : dtos) {
if (dto.getType().equals("workoutHistory")) {
Expand All @@ -184,9 +187,13 @@ public List<WorkoutConfirmationOrObjectionResponse> getWorkoutConfirmations(User
Objection objection = objectionRepository.getByObjectionId(dto.getId());
WorkoutHistory workoutHistory = workoutHistoryRepository.getByWorkoutConfirmationId(objection.getWorkoutConfirmation().getId());
responses.add(WorkoutConfirmationOrObjectionResponse.objection(loginedUser, objection, workoutHistory.getWorker().getUser()));
if (objection.isInProgress() && !objection.hasVoteBy(worker)) {
voteIncompletionCount++;
}
}
}
return responses;

return new WorkoutConfirmationResponse(responses, voteIncompletionCount);
}

public WorkoutConfirmationDetailResponse getWorkoutConfirmation(User loginedUser, Long workspaceId, Long workoutConfirmationId) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ cloud:
aws:
s3:
bucket: ${BUCKET_NAME}
access-key: ${AK}
secret-key: ${SK}
stack:
auto: false

7 changes: 7 additions & 0 deletions src/test/java/gymmi/helper/Persister.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import gymmi.entity.User;
import gymmi.photoboard.domain.entity.PhotoFeed;
import gymmi.photoboard.domain.entity.PhotoFeedImage;
import gymmi.photoboard.domain.entity.ThumbsUp;
import gymmi.repository.UserRepository;
import gymmi.workspace.domain.WorkspaceStatus;
import gymmi.workspace.domain.entity.*;
Expand Down Expand Up @@ -230,4 +231,10 @@ public PhotoFeedImage persistPhotoFeedImage(PhotoFeed photoFeed) {
entityManager.persist(photoFeedImage);
return photoFeedImage;
}

public ThumbsUp persistThumpsUp(User user, PhotoFeed photoFeed) {
ThumbsUp thumbsUp = new ThumbsUp(user, photoFeed);
entityManager.persist(thumbsUp);
return thumbsUp;
}
}
10 changes: 6 additions & 4 deletions src/test/java/gymmi/photoboard/service/PhotoFeedServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,15 @@ class PhotoFeedServiceTest extends IntegrationTest {
void 사진_피드를_확인_한다() {
// given
User user = persister.persistUser();
PhotoFeed photoFeed = persister.persistPhotoFeed(user, false);

PhotoFeed photoFeed = persister.persistPhotoFeed(user, true);
PhotoFeedImage photoFeedImage = persister.persistPhotoFeedImage(photoFeed);

// when
PhotoFeedDetailResponse result = photoFeedService.getPhotoFeed(user, photoFeed.getId());

// then
assertThat(result.getIsModified()).isFalse();
assertThat(result.getIsModified()).isTrue();
assertThat(result.getThumpsUpCount()).isEqualTo(photoFeed.getThumpsUpCount());
assertThat(result.getCreatedAt()).isEqualTo(photoFeed.getCreatedAt());
assertThat(result.getProfileImageUrl()).isEqualTo(user.getProfileImageName());
Expand All @@ -89,14 +90,14 @@ class PhotoFeedServiceTest extends IntegrationTest {

// then
assertThat(photoFeed.getThumpsUpCount()).isEqualTo(1);
assertThat(thumbsUpRepository.findByUserId(user1.getId())).isNotEmpty();
assertThat(thumbsUpRepository.findByUserIdAndPhotoFeedId(user1.getId(), photoFeed.getId())).isNotEmpty();

// when
photoFeedService.likePhotoFeed(user1, photoFeed.getId());

// then
assertThat(photoFeed.getThumpsUpCount()).isEqualTo(0);
assertThat(thumbsUpRepository.findByUserId(user1.getId())).isEmpty();
assertThat(thumbsUpRepository.findByUserIdAndPhotoFeedId(user1.getId(), photoFeed.getId())).isEmpty();
}

@Test
Expand All @@ -105,6 +106,7 @@ class PhotoFeedServiceTest extends IntegrationTest {
User user = persister.persistUser();
PhotoFeed photoFeed = persister.persistPhotoFeed(user);
PhotoFeedImage photoFeedImage = persister.persistPhotoFeedImage(photoFeed);
persister.persistThumpsUp(user, photoFeed);

// when
photoFeedService.delete(user, photoFeed.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,22 @@ class WorkspaceQueryServiceTest extends IntegrationTest {
WorkoutHistory workoutHistory = persister.persistWorkoutHistoryAndApply(creatorWorker, Map.of(mission, 2, mission1, 2), workoutConfirmation);
Objection objection = persister.persistObjection(userWorker, true, workoutConfirmation);
WorkoutHistory workoutHistory1 = persister.persistWorkoutHistoryAndApply(userWorker, Map.of(mission, 2, mission1, 2), workoutConfirmation1);

persister.persistVote(userWorker, objection, false);

// when
List<WorkoutConfirmationOrObjectionResponse> responses = workspaceQueryService.getWorkoutConfirmations(user, workspace.getId(), 0);
WorkoutConfirmationResponse workoutConfirmationResponse = workspaceQueryService.getWorkoutConfirmations(user, workspace.getId(), 0);

// then
List<WorkoutConfirmationOrObjectionResponse> responses = workoutConfirmationResponse.getData();
assertThat(responses).hasSize(3);
assertThat(workoutConfirmationResponse.getVoteIncompletionCount()).isEqualTo(0);

WorkoutConfirmationOrObjectionResponse response = responses.get(0);
assertThat(response.getIsMine()).isEqualTo(true);
assertThat(response.getProfileImageUrl()).isEqualTo(user.getProfileImageName());
assertThat(response.getWorkoutConfirmationId()).isEqualTo(workoutHistory1.getWorkoutConfirmation().getId());
assertThat(response.getIsMine()).isEqualTo(false);
assertThat(response.getProfileImageUrl()).isEqualTo(creator.getProfileImageName());
assertThat(response.getWorkoutConfirmationId()).isEqualTo(workoutHistory.getWorkoutConfirmation().getId());
assertThat(response.getIsObjection()).isEqualTo(false);
assertThat(response.getObjectionId()).isEqualTo(null);
assertThat(response.getObjectionId()).isEqualTo(objection.getId());

WorkoutConfirmationOrObjectionResponse response1 = responses.get(1);
assertThat(response1.getNickname()).isEqualTo(creator.getNickname());
Expand Down Expand Up @@ -233,7 +235,7 @@ class 이의_신청_목록 {
List<ObjectionAlarmResponse> responses = workspaceQueryService.getObjections(creator, workspace.getId(), 0, ObjectionStatus.INCOMPLETION);

// then
assertThat(responses).hasSize (7);
assertThat(responses).hasSize(7);
assertThat(responses.get(6).getObjectionId()).isEqualTo(objections.get(2).getId());
assertThat(responses.get(6).getVoteCompletion()).isEqualTo(false);
assertThat(responses.get(6).getTargetWorkerNickname()).isEqualTo(creator.getNickname());
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/application-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@ cloud:
aws:
s3:
bucket: abc
accesskey: abc
secretKey: abc
stack:
auto: false

0 comments on commit 357afd9

Please sign in to comment.