Skip to content

Commit

Permalink
Merge pull request #3 from LikeLion-KNU/Dong_demo
Browse files Browse the repository at this point in the history
Dong Demo first deploy
  • Loading branch information
himodu authored May 14, 2024
2 parents 0c52708 + deb4241 commit 8b3552b
Show file tree
Hide file tree
Showing 22 changed files with 195 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("api/v1/booth")
@RequiredArgsConstructor
Expand All @@ -35,14 +37,16 @@ public ResponseEntity<BoothDetail> getBooth(
}

@PatchMapping("{boothId}")
@Operation(summary = "특정 부스 좋아요 업데이트", description = "특정 부스의 좋아요를 +1 한다.")
@Operation(summary = "특정 부스 좋아요 업데이트", description = "특정 부스의 좋아요를 변경한다.")
public ResponseEntity<BasicResponse> updateLikes(
@PathVariable("boothId") Long boothId
@PathVariable("boothId") Long boothId,
@RequestParam("userHash") String userHash
){
service.updateLikes(boothId);
String message = service.updateLikes(boothId, userHash);
BasicResponse response = BasicResponse.builder()
.message("좋아요를 성공적으로 업데이트(+1) 하였습니다.")
.message(message)
.status(200)
.timeStamp(LocalDateTime.now())
.build();
return ResponseEntity.ok().body(response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public class BoothEntity extends BasicEntity {
@OneToMany(mappedBy = "booth", fetch = FetchType.LAZY)
private List<CommentEntity> commentEntityList;

@OneToMany(mappedBy = "boothEntity", fetch = FetchType.LAZY)
private UserBoothEntity userBoothEntity;
@OneToMany(mappedBy = "booth", fetch = FetchType.LAZY)
private List<UserBoothEntity> userBoothEntity;

@ElementCollection
@CollectionTable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import LlikelionKNU.KNUfest.domain.booth.dto.AllBooth;
import LlikelionKNU.KNUfest.domain.booth.dto.BoothDetail;
import LlikelionKNU.KNUfest.domain.booth.entity.BoothEntity;

public interface BoothService {
AllBooth getAllbooth(String userHash);
BoothDetail getBooth(Long id, String userHash);
void updateLikes(Long id);
String updateLikes(Long id, String userHash);

BoothEntity findById(Long id);

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import LlikelionKNU.KNUfest.domain.booth.repository.BoothRepository;
import LlikelionKNU.KNUfest.domain.comment.service.CommentService;
import LlikelionKNU.KNUfest.domain.user.entity.UserBoothEntity;
import LlikelionKNU.KNUfest.domain.user.service.UserBoothService;
import LlikelionKNU.KNUfest.domain.user.entity.UserEntity;
import LlikelionKNU.KNUfest.domain.user.repository.UserBoothRepository;
import LlikelionKNU.KNUfest.domain.user.service.UserService;
import LlikelionKNU.KNUfest.domain.user.service.UserServiceImpl;
import LlikelionKNU.KNUfest.global.error.NoExistException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -23,8 +23,9 @@
public class BoothServiceImpl implements BoothService{

private final BoothRepository boothrepository;
private final UserBoothRepository userBoothRepository;

private final CommentService commentService;
private final UserBoothService userBoothService;
private final UserService userService;

@Override
Expand All @@ -33,8 +34,7 @@ public AllBooth getAllbooth(String userHash) {
List<BoothEntity> boothes = boothrepository.findAll();
List<Booth> boothDtos;

List<UserBoothEntity> userBoothEntityList = userBoothService.getAllUserBooth(
userService.getUserByHash(userHash).getId());
List<UserBoothEntity> userBoothEntityList = userBoothRepository.findAllByUserId(userService.getUserByHash(userHash).getId());

if(boothes.isEmpty()) {
throw new NoExistException("부스 전체 정보가 없습니다.");
Expand All @@ -49,12 +49,15 @@ public AllBooth getAllbooth(String userHash) {
.build());
}

for(UserBoothEntity userBooth : userBoothEntityList){
Booth tempbooth = boothDtos.get(userBooth.getBoothEntity().getId().intValue());
tempbooth.setLikable(false);
boothDtos.set(userBooth.getBoothEntity().getId().intValue(), tempbooth);
if(!userBoothEntityList.isEmpty()){
for(UserBoothEntity userBooth : userBoothEntityList){
Booth tempbooth = boothDtos.get(userBooth.getBooth().getId().intValue()-1);
tempbooth.setLikable(false);
boothDtos.set(userBooth.getBooth().getId().intValue()-1, tempbooth);
}
}


return AllBooth.builder()
.count(boothDtos.size())
.boothDtoes(boothDtos)
Expand All @@ -63,41 +66,83 @@ public AllBooth getAllbooth(String userHash) {
}

@Override
public BoothDetail getBooth(Long id, String userHash) {
Optional<BoothEntity> boothOp = boothrepository.findById(id);

Optional<UserBoothEntity> userBoothEntity = userBoothService.getUserBooth(id, userService.getUserByHash(userHash).getId());
public BoothDetail getBooth(Long boothId, String userHash) {
Optional<BoothEntity> boothOp;

boolean temp = userBoothEntity.isEmpty();
Optional<UserBoothEntity> userBoothEntity = userBoothRepository.findByUserIdAndBoothId(userService.getUserByHash(userHash).getId(), boothId);
BoothEntity booth;

if(boothOp.isEmpty()){
throw new NoExistException("해당 부스 정보가 없습니다. (id 확인요망)");
boolean temp;
if(userBoothEntity.isEmpty()){
boothOp = boothrepository.findById(boothId);
temp = true;
if(boothOp.isEmpty()){
throw new NoExistException("해당 부스 정보가 없습니다. (id 확인요망)");
}else{
booth = boothOp.get();
}
}else{
BoothEntity booth = boothOp.get();

temp = false;
booth = userBoothEntity.get().getBooth();
}
return BoothDetail.builder()
.id(booth.getId())
.boothName(booth.getBoothName())
.likes(booth.getLikes())
.urls(booth.getUrls())
.Likable(temp)
.comments(commentService.getCommentPage(boothOp.get().getId(),5,1, "default"))
.comments(commentService.getCommentPage(booth.getId(),5,1, "default", userHash))
.build();
}
}

@Override
public void updateLikes(Long id) {
public String updateLikes(Long boothId, String userHash) {

UserEntity user = userService.getUserByHash(userHash);
Optional<UserBoothEntity> userBoothEntity = userBoothRepository.findByUserIdAndBoothId(user.getId(), boothId);
UserBoothEntity userBooth;

Optional<BoothEntity> boothOp = boothrepository.findById(id);
BoothEntity booth;

if(userBoothEntity.isEmpty()){
Optional<BoothEntity> boothOp = boothrepository.findById(boothId);

if(boothOp.isEmpty()){
throw new NoExistException("해당 부스 정보가 없습니다. (id 확인 요망)");
}else{
booth = boothOp.get();
booth.setLikes(booth.getLikes()+1);
boothrepository.save(booth);

userBooth = UserBoothEntity.builder()
.booth(booth)
.user(user)
.build();

userBoothRepository.save(userBooth);
return "좋아요를 업데이트(+1) 하였습니다.";
}


}else{
booth = userBoothEntity.get().getBooth();
booth.setLikes(booth.getLikes()-1);
boothrepository.save(booth);

userBoothRepository.delete(userBoothEntity.get());

return "좋아요를 업데이트(-1) 하였습니다.";
}

}

@Override
public BoothEntity findById(Long id){
Optional<BoothEntity> boothOp = boothrepository.findById(id);
if(boothOp.isEmpty()){
throw new NoExistException("해당 부스 정보가 없습니다. (id 확인 요망)");
}else{
booth = boothOp.get();
booth.setLikes(booth.getLikes()+1);
boothrepository.save(booth);
return boothOp.get();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.time.LocalDateTime;
import java.util.List;

@RestController
Expand All @@ -23,41 +24,44 @@ public ResponseEntity<List<Comment>> getExtraCommentPage(
@PathVariable("boothId") Long boothId,
@RequestParam("perpage") int perpage,
@RequestParam("page") int page,
@RequestParam("order") String order
@RequestParam("order") String order,
@RequestParam("userHash") String userHash
){
List<Comment> result = service.getCommentPage(boothId, perpage, page, order);
List<Comment> result = service.getCommentPage(boothId, perpage, page, order, userHash);
return ResponseEntity.ok().body(result);
}

@PostMapping("booth/{boothId}/comment")
@Operation(summary = "특정부스 댓글 생성", description = "특정 부스에 댓글을 생성한다.")
public ResponseEntity<BasicResponse> postComment(
@PathVariable("boothId") Long boothId,
@RequestBody CommentRequest comment
@RequestParam("userHash") String userHash,
@RequestBody CommentRequest commentRequest
){

Long id = service.postComment(boothId, comment);
Long id = service.postComment(boothId, commentRequest, userHash);

BasicResponse response = BasicResponse.builder()
.message(boothId+"번 부스에 댓글을 생성하였습니다.")
.message(boothId+"번 부스에 " + id + "번째 댓글을 생성하였습니다.")
.status(201)
.timeStamp(LocalDateTime.now())
.build();

return ResponseEntity.created(URI.create("comment"+id)).body(response);
}

@DeleteMapping("comment/{commentId}")
@Operation(summary = "특정 댓글 삭제", description = "특정 댓글을 password 가 일치할 경우 삭제한다.")
@Operation(summary = "특정 댓글 삭제", description = "특정 댓글을 userHash 값을 통해 비교하여 삭제한다.")
public ResponseEntity<BasicResponse> deleteComment(
@PathVariable("commentId") Long commentId,
@RequestParam("password") String password
@RequestParam("userHash") String userHash
){

service.deleteComment(commentId, password);
service.deleteComment(commentId, userHash);

BasicResponse basicResponse = BasicResponse.builder()
.message("성공적으로 삭제되었습니다.")
.status(200)
.timeStamp(LocalDateTime.now())
.build();

return ResponseEntity.ok().body(basicResponse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,28 @@

import LlikelionKNU.KNUfest.domain.booth.entity.BoothEntity;
import LlikelionKNU.KNUfest.domain.comment.entity.CommentEntity;
import LlikelionKNU.KNUfest.domain.user.entity.UserEntity;
import lombok.Builder;
import lombok.Data;

import java.time.Instant;
import java.time.LocalDateTime;

@Data
@Builder
public class Comment {
private Long id;
private String name;
private String comment;
private String password;
private Instant created;
private LocalDateTime created;
private Boolean deleteable;

public CommentEntity toEntity(BoothEntity booth){
public static CommentEntity toEntity(CommentRequest commentRequest, BoothEntity booth, UserEntity user){
return CommentEntity.builder()
.name(this.name)
.password(this.password)
.comment(this.comment)
.name(commentRequest.getName())
.comment(commentRequest.getComment())
.booth(booth)
.user(user)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@
public class CommentRequest {
private String name;
private String comment;
private String password;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import LlikelionKNU.KNUfest.domain.booth.entity.BoothEntity;
import LlikelionKNU.KNUfest.domain.comment.dto.Comment;
import LlikelionKNU.KNUfest.domain.user.entity.UserEntity;
import LlikelionKNU.KNUfest.global.basic.BasicEntity;
import jakarta.persistence.*;
import lombok.*;
Expand All @@ -23,18 +24,18 @@ public class CommentEntity extends BasicEntity {
@Column(name = "comment")
private String comment;

@Column(name = "password", length = 10)
private String password;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "booth_id")
@JoinColumn(name = "boothId")
private BoothEntity booth;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "userId")
private UserEntity user;

public Comment toDto(){
return Comment.builder()
.id(this.getId())
.name(this.name)
.password(this.password)
.comment(this.comment)
.created(this.getCreatedAt())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import java.util.List;

public interface CommentService {
List<Comment> getCommentPage(Long boothId, int perpage, int page, String order);
Long postComment(Long id, CommentRequest comment);
List<Comment> getCommentPage(Long boothId, int perpage, int page, String order, String userHash);
Long postComment(Long id, CommentRequest comment, String userHash);
void deleteComment(Long commentId, String password);

}
Loading

0 comments on commit 8b3552b

Please sign in to comment.