Skip to content

Commit

Permalink
Refactor : Career 활동 태그 관련 API 예외 처리 및 응답 형식 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeonda02 committed Jul 26, 2024
1 parent d51dfa5 commit 636a837
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import umc.kkijuk.server.career.dto.converter.CareerConverter;
import umc.kkijuk.server.career.repository.CareerRepository;
import umc.kkijuk.server.career.repository.CategoryRepository;
import umc.kkijuk.server.common.domian.exception.ResourceNotFoundException;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -90,7 +92,7 @@ public List<? extends CareerGroupedByResponse> getCareerGroupedBy(String status)
@Override
public Optional<Career> findCareer(Long careerId) {
return Optional.ofNullable(careerRepository.findById(careerId).orElseThrow(
() -> new CareerValidationException(CareerResponseMessage.CAREER_NOT_FOUND.toString())));
() -> new ResourceNotFoundException("Career",careerId)));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public ErrorResponse ReviewRecruitMatchException(ReviewRecruitNotMatchException

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(InvalidTagNameException.class)
public ErrorResponse DuplicateTagNameException(InvalidTagNameException exception) {
public ErrorResponse InvalidTagNameException(InvalidTagNameException exception) {
return new ErrorResponse(exception.getMessage());
}
}
19 changes: 8 additions & 11 deletions src/main/java/umc/kkijuk/server/tag/controller/TagController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import umc.kkijuk.server.tag.controller.response.TagResponse;
import umc.kkijuk.server.tag.domain.Tag;
import umc.kkijuk.server.tag.dto.TagRequestDto;
import umc.kkijuk.server.tag.dto.TagResponseDto;
Expand All @@ -22,25 +23,21 @@ public class TagController {

@PostMapping("/tag")
@Operation(summary = "태그 추가 API", description = "태그 - 태그를 생성하는 API")
public ResponseEntity<TagResponseDto.ResultTagDto> create(@RequestBody @Valid TagRequestDto.CreateTagDto request) {
public TagResponse<TagResponseDto.ResultTagDto> create(@RequestBody @Valid TagRequestDto.CreateTagDto request) {
Tag createTag = tagService.createTag(request);
return ResponseEntity
.ok()
.body(TagConverter.toTagResult(createTag));
return TagResponse.success(HttpStatus.OK,"태그를 성공적으로 생성했습니다.", TagConverter.toTagResult(createTag));
}
@GetMapping("/tag")
@Operation(summary = "태그 조회 API", description = "태그 - 태그 조회하는 API")
public ResponseEntity<TagResponseDto.ResultTagDtoList> read() {
return ResponseEntity
.ok()
.body(tagService.findAllTags());
public TagResponse<TagResponseDto.ResultTagDtoList> read() {
return TagResponse.success(HttpStatus.OK, "모든 태그를 성공적으로 조회했습니다.", tagService.findAllTags());
}
@DeleteMapping("/tag/{tagId}")
@Operation(summary = "태그 삭제 API",description = "태그 - 태그를 삭제하는 API")
@Parameter(name = "tagId",description = "태그 Id, path variable 입니다. 존재하는 태그 Id 값을 넘겨 주세요.",example = "1")
public ResponseEntity<Object> delete(@PathVariable Long tagId) {
public TagResponse<Object> delete(@PathVariable Long tagId) {
tagService.delete(tagId);
return ResponseEntity.ok().body(null);
return TagResponse.success(HttpStatus.OK, "태그 삭제가 성공적으로 이루어졌습니다.", null);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package umc.kkijuk.server.tag.controller.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Builder;
import lombok.Getter;
import org.springframework.http.HttpStatus;
import umc.kkijuk.server.career.controller.response.CareerResponse;

@Builder
@Getter
public class TagResponse<T> {
private int status;
private String message;
@JsonInclude(JsonInclude.Include.NON_NULL)
private T data;

public TagResponse(final int status,final String message,T data){
this.status = status;
this.message = message;
this.data = data;
}
public static <T> TagResponse<T> success(HttpStatus status, String message, T data){
return new TagResponse<>(status.value(),message,data);
}
}
6 changes: 4 additions & 2 deletions src/main/java/umc/kkijuk/server/tag/domain/Tag.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package umc.kkijuk.server.tag.domain;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.*;

@Entity
Expand All @@ -10,9 +11,10 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Tag {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="hashtag_id")
@Column(name="tag_id")
private Long id;
@Column(name="hashtag_name", length = 30) //일단 length 30 ( 최대 길이 여쭤보고 변경해야 함. )
@NotNull
@Column(name="tag_name", length = 30)
private String name;

}
5 changes: 1 addition & 4 deletions src/main/java/umc/kkijuk/server/tag/dto/TagRequestDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ public class TagRequestDto {
@AllArgsConstructor
@NoArgsConstructor
public static class CreateTagDto{
@NotBlank(message = "해시태그 이름은 필수 입력 사항입니다.")
@Size(max = 30)
@Schema(description = "해시태그 이름", example = "역량 키워드", type="string")
@Schema(description = "태그 이름", example = "역량 키워드", type="string")
String tagName;


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import umc.kkijuk.server.common.domian.exception.InvalidTagNameException;
import umc.kkijuk.server.common.domian.exception.ResourceNotFoundException;
import umc.kkijuk.server.common.domian.response.ErrorResponse;
import umc.kkijuk.server.tag.domain.Tag;
import umc.kkijuk.server.tag.dto.TagRequestDto;
import umc.kkijuk.server.tag.dto.TagResponseDto;
Expand All @@ -23,8 +25,8 @@ public class TagServiceImpl implements TagService {
@Transactional
public Tag createTag(TagRequestDto.CreateTagDto request) {
String tagName = request.getTagName();
if (tagName == null || tagName.trim().isEmpty() || tagRepository.existsByName(tagName)) {
throw new InvalidTagNameException("태그 이름은 공백일 수 없으며, 이미 존재하는 이름은 사용할 수 없습니다.");
if (tagName==null || tagName.trim().isEmpty() || tagRepository.existsByName(tagName)) {
throw new InvalidTagNameException("태그 이름은 비워둘 수 없으며, 이미 존재하는 이름은 사용할 수 없습니다.");
}
Tag hashTag = TagConverter.toTag(request);
return tagRepository.save(hashTag);
Expand All @@ -36,8 +38,9 @@ public TagResponseDto.ResultTagDtoList findAllTags() {
@Override
@Transactional
public void delete(Long tagId) {
Tag deleteTag = tagRepository.findById(tagId).get();
Tag deleteTag = tagRepository.findById(tagId).orElseThrow(() -> new ResourceNotFoundException("Tag", tagId));
tagRepository.delete(deleteTag);
}


}

0 comments on commit 636a837

Please sign in to comment.