Skip to content

Commit

Permalink
Merge pull request #39 from dsc-sookmyung/feat/Folder-API-생성-#29
Browse files Browse the repository at this point in the history
Feat/folder api 생성 #29
  • Loading branch information
kryptonite43 authored Mar 27, 2023
2 parents 6059c3b + 71135dc commit 7ea78bf
Show file tree
Hide file tree
Showing 23 changed files with 512 additions and 47 deletions.
10 changes: 8 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,23 @@ dependencies {
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
runtimeOnly 'com.h2database:h2'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
implementation 'org.springframework.session:spring-session-jdbc'
// implementation 'org.springframework.session:spring-session-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'io.jsonwebtoken:jjwt-api:0.11.1'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.1', 'io.jsonwebtoken:jjwt-jackson:0.11.1'
implementation 'org.springframework.cloud:spring-cloud-gcp-starter-storage:1.2.8.RELEASE'
// implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
// implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'
// implementation 'org.springframework.cloud:spring-cloud-gcp-starter-storage:1.2.8.RELEASE'
implementation group: 'org.springframework.cloud', name: 'spring-cloud-gcp-starter', version: '1.2.5.RELEASE'
implementation group: 'org.springframework.cloud', name: 'spring-cloud-gcp-storage', version: '1.2.5.RELEASE'
annotationProcessor 'org.projectlombok:lombok'
// implementation 'mysql:mysql-connector-java'
}



tasks.named('test') {
// outputs.dir snippetsDir
useJUnitPlatform()
Expand Down
20 changes: 14 additions & 6 deletions src/main/java/com/SollutionChallenge/HighLight/Folder/Folder.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.SollutionChallenge.HighLight.User.Entity.User;

import com.SollutionChallenge.HighLight.User.User;


import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Table(name = "folder")
@Table(name = "Folder")
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
Expand All @@ -29,15 +31,21 @@ public class Folder {
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
@JoinColumn(name = "user_id")
private User userId;

private static Folder createFolder(Long id, User userId){
@Column(nullable = false)
private String name;


public static Folder createFolder( User userId, String name){
Folder folder = new Folder();
folder.id = id;
// folder.id = id;
folder.userId=userId;
folder.name=name;

return folder;

return new Folder();
}

}
Original file line number Diff line number Diff line change
@@ -1,30 +1,39 @@
package com.SollutionChallenge.HighLight.Folder;

import java.util.List;
import java.util.Map;

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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.SollutionChallenge.HighLight.User.Entity.User;
import com.SollutionChallenge.HighLight.common.ApiResponse;
import com.SollutionChallenge.HighLight.common.Success;

import lombok.RequiredArgsConstructor;

@RestController
@RequestMapping()
@RequiredArgsConstructor
public class FolderController {
FolderService folderService;
private final FolderService folderService;

@PostMapping("/folder")
private ApiResponse createFolder(@RequestBody User userId){
return ApiResponse.successCode(Success.CREATE_FOLDER_SUCCESS,userId);
private ApiResponse createFolder(@RequestBody FolderRequestDto folderRequestDto){
FolderResponseDto response = folderService.save(folderRequestDto);
return ApiResponse.successCode(Success.CREATE_FOLDER_SUCCESS,response);
}

@GetMapping("/folder")
private ApiResponse<Map<String, List<FolderResponseDto>>> getFolder(){
Map<String, List<FolderResponseDto>> response = folderService.viewFolder();
return ApiResponse.successCode(Success.GET_FOLDER_SUCCESS,response);
}

// @GetMapping("/folder")
// private Folder getFolder(){
// return folderService.getFolder();
// }
@GetMapping("/folder/{folder_id}")
private ApiResponse getOneFolder(@PathVariable Long folder_id){
FolderResponseDto response = folderService.viewOneFolder(folder_id);
return ApiResponse.successCode(Success.GET_FOLDER_SUCCESS,response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.SollutionChallenge.HighLight.Folder;

import java.util.List;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
public class FolderListResponseDto {
private List<FolderResponseDto> folderList;

public FolderListResponseDto(List<FolderResponseDto> folderList) {
this.folderList = folderList;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package com.SollutionChallenge.HighLight.Folder;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.sun.istack.NotNull;

@Repository
public interface FolderRepository extends JpaRepository<Folder, Long> {
// List<Folder> findAll();

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.SollutionChallenge.HighLight.Folder;

import com.SollutionChallenge.HighLight.User.Entity.User;
import com.SollutionChallenge.HighLight.User.User;

import lombok.AllArgsConstructor;
import lombok.Getter;
Expand All @@ -12,6 +12,7 @@
@NoArgsConstructor
@AllArgsConstructor
public class FolderRequestDto {
private User userID;
private Long userId;
private String folderName;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.SollutionChallenge.HighLight.Folder;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class FolderResponseDto {
private Long id;
private String folderName;

public FolderResponseDto(Long id, String folderName) {
this.id = id;
this.folderName = folderName;
}

public static FolderResponseDto from(Long id, String folderName){
FolderResponseDto folderResponseDto = new FolderResponseDto(id, folderName);
folderResponseDto.id = id;
folderResponseDto.folderName = folderName;
return folderResponseDto;
}


}
Original file line number Diff line number Diff line change
@@ -1,16 +1,92 @@
package com.SollutionChallenge.HighLight.Folder;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.SollutionChallenge.HighLight.User.User;
import com.SollutionChallenge.HighLight.User.UserRepository;
import com.SollutionChallenge.HighLight.auth.ConfigUtils;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@Service
public class FolderService {
FolderRepository folderRepository;

private final FolderRepository folderRepository;
// private final
private final Folder folder;
// private final UserRepository userRepository;

@Autowired
public FolderService(FolderRepository folderRepository) {
this.folderRepository = folderRepository;
this.folder = Folder.createFolder(null, null);
}

// @Transactional
// public Folder getFolder() {
// return folderRepository.findAll();
// public FolderResponseDto save(FolderRequestDto folderRequestDto) {
// String folderName = folderRequestDto.getFolderName();
// return FolderResponseDto.from( folderName);
// }

// public FolderViewResponseDto viewFolder() {
// String id = folder.getName();
// List<Folder> folderName= folderRepository.findAll();
// return FolderViewResponseDto.of(folderName);
// }

@Transactional
public FolderResponseDto save(FolderRequestDto folderRequestDto) {
User userId = folder.getUserId(); // 해당 폴더를 생성한 유저 정보
String folderName = folderRequestDto.getFolderName();
Folder savedFolder = folderRepository.save(Folder.createFolder(userId, folderName));
return FolderResponseDto.from(savedFolder.getId(), savedFolder.getName());
}
public Map<String, List<FolderResponseDto>> viewFolder() {
List<Folder> folders = folderRepository.findAll();
List<FolderResponseDto> folderResponseDtos = new ArrayList<>();
if (folders != null && !folders.isEmpty()) {
folderResponseDtos = folders.stream()
.filter(f -> f.getName() != null)
.map(f-> new FolderResponseDto(f.getId(),f.getName()))
.collect(Collectors.toList());
}

Map<String, List<FolderResponseDto>> response = new HashMap<>();
response.put("folder", folderResponseDtos);
return response;
}

// public List<FolderResponseDto> viewFolder() {
// List<Folder> folders = folderRepository.findAll();
// List<FolderResponseDto> folderResponseDtos =new ArrayList<>();
// if (folders != null && !folders.isEmpty()) {
// folderResponseDtos = folders.stream()
// .filter(f -> f.getName() != null)
// .map(f-> new FolderResponseDto(f.getId(),f.getName()))
// .collect(Collectors.toList());
// }
// return folderResponseDtos;
// }

public FolderResponseDto viewOneFolder(Long folder_id) {
Optional<Folder> folder = folderRepository.findById(folder_id);
if(folder.isPresent()){
String folderName = folder.get().getName();
}
return FolderResponseDto.from(folder_id,folder.get().getName());

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.SollutionChallenge.HighLight.Folder;

import java.util.List;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class FolderViewResponseDto {
private List<Long> id;
private List<String> folderName;

public static FolderViewResponseDto of(List <Long> id,List<String >folderName){
FolderViewResponseDto folderViewResponseDto = new FolderViewResponseDto();
folderViewResponseDto.id= id;
folderViewResponseDto.folderName = folderName;

return folderViewResponseDto;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;


// @EnableJpaAuditing
@SpringBootApplication
@ComponentScan(basePackages = {"com.SollutionChallenge.HighLight","com.SollutionChallenge.HighLight.User"})
public class HighLightApplication {

public static void main(String[] args) {
SpringApplication.run(HighLightApplication.class, args);

}

}
57 changes: 57 additions & 0 deletions src/main/java/com/SollutionChallenge/HighLight/User/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.SollutionChallenge.HighLight.User;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@Entity
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false)
private String name;

@Column(nullable = false)
private String email;

@Column
private String picture;

/*
@Enumerated(EnumType.STRING)
JPA로 데이터베이스로 저장할 때 Enum 값을 어떤 형태로 저장할지를 결정함
기본적으로는 int로 된 숫자가 저장된다.
숫자로 저장되면 데이터베이스로 확인할 때 그 값이 무슨 코드를 의미하는지 알 수 가 없다.
그래서 문자열로 저장될 수 있도록 선언
*/


@Builder
public User(String name, String email, String picture){
this.name = name;
this.email = email;
this.picture = picture;

}

public User update(String name, String picture){
this.name = name;
this.picture = picture;

return this;
}



}
Loading

0 comments on commit 7ea78bf

Please sign in to comment.