Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ public ResponseEntity<SuccessResponse<?>> updateTag(
@PathVariable final long tagId,
@RequestBody final TagUpdateRequest tagUpdateRequest
) {
tagUpdateUseCase.update(TagUpdateCommand.of(
tagUpdateUseCase.update(TagUpdateCommand.updateWithHexColor(
authorizationUser.memberId(),
tagId,
tagUpdateRequest.color(),
tagUpdateRequest.hexCode(),
tagUpdateRequest.name()
));
return SuccessResponse.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public record TagUpdateRequest(
@Schema(description = "태그 이름")
String name,
@Schema(description = "태그 색상")
TagColor color
@Schema(description = "태그 색상 헥스코드")
String hexCode
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ public interface TagRepository {

Tag findById(final Long id);

List<Tag> findAllByMemberId(final Long memberId);
List<Tag> findAllByMemberIdOrderById(final Long memberId);

Tag findByMemberIdAndTagColor(final Long memberId, final TagColor tagColor);

Tag findDefaultTag(final Long memberId);

boolean existsByMemberIdAndName(final Long memberId, final String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public Tag findById(Long id) {
}

@Override
public List<Tag> findAllByMemberId(Long memberId) {
return tagJpaRepository.findAllByMemberId(memberId)
public List<Tag> findAllByMemberIdOrderById(Long memberId) {
return tagJpaRepository.findAllByMemberIdOrderById(memberId)
.stream()
.map(entity -> Tag.withId(
entity.getId(),
Expand Down Expand Up @@ -106,4 +106,9 @@ public Tag findDefaultTag(final Long memberId){
entity.getMemberId()
);
}

@Override
public boolean existsByMemberIdAndName(Long memberId, String name) {
return tagJpaRepository.existsByMemberIdAndName(memberId, name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

public interface TagJpaRepository extends JpaRepository<TagEntity, Long> {
@Query(
value = "SELECT * FROM tag WHERE member_id = :memberId",
value = "SELECT * FROM tag WHERE member_id = :memberId ORDER BY id ASC",
nativeQuery = true
)
List<TagEntity> findAllByMemberId(Long memberId);
List<TagEntity> findAllByMemberIdOrderById(Long memberId);

Optional<TagEntity> findByMemberIdAndColor(Long memberId, TagColor color);

Expand All @@ -23,4 +23,6 @@ public interface TagJpaRepository extends JpaRepository<TagEntity, Long> {
Optional<TagEntity> findDefaultTag(Long memberId);

void deleteAllByMemberId(final long memberId);

boolean existsByMemberIdAndName(Long memberId, String name);
}
10 changes: 9 additions & 1 deletion src/main/java/com/official/memento/tag/service/TagService.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class TagService implements TagCreateUseCase, TagGetUseCase, TagUpdateUse
@Override
@Transactional
public Tag create(final TagCreateCommand command) {
validateDuplicateTagName(command.memberId(), command.name());
final Tag tag = Tag.of(command.name(), command.color(), command.memberId());
return tagRepository.save(tag);
}
Expand All @@ -36,6 +37,7 @@ public Tag create(final TagCreateCommand command) {
public void update(final TagUpdateCommand command) {
Tag tag = tagRepository.findById(command.tagId());
tag.checkOwn(command.memberId());
validateDuplicateTagName(command.memberId(), command.name());

tag = tag.update(
command.name(),
Expand Down Expand Up @@ -71,10 +73,16 @@ private void validateNotDefaultTag(final Tag tag) {
}
}

private void validateDuplicateTagName(Long memberId, String name) {
if (tagRepository.existsByMemberIdAndName(memberId, name)) {
throw new InvalidRequestBodyException(ErrorCode.INVALID_JSON_FORMAT);
}
}

@Transactional(readOnly = true)
@Override
public List<Tag> getTags(final long memberId) {
return tagRepository.findAllByMemberId(memberId);
return tagRepository.findAllByMemberIdOrderById(memberId);
}

@Transactional(readOnly = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ public static TagUpdateCommand of(
) {
return new TagUpdateCommand(memberId, tagId, color, name);
}

public static TagUpdateCommand updateWithHexColor(Long memberId, Long tagId, String colorHex, String name) {
return new TagUpdateCommand(memberId, tagId, TagColor.fromHex(colorHex), name);
}
}