Skip to content

Commit

Permalink
[Refactor] todolist 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
jiyunio committed May 21, 2024
1 parent 8436aec commit 3569895
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 106 deletions.
14 changes: 9 additions & 5 deletions src/main/java/com/jiyunio/todolist/category/Category.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ public class Category {
@JoinColumn(name = "memberId")
private Member member;

private String category;
private String content;

private String color;

@Builder
protected Category(Member member, String category) {
protected Category(Member member, String content, String color) {
this.member = member;
this.category = category;
this.content = content;
this.color = color;
}

protected void updateCategory(String category) {
this.category = category;
protected void updateCategory(CategoryDTO categoryDTO) {
this.content = categoryDTO.getContent();
this.color = categoryDTO.getColor();
}
}
20 changes: 10 additions & 10 deletions src/main/java/com/jiyunio/todolist/category/CategoryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -16,37 +15,38 @@
@RestController
@RequiredArgsConstructor
@Validated
@RequestMapping("/category")
@Tag(name = "Category", description = "카테고리 API")
public class CategoryController {
private final CategoryService categoryService;

@PostMapping("/category{memberId}")
@PostMapping("/{member_id}")
@Operation(summary = "카테고리 생성")
public ResponseEntity<ResponseDTO> createCategory(@Parameter(description = "member의 id") @PathVariable Long memberId, @RequestParam @NotBlank String categoryName) {
categoryService.createCategory(memberId, categoryName);
public ResponseEntity<ResponseDTO> createCategory(@Parameter(description = "member의 id") @PathVariable Long memberId, @RequestBody CategoryDTO categoryDTO) {
categoryService.createCategory(memberId, categoryDTO);
ResponseDTO responseDTO = ResponseDTO.builder()
.msg("카테고리 생성 성공")
.build();
return new ResponseEntity<>(responseDTO, HttpStatus.CREATED);
}

@GetMapping("/category/{memberId}")
@GetMapping("/{member_id}")
@Operation(summary = "카테고리 조회")
public List<GetCategoryDTO> getCategory(@Parameter(description = "member의 id") @PathVariable Long memberId) {
public List<CategoryDTO> getCategory(@Parameter(description = "member의 id") @PathVariable Long memberId) {
return categoryService.getCategory(memberId);
}

@PutMapping("/category/{categoryId}")
@PutMapping("/{category_id}")
@Operation(summary = "카테고리 수정")
public ResponseEntity<ResponseDTO> updateCategory(@Parameter(description = "카테고리의 id") @PathVariable Long categoryId, @RequestParam @NotBlank String categoryName) {
categoryService.updateCategory(categoryId, categoryName);
public ResponseEntity<ResponseDTO> updateCategory(@Parameter(description = "카테고리의 id") @PathVariable Long categoryId, @RequestBody CategoryDTO categoryDTO) {
categoryService.updateCategory(categoryId, categoryDTO);
ResponseDTO responseDTO = ResponseDTO.builder()
.msg("카테고리 수정 성공")
.build();
return ResponseEntity.ok(responseDTO);
}

@DeleteMapping("/category/{categoryId}")
@DeleteMapping("/{category_id}")
@Operation(summary = "카테고리 삭제")
public ResponseEntity<ResponseDTO> deleteCategory(@Parameter(description = "카테고리의 id") @PathVariable Long categoryId) {
categoryService.deleteCategory(categoryId);
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/jiyunio/todolist/category/CategoryDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.jiyunio.todolist.category;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Schema(description = "카테고리 생성 & 조회 & 수정")
public class CategoryDTO {
@NotBlank
private String content;

@NotBlank
private String color;

@Builder
protected CategoryDTO(String content, String color) {
this.content = content;
this.color = color;
}

}
32 changes: 20 additions & 12 deletions src/main/java/com/jiyunio/todolist/category/CategoryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,44 @@ public class CategoryService {
private final CategoryRepository categoryRepository;
private final MemberRepository memberRepository;

public void createCategory(Long memberId, String categoryName) {
public void createCategory(Long memberId, CategoryDTO categoryDTO) {
Member member = memberRepository.findById(memberId).orElseThrow(
// 회원 존재 안함
() -> new CustomException(HttpStatus.NOT_FOUND, ErrorCode.NOT_EXIST_MEMBER)
);
Category category = Category.builder()

categoryRepository.save(Category.builder()
.member(member)
.category(categoryName)
.build();
categoryRepository.save(category);
.content(categoryDTO.getContent())
.color(categoryDTO.getColor())
.build());
}

public List<GetCategoryDTO> getCategory(Long memberId) {
public List<CategoryDTO> getCategory(Long memberId) {
List<Category> categories = categoryRepository.findByMemberId(memberId);
List<GetCategoryDTO> getCategoryDTO = new ArrayList<>();
List<CategoryDTO> getCategoryDTO = new ArrayList<>();

for (Category category : categories) {
getCategoryDTO.add(GetCategoryDTO.builder()
.category(category.getCategory()).build());
getCategoryDTO.add(CategoryDTO.builder()
.content(category.getContent())
.color(category.getColor())
.build());
}
return getCategoryDTO;
}

public void updateCategory(Long categoryId, String categoryName) {
public void updateCategory(Long categoryId, CategoryDTO categoryDTO) {
Category category = categoryRepository.findById(categoryId).get();
category.updateCategory(categoryName);
category.updateCategory(categoryDTO);
categoryRepository.save(category);
}

public void deleteCategory(Long categoryId) {
categoryRepository.deleteById(categoryId);
if (categoryRepository.count() == 1) {
//카데고리 개수 >= 1
throw new CustomException(HttpStatus.BAD_REQUEST, ErrorCode.NO_ANYMORE_CATEGORY);
} else {
categoryRepository.deleteById(categoryId);
}
}
}
18 changes: 0 additions & 18 deletions src/main/java/com/jiyunio/todolist/category/GetCategoryDTO.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public enum ErrorCode {
// 400 Bad Request
NOT_SAME_CONFIRM_PASSWORD("400_Bad_Request", "비밀번호 확인이 맞지 않습니다."),
WRONG_USERID_PASSWORD("400_Bad_Request", "아이디 및 비밀번호가 맞지 않습니다."),
NO_ANYMORE_CATEGORY("400_Bad_Request", "카테고리는 1개 이상이어야 합니다."),

// 404 Not Found
NOT_EXIST_MEMBER("404_Not_Found", "회원이 존재하지 않습니다."),
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/jiyunio/todolist/member/MemberController.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@

@RestController
@RequiredArgsConstructor
@RequestMapping("/member")
@Tag(name = "Member", description = "회원 API")
public class MemberController {
private final MemberService memberService;

@PostMapping("/signUp")
@PostMapping("/sign_up")
@Operation(summary = "회원가입", description = "아이디, 비밀번호, 이메일 이용\n\n 아이디 : 5 ~ 10자 \n\n 비밀번호: 8~16자의 영문 대/소문자, 숫자, 특수문자")
public ResponseEntity<?> signUp(@Valid @RequestBody SignUpDTO signUpDto) {
ResponseDTO responseDTO = ResponseDTO.builder()
Expand All @@ -29,7 +30,7 @@ public ResponseEntity<?> signUp(@Valid @RequestBody SignUpDTO signUpDto) {
return new ResponseEntity<>(responseDTO, HttpStatus.CREATED);
}

@PostMapping("/signIn")
@PostMapping("/sign_in")
@Operation(summary = "로그인", description = "아이디와 비밀번호 이용")
public ResponseEntity<?> signIn(@Valid @RequestBody SignInDTO signInDto) {
ResponseDTO responseDTO = ResponseDTO.builder()
Expand All @@ -39,7 +40,7 @@ public ResponseEntity<?> signIn(@Valid @RequestBody SignInDTO signInDto) {
return ResponseEntity.ok(responseDTO);
}

@PutMapping("/{id}/update")
@PutMapping("/{member_id}")
@Operation(summary = "회원 비밀번호 수정", description = "비밀번호, 수정 비밀번호 이용")
public ResponseEntity<?> updateUserPw(@Parameter(description = "member의 id") @PathVariable Long id, @Valid @RequestBody ChangeUserPwDTO changeUserPwDto) {
memberService.updateUserPw(id, changeUserPwDto);
Expand All @@ -49,7 +50,7 @@ public ResponseEntity<?> updateUserPw(@Parameter(description = "member의 id") @
return ResponseEntity.ok(responseDTO);
}

@DeleteMapping("/{id}/delete")
@DeleteMapping("/{member_id}")
@Operation(summary = "회원 탈퇴", description = "비밀번호 이용")
public ResponseEntity<ResponseDTO> deleteMember(@Parameter(description = "member의 id") @PathVariable Long id, @RequestParam String userPw) {
memberService.deleteMember(id, userPw);
Expand All @@ -59,3 +60,4 @@ public ResponseEntity<ResponseDTO> deleteMember(@Parameter(description = "member
return new ResponseEntity<>(responseDTO, HttpStatus.NO_CONTENT);
}
}

15 changes: 13 additions & 2 deletions src/main/java/com/jiyunio/todolist/member/MemberService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.jiyunio.todolist.member;

import com.jiyunio.todolist.category.CategoryDTO;
import com.jiyunio.todolist.category.CategoryService;
import com.jiyunio.todolist.customError.CustomException;
import com.jiyunio.todolist.customError.ErrorCode;
import com.jiyunio.todolist.member.dto.ChangeUserPwDTO;
Expand All @@ -14,6 +16,7 @@
@RequiredArgsConstructor
public class MemberService {
private final MemberRepository memberRepository;
private final CategoryService categoryService;

public String signUp(@Valid SignUpDTO signUpDto) {
if (memberRepository.existsByUserId(signUpDto.getUserId())) {
Expand All @@ -23,12 +26,20 @@ public String signUp(@Valid SignUpDTO signUpDto) {

if (signUpDto.getUserPw().equals(signUpDto.getConfirmUserPw())) {
// 회원가입 성공

Member member = Member.builder()
.userId(signUpDto.getUserId())
.userPw(signUpDto.getUserPw())
.build();

memberRepository.save(member);
return member.getUserId();

//기본 카테고리 생성
categoryService.createCategory(member.getId(), CategoryDTO.builder()
.content("기본")
.color("FFFFFF").build());

return signUpDto.getUserId();
}
// 비밀번호 불일치
throw new CustomException(HttpStatus.BAD_REQUEST, ErrorCode.NOT_SAME_CONFIRM_PASSWORD);
Expand All @@ -39,7 +50,7 @@ public String signIn(@Valid SignInDTO signInDto) {
Member member = memberRepository.findByUserId(signInDto.getUserId()).get();
if (member.getUserPw().equals(signInDto.getUserPw())) {
// 로그인 성공
return member.getUserId();
return signInDto.getUserId();
}
// 회원의 비밀번호와 불일치
throw new CustomException(HttpStatus.NOT_FOUND, ErrorCode.WRONG_USERID_PASSWORD);
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/jiyunio/todolist/todo/TodoController.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
public class TodoController {
private final TodoService todoService;

@PostMapping("/{memberId}")
@PostMapping("/{member_id}")
@Operation(summary = "todo 생성", description = "todo checked 기본 값 = False")
public ResponseEntity<?> createTodo(@Parameter(description = "member의 id") @PathVariable Long memberId, @Valid @RequestBody CreateTodoDTO createTodo) {
todoService.createTodo(memberId, createTodo);
Expand All @@ -32,13 +32,13 @@ public ResponseEntity<?> createTodo(@Parameter(description = "member의 id") @Pa
return new ResponseEntity<>(responseDTO, HttpStatus.CREATED);
}

@GetMapping("/{memberId}")
@GetMapping("/{member_id}")
@Operation(summary = "todo 조회")
public List<GetTodoDTO> getTodo(@Parameter(description = "member의 id") @PathVariable Long memberId) {
return todoService.getTodo(memberId);
}

@PutMapping("/{todoId}")
@PutMapping("/{todo_id}")
@Operation(summary = "todo 수정")
public ResponseEntity<?> updateTodo(@Parameter(description = "todo의 id") @PathVariable Long todoId, @Valid @RequestBody UpdateTodoDTO updateTodo) {
todoService.updateTodo(todoId, updateTodo);
Expand All @@ -48,7 +48,7 @@ public ResponseEntity<?> updateTodo(@Parameter(description = "todo의 id") @Path
return ResponseEntity.ok(responseDTO);
}

@DeleteMapping("/{todoId}")
@DeleteMapping("/{todo_id}")
@Operation(summary = "todo 삭제")
public ResponseEntity<ResponseDTO> deleteTodo(@Parameter(description = "todo의 id") @PathVariable Long todoId) {
todoService.deleteTodo(todoId);
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/jiyunio/todolist/todo/TodoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ public void createTodo(Long memberId, CreateTodoDTO createTodo) {
() -> new CustomException(HttpStatus.NOT_FOUND, ErrorCode.NOT_EXIST_MEMBER)
);

Todo todo = Todo.builder()
todoRepository.save(Todo.builder()
.member(member)
.content(createTodo.getContent())
.category(createTodo.getCategory())
.writeDate(createTodo.getWriteDate())
.setDate(createTodo.getSetDate())
.checked(false)
.build();
todoRepository.save(todo);
.build());
}

public List<GetTodoDTO> getTodo(Long memberId) {
Expand Down
55 changes: 7 additions & 48 deletions src/test/java/com/jiyunio/todolist/TodolistApplicationTests.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,7 @@
//package com.jiyunio.todolist;
//
//import com.jiyunio.todolist.member.Member;
//import com.jiyunio.todolist.member.MemberRepository;
//import com.jiyunio.todolist.member.MemberService;
//import com.jiyunio.todolist.member.dto.SignInDTO;
//import org.junit.jupiter.api.BeforeEach;
//import org.junit.jupiter.api.DisplayName;
//import org.junit.jupiter.api.Test;
//import org.mockito.Mockito;
//import org.springframework.boot.test.context.SpringBootTest;
//import org.springframework.boot.test.mock.mockito.MockBean;
//import static org.assertj.core.api.Assertions.*;
//
//@SpringBootTest
//class TodolistApplicationTests {
// MemberService memberService;
//
// @MockBean
// MemberRepository memberRepository;
//
// @BeforeEach
// void setUpTeat() {
// // 가짜 객체 주입
// memberService = new MemberService(memberRepository);
// }
// @DisplayName("회원 로그인 test")
// @Test
// void SignInMemberTest() {
// // given
// Member member = Member.builder()
// .userId("jiyun")
// .userPw("qwe123!")
// .build();
// Mockito.when(memberRepository.save(member)).thenReturn(member);
//
// // when
// SignInDTO signInDTO = new SignInDTO();
// signInDTO.setUserId("jiyun");
// signInDTO.setUserPw("qwe123!");
//
// String userId = memberService.signIn(signInDTO);
//
// // then
// assertThat(userId).isEqualTo("jiyun");
// }
//
//}
package com.jiyunio.todolist;

import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class TodolistApplicationTests {
}

0 comments on commit 3569895

Please sign in to comment.