Skip to content

Commit

Permalink
Merge pull request #366 from TrandPick/development
Browse files Browse the repository at this point in the history
트랜드픽 배포v1.1.3
  • Loading branch information
jooooonj authored Jul 10, 2023
2 parents 0ab14f4 + aef4a77 commit c9d35d6
Show file tree
Hide file tree
Showing 98 changed files with 2,492 additions and 2,280 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,10 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Fetch;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import project.trendpick_pro.domain.answer.entity.dto.request.AnswerRequest;
import project.trendpick_pro.domain.answer.entity.form.AnswerForm;
import project.trendpick_pro.domain.ask.entity.Ask;
import project.trendpick_pro.domain.ask.entity.dto.request.AskRequest;
import project.trendpick_pro.domain.common.base.BaseTimeEntity;

import java.time.LocalDateTime;

@Entity
@NoArgsConstructor
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,76 +1,15 @@
package project.trendpick_pro.domain.answer.service;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import project.trendpick_pro.domain.answer.entity.Answer;
import project.trendpick_pro.domain.answer.entity.dto.request.AnswerRequest;
import project.trendpick_pro.domain.answer.entity.dto.response.AnswerResponse;
import project.trendpick_pro.domain.answer.entity.form.AnswerForm;
import project.trendpick_pro.domain.answer.repository.AnswerRepository;
import project.trendpick_pro.domain.ask.entity.Ask;
import project.trendpick_pro.domain.ask.entity.dto.response.AskResponse;
import project.trendpick_pro.domain.ask.repository.AskRepository;
import project.trendpick_pro.domain.ask.service.AskService;
import project.trendpick_pro.domain.member.entity.Member;
import project.trendpick_pro.domain.member.entity.RoleType;
import project.trendpick_pro.global.rsData.RsData;

