diff --git a/src/main/java/com/sparta/oneandzerobest/OneAndZeroBestApplication.java b/src/main/java/com/sparta/oneandzerobest/OneAndZeroBestApplication.java index f5c5d21..6d77b49 100644 --- a/src/main/java/com/sparta/oneandzerobest/OneAndZeroBestApplication.java +++ b/src/main/java/com/sparta/oneandzerobest/OneAndZeroBestApplication.java @@ -2,8 +2,11 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; -@SpringBootApplication +@EnableJpaAuditing +@SpringBootApplication(exclude = {SecurityAutoConfiguration.class}) public class OneAndZeroBestApplication { public static void main(String[] args) { diff --git a/src/main/java/com/sparta/oneandzerobest/contents/ContentController.java b/src/main/java/com/sparta/oneandzerobest/contents/ContentController.java deleted file mode 100644 index 7832944..0000000 --- a/src/main/java/com/sparta/oneandzerobest/contents/ContentController.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.sparta.oneandzerobest.contents; - -public class ContentController { -} diff --git a/src/main/java/com/sparta/oneandzerobest/follow/controller/FollowController.java b/src/main/java/com/sparta/oneandzerobest/follow/controller/FollowController.java new file mode 100644 index 0000000..868c171 --- /dev/null +++ b/src/main/java/com/sparta/oneandzerobest/follow/controller/FollowController.java @@ -0,0 +1,113 @@ +package com.sparta.oneandzerobest.follow.controller; + +import com.sparta.oneandzerobest.follow.dto.ErrorResponseDTO; +import com.sparta.oneandzerobest.follow.dto.FollowResponseDTO; +import com.sparta.oneandzerobest.follow.service.FollowService; +import com.sparta.oneandzerobest.auth.util.JwtUtil; +import com.sparta.oneandzerobest.auth.entity.User; +import com.sparta.oneandzerobest.auth.repository.UserRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.stream.Collectors; + +/** + * FollowController는 팔로우 및 언팔로우 요청을 처리하는 컨트롤러입니다. + */ +@RestController +@RequestMapping("/api/follow") +public class FollowController { + + @Autowired + private FollowService followService; + + @Autowired + private JwtUtil jwtUtil; + + @Autowired + private UserRepository userRepository; + + /** + * 팔로우 요청을 처리합니다. + * @param token JWT 토큰 + * @param userId 팔로우 당하는 사용자의 ID + * @return 팔로우 응답 데이터를 담은 DTO + */ + @PostMapping + public ResponseEntity follow(@RequestHeader("Authorization") String token, + @PathVariable("user_id") Long userId) { + + // JWT 토큰에서 사용자 이름 추출 + String username = jwtUtil.getUsernameFromToken(token.replace("Bearer ", "")); + + // 사용자 이름으로 사용자 조회 + User user = userRepository.findByUsername(username) + .orElseThrow(() -> new RuntimeException("유저를 찾을 수 없습니다.")); + + // 자신을 팔로우할 수 없음 + if (user.getId().equals(userId)) { + return ResponseEntity.badRequest().body(new ErrorResponseDTO("스스로를 팔로우할 수 없습니다.")); + } + + // 팔로우 서비스 호출 + try { + FollowResponseDTO responseDTO = followService.follow(user.getId(), userId); + return new ResponseEntity<>(responseDTO, HttpStatus.OK); + } catch (Exception e) { + return ResponseEntity.badRequest().body(new ErrorResponseDTO("이미 팔로우한 유저입니다.")); + } + } + + /** + * 언팔로우 요청을 처리합니다. + * @param token JWT 토큰 + * @param userId 언팔로우 당하는 사용자의 ID + * @return 언팔로우 응답 데이터를 담은 DTO + */ + @DeleteMapping + public ResponseEntity unfollow(@RequestHeader("Authorization") String token, + @PathVariable("user_id") Long userId) { + // JWT 토큰에서 사용자 이름 추출 + String username = jwtUtil.getUsernameFromToken(token.replace("Bearer ", "")); + + // 사용자 이름으로 사용자 조회 + User user = userRepository.findByUsername(username) + .orElseThrow(() -> new RuntimeException("유저를 찾을 수 없습니다.")); + + // 언팔로우 서비스 호출 + try { + FollowResponseDTO responseDTO = followService.unfollow(user.getId(), userId); + return new ResponseEntity<>(responseDTO, HttpStatus.OK); + } catch (Exception e) { + return ResponseEntity.badRequest().body(new ErrorResponseDTO("팔로우하지 않은 유저를 언팔로우 할 수 없습니다.")); + } + } + + /** + * 팔로우 상태를 조회합니다. + * @param token JWT 토큰 + * @param userId 팔로우 당하는 사용자의 ID + * @return 팔로우 상태 + */ + @GetMapping + public ResponseEntity getFollowers(@RequestHeader("Authorization") String token, + @PathVariable("user_id") Long userId) { + // JWT 토큰에서 사용자 이름 추출 + String username = jwtUtil.getUsernameFromToken(token.replace("Bearer ", "")); + + // 사용자 이름으로 사용자 조회 + User user = userRepository.findByUsername(username) + .orElseThrow(() -> new RuntimeException("유저를 찾을 수 없습니다.")); + + // 팔로워 조회 + List followers = followService.getFollowers(userId); + List followersDTO = followers.stream() + .map(follower -> new FollowResponseDTO(follower.getId(), follower.getUsername())) + .collect(Collectors.toList()); + + return ResponseEntity.ok(followersDTO); + } +} \ No newline at end of file diff --git a/src/main/java/com/sparta/oneandzerobest/follow/dto/ErrorResponseDTO.java b/src/main/java/com/sparta/oneandzerobest/follow/dto/ErrorResponseDTO.java new file mode 100644 index 0000000..fd2ec34 --- /dev/null +++ b/src/main/java/com/sparta/oneandzerobest/follow/dto/ErrorResponseDTO.java @@ -0,0 +1,13 @@ +package com.sparta.oneandzerobest.follow.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * ErrorResponseDTO는 오류 응답 시 반환되는 데이터를 담는 DTO입니다. + */ +@Getter +@AllArgsConstructor +public class ErrorResponseDTO { + private String error; +} \ No newline at end of file diff --git a/src/main/java/com/sparta/oneandzerobest/follow/dto/FollowRequestDTO.java b/src/main/java/com/sparta/oneandzerobest/follow/dto/FollowRequestDTO.java new file mode 100644 index 0000000..40b4741 --- /dev/null +++ b/src/main/java/com/sparta/oneandzerobest/follow/dto/FollowRequestDTO.java @@ -0,0 +1,13 @@ +package com.sparta.oneandzerobest.follow.dto; + +import lombok.Getter; +import lombok.Setter; + +/** + * FollowRequestDTO는 팔로우 요청 시 필요한 데이터를 담는 DTO입니다. + */ +@Getter +@Setter +public class FollowRequestDTO { + private Long followeeId; // 팔로우할 사용자의 ID +} \ No newline at end of file diff --git a/src/main/java/com/sparta/oneandzerobest/follow/dto/FollowResponseDTO.java b/src/main/java/com/sparta/oneandzerobest/follow/dto/FollowResponseDTO.java new file mode 100644 index 0000000..2dbfea6 --- /dev/null +++ b/src/main/java/com/sparta/oneandzerobest/follow/dto/FollowResponseDTO.java @@ -0,0 +1,31 @@ +package com.sparta.oneandzerobest.follow.dto; + +import lombok.Getter; +import lombok.Setter; + +/** + * FollowResponseDTO는 팔로우 응답 시 반환되는 데이터를 담는 DTO입니다. + */ +@Getter +@Setter +public class FollowResponseDTO { + private Long followerId; + private Long followeeId; + private String message; + private String username; + + /** + * 팔로우 응답 DTO를 생성하는 생성자입니다. + * + * @param followerId 팔로워의 ID + * @param username 팔로워의 사용자 이름 + */ + + public FollowResponseDTO(Long followerId, String username) { + this.followerId = followerId; + this.username = username; + } + + public FollowResponseDTO() { + } +} \ No newline at end of file diff --git a/src/main/java/com/sparta/oneandzerobest/follow/entity/Follow.java b/src/main/java/com/sparta/oneandzerobest/follow/entity/Follow.java new file mode 100644 index 0000000..19a41dc --- /dev/null +++ b/src/main/java/com/sparta/oneandzerobest/follow/entity/Follow.java @@ -0,0 +1,48 @@ +package com.sparta.oneandzerobest.follow.entity; + +import com.sparta.oneandzerobest.timestamp.TimeStamp; +import jakarta.persistence.*; +import com.sparta.oneandzerobest.auth.entity.User; + +/** + * Follow 엔티티는 팔로우 관계를 나타내는 엔티티입니다. + */ +@Entity +public class Follow extends TimeStamp { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @ManyToOne + @JoinColumn(name = "follower_id", nullable = false) + private User follower; + + @ManyToOne + @JoinColumn(name = "followee_id", nullable = false) + private User followee; + + // Getter, Setter + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public User getFollower() { + return follower; + } + + public void setFollower(User follower) { + this.follower = follower; + } + + public User getFollowee() { + return followee; + } + + public void setFollowee(User followee) { + this.followee = followee; + } +} \ No newline at end of file diff --git a/src/main/java/com/sparta/oneandzerobest/follow/repository/FollowRepository.java b/src/main/java/com/sparta/oneandzerobest/follow/repository/FollowRepository.java new file mode 100644 index 0000000..a9d70ab --- /dev/null +++ b/src/main/java/com/sparta/oneandzerobest/follow/repository/FollowRepository.java @@ -0,0 +1,16 @@ +package com.sparta.oneandzerobest.follow.repository; + +import com.sparta.oneandzerobest.follow.entity.Follow; +import com.sparta.oneandzerobest.auth.entity.User; +import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; +import java.util.Optional; + +/** + * FollowRepository는 Follow 엔티티에 대한 데이터베이스 작업을 수행하는 리포지토리입니다. + */ +public interface FollowRepository extends JpaRepository { + List findByFollower(User follower); + List findByFollowee(User followee); + Optional findByFollowerAndFollowee(User follower, User followee); +} \ No newline at end of file diff --git a/src/main/java/com/sparta/oneandzerobest/follow/service/FollowService.java b/src/main/java/com/sparta/oneandzerobest/follow/service/FollowService.java new file mode 100644 index 0000000..b622e6e --- /dev/null +++ b/src/main/java/com/sparta/oneandzerobest/follow/service/FollowService.java @@ -0,0 +1,112 @@ +package com.sparta.oneandzerobest.follow.service; + +import com.sparta.oneandzerobest.follow.dto.FollowResponseDTO; +import com.sparta.oneandzerobest.follow.entity.Follow; +import com.sparta.oneandzerobest.auth.entity.User; +import com.sparta.oneandzerobest.follow.repository.FollowRepository; +import com.sparta.oneandzerobest.auth.repository.UserRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.stream.Collectors; + +/** + * FollowService는 팔로우와 언팔로우 기능을 제공하는 서비스입니다. + * 이 서비스는 사용자 간의 팔로우 관계를 관리합니다. + */ +@Service +public class FollowService { + + @Autowired + private FollowRepository followRepository; + + @Autowired + private UserRepository userRepository; + + /** + * 팔로우 기능을 수행합니다. + * @param followerId 팔로우하는 사용자의 ID + * @param followeeId 팔로우 당하는 사용자의 ID + * @return 팔로우 응답 데이터를 담은 DTO + */ + public FollowResponseDTO follow(Long followerId, Long followeeId) { + + // 팔로우하는 사용자 조회 + User follower = userRepository.findById(followerId) + .orElseThrow(() -> new RuntimeException("팔로워를 찾을 수 없습니다.")); + + // 팔로우 당하는 사용자 조회 + User followee = userRepository.findById(followeeId) + .orElseThrow(() -> new RuntimeException("팔로위를 찾을 수 없습니다.")); + + + // 이미 팔로우한 경우 예외 발생 + if (followRepository.findByFollowerAndFollowee(follower, followee).isPresent()) { + throw new RuntimeException("이미 팔로우한 유저입니다."); + } + + // 새로운 팔로우 관계 생성 및 저장 + Follow follow = new Follow(); + follow.setFollower(follower); + follow.setFollowee(followee); + followRepository.save(follow); + + // ResponseDTO 생성 및 반환 + FollowResponseDTO responseDTO = new FollowResponseDTO(); + responseDTO.setFollowerId(follower.getId()); + responseDTO.setFolloweeId(followee.getId()); + responseDTO.setMessage("유저를 성공적으로 팔로우 했습니다."); + + return responseDTO; + } + + /** + * 언팔로우 기능을 수행합니다. + * @param followerId 언팔로우하는 사용자의 ID + * @param followeeId 언팔로우 당하는 사용자의 ID + * @return 언팔로우 응답 데이터를 담은 DTO + */ + public FollowResponseDTO unfollow(Long followerId, Long followeeId) { + + // 언팔로우하는 사용자 조회 + User follower = userRepository.findById(followerId) + .orElseThrow(() -> new RuntimeException("팔로워를 찾을 수 없습니다.")); + + // 언팔로우 당하는 사용자 조회 + User followee = userRepository.findById(followeeId) + .orElseThrow(() -> new RuntimeException("팔로위를 찾을 수 없습니다.")); + + // 팔로우 관계가 존재하지 않으면 예외 발생 + Follow follow = followRepository.findByFollowerAndFollowee(follower, followee) + .orElseThrow(() -> new RuntimeException("팔로우하지 않은 유저를 언팔로우 할 수 없습니다.")); + + // 팔로우 관계 삭제 + followRepository.delete(follow); + + // ResponseDTO 생성 및 반환 + FollowResponseDTO responseDTO = new FollowResponseDTO(); + responseDTO.setFollowerId(follower.getId()); + responseDTO.setFolloweeId(followee.getId()); + responseDTO.setMessage("유저를 성공적으로 언팔로우 했습니다."); + + return responseDTO; + } + + /** + * 특정 사용자의 팔로워 목록을 조회합니다. + * @param userId 팔로우 당하는 사용자의 ID + * @return 팔로워 목록 + */ + public List getFollowers(Long userId) { + // 팔로우 당하는 사용자 조회 + User followee = userRepository.findById(userId) + .orElseThrow(() -> new RuntimeException("유저를 찾을 수 없습니다.")); + + // 팔로우 관계에서 팔로워 목록 추출 + return followRepository.findByFollowee(followee) + .stream() + .map(Follow::getFollower) + .collect(Collectors.toList()); + } +} \ No newline at end of file diff --git a/src/main/java/com/sparta/oneandzerobest/newsfeed/controller/NewsfeedController.java b/src/main/java/com/sparta/oneandzerobest/newsfeed/controller/NewsfeedController.java new file mode 100644 index 0000000..a824999 --- /dev/null +++ b/src/main/java/com/sparta/oneandzerobest/newsfeed/controller/NewsfeedController.java @@ -0,0 +1,50 @@ +package com.sparta.oneandzerobest.newsfeed.controller; + + +import com.sparta.oneandzerobest.newsfeed.dto.NewsfeedRequestDto; +import com.sparta.oneandzerobest.newsfeed.dto.NewsfeedResponseDto; +import com.sparta.oneandzerobest.newsfeed.service.NewsfeedService; +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +public class NewsfeedController { + + private final NewsfeedService contentService; + + @PostMapping("/contents") + public ResponseEntity postNewsfeed(@Valid @RequestBody NewsfeedRequestDto contentRequestDto) { + + return contentService.postContent(contentRequestDto); + } + + @GetMapping("/contents") + public ResponseEntity> getAllNewsfeed( + @RequestParam("page")int page, + @RequestParam("size") int size) { + + return contentService.getAllContents(page,size); + } + + @PutMapping("contents/{id}") + public ResponseEntity putNewsfeed(@PathVariable Long id,@Valid @RequestBody NewsfeedRequestDto contentRequestDto) { + + return contentService.putContent(id,contentRequestDto); + } + + @DeleteMapping("contents/{id}") + public ResponseEntity deleteNewsfeed(@PathVariable Long id) { + return contentService.deleteContent(id); + } +} diff --git a/src/main/java/com/sparta/oneandzerobest/newsfeed/dto/NewsfeedRequestDto.java b/src/main/java/com/sparta/oneandzerobest/newsfeed/dto/NewsfeedRequestDto.java new file mode 100644 index 0000000..07211a7 --- /dev/null +++ b/src/main/java/com/sparta/oneandzerobest/newsfeed/dto/NewsfeedRequestDto.java @@ -0,0 +1,14 @@ +package com.sparta.oneandzerobest.newsfeed.dto; + +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import lombok.Getter; + +@Getter +public class NewsfeedRequestDto { + + @NotNull(message = "userid가 없습니다.") + private Long userid; + @NotBlank(message = "내용이 비어있습니다.") + private String content; +} diff --git a/src/main/java/com/sparta/oneandzerobest/newsfeed/dto/NewsfeedResponseDto.java b/src/main/java/com/sparta/oneandzerobest/newsfeed/dto/NewsfeedResponseDto.java new file mode 100644 index 0000000..2447b65 --- /dev/null +++ b/src/main/java/com/sparta/oneandzerobest/newsfeed/dto/NewsfeedResponseDto.java @@ -0,0 +1,16 @@ +package com.sparta.oneandzerobest.newsfeed.dto; + +import com.sparta.oneandzerobest.newsfeed.entity.Newsfeed; +import lombok.Getter; + +@Getter +public class NewsfeedResponseDto { + private Long userid; + private String content; + + public NewsfeedResponseDto(Newsfeed content) { + this.userid = content.getUserid(); + this.content = content.getContent(); + } + +} diff --git a/src/main/java/com/sparta/oneandzerobest/newsfeed/entity/Newsfeed.java b/src/main/java/com/sparta/oneandzerobest/newsfeed/entity/Newsfeed.java new file mode 100644 index 0000000..57d42f7 --- /dev/null +++ b/src/main/java/com/sparta/oneandzerobest/newsfeed/entity/Newsfeed.java @@ -0,0 +1,30 @@ +package com.sparta.oneandzerobest.newsfeed.entity; + +import com.sparta.oneandzerobest.timestamp.TimeStamp; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; + +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Entity +@Getter +@Setter +@NoArgsConstructor +public class Newsfeed extends TimeStamp { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + private Long userid; + private String content; + + public Newsfeed(Long userid, String content) { + this.userid = userid; + this.content = content; + } + +} diff --git a/src/main/java/com/sparta/oneandzerobest/newsfeed/repository/NewsfeedRepository.java b/src/main/java/com/sparta/oneandzerobest/newsfeed/repository/NewsfeedRepository.java new file mode 100644 index 0000000..0138d2a --- /dev/null +++ b/src/main/java/com/sparta/oneandzerobest/newsfeed/repository/NewsfeedRepository.java @@ -0,0 +1,8 @@ +package com.sparta.oneandzerobest.newsfeed.repository; + +import com.sparta.oneandzerobest.newsfeed.entity.Newsfeed; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface NewsfeedRepository extends JpaRepository { + +} diff --git a/src/main/java/com/sparta/oneandzerobest/newsfeed/service/NewsfeedService.java b/src/main/java/com/sparta/oneandzerobest/newsfeed/service/NewsfeedService.java new file mode 100644 index 0000000..46b414e --- /dev/null +++ b/src/main/java/com/sparta/oneandzerobest/newsfeed/service/NewsfeedService.java @@ -0,0 +1,85 @@ +package com.sparta.oneandzerobest.newsfeed.service; + +import com.sparta.oneandzerobest.newsfeed.dto.NewsfeedRequestDto; +import com.sparta.oneandzerobest.newsfeed.dto.NewsfeedResponseDto; +import com.sparta.oneandzerobest.newsfeed.entity.Newsfeed; +import com.sparta.oneandzerobest.newsfeed.repository.NewsfeedRepository; +import jakarta.validation.ConstraintViolationException; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.data.domain.Sort.Direction; +import org.springframework.http.ResponseEntity; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +public class NewsfeedService { + + private final NewsfeedRepository newsfeedRepository; + + public ResponseEntity postContent(NewsfeedRequestDto contentRequestDto) { + + try { + Newsfeed newsfeed = new Newsfeed(contentRequestDto.getUserid(), contentRequestDto.getContent()); + newsfeedRepository.save(newsfeed); + + NewsfeedResponseDto newsfeedResponseDto = new NewsfeedResponseDto(newsfeed); + + return ResponseEntity.ok(newsfeedResponseDto); + } catch (ConstraintViolationException e) { + + return ResponseEntity.badRequest().body(null); + } + + } + + public ResponseEntity> getAllContents(int page, int size) { + + try { + + Sort.Direction direction = Direction.DESC; + Sort sort = Sort.by(direction,"createdAt"); + Pageable pageable = PageRequest.of(page, size, sort); + Page newsfeedList = newsfeedRepository.findAll(pageable); + Page newsfeedResponseDtoPage = newsfeedList.map(NewsfeedResponseDto::new); + return ResponseEntity.ok(newsfeedResponseDtoPage); + } catch (RuntimeException e) { + + return ResponseEntity.badRequest().header("잘못된 요청입니다.").body(null); + } + + } + + @Transactional + public ResponseEntity putContent(Long contentId, + NewsfeedRequestDto contentRequestDto) { + + try { + Newsfeed newsfeed = newsfeedRepository.findById(contentId).orElseThrow(() -> new RuntimeException("Content not found")); + + newsfeed.setContent(contentRequestDto.getContent()); + + } catch (ConstraintViolationException e) { + return ResponseEntity.badRequest().header("잘못된 요청입니다.").body(null); + } + + return null; + } + + + public ResponseEntity deleteContent(Long contentId) { + + try{ + Newsfeed content = newsfeedRepository.findById(contentId).orElseThrow(() -> new RuntimeException("Content not found")); + newsfeedRepository.delete(content); + return ResponseEntity.ok(content.getId()); + } + catch(RuntimeException e){ + return ResponseEntity.badRequest().header("잘못된 요청입니다.").body(null); + } + } +} diff --git a/src/main/java/com/sparta/oneandzerobest/page/PageController.java b/src/main/java/com/sparta/oneandzerobest/page/PageController.java deleted file mode 100644 index 344b585..0000000 --- a/src/main/java/com/sparta/oneandzerobest/page/PageController.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.sparta.oneandzerobest.page; - -public class PageController { -} diff --git a/src/main/java/com/sparta/oneandzerobest/timestamp/TimeStamp.java b/src/main/java/com/sparta/oneandzerobest/timestamp/TimeStamp.java new file mode 100644 index 0000000..f1c4330 --- /dev/null +++ b/src/main/java/com/sparta/oneandzerobest/timestamp/TimeStamp.java @@ -0,0 +1,28 @@ +package com.sparta.oneandzerobest.timestamp; + +import jakarta.persistence.Column; +import jakarta.persistence.EntityListeners; +import jakarta.persistence.MappedSuperclass; +import jakarta.persistence.Temporal; +import jakarta.persistence.TemporalType; +import java.time.LocalDateTime; +import lombok.Getter; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + +@Getter +@MappedSuperclass +@EntityListeners(AuditingEntityListener.class) +public abstract class TimeStamp { + + @CreatedDate + @Column(updatable = false) + @Temporal(TemporalType.TIMESTAMP) + private LocalDateTime createdAt; + + @LastModifiedDate + @Column + @Temporal(TemporalType.TIMESTAMP) + private LocalDateTime modifiedAt; +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 4f1de76..091a282 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -5,7 +5,7 @@ spring: url: jdbc:mysql://localhost:3306/siumechu driver-class-name: com.mysql.cj.jdbc.Driver username: root - password: Ekalekal1@ + password: 123456789 jpa: hibernate: ddl-auto: create diff --git a/src/test/java/com/sparta/oneandzerobest/OneAndZeroBestApplicationTests.java b/src/test/java/com/sparta/oneandzerobest/OneAndZeroBestApplicationTests.java index f3db274..da7c0bc 100644 --- a/src/test/java/com/sparta/oneandzerobest/OneAndZeroBestApplicationTests.java +++ b/src/test/java/com/sparta/oneandzerobest/OneAndZeroBestApplicationTests.java @@ -2,6 +2,8 @@ import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; + @SpringBootTest class OneAndZeroBestApplicationTests {