Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be #90

Merged
merged 9 commits into from
Mar 23, 2024
Merged
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ build/

## 서버 설정 정보 ##
!**/src/main/resources/application.yml
!**/src/main/resources/application-dev.yml

### STS ###
.apt_generated
Expand Down
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dependencies {
// security
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation "io.jsonwebtoken:jjwt:0.9.1"
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'

// test
testImplementation 'org.springframework.boot:spring-boot-starter-test'
Expand All @@ -47,6 +48,8 @@ dependencies {
implementation 'com.sun.xml.bind:jaxb-impl:4.0.1'
implementation 'com.sun.xml.bind:jaxb-core:4.0.1'
implementation 'javax.xml.bind:jaxb-api:2.4.0-b180830.0359'

implementation 'commons-io:commons-io:2.6'
}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;

@SpringBootApplication
public class PlanItApplication {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package com.sideProject.PlanIT.common.modules;

import com.sideProject.PlanIT.common.response.CustomException;
import com.sideProject.PlanIT.common.response.ErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@Component
@Slf4j
public class FileHandler {
@Value("${spring.fileStorage.dir}")
private String fileStorageDir;
Expand All @@ -28,6 +34,20 @@ public String saveFile(MultipartFile file) {
}
}

public byte[] loadImage(String path) throws IOException {
String allPath = fileStorageDir +"images"+ path;
log.info(allPath);
File imageFile = new File(allPath);
if (imageFile.exists()) {
FileInputStream fileInputStream = new FileInputStream(imageFile);
byte[] imageBytes = IOUtils.toByteArray(fileInputStream);
fileInputStream.close();
return imageBytes;
} else{
throw new CustomException("이미지가 존재하지 않습니다.", ErrorCode.IMAGE_NOT_FOUND);
}
}

public MultipartFile sendFile(String dir) {
//todo: 파일 전송
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public enum ErrorCode {
ALREADY_APPROVE_PROGRAM(400, "이미 등록된 프로그램입니다."),
ALREADY_EXIST_EMAIL(400, "이미 존재하는 이메일입니다."),
INVALID_PASSWORD(400, "비밀번호가 틀렸습니다."),
NOT_PT(400, "PT이용권이 아닙니다."),
NOT_YOUR_TRAINER(400, "예약 가능한 트레이너가 아닙니다."),
ALREADY_RESERVATION(400, "이미 예약 되어있습니다."),



//401
INVALID_ACCESS_TOKEN(401, "ACCESS TOKEN 오류"),
Expand All @@ -26,6 +31,9 @@ public enum ErrorCode {
REGISTRATION_NOT_FOUND(404,"등록을 찾을 수 없습니다"),
MEMBER_NOT_FOUND(404,"회원을 찾을 수 없습니다"),
EMPLOYEE_NOT_FOUND(404,"직원을 찾을 수 없습니다"),
IMAGE_NOT_FOUND(404,"이미지를 찾을 수 없습니다"),
RESERVATION_NOT_FOUND(404,"등록되지 않은 예약입니다."),
FILE_NOT_FOUND(404,"파일을 찾을 수 없습니다"),

NOT_SUSPEND_PROGRAM(422, "일시정지 요청이 거부되었습니다."),
SUSPEND_REQUEST_DENIED(422, "일시정지 요청이 거부되었습니다.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,16 @@
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.springframework.security.config.Customizer.withDefaults;

@EnableWebSecurity
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig {


@Bean
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
Expand Down Expand Up @@ -62,12 +59,13 @@ public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {
.exceptionHandling((exceptionHandling) ->
exceptionHandling.authenticationEntryPoint(jwtAuthenticationEntryPoint))
.authorizeHttpRequests((authorizeRequests) -> authorizeRequests
.requestMatchers("/member/signin", "/member/signup", "/refresh").permitAll()
.requestMatchers("/member/signin", "/member/signup", "member/refresh").permitAll()
.requestMatchers("/admin/**").hasAnyAuthority("ADMIN")
.anyRequest().authenticated()
// .anyRequest().permitAll()
)
.addFilterBefore(jwtTokenFilter, UsernamePasswordAuthenticationFilter.class)

.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.sideProject.PlanIT.domain.file.controller;

import com.sideProject.PlanIT.common.response.ApiResponse;
import com.sideProject.PlanIT.domain.file.service.FileService;
import com.sideProject.PlanIT.domain.post.dto.request.NoticeRequestDto;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("/file")
public class FileController {
@Autowired
FileService fileService;

@PostMapping
public ApiResponse<String> saveFile(@ModelAttribute NoticeRequestDto noticeRequestDto) {
return ApiResponse.ok(fileService.saveFile(noticeRequestDto.getImage()));
}
@GetMapping("/{file_name:.+}")
public ResponseEntity<?> sendFile(@PathVariable String file_name, HttpServletRequest request) {
Resource resource = fileService.sendFile(file_name);

String contentType = null;
try {
contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}

if (contentType == null) {
contentType = "application/octet-stream";
}

return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(contentType))
.body(resource);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.sideProject.PlanIT.domain.file.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

public interface FileService {
public String saveFile(MultipartFile file);
public Resource sendFile(String fileName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.sideProject.PlanIT.domain.file.service;

import com.sideProject.PlanIT.common.response.CustomException;
import com.sideProject.PlanIT.common.response.ErrorCode;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.UUID;

@Service
public class FileServiceImpl implements FileService{
@Value("${spring.fileStorage.dir}")
private String fileStorageDir;

@Override
public String saveFile(MultipartFile file) {
if (file == null || file.isEmpty()) {
return null;
}
try {
UUID uuid = UUID.randomUUID();
String fileName = uuid + file.getOriginalFilename();
File dest = new File(fileStorageDir + File.separator + fileName);
file.transferTo(dest);

return fileName;
} catch (IOException e) {
return "이미지 업로드 오류 발생";
}
}

@Override
public Resource sendFile(String fileName) {
try {
File file = new File(fileStorageDir + File.separator + fileName);

Resource resource = new FileSystemResource(file);

if (resource.exists() || resource.isReadable()) {
return resource;
} else {
throw new CustomException(ErrorCode.FILE_NOT_FOUND);
}
} catch (Exception e) {
throw new CustomException(ErrorCode.FILE_NOT_FOUND);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.sideProject.PlanIT.domain.post.controller;

import com.sideProject.PlanIT.common.response.ApiResponse;
import com.sideProject.PlanIT.domain.post.dto.request.BannerRequestDto;
import com.sideProject.PlanIT.domain.post.dto.response.BannerResponseDto;
import com.sideProject.PlanIT.domain.post.service.BannerService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/banner")
@RequiredArgsConstructor
public class BannerAdminController {
private final BannerService bannerService;

@PostMapping
public ApiResponse<String> createBanner(@ModelAttribute BannerRequestDto bannerRequestDto) {
return ApiResponse.ok(bannerService.createBanner(bannerRequestDto));
}

@PutMapping("/{banner_id}")
public ApiResponse<String> editBanner(@PathVariable Long banner_id, @ModelAttribute BannerRequestDto bannerRequestDto) {
return ApiResponse.ok(bannerService.editBanner(banner_id, bannerRequestDto));
}

@DeleteMapping("/{banner_id}")
public ApiResponse<String> deleteBanner(@PathVariable Long banner_id) {
return ApiResponse.ok(bannerService.deleteBanner(banner_id));
}

@GetMapping
public ApiResponse<List<BannerResponseDto>> findAllBanners() {
return ApiResponse.ok(bannerService.findAllBanners());
}
}
Original file line number Diff line number Diff line change
@@ -1,48 +1,31 @@
package com.sideProject.PlanIT.domain.post.controller;

import com.sideProject.PlanIT.common.response.ApiResponse;
import com.sideProject.PlanIT.domain.post.dto.request.BannerRequestDto;
import com.sideProject.PlanIT.domain.post.dto.response.BannerResponseDto;
import com.sideProject.PlanIT.domain.post.service.BannerService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


//todo: Banner, Notice 둘 다 Edit 발생 시 기존 첨부파일 삭제 후 다시 저장
@RestController
@AllArgsConstructor
@RequiredArgsConstructor
@RequestMapping("/banner")
public class BannerController {

private final BannerService bannerService;

@PostMapping("/admin/banner")
public ApiResponse<String> createBanner(@ModelAttribute BannerRequestDto bannerRequestDto) {
return ApiResponse.ok(bannerService.createBanner(bannerRequestDto));
}

@PutMapping("admin/banner/{banner_id}")
public ApiResponse<String> editBanner(@PathVariable Long banner_id, @ModelAttribute BannerRequestDto bannerRequestDto) {
return ApiResponse.ok(bannerService.editBanner(banner_id, bannerRequestDto));
}

@DeleteMapping("/admin/banner/{banner_id}")
public ApiResponse<String> deleteBanner(@PathVariable Long banner_id) {
return ApiResponse.ok(bannerService.deleteBanner(banner_id));
}

@GetMapping("/admin/banner")
public ApiResponse<List<BannerResponseDto>> findAllBanners() {
return ApiResponse.ok(bannerService.findAllBanners());
}

@GetMapping("/banner")
@GetMapping
public ApiResponse<List<BannerResponseDto>> findAllBannersInTime() {
return ApiResponse.ok(bannerService.findAllBannersInTime());
}

@GetMapping("/banner/{banner_id}")
@GetMapping("/{banner_id}")
public ApiResponse<BannerResponseDto> findBanner(@PathVariable Long banner_id) {
return ApiResponse.ok(bannerService.findBanner(banner_id));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.sideProject.PlanIT.domain.post.controller;

import com.sideProject.PlanIT.common.response.ApiResponse;
import com.sideProject.PlanIT.domain.post.dto.request.NoticeRequestDto;
import com.sideProject.PlanIT.domain.post.dto.response.NoticeResponseDto;
import com.sideProject.PlanIT.domain.post.service.NoticeService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/notice")
@RequiredArgsConstructor
public class NoticeAdminController {
private final NoticeService noticeService;

@PostMapping
public ApiResponse<String> createNotice(@ModelAttribute NoticeRequestDto noticeRequestDto) {
return ApiResponse.ok(noticeService.createNotice(noticeRequestDto));
}

@PutMapping("/{notice_id}")
public ApiResponse<String> editNotice(@PathVariable Long notice_id , @ModelAttribute NoticeRequestDto noticeRequestDto) {
return ApiResponse.ok(noticeService.editNotice(notice_id, noticeRequestDto));
}

@DeleteMapping("/{notice_id}")
public ApiResponse<String> deleteNotice(@PathVariable Long notice_id) {
return ApiResponse.ok(noticeService.deleteNotice(notice_id));
}

@GetMapping
public ApiResponse<List<NoticeResponseDto>> findAllNotices() {
return ApiResponse.ok(noticeService.findAllNotices());
}
}
Loading
Loading