Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 75 additions & 9 deletions src/main/java/com/mapu/domain/figure/api/FigureController.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.mapu.domain.figure.api;

import com.mapu.domain.figure.api.request.FigureRateRequestDTO;
import com.mapu.domain.figure.api.request.FigureRelationRequestDTO;
import com.mapu.domain.figure.api.request.FigureTagRequestDTO;
import com.mapu.domain.figure.application.FigureService;
import com.mapu.domain.figure.application.response.FigureResponseDTO;
import com.mapu.global.common.response.BaseResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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 org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
Expand All @@ -18,7 +18,7 @@ public class FigureController {

private final FigureService figureService;

//객체 기본정보 조회ㅣ
//객체 정보 조회
@GetMapping("/{figureId}")
public BaseResponse<FigureResponseDTO> getFigure(@PathVariable Long figureId) {
System.out.println(figureId);
Expand All @@ -27,10 +27,76 @@ public BaseResponse<FigureResponseDTO> getFigure(@PathVariable Long figureId) {
return new BaseResponse<>(figureDTO);
}

//객체 정보 속성
@GetMapping("/{figureId}/detail")
public BaseResponse<Void> getFigureDetail(@PathVariable Long figureId) {
return null;
/**
* 객체 연결 관련 API
*/

//객체 연결 추가
@PostMapping("/{figureId}/relation")
public BaseResponse<Long> addRelation(@PathVariable Long figureId, @RequestBody FigureRelationRequestDTO request) {
Long relationId = figureService.addRelation(figureId, request.getRelatedFigureId());
return new BaseResponse<>(relationId);
}

@DeleteMapping("/{figureId}/relation")
public BaseResponse<Void> removeRelation(@PathVariable Long figureId, @RequestParam Long relatedFigureId) {
figureService.removeRelation(figureId, relatedFigureId);
return new BaseResponse<>(null);
}

// 모든 연결 삭제
@DeleteMapping("/{figureId}/relations")
public BaseResponse<Void> removeAllRelations(@PathVariable Long figureId) {
figureService.removeAllRelations(figureId);
return new BaseResponse<>(null);
}

/**
* 태그 관련 API
*/

// 태그 추가
@PostMapping("/{figureId}/tag")
public BaseResponse<Long> addTag(@PathVariable Long figureId, @RequestBody FigureTagRequestDTO request) {
Long tagId = figureService.addTag(figureId, request.getTag());
return new BaseResponse<>(tagId);
}

@DeleteMapping("/{figureId}/tag/{tagId}")
public BaseResponse<Void> removeTag(@PathVariable Long figureId, @PathVariable Long tagId) {
figureService.removeTag(figureId, tagId);
return new BaseResponse<>(null);
}


// 태그 모두 삭제
@DeleteMapping("/{figureId}/tags")
public BaseResponse<Void> removeAllTags(@PathVariable Long figureId) {
figureService.removeAllTags(figureId);
return new BaseResponse<>(null);
}


/**
*별점 관련 API
* 반환값 rateId 추가되어야함
*/
@PostMapping("/{figureId}/rate")
public BaseResponse<Long> addRate(@PathVariable Long figureId, @RequestBody FigureRateRequestDTO request) {
Long rateId = figureService.addRate(figureId, request.getRateName(), request.getRateStar());
return new BaseResponse<>(rateId);
}

@DeleteMapping("/{figureId}/rate/{rateId}")
public BaseResponse<Void> removeRate(@PathVariable Long figureId, @PathVariable Long rateId) {
figureService.removeRate(figureId, rateId);
return new BaseResponse<>(null);
}

@DeleteMapping("/{figureId}/rates")
public BaseResponse<Void> removeAllRates(@PathVariable Long figureId) {
figureService.removeAllRates(figureId);
return new BaseResponse<>(null);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.mapu.domain.figure.api.request;

import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class FigureRateRequestDTO {
private String rateName;
private int rateStar;

@NotNull(message = "Rate의 이름 필요")
public String getRateName() {
return rateName;
}

@NotNull(message = "Rate의 값 명시")
public int getRateStar() {
return rateStar;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.mapu.domain.figure.api.request;

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

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class FigureRelationRequestDTO {
private Long relatedFigureId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.mapu.domain.figure.api.request;

import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class FigureTagRequestDTO {
@NotNull(message = "tag값은 필수입니다.")
private String tag;
}
109 changes: 108 additions & 1 deletion src/main/java/com/mapu/domain/figure/application/FigureService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
package com.mapu.domain.figure.application;

import com.mapu.domain.figure.application.response.FigureResponseDTO;
import com.mapu.domain.figure.dao.FigureRateRepository;
import com.mapu.domain.figure.dao.FigureRelationRepository;
import com.mapu.domain.figure.dao.FigureRepository;
import com.mapu.domain.figure.dao.FigureTagRepository;
import com.mapu.domain.figure.domain.Figure;
import com.mapu.domain.figure.domain.FigureRate;
import com.mapu.domain.figure.domain.FigureRelation;
import com.mapu.domain.figure.domain.FigureTag;
import com.mapu.domain.figure.exception.FigureException;
import com.mapu.domain.figure.exception.errorcode.FigureExceptionErrorCode;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -16,10 +25,108 @@
public class FigureService {

private final FigureRepository figureRepository;
private final FigureRelationRepository figureRelationRepository;
private final FigureTagRepository figureTagRepository;
private final FigureRateRepository figureRateRepository;

public FigureResponseDTO getFigure(Long figureId) {
Figure figure = figureRepository.findById(figureId).orElseThrow(() -> new RuntimeException());
Figure figure = findFigureById(figureId);
return FigureResponseDTO.from(figure);
}

// 객체 연결 관계 추가
public Long addRelation(Long figureId, Long relatedFigureId) {
try {
Figure figure = findFigureById(figureId);
Figure relatedFigure = findFigureById(relatedFigureId);

FigureRelation relation = new FigureRelation(figure, relatedFigure);
relation = figureRelationRepository.save(relation);
return relation.getId();
} catch (EntityNotFoundException e) {
throw new FigureException(FigureExceptionErrorCode.FIGURE_NOT_FOUND);
} catch (FigureException e) {
throw e; // 이미 존재하는 관계에 대한 예외 처리
} catch (Exception e) {
throw new FigureException(FigureExceptionErrorCode.FIGURE_OPERATION_FAILED);
}
}


// 객체 연결 삭제
public void removeRelation(Long figureId, Long relatedFigureId) {
try {
figureRelationRepository.deleteByFigureIdAndFigure2Id(figureId, relatedFigureId);
} catch (Exception e) {
throw new FigureException(FigureExceptionErrorCode.FIGURE_RELATION_NOT_FOUND);
}
}
// 모든 연결 삭제
public void removeAllRelations(Long figureId) {
figureRelationRepository.deleteAllByFigureId(figureId);
}

// 태그 추가
public Long addTag(Long figureId, String tag) {
try {
Figure figure = findFigureById(figureId);
FigureTag figureTag = new FigureTag(figure, tag);
figureTag = figureTagRepository.save(figureTag);
return figureTag.getId();
} catch (EntityNotFoundException e) {
throw new FigureException(FigureExceptionErrorCode.FIGURE_NOT_FOUND);
} catch (Exception e) {
throw new FigureException(FigureExceptionErrorCode.FIGURE_OPERATION_FAILED);
}
}

public void removeTag(Long figureId, Long tagId) {
try {
FigureTag tag = figureTagRepository.findByIdAndFigureId(tagId, figureId)
.orElseThrow(() -> new FigureException(FigureExceptionErrorCode.FIGURE_TAG_NOT_FOUND));
figureTagRepository.delete(tag);
} catch (Exception e) {
throw new FigureException(FigureExceptionErrorCode.FIGURE_OPERATION_FAILED);
}
}

// 모든 태그 삭제
public void removeAllTags(Long figureId) {
figureTagRepository.deleteAllByFigureId(figureId);
}

// 별점 추가
public Long addRate(Long figureId, String rateName, int rateStar) {
try {
Figure figure = findFigureById(figureId);
FigureRate figureRate = new FigureRate(figure, rateName, rateStar);
figureRate = figureRateRepository.save(figureRate);
return figureRate.getId();
} catch (EntityNotFoundException e) {
throw new FigureException(FigureExceptionErrorCode.FIGURE_NOT_FOUND);
} catch (Exception e) {
throw new FigureException(FigureExceptionErrorCode.FIGURE_OPERATION_FAILED);
}
}

// 별점 하나 제거
public void removeRate(Long figureId, Long rateId) {
try {
FigureRate rate = figureRateRepository.findByIdAndFigureId(rateId, figureId)
.orElseThrow(() -> new FigureException(FigureExceptionErrorCode.FIGURE_RATE_NOT_FOUND));
figureRateRepository.delete(rate);
} catch (Exception e) {
throw new FigureException(FigureExceptionErrorCode.FIGURE_OPERATION_FAILED);
}
}

//별점 모두 제거
public void removeAllRates(Long figureId) {
figureRateRepository.deleteAllByFigureId(figureId);
}

private Figure findFigureById(Long figureId) {
return figureRepository.findById(figureId)
.orElseThrow(() -> new EntityNotFoundException("Figure 없음 " + figureId));
}
}

This file was deleted.

Loading