Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[POM-80] feat: 옵션 등록 기능 구현 #55

Merged
merged 19 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
juno-junho marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,24 +1,62 @@
package com.ray.pominowner.menu.controller;

import com.ray.pominowner.menu.controller.dto.OptionRequest;
import com.ray.pominowner.menu.controller.dto.OptionUpdateRequest;
import com.ray.pominowner.menu.domain.Option;
import com.ray.pominowner.menu.domain.OptionGroup;
import com.ray.pominowner.menu.service.OptionGroupService;
import com.ray.pominowner.menu.service.OptionService;
import com.ray.pominowner.menu.service.vo.OptionUpdateInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.net.URI;

import static com.ray.pominowner.menu.controller.dto.OptionGroupWithOptionResponse.OptionResponse;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/options")
public class OptionController {

private final OptionService optionService;
private final OptionGroupService optionGroupService;

// 개별 옵션 조회
@GetMapping
public OptionResponse getOption(@RequestParam Long optionId) {
Option option = optionService.getOption(optionId);
return OptionResponse.from(option);
juno-junho marked this conversation as resolved.
Show resolved Hide resolved
juno-junho marked this conversation as resolved.
Show resolved Hide resolved
}

// 개별 옵션 등록
@PostMapping
public void registerOption(@RequestBody OptionRequest request) {
optionService.registerOption(request.toEntity(), request.optionGroupId());
public ResponseEntity<Void> registerOption(@RequestBody OptionRequest request) {
OptionGroup optionGroup = optionGroupService.getOptionGroup(request.optionGroupId());
Long optionId = optionService.registerOption(request.toEntity(optionGroup), optionGroup);

return ResponseEntity.created(URI.create("/api/v1/options/" + optionId)).build();
}

// 개별 옵션 수정
@PutMapping
public void updateOption(@RequestBody OptionUpdateRequest request) {
OptionUpdateInfo optionUpdateInfo = new OptionUpdateInfo(request.name(), request.price(), request.selected());
optionService.updateOption(optionUpdateInfo, request.optionId());
juno-junho marked this conversation as resolved.
Show resolved Hide resolved
}

// 개별 옵션 삭제
@DeleteMapping
public void deleteOption(@RequestParam Long optionId) {
optionService.deleteOption(optionId);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.ray.pominowner.menu.controller.dto;

import com.ray.pominowner.menu.domain.Option;
import com.ray.pominowner.menu.domain.OptionGroup;

import java.util.List;
import java.util.stream.Collectors;

public record OptionGroupWithOptionResponse(
OptionGroupResponse optionGroupResponse,
List<OptionResponse> optionResponses) {

public static OptionGroupWithOptionResponse from(OptionGroup optionGroup) {
return new OptionGroupWithOptionResponse(
new OptionGroupResponse(
optionGroup.getName(),
optionGroup.getMaxOptionCount(),
optionGroup.isRequired()
),
OptionResponse.from(optionGroup.getOptions())
);
}

private record OptionGroupResponse(String name, int maxOptionCount, boolean required) {
}

public record OptionResponse(String name, int price, boolean selected) {

juno-junho marked this conversation as resolved.
Show resolved Hide resolved
public static List<OptionResponse> from(List<Option> options) {
return options.stream()
.map(option -> new OptionResponse(
option.getName(),
option.getPrice(),
option.isSelected()
))
.collect(Collectors.toList());
}

public static OptionResponse from(Option option) {
return new OptionResponse(
option.getName(),
option.getPrice(),
option.isSelected()
);
}

}

}


Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package com.ray.pominowner.menu.controller.dto;

import com.ray.pominowner.menu.domain.Option;
import com.ray.pominowner.menu.domain.OptionGroup;

public record OptionRequest(String name, int price, boolean selected, Long optionGroupId) {

public Option toEntity() {
public Option toEntity(OptionGroup optionGroup) {
return Option.builder()
.name(name)
.price(price)
.selected(selected)
.optionGroup(optionGroup)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.ray.pominowner.menu.controller.dto;

public record OptionUpdateRequest(String name, int price, boolean selected, Long optionId) {
}
15 changes: 15 additions & 0 deletions src/main/java/com/ray/pominowner/menu/domain/Option.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ray.pominowner.menu.domain;

import com.ray.pominowner.global.domain.BaseTimeEntity;
import com.ray.pominowner.menu.service.vo.OptionUpdateInfo;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
Expand Down Expand Up @@ -67,4 +68,18 @@ public Option changeSelectionStatus(boolean selected) {
.build();
}

public Option update(OptionUpdateInfo request) {
return Option.builder()
.id(this.id)
.name(request.name())
.price(request.price())
.selected(request.selected())
.optionGroup(this.optionGroup)
.build();
}

public Long getId() {
return id;
}

}
27 changes: 23 additions & 4 deletions src/main/java/com/ray/pominowner/menu/domain/OptionGroup.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.ray.pominowner.menu.domain;

import com.ray.pominowner.global.domain.BaseTimeEntity;
import com.ray.pominowner.menu.service.vo.OptionGroupUpdateInfo;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.util.Assert;

Expand All @@ -20,12 +22,11 @@
import static lombok.AccessLevel.PROTECTED;

@Entity
@Getter
@EqualsAndHashCode(of = "id")
@NoArgsConstructor(access = PROTECTED)
public class OptionGroup extends BaseTimeEntity {

private static final int MAX_OPTION_COUNT = 10;

@Id
@Column(name = "OPTION_GROUP_ID")
@GeneratedValue
Expand Down Expand Up @@ -58,7 +59,7 @@ private void validateConstructor(String name, int maxOptionCount) {
Assert.isTrue(maxOptionCount >= 0, "최대 옵션 개수는 0 이상이어야 합니다.");
}

public void addOption(Option option) {
public Option addOption(Option option) {
Option optionGroupChangedOption = Option.builder()
.id(option.getId())
.name(option.getName())
Expand All @@ -70,10 +71,14 @@ public void addOption(Option option) {
this.options.add(optionGroupChangedOption);
checkMaxOptionCount();
checkSelectedOptionCount();

return optionGroupChangedOption;
}

private void checkMaxOptionCount() {
if (options.size() > MAX_OPTION_COUNT) {
final int maxOptionCount = 10;

if (options.size() > maxOptionCount) {
throw new IllegalArgumentException("옵션 개수는 10개를 초과할 수 없습니다.");
juno-junho marked this conversation as resolved.
Show resolved Hide resolved
}
}
Expand All @@ -98,8 +103,22 @@ public void checkRequiredOption() {
}
}

public OptionGroup update(OptionGroupUpdateInfo optionGroupUpdateInfo) {
return OptionGroup.builder()
.id(id)
.name(optionGroupUpdateInfo.name())
.maxOptionCount(optionGroupUpdateInfo.maxOptionCount())
.required(optionGroupUpdateInfo.required())
.storeId(storeId)
.build();
}

public List<Option> getOptions() {
return options;
}

public Long getId() {
return id;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.ray.pominowner.menu.repository;

import com.ray.pominowner.menu.domain.Option;
import org.springframework.data.jpa.repository.JpaRepository;

public interface OptionRepository extends JpaRepository<Option, Long> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,40 @@

import com.ray.pominowner.menu.domain.OptionGroup;
import com.ray.pominowner.menu.repository.OptionGroupRepository;
import com.ray.pominowner.menu.service.vo.OptionGroupUpdateInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@RequiredArgsConstructor
public class OptionGroupService {

private final OptionGroupRepository optionGroupRepository;

public void registerOptionGroup(OptionGroup optiongroup) {
optionGroupRepository.save(optiongroup);
public OptionGroup getOptionGroup(Long optionGroupId) {
return findOptionGroup(optionGroupId);
}

public Long registerOptionGroup(OptionGroup optiongroup) {
return optionGroupRepository.save(optiongroup).getId();
}

public void updateOptionGroup(OptionGroupUpdateInfo optionGroupUpdateInfo, Long optionGroupId) {
OptionGroup optionGroupToUpdate = findOptionGroup(optionGroupId);
juno-junho marked this conversation as resolved.
Show resolved Hide resolved
OptionGroup updatedOptionGroup = optionGroupToUpdate.update(optionGroupUpdateInfo);
optionGroupRepository.save(updatedOptionGroup);
}

public void deleteOptionGroup(Long optionGroupId) {
OptionGroup optionGroup = findOptionGroup(optionGroupId);
optionGroupRepository.delete(optionGroup);
}

private OptionGroup findOptionGroup(Long optionGroupId) {
return optionGroupRepository.findById(optionGroupId)
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 옵션그룹입니다."));
}
juno-junho marked this conversation as resolved.
Show resolved Hide resolved

}
33 changes: 29 additions & 4 deletions src/main/java/com/ray/pominowner/menu/service/OptionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,45 @@
import com.ray.pominowner.menu.domain.Option;
import com.ray.pominowner.menu.domain.OptionGroup;
import com.ray.pominowner.menu.repository.OptionGroupRepository;
import com.ray.pominowner.menu.repository.OptionRepository;
import com.ray.pominowner.menu.service.vo.OptionUpdateInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@RequiredArgsConstructor
public class OptionService {

private final OptionGroupRepository optionGroupRepository;
private final OptionRepository optionRepository;

public void registerOption(Option option, Long optionGroupId) {
OptionGroup optionGroup = optionGroupRepository.findById(optionGroupId)
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 옵션그룹입니다."));
optionGroup.addOption(option);
public Option getOption(Long optionId) {
return findOption(optionId);
}

public Long registerOption(Option option, OptionGroup optionGroup) {
Option optionGroupAddedOption = optionGroup.addOption(option);
optionGroupRepository.save(optionGroup);

return optionGroupAddedOption.getId();
}

public void updateOption(OptionUpdateInfo optionUpdateInfo, Long optionId) {
Option option = findOption(optionId);
Option updatedOption = option.update(optionUpdateInfo);
optionRepository.save(updatedOption);
}

public void deleteOption(Long optionId) {
Option option = findOption(optionId);
optionRepository.delete(option);
}

private Option findOption(Long optionId) {
return optionRepository.findById(optionId)
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 옵션입니다."));
juno-junho marked this conversation as resolved.
Show resolved Hide resolved
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.ray.pominowner.menu.service.vo;

public record OptionGroupUpdateInfo(String name, int maxOptionCount, boolean required) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.ray.pominowner.menu.service.vo;

public record OptionUpdateInfo(String name, int price, boolean selected) {
}
Loading