Skip to content

Commit

Permalink
Merge pull request #9 from URECA-PODONG/feat/#6
Browse files Browse the repository at this point in the history
Feat/#6
  • Loading branch information
EunSeok-222 authored Nov 3, 2024
2 parents 7f54f39 + 2e9929c commit 0e2f87e
Show file tree
Hide file tree
Showing 36 changed files with 868 additions and 307 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ out/

### VS Code ###
.vscode/
logs/spring-boot.log
Binary file modified logs/spring-boot.log.2024-11-02.0.gz
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,69 +1,72 @@
//package com.ureca.sole_paradise.community.controller;
//
//import java.util.List;
//
//import org.springframework.http.HttpStatus;
//import org.springframework.http.MediaType;
//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.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
//import com.ureca.sole_paradise.community.db.dto.CommunityCommentDTO;
//import com.ureca.sole_paradise.community.service.CommunityCommentService;
//
//import io.swagger.v3.oas.annotations.responses.ApiResponse;
//import jakarta.validation.Valid;
//
//@RestController
//@RequestMapping(value = "/api/communityComments", produces = MediaType.APPLICATION_JSON_VALUE)
//public class CommunityCommentController {
//
// private final CommunityCommentService communityCommentService;
//
// public CommunityCommentController(final CommunityCommentService communityCommentService) {
// this.communityCommentService = communityCommentService;
// }
//
// @GetMapping
// public ResponseEntity<List<CommunityCommentDTO>> getAllCommunityComments() {
// return ResponseEntity.ok(communityCommentService.findAll());
// }
//
// @GetMapping("/{commentId}")
// public ResponseEntity<CommunityCommentDTO> getCommunityComment(
// @PathVariable(name = "commentId") final Integer commentId) {
// return ResponseEntity.ok(communityCommentService.get(commentId));
// }
//
// @PostMapping
// @ApiResponse(responseCode = "201")
// public ResponseEntity<Integer> createCommunityComment(
// @RequestBody @Valid final CommunityCommentDTO communityCommentDTO) {
// final Integer createdCommentId = communityCommentService.create(communityCommentDTO);
// return new ResponseEntity<>(createdCommentId, HttpStatus.CREATED);
// }
//
// @PutMapping("/{commentId}")
// public ResponseEntity<Integer> updateCommunityComment(
// @PathVariable(name = "commentId") final Integer commentId,
// @RequestBody @Valid final CommunityCommentDTO communityCommentDTO) {
// communityCommentService.update(commentId, communityCommentDTO);
// return ResponseEntity.ok(commentId);
// }
//
// @DeleteMapping("/{commentId}")
// @ApiResponse(responseCode = "204")
// public ResponseEntity<Void> deleteCommunityComment(
// @PathVariable(name = "commentId") final Integer commentId) {
// communityCommentService.delete(commentId);
// return ResponseEntity.noContent().build();
// }
//
//
//}
package com.ureca.sole_paradise.community.controller;

import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ureca.sole_paradise.community.db.dto.CommunityCommentDTO;
import com.ureca.sole_paradise.community.service.CommunityCommentService;

import io.swagger.v3.oas.annotations.responses.ApiResponse;
import jakarta.validation.Valid;

@CrossOrigin("*")
@RestController
@RequestMapping(value = "/api/communityComments", produces = MediaType.APPLICATION_JSON_VALUE)
public class CommunityCommentController {

private final CommunityCommentService communityCommentService;

public CommunityCommentController(final CommunityCommentService communityCommentService) {
this.communityCommentService = communityCommentService;
}

@GetMapping
public ResponseEntity<List<CommunityCommentDTO>> getAllCommunityComments() {
return ResponseEntity.ok(communityCommentService.findAll());
}

@GetMapping("/{commentId}")
public ResponseEntity<CommunityCommentDTO> getCommunityComment(
@PathVariable(name = "commentId") final Integer commentId) {
return ResponseEntity.ok(communityCommentService.get(commentId));
}

@PostMapping
@ApiResponse(responseCode = "201")
public ResponseEntity<Integer> createCommunityComment(
@RequestBody @Valid final CommunityCommentDTO communityCommentDTO) {
final Integer createdCommentId = communityCommentService.create(communityCommentDTO);
return new ResponseEntity<>(createdCommentId, HttpStatus.CREATED);
}

@PutMapping("/{commentId}")
public ResponseEntity<Integer> updateCommunityComment(
@PathVariable(name = "commentId") final Integer commentId,
@RequestBody @Valid final CommunityCommentDTO communityCommentDTO) {
communityCommentService.update(commentId, communityCommentDTO);
return ResponseEntity.ok(commentId);
}

@DeleteMapping("/{commentId}")
@ApiResponse(responseCode = "204")
public ResponseEntity<Void> deleteCommunityComment(
@PathVariable(name = "commentId") final Integer commentId) {
communityCommentService.delete(commentId);
return ResponseEntity.noContent().build();
}


}

Original file line number Diff line number Diff line change
@@ -1,76 +1,128 @@
//package com.ureca.sole_paradise.community.controller;
//
//import java.util.List;
//
//import org.springframework.http.HttpStatus;
//import org.springframework.http.MediaType;
//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.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
////import io.bootify.back_end01.model.CommunityDTO;
////import io.bootify.back_end01.service.CommunityService;
////import io.bootify.back_end01.util.ReferencedException;
////import io.bootify.back_end01.util.ReferencedWarning;
////import io.swagger.v3.oas.annotations.responses.ApiResponse;
////import jakarta.validation.Valid;
//
//@RestController
//@RequestMapping(value = "/api/communities", produces = MediaType.APPLICATION_JSON_VALUE)
//public class CommunityController {
//
//
// private final CommunityService communityService;
//
// public CommunityResource(final CommunityService communityService) {
// this.communityService = communityService;
// }
//
// @GetMapping
// public ResponseEntity<List<CommunityDTO>> getAllCommunities() {
// return ResponseEntity.ok(communityService.findAll());
// }
//
// @GetMapping("/{postId}")
// public ResponseEntity<CommunityDTO> getCommunity(
// @PathVariable(name = "postId") final Integer postId) {
// return ResponseEntity.ok(communityService.get(postId));
// }
//
package com.ureca.sole_paradise.community.controller;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.OffsetDateTime;
import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ureca.sole_paradise.community.db.dto.CommunityDTO;
import com.ureca.sole_paradise.community.service.CommunityService;
import com.ureca.sole_paradise.util.ReferencedException;
import com.ureca.sole_paradise.util.ReferencedWarning;

import io.swagger.v3.oas.annotations.responses.ApiResponse;
import jakarta.validation.Valid;

