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

[202101947 김민지] Level8-9 미션 제출합니다 #37

Open
wants to merge 2 commits into
base: level8-initial
Choose a base branch
from
Open
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
18 changes: 14 additions & 4 deletions src/main/java/backend/likelion/todos/goal/GoalController.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

@RequiredArgsConstructor
@RequestMapping("/goals")
Expand All @@ -27,7 +28,10 @@ public ResponseEntity<Void> create(
@RequestBody GoalCreateRequest request
) {
// TODO [8단계] GoalCreateRequest에서 이름과 색상을 추출하여 goalService의 save 메소드를 호출하고, 생성된 Goal의 ID로 URI를 생성하여 ResponseEntity를 반환하세요.
return null;

Long goalId = goalService.save(request.name(), request.color(), memberId);
return ResponseEntity.created(URI.create("/goals/" + goalId)).build();

}

@PutMapping("/{id}")
Expand All @@ -37,7 +41,11 @@ public ResponseEntity<Void> update(
@RequestBody GoalUpdateRequest request
) {
// TODO [8단계] GoalUpdateRequest에서 이름과 색상을 추출하고, id와 memberId를 함께 goalService의 update 메소드에 전달하여 Goal 정보를 업데이트한 후, ResponseEntity.ok()를 반환하세요.
return null;
String name = request.getName();
String color = request.getColor();

goalService.update(id,name, color, memberId);
return ResponseEntity.ok().build();
}

@DeleteMapping("/{id}")
Expand All @@ -46,14 +54,16 @@ public ResponseEntity<Void> delete(
@Auth Long memberId
) {
// TODO [8단계] id와 memberId를 goalService의 delete 메소드에 전달하여 Goal을 삭제하고, ResponseEntity.ok()를 반환하세요.
return null;
goalService.delete(id,memberId);
return ResponseEntity.ok().build();
}

@GetMapping("/my")
public ResponseEntity<List<GoalResponse>> findMine(
@Auth Long memberId
) {
// TODO [8단계] memberId를 goalService의 findAllByMemberId 메소드에 전달하여 해당 회원의 모든 Goal 정보를 조회하고, 조회된 정보를 ResponseEntity.ok()에 담아 반환하세요.
return null;
List<GoalResponse> goals = goalService.findAllByMemberId(memberId);
return ResponseEntity.ok(goals);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,13 @@ public record GoalCreateRequest(
String name,
String color
) {

public String getName() {
return name;
}


public String getColor() {
return color;
}
}
35 changes: 32 additions & 3 deletions src/main/java/backend/likelion/todos/goal/GoalService.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package backend.likelion.todos.goal;

import backend.likelion.todos.common.ForbiddenException;
import backend.likelion.todos.common.NotFoundException;
import backend.likelion.todos.member.Member;
import backend.likelion.todos.member.MemberRepository;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand All @@ -16,28 +20,53 @@ public class GoalService {

public Long save(String name, String color, Long memberId) {
// TODO [8단계] memberId로 회원을 조회하고, 조회에 실패하면 "회원 정보가 없습니다." 예외를 발생시키세요.
Member member = memberRepository.findById(memberId)
.orElseThrow(() ->new NotFoundException("회원 정보가 없습니다."));
// TODO [8단계] 조회된 회원 정보를 사용하여 새 Goal 객체를 생성하세요.
Goal newGoal = new Goal(name,color,member);
// TODO [8단계] 생성된 Goal 객체를 goalRepository에 저장하고, 저장된 Goal의 ID를 반환하세요.
return null;
goalRepository.save(newGoal);
return newGoal.getId();
}

public void update(Long goalId, String name, String color, Long memberId) {
// TODO [8단계] memberId로 회원을 조회하고, 조회에 실패하면 "회원 정보가 없습니다." 예외를 발생시키세요.
Member member = memberRepository.findById(memberId)
.orElseThrow(() ->new NotFoundException("회원 정보가 없습니다."));
// TODO [8단계] goalId로 목표(Goal)를 조회하고, 조회에 실패하면 "목표 정보가 없습니다." 예외를 발생시키세요.
Goal goal = goalRepository.findById(goalId)
.orElseThrow(() ->new NotFoundException("목표 정보가 없습니다."));
// TODO [8단계] 조회된 Goal의 회원 정보가 입력된 memberId와 일치하는지 검증하세요.
if(member.getId() != goal.getMember().getId()){
throw new ForbiddenException("해당 목표에 대한 권한이 없습니다.");
}
// TODO [8단계] Goal 객체의 정보를 새로운 name과 color로 업데이트하세요.
goal.update(name,color);
}

public void delete(Long goalId, Long memberId) {
// TODO [8단계] memberId로 회원을 조회하고, 조회에 실패하면 "회원 정보가 없습니다." 예외를 발생시키세요.
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new NotFoundException("회원 정보가 없습니다."));
// TODO [8단계] goalId로 목표(Goal)를 조회하고, 조회에 실패하면 "목표 정보가 없습니다." 예외를 발생시키세요.
Goal goal = goalRepository.findById(goalId)
.orElseThrow(() -> new NotFoundException("목표 정보가 없습니다."));
// TODO [8단계] 조회된 Goal의 회원 정보가 입력된 memberId와 일치하는지 검증하세요.
// TODO [8단계] 검증이 완료되면 Goal을 goalRepository에서 삭제하세요.
if (member.getId() != goal.getMember().getId()) {
throw new ForbiddenException("해당 목표에 대한 권한이 없습니다.");
// TODO [8단계] 검증이 완료되면 Goal을 goalRepository에서 삭제하세요.
}
goalRepository.delete(goal);
}

public List<GoalResponse> findAllByMemberId(Long memberId) {
// TODO [8단계] memberId로 모든 목표(Goal)를 조회하세요.
List<Goal> goals = goalRepository.findAllByMemberId(memberId);
// TODO [8단계] 조회된 Goal들을 GoalResponse 리스트로 변환하여 반환하세요.
return null;
List<GoalResponse> responses = goals.stream()
.map(goal -> new GoalResponse(goal.getId(), goal.getName(), goal.getColor()))
.collect(Collectors.toList());

return responses;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,11 @@ public record GoalUpdateRequest(
String name,
String color
) {
public String getName() {
return name;
}

public String getColor() {
return color;
}
}
16 changes: 16 additions & 0 deletions src/main/java/backend/likelion/todos/todo/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package backend.likelion.todos.todo;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class User {
private String id;
private String name;

public User(String id, String name){
this.id = id;
this.name = name;
}
}