Skip to content

Commit

Permalink
Merge pull request #6 from Choco-Express/dev
Browse files Browse the repository at this point in the history
πŸš€ [Deploy] - λ°•μŠ€ API κ΅¬ν˜„μ„ μ„œλ²„μ— λ°˜μ˜ν•œλ‹€
  • Loading branch information
JeongHeumChoi authored Aug 30, 2024
2 parents 6bba37b + 43e1cc6 commit c9154e2
Show file tree
Hide file tree
Showing 19 changed files with 389 additions and 5 deletions.
49 changes: 49 additions & 0 deletions src/main/java/dgu/choco_express/controller/BoxController.java
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));
}
}
63 changes: 63 additions & 0 deletions src/main/java/dgu/choco_express/domain/Box/Box.java
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;
}
}
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
) {
}
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
) {
}
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();
}
}
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 src/main/java/dgu/choco_express/exception/BoxErrorCode.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 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 src/main/java/dgu/choco_express/repository/BoxRepository.java
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
Expand Down Expand Up @@ -47,6 +48,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.authorizeHttpRequests(request ->
request
.requestMatchers(Constants.NO_NEED_AUTH.toArray(String[]::new)).permitAll()
.requestMatchers(HttpMethod.GET, "/api/box/{boxId:[0-9]+}").permitAll()
.requestMatchers("/api/**").hasAnyRole("USER")
.anyRequest().authenticated()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {

@Override
protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException{
String requestURI = request.getRequestURI();
String requestMethod = request.getMethod();

if (requestURI.matches("/api/box/\\d+") && "GET".equalsIgnoreCase(requestMethod))
return true;
return Constants.NO_NEED_AUTH.contains(request.getRequestURI());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
public class JwtExceptionFilter extends OncePerRequestFilter {
@Override
protected boolean shouldNotFilter(HttpServletRequest request) {
String requestURI = request.getRequestURI();
String requestMethod = request.getMethod();

if (requestURI.matches("/api/box/\\d+") && "GET".equalsIgnoreCase(requestMethod))
return true;
return Constants.NO_NEED_AUTH.contains(request.getRequestURI());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package dgu.choco_express.security.handler.login;

import dgu.choco_express.dto.jwt.response.JwtDto;
import dgu.choco_express.repository.UserRepository;
import dgu.choco_express.security.info.AuthenticationResponse;
import dgu.choco_express.security.info.UserPrincipal;
import dgu.choco_express.service.JwtService;
import dgu.choco_express.service.jwt.JwtService;
import dgu.choco_express.util.JwtUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
Expand All @@ -25,7 +24,6 @@ public class Oauth2SuccessHandler implements AuthenticationSuccessHandler {
@Value("${server.domain}")
private String domain;
private final JwtUtil jwtUtil;
private final UserRepository userRepository;
private final JwtService jwtService;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import dgu.choco_express.constant.Constants;
import dgu.choco_express.exception.CommonException;
import dgu.choco_express.exception.GlobalErrorCode;
import dgu.choco_express.service.JwtService;
import dgu.choco_express.service.jwt.JwtService;
import dgu.choco_express.util.HeaderUtil;
import dgu.choco_express.util.JwtUtil;
import io.jsonwebtoken.Claims;
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/dgu/choco_express/service/box/BoxRetriever.java
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));
}
}
18 changes: 18 additions & 0 deletions src/main/java/dgu/choco_express/service/box/BoxSaver.java
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);
}
}
Loading

0 comments on commit c9154e2

Please sign in to comment.