@RestController
@CrossOrigin("*")
@RequestMapping(value = "/api/communities", produces = MediaType.APPLICATION_JSON_VALUE)
public class CommunityController {


private final CommunityService communityService;

public CommunityController(final CommunityService communityService) {
this.communityService = communityService;
}
//목록
@GetMapping
public ResponseEntity<List<CommunityDTO>> getAllCommunities() {
return ResponseEntity.ok(communityService.findAll());
}
// 상세
@GetMapping("/{postId}")
public ResponseEntity<CommunityDTO> getCommunity(
@PathVariable(name = "postId") final Integer postId) {
return ResponseEntity.ok(communityService.get(postId));
}
//등록
// @PostMapping
// @ApiResponse(responseCode = "201")
// public ResponseEntity<Integer> createCommunity(
// @RequestBody @Valid final CommunityDTO communityDTO) {
// final Integer createdPostId = communityService.create(communityDTO);
// return new ResponseEntity<>(createdPostId, HttpStatus.CREATED);
// }
//
// @PutMapping("/{postId}")
// public ResponseEntity<Integer> updateCommunity(
// @PathVariable(name = "postId") final Integer postId,
// @RequestBody @Valid final CommunityDTO communityDTO) {
// communityService.update(postId, communityDTO);
// return ResponseEntity.ok(postId);
// }
//
// @DeleteMapping("/{postId}")
// @ApiResponse(responseCode = "204")
// public ResponseEntity<Void> deleteCommunity(
// @PathVariable(name = "postId") final Integer postId) {
// final ReferencedWarning referencedWarning = communityService.getReferencedWarning(postId);
// if (referencedWarning != null) {
// throw new ReferencedException(referencedWarning);
// }
// communityService.delete(postId);
// return ResponseEntity.noContent().build();
// }
//
//
//}
//수정
@PutMapping("/{postId}")
public ResponseEntity<Integer> updateCommunity(
@PathVariable(name = "postId") final Integer postId,
@RequestBody @Valid final CommunityDTO communityDTO) {
communityService.update(postId, communityDTO);
return ResponseEntity.ok(postId);
}
//삭제
@DeleteMapping("/{postId}")
@ApiResponse(responseCode = "204")
public ResponseEntity<Void> deleteCommunity(
@PathVariable(name = "postId") final Integer postId) {
final ReferencedWarning referencedWarning = communityService.getReferencedWarning(postId);
if (referencedWarning != null) {
throw new ReferencedException(referencedWarning);
}
communityService.delete(postId);
return ResponseEntity.noContent().build();
}




// 사진 업로드 등록

private static final String UPLOAD_DIR = "src/main/resources/static/uploads/";;
@PostMapping
public ResponseEntity<?> uploadPet(@RequestParam("title") String title,
@RequestParam("contents") String contents,
@RequestParam("user") Integer user,
@RequestParam("createdAt") OffsetDateTime createdAt,
@RequestParam(value = "imageUrl", required = false) MultipartFile file){
try {
CommunityDTO communityDTO = new CommunityDTO();
communityDTO.setTitle(title);
communityDTO.setContents(contents);
communityDTO.setCreatedAt(createdAt);
communityDTO.setUser(user); // user ID 설정

if (file != null && !file.isEmpty()) {
String fileName = System.currentTimeMillis()+"";// + "_" + file.getOriginalFilename();
String[] exts = file.getOriginalFilename().split("\\.");
String ext = exts[exts.length-1];//확장자
Path filePath = Paths.get(UPLOAD_DIR + fileName+"."+ext);
Files.createDirectories(filePath.getParent());
Files.copy(file.getInputStream(), filePath);
String fp = filePath.toString();
System.out.println("fp="+fp);
int staticIndex = fp.lastIndexOf("uploads");
String ss = fp.substring(staticIndex+8);
communityDTO.setImageUrl(ss); // 파일 이름만 저장
System.out.println("ss="+ss);
}

final Integer createdpostId = communityService.create(communityDTO);
return new ResponseEntity<>(createdpostId, HttpStatus.CREATED);

} catch (IOException e) {
return ResponseEntity.internalServerError().body("Could not upload the petItem: " + e.getMessage());
}
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class CommunityDTO {
@Size(max = 255)
private String title;

private String imageUrl;//사진

@NotNull
private String contents;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package com.ureca.sole_paradise.community.db.entity;

import com.ureca.sole_paradise.user.db.entity.UserEntity;


import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;

import java.time.OffsetDateTime;
import java.util.Set;

@Entity
@Getter
@Setter
public class CommunityEntity {

@Id
Expand All @@ -20,6 +26,9 @@ public class CommunityEntity {
@Column(nullable = false, columnDefinition = "longtext")
private String contents;

@Column(columnDefinition = "longtext")
private String imageUrl;

@Column
private Integer good;

Expand All @@ -36,6 +45,6 @@ public class CommunityEntity {
@JoinColumn(name = "user_id", nullable = false)
private UserEntity user;

// @OneToMany(mappedBy = "post")
//private Set<CommunityComment> postCommunityComments;
@OneToMany(mappedBy = "post")
private Set<CommunityCommentEntity> postCommunityComments;
}
Loading

0 comments on commit 0e2f87e

Please sign in to comment.