Skip to content

Conversation

@svetashov
Copy link

No description provided.

public ResponseEntity<Integer> getSocks(@RequestParam String color,
@RequestParam String operation,
@RequestParam int cottonPart) {
if (cottonPart < 0 || cottonPart > 100)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

можно в принципе сделать аннотациями

Sock sock = sockRepository
.findByColorAndCottonPart(color, sockModel.getCottonPart())
.orElseThrow(NotEnoughSocksException::new);
if (sock.getQuantity() < sockModel.getQuantity())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

я бы так исключениями не увлекался - это дорого и медленно, а например ситуация когда нет нужного числа носков. -вполне штатная. поэтому тут лучше либо ответ с результатом операции, который маппится в http, либо в сервисе не хранить тяжелую логику, а переложить ее на контроллер.

В целом тут два варианта - либо сервис возвращает сложный формат ответа, включающий ошибки и их описание, либо контроллер у себя логику всю считает - оба валидные

private final ColorRepository colorRepository;
private final SockRepository sockRepository;

public int addSocks(@NotNull SockModel sockModel) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тут конечно нужны транзакции


int quantity = switch (operation) {
case "moreThan" -> sockRepository
.findByColorAndCottonPartGreaterThan(color, cottonPart)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тут ради одной цифры тащить целый объект не оптимально

package com.example.socks;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

С поставленной задачей справился, есть недочеты или спорные моменты - они есть всегда и везде, главное, что решение валидно.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants