Skip to content

Commit

Permalink
Merge pull request #43 from Team-SWAcademy/refactor/#41
Browse files Browse the repository at this point in the history
[refactor/#41] erd 수정 및 다회용기 이용 api 로직 수정
  • Loading branch information
Gyuhyeok99 authored May 14, 2024
2 parents f457c54 + 4939977 commit 2be30d8
Show file tree
Hide file tree
Showing 15 changed files with 96 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public class GetMyPageRes {
private boolean gender;

private int currentPoint;
private int useCount;
private int returnCount;
private int totalUseCount;
private int totalReturnCount;
private List<GetDailyStatisticsRes> dailyStatisticsResList;
private List<GetMonthlyStatisticsRes> monthlyStatisticsResList;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package carbonneutral.academy.api.converter.auth;

import carbonneutral.academy.api.controller.auth.dto.response.GetKakaoRes;
import carbonneutral.academy.api.controller.auth.dto.response.PatchAdditionalInfoRes;
import carbonneutral.academy.api.controller.auth.dto.response.PostSocialRes;
import carbonneutral.academy.domain.point.Point;
import carbonneutral.academy.domain.use_statistics.UseStatistics;
import carbonneutral.academy.domain.user.User;
import carbonneutral.academy.domain.user.enums.SocialType;
import lombok.AccessLevel;
Expand Down Expand Up @@ -41,4 +41,11 @@ public static Point toPoint(User user) {
.build();
}

public static UseStatistics toUseStatistics(User user) {
return UseStatistics.builder()
.user(user)
.totalUseCount(0)
.totalReturnCount(0)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package carbonneutral.academy.api.converter.user;

import carbonneutral.academy.api.controller.auth.dto.response.PatchAdditionalInfoRes;
import carbonneutral.academy.api.controller.use.dto.response.PatchReturnRes;
import carbonneutral.academy.api.controller.use.dto.response.statistics.daily.GetDailyStatisticsRes;
import carbonneutral.academy.api.controller.use.dto.response.GetMyPageRes;
import carbonneutral.academy.api.controller.use.dto.response.statistics.monthly.GetMonthlyStatisticsRes;
import carbonneutral.academy.api.controller.user.dto.response.PatchInfoRes;
import carbonneutral.academy.domain.location.Location;
import carbonneutral.academy.domain.point.Point;
import carbonneutral.academy.domain.use.Use;
import carbonneutral.academy.domain.use_statistics.UseStatistics;
import carbonneutral.academy.domain.user.User;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
Expand All @@ -24,13 +22,14 @@ public static PatchAdditionalInfoRes toPatchAdditionalInfoRes(User user) {
.isFinished(user.isFinished())
.build();
}
public static GetMyPageRes toGetMyPageRes(User user, List<GetDailyStatisticsRes> getDailyStatisticsResList, List<GetMonthlyStatisticsRes> monthlyStatisticsResList, Point point) {
public static GetMyPageRes toGetMyPageRes(User user, List<GetDailyStatisticsRes> getDailyStatisticsResList, List<GetMonthlyStatisticsRes> monthlyStatisticsResList,
Point point, UseStatistics useStatistics) {
return GetMyPageRes.builder()
.nickname(user.getNickname())
.gender(user.isGender())
.currentPoint(point.getAccumulatedPoint() - point.getUtilizedPoint())
.useCount(getDailyStatisticsResList.stream().mapToInt(GetDailyStatisticsRes::getUseCount).sum())
.returnCount(getDailyStatisticsResList.stream().mapToInt(GetDailyStatisticsRes::getReturnCount).sum())
.totalUseCount(useStatistics.getTotalUseCount())
.totalReturnCount(useStatistics.getTotalReturnCount())
.dailyStatisticsResList(getDailyStatisticsResList)
.monthlyStatisticsResList(monthlyStatisticsResList)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import carbonneutral.academy.api.service.auth.social.kakao.KakaoLoginService;
import carbonneutral.academy.common.exceptions.BaseException;
import carbonneutral.academy.domain.point.repository.PointJpaRepository;
import carbonneutral.academy.domain.use_statistics.repository.UseStatisticsJpaRepository;
import carbonneutral.academy.domain.user.User;
import carbonneutral.academy.domain.user.enums.SocialType;
import carbonneutral.academy.domain.user.repository.UserJpaRepository;
Expand All @@ -30,6 +31,7 @@ public class AuthServiceImpl implements AuthService {

private final UserJpaRepository userJpaRepository;
private final PointJpaRepository pointJpaRepository;
private final UseStatisticsJpaRepository useStatisticsJpaRepository;
private final PasswordEncoder passwordEncoder;
private final AuthenticationManager authenticationManager;
private final JwtProvider jwtProvider;
Expand All @@ -50,6 +52,7 @@ public PostSocialRes socialLogin(SocialType socialType, String authorizationCode
if (!isRegistered) {
User user = AuthConverter.toUser(getKakaoRes);
pointJpaRepository.save(AuthConverter.toPoint(user));
useStatisticsJpaRepository.save(AuthConverter.toUseStatistics(user));
userJpaRepository.save(user);
}
User user = userJpaRepository.findByUsernameAndState(getKakaoRes.getId(), ACTIVE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import carbonneutral.academy.common.code.status.ErrorStatus;
import carbonneutral.academy.common.exceptions.BaseException;
import carbonneutral.academy.domain.multi_use_container.MultiUseContainer;
import carbonneutral.academy.domain.multi_use_container.MultiUseContainerJpaRepository;
import carbonneutral.academy.domain.multi_use_container.repository.MultiUseContainerJpaRepository;
import carbonneutral.academy.domain.use.Use;
import carbonneutral.academy.domain.use.repository.UseJpaRepository;
import carbonneutral.academy.domain.user.User;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
import carbonneutral.academy.domain.mapping.LocationContainer;
import carbonneutral.academy.domain.mapping.repository.LocationContainerJpaRepository;
import carbonneutral.academy.domain.multi_use_container.MultiUseContainer;
import carbonneutral.academy.domain.multi_use_container.MultiUseContainerJpaRepository;
import carbonneutral.academy.domain.multi_use_container.repository.MultiUseContainerJpaRepository;
import carbonneutral.academy.domain.point.Point;
import carbonneutral.academy.domain.point.repository.PointJpaRepository;
import carbonneutral.academy.domain.use.Use;
import carbonneutral.academy.domain.use.repository.UseJpaRepository;
import carbonneutral.academy.domain.use_statistics.repository.UseStatisticsJpaRepository;
import carbonneutral.academy.domain.user.User;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -42,6 +43,7 @@ public class UseServiceImpl implements UseService {
private final MultiUseContainerJpaRepository multiUseContainerJpaRepository;
private final LocationContainerJpaRepository locationContainerJpaRepository;
private final PointJpaRepository pointJpaRepository;
private final UseStatisticsJpaRepository useStatisticsJpaRepository;

@Override
public GetHomeRes getInUsesMultipleTimeContainers(User user) {
Expand Down Expand Up @@ -88,6 +90,7 @@ public PostUseRes useMultipleTimeContainers(User user, PostUseReq postUseReq) {
.orElseThrow(() -> new BaseException(NOT_FIND_LOCATION));
Use use = UseConverter.toUse(user, location, postUseReq.getPoint(), postUseReq.getMultiUseContainerId());
useJpaRepository.save(use);
useStatisticsJpaRepository.findById(user.getId()).orElseThrow(() -> new BaseException(NOT_FIND_USE_STATISTICS)).addTotalUseCount();
return UseConverter.toPostUseRes(use);
}

Expand All @@ -112,6 +115,9 @@ public PatchReturnRes returnMultipleTimeContainers(User user, PatchReturnReq pat
Point userPoint = pointJpaRepository.findByUserId(user.getId())
.orElseThrow(() -> new BaseException(NOT_FIND_POINT));
userPoint.addPoint(use.getPoint());
useStatisticsJpaRepository.findById(user.getId())
.orElseThrow(() -> new BaseException(NOT_FIND_USE_STATISTICS))
.addTotalReturnCount();
return UseConverter.toPatchReturnRes(user, returnLocation, use);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import carbonneutral.academy.domain.point.Point;
import carbonneutral.academy.domain.point.repository.PointJpaRepository;
import carbonneutral.academy.domain.use.repository.UseQueryRepository;
import carbonneutral.academy.domain.use_statistics.UseStatistics;
import carbonneutral.academy.domain.use_statistics.repository.UseStatisticsJpaRepository;
import carbonneutral.academy.domain.user.User;
import carbonneutral.academy.domain.user.repository.UserJpaRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -36,6 +38,7 @@ public class UserServiceImpl implements UserService {
private final UserJpaRepository userJpaRepository;
private final UseQueryRepository useQueryRepository;
private final PointJpaRepository pointJpaRepository;
private final UseStatisticsJpaRepository useStatisticsJpaRepository;
@Override
@Transactional
public PatchAdditionalInfoRes additionalInfo(User user, PatchAdditionalInfoReq request) {
Expand All @@ -52,7 +55,9 @@ public GetMyPageRes mypage(User user) {
List<GetMonthlyStatisticsRes> monthlyStatisticsResList = getMonthlyStatistics(user);
Point point = pointJpaRepository.findByUserId(user.getId())
.orElseThrow(() -> new BaseException(NOT_FIND_POINT));
return UserConverter.toGetMyPageRes(user, dailyStatisticsResList, monthlyStatisticsResList, point);
UseStatistics useStatistics = useStatisticsJpaRepository.findById(user.getId())
.orElseThrow(() -> new BaseException(NOT_FIND_POINT));
return UserConverter.toGetMyPageRes(user, dailyStatisticsResList, monthlyStatisticsResList, point, useStatistics);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public enum ErrorStatus implements BaseErrorCode {
NOT_FIND_MULTI_USE_CONTAINER(HttpStatus.NOT_FOUND, "MULTI_USE_CONTAINER4000", "존재하지 않는 다회용기입니다."),

NOT_FIND_POINT(HttpStatus.NOT_FOUND, "POINT4000", "존재하지 않는 포인트입니다."),

NOT_FIND_USE_STATISTICS(HttpStatus.NOT_FOUND, "USE_STATISTICS4000", "존재하지 않는 이용 통계입니다."),
EMPTY_JWT(HttpStatus.UNAUTHORIZED, "JWT4000", "JWT를 입력해주세요"),
INVALID_JWT(HttpStatus.UNAUTHORIZED, "JWT4001", "유효하지 않은 JWT입니다."),
INVALID_USER_JWT(HttpStatus.FORBIDDEN, "JWT4002", "권한이 없는 유저의 접근입니다."),
Expand All @@ -63,6 +63,7 @@ public enum ErrorStatus implements BaseErrorCode {




private final HttpStatus httpStatus;
private final String code;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,16 @@
@Getter
@Builder
@Entity
@IdClass(LocationContainerId.class)
@Table(name = "location_container")
public class LocationContainer {

@Id
@Column(name = "location_container_id", nullable = false, updatable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "location_id")
private Location location;

@Id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "multi_use_container_id")
private MultiUseContainer multiUseContainer;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package carbonneutral.academy.domain.mapping;


import java.io.Serializable;

public class LocationContainerId implements Serializable {

private int location;
private int multiUseContainer;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import carbonneutral.academy.domain.location.Location;
import carbonneutral.academy.domain.mapping.LocationContainer;
import carbonneutral.academy.domain.mapping.LocationContainerId;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface LocationContainerJpaRepository extends JpaRepository<LocationContainer, Integer> {
public interface LocationContainerJpaRepository extends JpaRepository<LocationContainer, LocationContainerId> {

List<LocationContainer> findByMultiUseContainerId(int multiUseContainerId);
List<LocationContainer> findByLocation_Id(int locationId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package carbonneutral.academy.domain.multi_use_container;
package carbonneutral.academy.domain.multi_use_container.repository;

import carbonneutral.academy.domain.multi_use_container.MultiUseContainer;
import org.springframework.data.jpa.repository.JpaRepository;

public interface MultiUseContainerJpaRepository extends JpaRepository<MultiUseContainer, Integer> {
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/carbonneutral/academy/domain/point/Point.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package carbonneutral.academy.domain.point;

import carbonneutral.academy.common.BaseEntity;
import carbonneutral.academy.domain.user.User;
import jakarta.persistence.*;
import lombok.*;
Expand All @@ -16,8 +15,7 @@ public class Point {


@Id
@Column(name = "point_id", nullable = false, updatable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id", nullable = false, updatable = false)
private int id;

@OneToOne(fetch = FetchType.LAZY)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package carbonneutral.academy.domain.use_statistics;


import carbonneutral.academy.domain.user.User;
import jakarta.persistence.*;
import lombok.*;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
@Getter
@Builder
@Entity
@Table(name = "use_statistics")
public class UseStatistics {

@Id
@Column(name = "user_id", nullable = false, updatable = false)
private int id;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;
@Column(nullable = false, name = "total_use_count")
private int totalUseCount;

@Column(nullable = false, name = "total_return_count")
private int totalReturnCount;


public void addTotalUseCount() {
this.totalUseCount++;
}

public void addTotalReturnCount() {
this.totalReturnCount++;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package carbonneutral.academy.domain.use_statistics.repository;

import carbonneutral.academy.domain.use_statistics.UseStatistics;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UseStatisticsJpaRepository extends JpaRepository<UseStatistics, Integer> {
}

0 comments on commit 2be30d8

Please sign in to comment.