-
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.
Merge pull request #6 from Choco-Express/dev
π [Deploy] - λ°μ€ API ꡬνμ μλ²μ λ°μνλ€
- Loading branch information
Showing
19 changed files
with
389 additions
and
5 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/main/java/dgu/choco_express/controller/BoxController.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,49 @@ | ||
package dgu.choco_express.controller; | ||
|
||
|
||
import dgu.choco_express.annotation.UserId; | ||
import dgu.choco_express.dto.box.request.BoxCreateRequestDto; | ||
import dgu.choco_express.dto.box.request.BoxPatchRequestDto; | ||
import dgu.choco_express.service.box.BoxService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/box") | ||
public class BoxController { | ||
private final BoxService boxService; | ||
|
||
@PostMapping | ||
public ResponseEntity<?> createBox( | ||
@UserId Long userId, | ||
@RequestBody BoxCreateRequestDto boxCreateRequestDto | ||
) { | ||
|
||
return ResponseEntity.created( | ||
boxService.createBox(userId, boxCreateRequestDto) | ||
).build(); | ||
} | ||
|
||
@GetMapping("/{boxId}") | ||
public ResponseEntity<?> getOtherUserBox( | ||
@PathVariable Long boxId | ||
) { | ||
return ResponseEntity.ok(boxService.getOtherUserBox(boxId)); | ||
} | ||
|
||
@PatchMapping("/{boxId}") | ||
public ResponseEntity<?> updateBox( | ||
@UserId Long userId, | ||
@PathVariable Long boxId, | ||
@RequestBody BoxPatchRequestDto boxPatchRequestDto | ||
) { | ||
return ResponseEntity.ok(boxService.updateBox(userId, boxId, boxPatchRequestDto)); | ||
} | ||
|
||
@GetMapping("user-box") | ||
public ResponseEntity<?> getCurrentUserBox(@UserId Long userId) { | ||
return ResponseEntity.ok(boxService.getCurrentUserBox(userId)); | ||
} | ||
} |
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,63 @@ | ||
package dgu.choco_express.domain.Box; | ||
|
||
import dgu.choco_express.domain.global.BaseTimeEntity; | ||
import dgu.choco_express.domain.user.User; | ||
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 = "boxes") | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Box extends BaseTimeEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column | ||
private Long id; | ||
|
||
@Column(name = "name") | ||
private String name; | ||
|
||
@Column(name = "type") | ||
private Short type; | ||
|
||
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) | ||
@JoinColumn(name = "users_id") | ||
private User user; | ||
|
||
@Builder | ||
private Box( | ||
final String name, | ||
final Short type, | ||
final User user | ||
) { | ||
this.name = name; | ||
this.type = type; | ||
this.user = user; | ||
} | ||
|
||
public static Box from( | ||
final String name, | ||
final Short type, | ||
final User user | ||
) { | ||
return Box.builder() | ||
.name(name) | ||
.type(type) | ||
.user(user) | ||
.build(); | ||
} | ||
|
||
public void updateBox( | ||
final String name, | ||
final Short type | ||
) { | ||
this.name = name; | ||
this.type = type; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/dgu/choco_express/dto/box/request/BoxCreateRequestDto.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,11 @@ | ||
package dgu.choco_express.dto.box.request; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public record BoxCreateRequestDto( | ||
@JsonProperty("boxName") | ||
String boxName, | ||
@JsonProperty("boxType") | ||
Short boxType | ||
) { | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/dgu/choco_express/dto/box/request/BoxPatchRequestDto.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,11 @@ | ||
package dgu.choco_express.dto.box.request; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public record BoxPatchRequestDto( | ||
@JsonProperty("boxName") | ||
String boxName, | ||
@JsonProperty("boxType") | ||
Short boxType | ||
) { | ||
} |
23 changes: 23 additions & 0 deletions
23
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package dgu.choco_express.dto.box.response; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import dgu.choco_express.domain.Box.Box; | ||
import lombok.Builder; | ||
|
||
@Builder | ||
public record BoxCurrentUserRetrieverResponseDto( | ||
@JsonProperty("boxId") | ||
Long boxId, | ||
@JsonProperty("boxName") | ||
String boxName, | ||
@JsonProperty("boxType") | ||
Short boxType | ||
) { | ||
public static BoxCurrentUserRetrieverResponseDto of(Box box) { | ||
return BoxCurrentUserRetrieverResponseDto.builder() | ||
.boxId(box.getId()) | ||
.boxName(box.getName()) | ||
.boxType(box.getType()) | ||
.build(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package dgu.choco_express.dto.box.response; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import dgu.choco_express.domain.Box.Box; | ||
import lombok.Builder; | ||
|
||
@Builder | ||
public record BoxOtherUserRetrieverResponseDto( | ||
@JsonProperty("boxId") | ||
Long boxId, | ||
@JsonProperty("boxName") | ||
String boxName, | ||
@JsonProperty("boxType") | ||
Short boxType | ||
) { | ||
public static BoxOtherUserRetrieverResponseDto of(Box box) { | ||
return BoxOtherUserRetrieverResponseDto.builder() | ||
.boxId(box.getId()) | ||
.boxName(box.getName()) | ||
.boxType(box.getType()) | ||
.build(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/dgu/choco_express/exception/BoxErrorCode.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 BoxErrorCode implements ErrorCode { | ||
INVALID_BOX_TYPE(HttpStatus.BAD_REQUEST, "BOX_001", "λ°μ€ νμ μ΄ μ ν¨νμ§ μμ΅λλ€."), | ||
NOT_FOUND_BOX(HttpStatus.NOT_FOUND, "BOX_002", "ν΄λΉ λ°μ€κ° μ‘΄μ¬νμ§ μμ΅λλ€."), | ||
MISMATCH_USER_AND_BOX_ID(HttpStatus.BAD_REQUEST, "BOX_003", "ν΄λΉ λ°μ€μ λ‘κ·ΈμΈ ν μ¬μ©μκ° μΌμΉνμ§ μμ΅λλ€."), | ||
YET_USER_HAS_BOX(HttpStatus.NOT_FOUND, "BOX_004", "ν΄λΉ μ μ λ λ°μ€κ° μμ΅λλ€."), | ||
INVALID_BOX_NAME(HttpStatus.BAD_REQUEST, "BOX_005", "λ°μ€ μ΄λ¦μ΄ λΉμ΄μμ΅λλ€.") | ||
; | ||
|
||
private final HttpStatus status; | ||
private final String errorCode; | ||
private final String message; | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/dgu/choco_express/repository/BoxRepository.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,12 @@ | ||
package dgu.choco_express.repository; | ||
|
||
import dgu.choco_express.domain.Box.Box; | ||
import dgu.choco_express.domain.user.User; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.Optional; | ||
|
||
public interface BoxRepository extends JpaRepository<Box, Long> { | ||
Optional<Box> findByIdAndUser(Long boxId, User user); | ||
Optional<Box> findByUser(User user); | ||
} |
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
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
30 changes: 30 additions & 0 deletions
30
src/main/java/dgu/choco_express/service/box/BoxRetriever.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.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; | ||
import dgu.choco_express.repository.BoxRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class BoxRetriever { | ||
private final BoxRepository boxRepository; | ||
|
||
public Box findById(Long id) { | ||
return boxRepository.findById(id) | ||
.orElseThrow(() -> CommonException.type(BoxErrorCode.NOT_FOUND_BOX)); | ||
} | ||
|
||
public Box findByIdAndUser(Long boxId, User user) { | ||
return boxRepository.findByIdAndUser(boxId, user) | ||
.orElseThrow(() -> CommonException.type(BoxErrorCode.MISMATCH_USER_AND_BOX_ID)); | ||
} | ||
|
||
public Box findByUser(User user) { | ||
return boxRepository.findByUser(user) | ||
.orElseThrow(() -> CommonException.type(BoxErrorCode.YET_USER_HAS_BOX)); | ||
} | ||
} |
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,18 @@ | ||
package dgu.choco_express.service.box; | ||
|
||
import dgu.choco_express.domain.Box.Box; | ||
import dgu.choco_express.repository.BoxRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class BoxSaver { | ||
private final BoxRepository boxRepository; | ||
|
||
@Transactional | ||
public Box saveBox(Box box) { | ||
return boxRepository.save(box); | ||
} | ||
} |
Oops, something went wrong.