Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛Fix: 현재 접속한 유저 정보 확인 후 PDS 작성/수정/삭제 가능하게 수정 #32

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public Category createCategory(long userId, CategoryRequestDTO categoryRequestDT
public void updateCategory(long userId, long categoryId, CategoryRequestDTO categoryRequestDTO) {
Category category = categoryRepository.findById(categoryId)
.orElseThrow(() -> new CustomException(ErrorStatus.CATEGORY_NOT_FOUND_ERROR));
findUser(userId);
if (category.getUser().getId() != userId){
throw new CustomException(ErrorStatus.NO_PERMISSION_ERROR);
}
Expand All @@ -58,9 +59,15 @@ public void updateCategory(long userId, long categoryId, CategoryRequestDTO cate
public void deleteCategory(long userId, long categoryId) {
Category category = categoryRepository.findById(categoryId)
.orElseThrow(() -> new CustomException(ErrorStatus.CATEGORY_NOT_FOUND_ERROR));
findUser(userId);
if (category.getUser().getId() != userId){
throw new CustomException(ErrorStatus.NO_PERMISSION_ERROR);
}
categoryRepository.delete(category);
}

public void findUser(long userId){
User user = userRepository.findById(userId)
.orElseThrow(() -> new CustomException(ErrorStatus.USER_NOT_FOUND_ERROR));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.trendithon.timetris.domain.mainpage.repository.DateRepository;
import com.trendithon.timetris.domain.mainpage.repository.DoRepository;
import com.trendithon.timetris.domain.mainpage.repository.UserDateRepository;
import com.trendithon.timetris.domain.member.domain.User;
import com.trendithon.timetris.domain.member.repository.UserRepository;
import com.trendithon.timetris.global.exception.CustomException;
import com.trendithon.timetris.global.exception.enums.ErrorStatus;
import lombok.RequiredArgsConstructor;
Expand All @@ -31,9 +33,11 @@ public class DoServiceImpl implements DoService{
private final DateRepository dateRepository;
private final UserDateRepository userDateRepository;
private final CategoryRepository categoryRepository;
private final UserRepository userRepository;

@Override
public Do createDo(long userId, DoRequestDTO doRequestDTO) {
findUser(userId);
LocalDate localDate = LocalDate.now();
Date date = dateRepository.findByDate(localDate);
UserDate userDate = userDateRepository.findByUser_IdAndDate_Id(userId, date.getId());
Expand All @@ -50,6 +54,7 @@ public Do createDo(long userId, DoRequestDTO doRequestDTO) {

@Override
public void updateDo(long userId, long doId, DoRequestDTO doRequestDTO) {
findUser(userId);
Do done = doRepository.findById(doId)
.orElseThrow(() -> new CustomException(ErrorStatus.DO_NOT_FOUND_ERROR));
if (done.getUserDate().getUser().getId() != userId){
Expand All @@ -67,11 +72,17 @@ public void updateDo(long userId, long doId, DoRequestDTO doRequestDTO) {

@Override
public void deleteDo(long userId, long doId) {
findUser(userId);
Do done = doRepository.findById(doId)
.orElseThrow(() -> new CustomException(ErrorStatus.DO_NOT_FOUND_ERROR));
if (done.getUserDate().getUser().getId() != userId){
throw new CustomException(ErrorStatus.NO_PERMISSION_ERROR);
}
doRepository.delete(done);
}

public void findUser(long userId){
User user = userRepository.findById(userId)
.orElseThrow(() -> new CustomException(ErrorStatus.USER_NOT_FOUND_ERROR));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.trendithon.timetris.domain.mainpage.domain.*;
import com.trendithon.timetris.domain.mainpage.dto.*;
import com.trendithon.timetris.domain.mainpage.repository.*;
import com.trendithon.timetris.domain.member.domain.User;
import com.trendithon.timetris.domain.member.repository.UserRepository;
import com.trendithon.timetris.global.exception.CustomException;
import com.trendithon.timetris.global.exception.enums.ErrorStatus;
import lombok.RequiredArgsConstructor;
Expand All @@ -24,12 +26,13 @@ public class PlanServiceImpl implements PlanService {
private final DateRepository dateRepository;
private final UserDateRepository userDateRepository;
private final CategoryRepository categoryRepository;
private final CycleRepository cycleRepository;
private final UserRepository userRepository;

@Override
public Plan createPlan(long userId, PlanRequestDTO planRequestDTO) {
LocalDate localDate = LocalDate.now();
Date date = dateRepository.findByDate(localDate);
findUser(userId);
UserDate userDate = userDateRepository.findByUser_IdAndDate_Id(userId, date.getId());
Category category = categoryRepository.findById(planRequestDTO.getCategoryId())
.orElseThrow(() -> new CustomException(ErrorStatus.CATEGORY_NOT_FOUND_ERROR));
Expand All @@ -45,6 +48,7 @@ public Plan createPlan(long userId, PlanRequestDTO planRequestDTO) {

@Override
public void updatePlan(long userId, long planId, PlanRequestDTO planRequestDTO) {
findUser(userId);
Plan plan = planRepository.findById(planId)
.orElseThrow(() -> new CustomException(ErrorStatus.PLAN_NOT_FOUND_ERROR));
long categoryId = planRequestDTO.getCategoryId();
Expand All @@ -63,6 +67,7 @@ public void updatePlan(long userId, long planId, PlanRequestDTO planRequestDTO)

@Override
public void deletePlan(long userId, long planId) {
findUser(userId);
Plan plan = planRepository.findById(planId)
.orElseThrow(() -> new CustomException(ErrorStatus.PLAN_NOT_FOUND_ERROR));
if (plan.getUserDate().getUser().getId() != userId) {
Expand All @@ -73,6 +78,7 @@ public void deletePlan(long userId, long planId) {

@Override
public void donePlan(long userId, long planId) {
findUser(userId);
Plan plan = planRepository.findById(planId)
.orElseThrow(() -> new CustomException(ErrorStatus.PLAN_NOT_FOUND_ERROR));
if (plan.getUserDate().getUser().getId() != userId) {
Expand All @@ -81,4 +87,9 @@ public void donePlan(long userId, long planId) {
plan.donePlan();
planRepository.save(plan);
}

public void findUser(long userId){
User user = userRepository.findById(userId)
.orElseThrow(() -> new CustomException(ErrorStatus.USER_NOT_FOUND_ERROR));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import com.trendithon.timetris.domain.mainpage.repository.DateRepository;
import com.trendithon.timetris.domain.mainpage.repository.SeeRepository;
import com.trendithon.timetris.domain.mainpage.repository.UserDateRepository;
import com.trendithon.timetris.domain.member.domain.User;
import com.trendithon.timetris.domain.member.repository.UserRepository;
import com.trendithon.timetris.global.exception.CustomException;
import com.trendithon.timetris.global.exception.enums.ErrorStatus;
import lombok.RequiredArgsConstructor;
Expand All @@ -29,9 +31,11 @@ public class SeeServiceImpl implements SeeService{
private final SeeRepository seeRepository;
private final DateRepository dateRepository;
private final UserDateRepository userDateRepository;
private final UserRepository userRepository;

@Override
public See createSee(long userId, SeeRequestDTO seeRequestDTO) {
findUser(userId);
LocalDate localDate = LocalDate.now();
Date date = dateRepository.findByDate(localDate);
UserDate userDate = userDateRepository.findByUser_IdAndDate_Id(userId, date.getId());
Expand All @@ -45,6 +49,7 @@ public See createSee(long userId, SeeRequestDTO seeRequestDTO) {

@Override
public void updateSee(long userId, long seeId, SeeRequestDTO seeRequestDTO) {
findUser(userId);
See see = seeRepository.findById(seeId)
.orElseThrow(() -> new CustomException(ErrorStatus.SEE_NOT_FOUND_ERROR));
if (see.getUserDate().getUser().getId() != userId){
Expand All @@ -56,11 +61,17 @@ public void updateSee(long userId, long seeId, SeeRequestDTO seeRequestDTO) {

@Override
public void deleteSee(long userId, long seeId) {
findUser(userId);
See see = seeRepository.findById(seeId)
.orElseThrow(() -> new CustomException(ErrorStatus.SEE_NOT_FOUND_ERROR));
if (see.getUserDate().getUser().getId() != userId){
throw new CustomException(ErrorStatus.NO_PERMISSION_ERROR);
}
seeRepository.delete(see);
}

public void findUser(long userId){
User user = userRepository.findById(userId)
.orElseThrow(() -> new CustomException(ErrorStatus.USER_NOT_FOUND_ERROR));
}
}