import java.util.List;
import java.util.Objects;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class AnswerService {
private final AnswerRepository answerRepository;
private final AskRepository askRepository;

@Transactional
public RsData<Long> register(Member member, Long askId, AnswerForm answerForm) {
Ask ask = askRepository.findById(askId).orElseThrow(
() -> new IllegalArgumentException("해당 문의는 없는 문의입니다.")
);
if (!member.getBrand().equals(ask.getProduct().getBrand().getName()))
return RsData.of("F-1", "타 브랜드 상품에 대한 문의글에는 답변 권한이 없습니다.");

Answer answer = Answer.write(answerForm);
answer.connectAsk(ask);
answerRepository.save(answer);
return RsData.of("S-1", "답변이 성공적으로 등록되었습니다.", ask.getId());
}

@Transactional
public RsData<Long> delete(Member member, Long answerId) {
Answer answer = answerRepository.findById(answerId).orElseThrow(
() -> new IllegalArgumentException("해당 답변은 없는 답변입니다.")
);

if(!answer.getAsk().getProduct().getBrand().equals(member.getBrand()))
return RsData.of("F-1", "접근 권한이 없습니다.");

Ask ask = answer.getAsk();
ask.getAnswerList().remove(answer);
if(ask.getAnswerList().size() == 0)
ask.changeStatusYet();

return RsData.of("S-1", "답변이 삭제되었습니다.", answer.getAsk().getId());
}

public RsData<Long> modify(Member member, Long answerId, AnswerForm answerForm) {
Answer answer = answerRepository.findById(answerId).orElseThrow(
() -> new IllegalArgumentException("해당 답변은 없는 답변입니다.")
);

if(!Objects.equals(answer.getAsk().getAuthor().getBrand(), member.getBrand()))
return RsData.of("F-1", "접근 권한이 없습니다.");
answer.update(answerForm);

return RsData.of("S-1", "답변이 수정되었습니다.", answer.getAsk().getId());
}

public List<AnswerResponse> showAll(Long askId) {
List<Answer> answers = answerRepository.findAllByAskId(askRepository.findById(askId).get());
return AnswerResponse.of(answers);
}
}
public interface AnswerService {
RsData<Long> register(Member member, Long askId, AnswerForm answerForm);
RsData<Long> delete(Member member, Long answerId);
RsData<Long> modify(Member member, Long answerId, AnswerForm answerForm);
List<AnswerResponse> showAll(Long askId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package project.trendpick_pro.domain.answer.service;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import project.trendpick_pro.domain.answer.entity.Answer;
import project.trendpick_pro.domain.answer.entity.dto.response.AnswerResponse;
import project.trendpick_pro.domain.answer.entity.form.AnswerForm;
import project.trendpick_pro.domain.answer.repository.AnswerRepository;
import project.trendpick_pro.domain.ask.entity.Ask;
import project.trendpick_pro.domain.ask.repository.AskRepository;
import project.trendpick_pro.domain.member.entity.Member;
import project.trendpick_pro.global.rsData.RsData;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;

@Service
@RequiredArgsConstructor
public class AnswerServiceImpl implements AnswerService{
private final AnswerRepository answerRepository;
private final AskRepository askRepository;

@Transactional
public RsData<Long> register(Member member, Long askId, AnswerForm answerForm) {
Ask ask = askRepository.findById(askId).orElseThrow(
() -> new NoSuchElementException("해당 문의는 없는 문의입니다.")
);
if (!member.getBrand().equals(ask.getProduct().getBrand().getName()))
return RsData.of("F-1", "타 브랜드 상품에 대한 문의글에는 답변 권한이 없습니다.");

Answer answer = Answer.write(answerForm);
answer.connectAsk(ask);
answerRepository.save(answer);
return RsData.of("S-1", "답변이 성공적으로 등록되었습니다.", ask.getId());
}

@Transactional
public RsData<Long> delete(Member member, Long answerId) {
Answer answer = answerRepository.findById(answerId).orElseThrow(
() -> new NoSuchElementException("해당 답변은 없는 답변입니다.")
);

if(!answer.getAsk().getProduct().getBrand().getName().equals(member.getBrand()))
return RsData.of("F-1", "접근 권한이 없습니다.");

Ask ask = answer.getAsk();
ask.getAnswerList().remove(answer);
if(ask.getAnswerList().size() == 0)
ask.changeStatusYet();

return RsData.of("S-1", "답변이 삭제되었습니다.", answer.getAsk().getId());
}

@Transactional
public RsData<Long> modify(Member member, Long answerId, AnswerForm answerForm) {
Answer answer = answerRepository.findById(answerId).orElseThrow(
() -> new NoSuchElementException("해당 답변은 없는 답변입니다.")
);

if(!Objects.equals(answer.getAsk().getAuthor().getBrand(), member.getBrand()))
return RsData.of("F-1", "접근 권한이 없습니다.");
answer.update(answerForm);

return RsData.of("S-1", "답변이 수정되었습니다.", answer.getAsk().getId());
}

@Transactional
public List<AnswerResponse> showAll(Long askId) {
List<Answer> answers = answerRepository.findAllByAskId(askRepository.findById(askId).get());
return AnswerResponse.of(answers);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,30 @@
public class AskController {
private final AskService askService;
private final AnswerService answerService;

private final Rq rq;

@PreAuthorize("isAuthenticated()")
@PreAuthorize("hasAuthority('MEMBER')")
@GetMapping("/register")
public String registerForm(@RequestParam("product") Long productId, AskForm askForm, Model model) {
askForm.setProductId(productId);
model.addAttribute("askForm", askForm);
return "trendpick/customerservice/asks/register";
}

@PreAuthorize("isAuthenticated()")
@PreAuthorize("hasAuthority('MEMBER')")
@PostMapping("/register")
public String registerAsk(@Valid AskForm askForm) {
RsData<Long> result = askService.register(rq.getMember(), askForm);
if(result.isFail())
return rq.redirectWithMsg("/trendpick/products/%s".formatted(askForm.getProductId()), result);

if(result.isFail()) {
return rq.historyBack(result);
}
return rq.redirectWithMsg("/trendpick/products/%s".formatted(askForm.getProductId()), result);
}

@PreAuthorize("permitAll()")
@GetMapping("/{askId}")
public String showAsk(@PathVariable Long askId, AnswerForm answerForm, Model model) {
public String showAsk(@PathVariable Long askId, Model model) {
model.addAttribute("ask", askService.show(askId));
model.addAttribute("answers", answerService.showAll(askId));
model.addAttribute("answerForm", new AnswerForm());
Expand All @@ -54,31 +56,30 @@ public String showAsk(@PathVariable Long askId, AnswerForm answerForm, Model mod
@PostMapping("/delete/{askId}")
public String deleteAsk(@PathVariable Long askId) {
RsData<Long> result = askService.delete(rq.getMember(), askId);
if(result.isFail())
if(result.isFail()) {
return rq.historyBack(result);

}
return rq.redirectWithMsg("/trendpick/products/%s".formatted(result.getData()), result);
}

@PreAuthorize("isAuthenticated()")
@PreAuthorize("hasAuthority('MEMBER')")
@GetMapping("/edit/{askId}")
public String modifyForm(@PathVariable Long askId, Model model) {
AskResponse ask = askService.show(askId);
if(!Objects.equals(ask.getMemberId(), rq.getMember().getId()))
if(!Objects.equals(ask.getMemberId(), rq.getMember().getId())) {
return rq.historyBack("자신이 올린 문의글에 대해서만 수정 권한이 있습니다.");

}
model.addAttribute("askForm", new AskForm(ask.getAskId(), ask.getTitle(), ask.getContent()));

return "trendpick/customerservice/asks/register";
}

@PreAuthorize("isAuthenticated()")
@PreAuthorize("hasAuthority('MEMBER')")
@PostMapping("/edit/{askId}")
public String modifyAsk(@PathVariable Long askId, @Valid AskForm askForm) {
RsData<AskResponse> result = askService.modify(rq.getMember(), askId, askForm);
if (result.isFail())
if (result.isFail()) {
return rq.redirectWithMsg("/trendpick/customerservice/asks/%s".formatted(askId), result);

}
return rq.redirectWithMsg("/trendpick/customerservice/asks/%s".formatted(askId), "문의글 수정이 완료되었습니다.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import project.trendpick_pro.domain.common.base.BaseTimeEntity;
import project.trendpick_pro.domain.member.entity.Member;
import project.trendpick_pro.domain.product.entity.product.Product;

import java.util.ArrayList;
import java.util.List;

Expand All @@ -33,7 +34,10 @@ public class Ask extends BaseTimeEntity {
@JoinColumn(name = "product_id")
private Product product;

@Column(nullable = false)
private String title;

@Column(nullable = false)
private String content;

@Enumerated(EnumType.STRING)
Expand All @@ -50,8 +54,7 @@ public static Ask of(Member member, Product product, AskForm askForm) {
.title(askForm.getTitle())
.content(askForm.getContent())
.status(AskStatus.YET)
.build()
;
.build();
}

public void update(AskForm askForm) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@

import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AskForm {

private Long productId;

@NotBlank
private String title;

@NotBlank
private String content;
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public class AskResponse {
private String status;
private LocalDateTime createdDate;

@QueryProjection
@Builder
@QueryProjection
public AskResponse(Long askId, Long productId, String productName, String memberName, Long memberId, String title,String content, String status, LocalDateTime createdDate) {
this.askId = askId;
this.productId = productId;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package project.trendpick_pro.domain.ask.exception;

import org.springframework.http.HttpStatus;
import project.trendpick_pro.global.exception.BaseException;
import project.trendpick_pro.global.exception.ErrorCode;

public class AskNotFoundException extends BaseException {

private static final ErrorCode code = ErrorCode.ASK_NOT_FOUND;

public AskNotFoundException(String message) {
super(code, HttpStatus.BAD_REQUEST, message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package project.trendpick_pro.domain.ask.exception;

import org.springframework.http.HttpStatus;
import project.trendpick_pro.global.exception.BaseException;
import project.trendpick_pro.global.exception.ErrorCode;

public class AskNotMatchException extends BaseException {

private static final ErrorCode code = ErrorCode.ASK_NOT_MATCH;

public AskNotMatchException(String message) {
super(code, HttpStatus.BAD_REQUEST, message);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
package project.trendpick_pro.domain.ask.repository;


import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import project.trendpick_pro.domain.ask.entity.Ask;
import project.trendpick_pro.domain.member.entity.Member;

public interface AskRepository extends JpaRepository<Ask, Long>, AskRepositoryCustom {
@Query("select a from Ask a where a.author = :member")
Page<Ask> findAllByMember(Member member, Pageable pageable);
}
Loading

0 comments on commit c9d35d6

Please sign in to comment.