-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Refactor: 디렉토리 이름 수정 * Feat: Choco API 생성 완료 Co-authored-by: 민장규 <[email protected]>
- Loading branch information
1 parent
c9154e2
commit 239be55
Showing
21 changed files
with
460 additions
and
8 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
src/main/java/dgu/choco_express/controller/ChocoController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package dgu.choco_express.controller; | ||
|
||
import dgu.choco_express.annotation.UserId; | ||
import dgu.choco_express.dto.choco.request.ChocoCreateRequestDto; | ||
import dgu.choco_express.service.choco.ChocoService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api") | ||
public class ChocoController { | ||
private final ChocoService chocoService; | ||
|
||
@PostMapping("/box/{boxId}/choco") | ||
public ResponseEntity<?> createChoco( | ||
@UserId Long userId, | ||
@PathVariable Long boxId, | ||
@RequestBody ChocoCreateRequestDto chocoCreateRequestDto | ||
) { | ||
return ResponseEntity.created( | ||
chocoService.createChoco(userId, boxId, chocoCreateRequestDto) | ||
).build(); | ||
} | ||
|
||
@GetMapping("/choco") | ||
public ResponseEntity<?> getChocoList( | ||
@UserId Long userId, | ||
@RequestParam(defaultValue = "1") int page | ||
) { | ||
return ResponseEntity.ok(chocoService.getChocoList(userId, page)); | ||
} | ||
|
||
@GetMapping("/choco/{chocoId}") | ||
public ResponseEntity<?> getChocoDetails( | ||
@UserId Long userId, | ||
@PathVariable Long chocoId | ||
) { | ||
return ResponseEntity.ok( | ||
chocoService.getChocoDetails(userId, chocoId) | ||
); | ||
} | ||
|
||
@GetMapping("/choco/count") | ||
public ResponseEntity<?> getChocoPeek( | ||
@UserId Long userId | ||
) { | ||
return ResponseEntity.ok(chocoService.getChocoPeek(userId)); | ||
} | ||
|
||
@DeleteMapping("/choco/{chocoId}") | ||
public ResponseEntity<?> deleteChoco( | ||
@UserId Long userId, | ||
@PathVariable Long chocoId | ||
) { | ||
return ResponseEntity.ok(chocoService.deleteChoco(userId, chocoId)); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...ava/dgu/choco_express/domain/Box/Box.java → ...ava/dgu/choco_express/domain/box/Box.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package dgu.choco_express.domain.choco; | ||
|
||
import dgu.choco_express.domain.box.Box; | ||
import dgu.choco_express.domain.global.BaseTimeEntity; | ||
import jakarta.persistence.*; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import org.hibernate.annotations.DynamicUpdate; | ||
|
||
@Entity | ||
@Getter | ||
@DynamicUpdate | ||
@Table(name = "choco") | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Choco extends BaseTimeEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "id") | ||
private Long id; | ||
|
||
@Column(name = "type") | ||
private Short type; | ||
|
||
@Column(name = "nickname") | ||
private String nickname; | ||
|
||
@Column(name = "contents") | ||
private String contents; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "box_id") | ||
private Box box; | ||
|
||
@Builder | ||
public Choco( | ||
final Short type, | ||
final String nickname, | ||
final String contents, | ||
final Box box | ||
) { | ||
this.type = type; | ||
this.nickname = nickname; | ||
this.contents = contents; | ||
this.box = box; | ||
} | ||
|
||
public static Choco from( | ||
final Short type, | ||
final String nickname, | ||
final String contents, | ||
final Box box | ||
) { | ||
return Choco.builder() | ||
.type(type) | ||
.nickname(nickname) | ||
.contents(contents) | ||
.box(box) | ||
.build(); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/main/java/dgu/choco_express/dto/box/response/BoxCurrentUserRetrieverResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/main/java/dgu/choco_express/dto/box/response/BoxOtherUserRetrieverResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/main/java/dgu/choco_express/dto/choco/request/ChocoCreateRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package dgu.choco_express.dto.choco.request; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public record ChocoCreateRequestDto( | ||
@JsonProperty("nickname") | ||
String nickname, | ||
|
||
@JsonProperty("contents") | ||
String contents, | ||
|
||
@JsonProperty("chocoType") | ||
Short chocoType | ||
) { | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/dgu/choco_express/dto/choco/response/ChocoDetailsResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package dgu.choco_express.dto.choco.response; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import dgu.choco_express.domain.choco.Choco; | ||
import lombok.Builder; | ||
|
||
@Builder | ||
public record ChocoDetailsResponseDto( | ||
@JsonProperty("id") | ||
Long id, | ||
|
||
@JsonProperty("nickname") | ||
String nickname, | ||
|
||
@JsonProperty("contents") | ||
String contents | ||
) { | ||
public static ChocoDetailsResponseDto of(Choco choco) { | ||
return ChocoDetailsResponseDto.builder() | ||
.id(choco.getId()) | ||
.nickname(choco.getNickname()) | ||
.contents(choco.getContents()) | ||
.build(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/dgu/choco_express/dto/choco/response/ChocoListResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package dgu.choco_express.dto.choco.response; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Builder; | ||
|
||
import java.util.List; | ||
|
||
@Builder | ||
public record ChocoListResponseDto( | ||
@JsonProperty("chocoList") | ||
List<ChocoObjectResponseDto> chocoObjectResponseDtoList, | ||
|
||
@JsonProperty("totalPage") | ||
Integer totalPage | ||
) { | ||
public static ChocoListResponseDto of( | ||
final List<ChocoObjectResponseDto> chocoObjectResponseDtoList, | ||
final Integer totalPage | ||
) { | ||
return ChocoListResponseDto.builder() | ||
.chocoObjectResponseDtoList(chocoObjectResponseDtoList) | ||
.totalPage(totalPage) | ||
.build(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/dgu/choco_express/dto/choco/response/ChocoObjectResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package dgu.choco_express.dto.choco.response; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import dgu.choco_express.domain.choco.Choco; | ||
import lombok.Builder; | ||
|
||
@Builder | ||
public record ChocoObjectResponseDto( | ||
@JsonProperty("id") | ||
Long id, | ||
|
||
@JsonProperty("chocoType") | ||
Short chocoType | ||
) { | ||
public static ChocoObjectResponseDto of(final Choco choco) { | ||
return ChocoObjectResponseDto.builder() | ||
.id(choco.getId()) | ||
.chocoType(choco.getType()) | ||
.build(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/dgu/choco_express/dto/choco/response/ChocoPeekResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package dgu.choco_express.dto.choco.response; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import dgu.choco_express.domain.box.Box; | ||
import lombok.Builder; | ||
|
||
@Builder | ||
public record ChocoPeekResponseDto( | ||
@JsonProperty("count") | ||
Integer count | ||
) { | ||
} | ||
|
20 changes: 20 additions & 0 deletions
20
src/main/java/dgu/choco_express/exception/ChocoErrorCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package dgu.choco_express.exception; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum ChocoErrorCode implements ErrorCode { | ||
INVALID_CHOCO_TYPE(HttpStatus.BAD_REQUEST, "CHOCO_001", "초코 타입이 유효하지 않습니다."), | ||
NOT_FOUND_CHOCO(HttpStatus.NOT_FOUND, "CHOCO_002", "해당 초코가 존재하지 않습니다."), | ||
INVALID_CHOCO_NAME(HttpStatus.BAD_REQUEST, "CHOCO_003", "초코 작성자 이름이 비어있습니다."), | ||
CANT_CHOCO_RECURSIVE(HttpStatus.BAD_REQUEST, "CHOCO_004", "자기 자신에게 초코를 보낼 수 없습니다."), | ||
INVALID_PAGE_CHOCO(HttpStatus.BAD_REQUEST, "CHOCO_005", "유효하지 않은 페이지 넘버입니다."), | ||
; | ||
|
||
private final HttpStatus status; | ||
private final String errorCode; | ||
private final String message; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/main/java/dgu/choco_express/repository/ChocoRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package dgu.choco_express.repository; | ||
|
||
import dgu.choco_express.domain.box.Box; | ||
import dgu.choco_express.domain.choco.Choco; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
|
||
public interface ChocoRepository extends JpaRepository<Choco, Long> { | ||
@Query("SELECT count(*) FROM Choco WHERE box = :boxId") | ||
Integer findChocoCountByBox(@Param("boxId") Box box); | ||
|
||
@Query( | ||
value = "SELECT * " + | ||
"FROM choco c " + | ||
"WHERE c.box_id = :boxId", | ||
countQuery = "SELECT COUNT(*) " + | ||
"FROM choco c " + | ||
"WHERE c.box_id = :boxId", | ||
nativeQuery = true | ||
) | ||
Page<Choco> findAllByBoxId(Long boxId, Pageable pageable); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/java/dgu/choco_express/service/choco/ChocoRemover.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package dgu.choco_express.service.choco; | ||
|
||
import dgu.choco_express.domain.choco.Choco; | ||
import dgu.choco_express.repository.ChocoRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class ChocoRemover { | ||
private final ChocoRepository chocoRepository; | ||
|
||
@Transactional | ||
public void deleteChoco( | ||
Choco choco | ||
) { | ||
chocoRepository.delete(choco); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/dgu/choco_express/service/choco/ChocoRetriever.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package dgu.choco_express.service.choco; | ||
|
||
import dgu.choco_express.domain.box.Box; | ||
import dgu.choco_express.domain.choco.Choco; | ||
import dgu.choco_express.exception.ChocoErrorCode; | ||
import dgu.choco_express.exception.CommonException; | ||
import dgu.choco_express.repository.ChocoRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class ChocoRetriever { | ||
private final ChocoRepository chocoRepository; | ||
|
||
public Choco findById(Long id) { | ||
return chocoRepository.findById(id) | ||
.orElseThrow(() -> CommonException.type(ChocoErrorCode.NOT_FOUND_CHOCO)); | ||
} | ||
|
||
public Page<Choco> findAllByBoxId(Long boxId, Pageable pageable) { | ||
return chocoRepository.findAllByBoxId(boxId, pageable); | ||
} | ||
|
||
public Integer findChocoCountByBox(Box box) { | ||
return chocoRepository.findChocoCountByBox(box); | ||
} | ||
} |
Oops, something went wrong.