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

[#15] Plan API 구현 #25

Merged
merged 3 commits into from
Apr 16, 2022
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
25 changes: 21 additions & 4 deletions src/main/java/planshare/server/planshare/domain/Plan.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package planshare.server.planshare.domain;

import lombok.*;
import org.springframework.security.core.parameters.P;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.time.LocalDate;

@Entity
@Data
Expand All @@ -18,9 +19,7 @@ public class Plan {

private String name;

private LocalDateTime start;

private LocalDateTime end;
private LocalDate date;

// 체크 여부
private boolean checkStatus;
Expand All @@ -29,4 +28,22 @@ public class Plan {
@JoinColumn(name = "g_id")
private Goal goal;

public static Plan createPlan(String name, LocalDate date, boolean checkStatus, Goal goal){
Plan plan = new Plan();
plan.name = name;
plan.date = date;
plan.checkStatus = checkStatus;
plan.goal = goal;

return plan;
}

public void modifyName(String name){
this.name = name;
}

public void modifyCheck(boolean checkStatus){
this.checkStatus = checkStatus;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package planshare.server.planshare.plan.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import planshare.server.planshare.domain.Plan;
import planshare.server.planshare.plan.dto.PlanForm;
import planshare.server.planshare.plan.dto.PlanName;
import planshare.server.planshare.plan.service.PlanService;
import planshare.server.planshare.user.dto.CustomUserDetailsVO;

import java.util.List;
import java.util.Optional;

@RequiredArgsConstructor
@RestController
public class PlanController {

private final PlanService planService;

@ApiOperation(value = "plan 생성 API", notes = "생성한 plan 객체 반환")
@PostMapping("/goals/{goalId}/plans")
public Plan create(@AuthenticationPrincipal CustomUserDetailsVO userDetailsVO,
@PathVariable(name = "goalId") Long goalId,
@RequestBody PlanForm planForm){
System.out.println("planController : goalId : "+goalId+" , body "+planForm);
return planService.addPlan(userDetailsVO, goalId, planForm);
}

@ApiOperation(value = "plan 하나 조회(by goalId + planId) API", notes = "찾은 plan 객체 반환")
@GetMapping("/goals/{goalId}/plans/{planId}")
public Optional<Plan> readPlanByPlanId(@AuthenticationPrincipal CustomUserDetailsVO userDetailsVO,
@PathVariable(name = "goalId") Long goalId,
@PathVariable(name = "planId") Long planId){
return planService.findPlanOfId(goalId, planId);
}

@ApiOperation(value = "해당 goal에 속한 plan 목록 조회 API", notes = "plan 리스트 반환")
@GetMapping("/goals/{goalId}/plans")
public List<Plan> readPlansByGoal(@AuthenticationPrincipal CustomUserDetailsVO userDetailsVO,
@PathVariable(name = "goalId") Long goalId){
return planService.findPlansOfGoal(goalId);
}

@ApiOperation(value = "특정 plan 이름 수정 API", notes = "수정한 plan 객체 반환")
@PutMapping("/goals/{goalId}/plans/{planId}")
public Plan modifyNameOfPlan(@AuthenticationPrincipal CustomUserDetailsVO userDetailsVO,
@PathVariable(name = "goalId") Long goalId,
@PathVariable(name = "planId") Long planId,
@RequestBody PlanName planName){
return planService.updatePlanName(userDetailsVO, goalId, planId, planName.getName());
}

@ApiOperation(value = "특정 plan 완료여부 수정 API", notes = "수정한 plan 객체 반환")
@PutMapping("/goals/{goalId}/plans/{planId}/check")
public Plan modifyCheckOfPlan(@AuthenticationPrincipal CustomUserDetailsVO userDetailsVO,
@PathVariable(name = "goalId") Long goalId,
@PathVariable(name = "planId") Long planId){
return planService.updatePlanStatus(userDetailsVO, goalId, planId);
}

@ApiOperation(value = "특정 plan 삭제 API", notes = "삭제 성공시 정수 1 반환 / 실패시 0 반환")
@DeleteMapping("/goals/{goalId}/plans/{planId}")
public int deletePlan(@AuthenticationPrincipal CustomUserDetailsVO userDetailsVO,
@PathVariable(name = "goalId") Long goalId,
@PathVariable(name = "planId") Long planId){
return planService.deletePlan(userDetailsVO, goalId, planId);
}

}
19 changes: 19 additions & 0 deletions src/main/java/planshare/server/planshare/plan/dto/PlanForm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package planshare.server.planshare.plan.dto;

import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDate;

@Getter
@Setter
@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class)
public class PlanForm {

private String name;

private LocalDate date;

}
15 changes: 15 additions & 0 deletions src/main/java/planshare/server/planshare/plan/dto/PlanName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package planshare.server.planshare.plan.dto;

import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class)
public class PlanName {

private String name;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package planshare.server.planshare.plan.repository;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import planshare.server.planshare.domain.Goal;
import planshare.server.planshare.domain.Plan;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

@Transactional
@RequiredArgsConstructor
@Repository
public class JpaPlanRepository implements PlanRepository{

private final EntityManager em;

@Override
public Plan save(Plan plan) {
if (plan.getId() == null){
System.out.println("planRepo : create : persist :");
em.persist(plan);
} else {
System.out.println("planRepo : update : merge : ");
em.merge(plan);
}
return plan;
}

@Override
public Optional<Plan> findById(Long id) {
Plan plan = em.find(Plan.class, id);
return Optional.ofNullable(plan);
}

@Override
public List<Plan> findByGoal(Goal goal) {
return em.createQuery("select p from Plan p where p.goal.id = :goalId", Plan.class)
.setParameter("goalId", goal.getId())
.getResultList();
}

@Override
public List<Plan> findByDate(LocalDate date) {
return em.createQuery("select p from Plan p where p.date = :planDate", Plan.class)
.setParameter("planDate", date)
.getResultList();
}

@Override
public List<Plan> findAll() {
return em.createQuery("select p from Plan p", Plan.class)
.getResultList();
}

@Override
public int deleteById(Long id) {
Query query = em.createQuery("delete from Plan p where p.id = :planId")
.setParameter("planId", id);
int rows = query.executeUpdate();
return rows;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package planshare.server.planshare.plan.repository;

import planshare.server.planshare.domain.Goal;
import planshare.server.planshare.domain.Plan;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

public interface PlanRepository {

Plan save(Plan plan);
Optional<Plan> findById(Long id);
List<Plan> findByGoal(Goal goal);
List<Plan> findByDate(LocalDate date);
List<Plan> findAll();
int deleteById(Long id);


}
125 changes: 125 additions & 0 deletions src/main/java/planshare/server/planshare/plan/service/PlanService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package planshare.server.planshare.plan.service;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import planshare.server.planshare.domain.Goal;
import planshare.server.planshare.domain.Member;
import planshare.server.planshare.domain.Plan;
import planshare.server.planshare.goal.repository.GoalRepository;
import planshare.server.planshare.plan.dto.PlanForm;
import planshare.server.planshare.plan.repository.PlanRepository;
import planshare.server.planshare.repository.MemberRepository;
import planshare.server.planshare.user.dto.CustomUserDetailsVO;

import java.util.List;
import java.util.Optional;

@RequiredArgsConstructor
@Service
public class PlanService {

private final PlanRepository planRepository;
private final GoalRepository goalRepository;
private final MemberRepository memberRepository;

/**
* plan store
*/
public Plan addPlan(CustomUserDetailsVO userDetailsVO, Long goalId, PlanForm planForm){

Optional<Member> member = memberRepository.findByEmail(userDetailsVO.getUsername());
Optional<Goal> goal = goalRepository.findById(goalId);

// 사용자와 goal 사용자가 같은 지 확인
if(member.get().getId() == goal.get().getMember().getId()){
Plan plan = Plan.createPlan(planForm.getName(), planForm.getDate(), false, goal.get());

System.out.println("service : "+goalId);
return planRepository.save(plan);
} else{
return null;
}
}

/**
* select plan by planId
*/
public Optional<Plan> findPlanOfId(Long goalId, Long planId){

Optional<Plan> plan = planRepository.findById(planId);
if (plan.get().getGoal().getId() == goalId){
return plan;
} else {
return null;
}
}

/**
* select plan list by goalId
*/
public List<Plan> findPlansOfGoal(Long goalId){

Optional<Goal> goal = goalRepository.findById(goalId);
if(goal.isPresent()){
return planRepository.findByGoal(goal.get());
} else {
return null;
}
}

/**
* plan name update
*/
public Plan updatePlanName(CustomUserDetailsVO userDetailsVO, Long goalId, Long planId, String name){

Optional<Member> member = memberRepository.findByEmail(userDetailsVO.getUsername());
Optional<Goal> goal = goalRepository.findById(goalId);
Optional<Plan> plan = planRepository.findById(planId);

if (member.get().getId() == goal.get().getMember().getId() && goalId == plan.get().getGoal().getId()){
plan.get().modifyName(name);

return planRepository.save(plan.get());
} else {
return null;
}
}

/**
* plan checkStatus update
*/
public Plan updatePlanStatus(CustomUserDetailsVO userDetailsVO, Long goalId, Long planId){

Optional<Member> member = memberRepository.findByEmail(userDetailsVO.getUsername());
Optional<Goal> goal = goalRepository.findById(goalId);
Optional<Plan> plan = planRepository.findById(planId);

if (member.get().getId() == goal.get().getMember().getId() && goalId == plan.get().getGoal().getId()){
if (plan.get().isCheckStatus() == false){
plan.get().modifyCheck(true);
} else {
plan.get().modifyCheck(false);
}
return planRepository.save(plan.get());
} else {
return null;
}
}

/**
* plan delete
*/
public int deletePlan(CustomUserDetailsVO userDetailsVO, Long goalId, Long planId){

int rows = 0;
Optional<Member> member = memberRepository.findByEmail(userDetailsVO.getUsername());
Optional<Goal> goal = goalRepository.findById(goalId);
Optional<Plan> plan = planRepository.findById(planId);

if (member.get().getId() == goal.get().getMember().getId() && goalId == plan.get().getGoal().getId()){
rows = planRepository.deleteById(planId);
}
return rows;
}

}