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

My branch #322

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

My branch #322

wants to merge 4 commits into from

Conversation

V-Solianyk
Copy link

No description provided.


@RestController
@RequestMapping("/categories")
public class CategoryController {

Choose a reason for hiding this comment

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

it's not a mistake, but you can use @requiredargsconstructor instead of creating a constructor

@ManyToOne(fetch = FetchType.LAZY)
private Category category;

@Override

Choose a reason for hiding this comment

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

you can use @tostring

Copy link
Author

Choose a reason for hiding this comment

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

Yes, but I relied on the materials described in our lecture:
image

public class CategoryServiceImpl implements CategoryService {
private final CategoryRepository categoryRepository;

public CategoryServiceImpl(CategoryRepository categoryRepository) {

Choose a reason for hiding this comment

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

the same, you can use annotation


@Override
public Category update(Category category, Long id) {
Category oldCategory = categoryRepository.findById(id)

Choose a reason for hiding this comment

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

I prefer to use getById method, Category oldCategory= this.getById(id);

Copy link
Author

Choose a reason for hiding this comment

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

I don't know what would be better here, I thought that when updating an entity, I should check whether it is in the database with this ID and then update it.

Choose a reason for hiding this comment

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

I think @ykhmaruk meant that you have code duplication here and exactly the same 'find' operation was implemented within your service's getById() method, so you can reuse it here

Copy link
Author

Choose a reason for hiding this comment

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

Yes, thank you, I later figured out what he meant and really realized that it would be better, or at least avoid duplication of code

.orElseThrow(() -> new NoSuchElementException("Can't find the category by "
+ "id: " + id));
oldCategory.setName(category.getName());
oldCategory.setId(id);

Choose a reason for hiding this comment

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

The ID you are trying to set is already set, it is a redundant line


@Override
public Product update(Product product, Long id) {
Product oldProduct = productRepository.findById(id)

Choose a reason for hiding this comment

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

the same as in CategoryServiceImpl

Copy link
Author

Choose a reason for hiding this comment

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

I understand that this is also a working approach, but my version is more readable for me, but if the mentor tells me to correct it, I will do it)

Copy link

@sarakhmen sarakhmen left a comment

Choose a reason for hiding this comment

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

Good job, you can improve your solution if you want :)


@Override
public Category update(Category category, Long id) {
Category oldCategory = categoryRepository.findById(id)

Choose a reason for hiding this comment

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

I think @ykhmaruk meant that you have code duplication here and exactly the same 'find' operation was implemented within your service's getById() method, so you can reuse it here

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.

3 participants