Skip to content

Commit

Permalink
✨ [Feature] - Choco API를 구현한다 (#8) (#9)
Browse files Browse the repository at this point in the history
* Refactor: 디렉토리 이름 수정

* Feat: Choco API 생성 완료

Co-authored-by: 민장규 <[email protected]>
  • Loading branch information
JeongHeumChoi and Jang99u authored Aug 30, 2024
1 parent c9154e2 commit 239be55
Show file tree
Hide file tree
Showing 21 changed files with 460 additions and 8 deletions.
59 changes: 59 additions & 0 deletions src/main/java/dgu/choco_express/controller/ChocoController.java
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));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dgu.choco_express.domain.Box;
package dgu.choco_express.domain.box;

import dgu.choco_express.domain.global.BaseTimeEntity;
import dgu.choco_express.domain.user.User;
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/dgu/choco_express/domain/choco/Choco.java
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();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dgu.choco_express.dto.box.response;

import com.fasterxml.jackson.annotation.JsonProperty;
import dgu.choco_express.domain.Box.Box;
import dgu.choco_express.domain.box.Box;
import lombok.Builder;

@Builder
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dgu.choco_express.dto.box.response;

import com.fasterxml.jackson.annotation.JsonProperty;
import dgu.choco_express.domain.Box.Box;
import dgu.choco_express.domain.box.Box;
import lombok.Builder;

@Builder
Expand Down
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
) {
}
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();
}
}
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();
}
}
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();
}
}
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 src/main/java/dgu/choco_express/exception/ChocoErrorCode.java
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;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dgu.choco_express.repository;

import dgu.choco_express.domain.Box.Box;
import dgu.choco_express.domain.box.Box;
import dgu.choco_express.domain.user.User;
import org.springframework.data.jpa.repository.JpaRepository;

Expand Down
25 changes: 25 additions & 0 deletions src/main/java/dgu/choco_express/repository/ChocoRepository.java
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);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dgu.choco_express.service.box;

import dgu.choco_express.domain.Box.Box;
import dgu.choco_express.domain.box.Box;
import dgu.choco_express.domain.user.User;
import dgu.choco_express.exception.BoxErrorCode;
import dgu.choco_express.exception.CommonException;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dgu/choco_express/service/box/BoxSaver.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dgu.choco_express.service.box;

import dgu.choco_express.domain.Box.Box;
import dgu.choco_express.domain.box.Box;
import dgu.choco_express.repository.BoxRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dgu.choco_express.service.box;

import dgu.choco_express.domain.Box.Box;
import dgu.choco_express.domain.box.Box;
import dgu.choco_express.domain.user.User;
import dgu.choco_express.dto.box.request.BoxCreateRequestDto;
import dgu.choco_express.dto.box.request.BoxPatchRequestDto;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dgu.choco_express.service.box;

import dgu.choco_express.domain.Box.Box;
import dgu.choco_express.domain.box.Box;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/dgu/choco_express/service/choco/ChocoRemover.java
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 src/main/java/dgu/choco_express/service/choco/ChocoRetriever.java
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);
}
}
Loading

0 comments on commit 239be55

Please sign in to comment.