Skip to content

Commit

Permalink
Merge pull request #19 from UMC-HACKATHON-SnapSpot/feat/#14
Browse files Browse the repository at this point in the history
Feat/#14
  • Loading branch information
wonjun0120 authored Jul 4, 2024
2 parents 3298f46 + 1347c9b commit 055219d
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import jakarta.persistence.MappedSuperclass;
import java.time.LocalDateTime;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
Expand All @@ -22,6 +23,7 @@ public abstract class BaseEntity {
@Column(name = "updated_at")
private LocalDateTime updatedAt;

@Setter
@Column(name = "deleted", nullable = false)
private Boolean isDeleted = false;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.umc.hackaton.snapspot.savespot.controller;

public class SaveSpotController {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.umc.hackaton.snapspot.savespot.controller;

import com.umc.hackaton.snapspot.savespot.dto.UserFolderRequestDto;
import com.umc.hackaton.snapspot.savespot.entity.UserFolder;
import com.umc.hackaton.snapspot.savespot.service.UserFolderService;
import com.umc.hackaton.snapspot.user.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/api/v1/folders")
public class UserFolderController {
private final UserFolderService userFolderService;
private final UserService userService;

@PostMapping("/")
public ResponseEntity<?> createFolder(@RequestBody UserFolderRequestDto folder) {
try {
userFolderService.save(folder);
return ResponseEntity.ok().body(folder);
} catch (Exception e){
log.info("유저 폴더 생성에 실패하였습니다.", e);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("폴더 생성에 실패하였습니다.");
}
}

@DeleteMapping("/{folderId}")
public ResponseEntity<?> deleteFolder(
@PathVariable("folderId") Long folderId
) {
try {
userFolderService.delete(folderId);
return ResponseEntity.ok().body("Success");
} catch (Exception e){
log.info("유저 폴더 삭제에 실패하였습니다.", e);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("폴더 삭제에 실패하였습니다.");
}
}

@PatchMapping("/{folderId}/")
public ResponseEntity<?> updateFolder(
@PathVariable("folderId") Long folderId,
@RequestBody UserFolderRequestDto folder
) {
try {
UserFolder userFolder = userFolderService.update(folderId, folder);
return ResponseEntity.ok().body(userFolder);
} catch (Exception e){
log.info("유저 폴더 업데이트에 실패하였습니다.", e);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("폴더 삭제에 실패하였습니다.");
}
}

@GetMapping("/{userId}")
public ResponseEntity<?> getFolders(@PathVariable("userId") Long userId) {
try {
List<UserFolder> userFolders = userFolderService.getAllFoldersByUserId(userId);
return ResponseEntity.ok().body(userFolders);
} catch (Exception e) {
log.info("유저 폴더 조회에 실패하였습니다.", e);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("폴더 조회에 실패하였습니다.");
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.umc.hackaton.snapspot.savespot.dto;

import com.umc.hackaton.snapspot.savespot.entity.UserFolder;
import com.umc.hackaton.snapspot.user.entity.User;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Getter;

@Getter
public class UserFolderRequestDto {
@NotNull private Long userId;
@NotNull private String folderName;

@Builder
public UserFolder toEntity(User user){
return UserFolder.builder()
.user(user)
.folderName(folderName)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.umc.hackaton.snapspot.savespot.dto;

public class UserFolderResponseDto {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.umc.hackaton.snapspot.savespot.entity;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.umc.hackaton.snapspot.config.entity.BaseEntity;
import com.umc.hackaton.snapspot.user.entity.User;
import jakarta.persistence.*;
import lombok.*;

@Entity
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class UserFolder extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
@JsonBackReference
private User user;

@Setter
@Column(name = "folder_name", nullable = false)
private String folderName;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.umc.hackaton.snapspot.savespot.repository;

import com.umc.hackaton.snapspot.savespot.entity.UserFolder;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface UserFolderRepository extends JpaRepository<UserFolder, Long> {
List<UserFolder> findAllByUserIdAndIsDeletedFalse(Long userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.umc.hackaton.snapspot.savespot.service;

import com.umc.hackaton.snapspot.savespot.dto.UserFolderRequestDto;
import com.umc.hackaton.snapspot.savespot.entity.UserFolder;
import com.umc.hackaton.snapspot.savespot.repository.UserFolderRepository;
import com.umc.hackaton.snapspot.user.entity.User;
import com.umc.hackaton.snapspot.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class UserFolderService {
private final UserFolderRepository userFolderRepository;
private final UserRepository userRepository;

@Transactional
public void save(UserFolderRequestDto userFolder) {
User user = userRepository.findById(userFolder.getUserId()).orElse(null);
userFolderRepository.save(userFolder.toEntity(user));
}

public void delete(Long folderId) {
UserFolder userFolder = userFolderRepository.findById(folderId).orElse(null);
userFolder.setIsDeleted(true);
userFolderRepository.save(userFolder);
}

public UserFolder update(Long folderId, UserFolderRequestDto userFolderRequestDto) {
UserFolder userFolder = userFolderRepository.findById(folderId).orElse(null);
userFolder.setFolderName(userFolderRequestDto.getFolderName());
userFolderRepository.save(userFolder);
return userFolderRepository.findById(folderId).get();
}

public List<UserFolder> getAllFoldersByUserId(Long userId) {
return userFolderRepository.findAllByUserIdAndIsDeletedFalse(userId);
}
}
16 changes: 12 additions & 4 deletions src/main/java/com/umc/hackaton/snapspot/user/entity/User.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.umc.hackaton.snapspot.user.entity;

import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.umc.hackaton.snapspot.config.entity.BaseEntity;
import com.umc.hackaton.snapspot.savespot.entity.UserFolder;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;

import java.util.LinkedHashSet;
import java.util.Set;

@Entity
@Getter
Expand Down Expand Up @@ -42,4 +44,10 @@ public void prePersist() {
public void updateProfileImg(String profileImg) {
this.profileImg = profileImg;
}

@ToString.Exclude
@JsonManagedReference
@OrderBy("id")
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private final Set<UserFolder> userFolders = new LinkedHashSet<>();
}

0 comments on commit 055219d

Please sign in to comment.