-
Notifications
You must be signed in to change notification settings - Fork 343
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
base: main
Are you sure you want to change the base?
My branch #322
Conversation
|
||
@RestController | ||
@RequestMapping("/categories") | ||
public class CategoryController { |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can use @tostring
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public class CategoryServiceImpl implements CategoryService { | ||
private final CategoryRepository categoryRepository; | ||
|
||
public CategoryServiceImpl(CategoryRepository categoryRepository) { |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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);
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)
There was a problem hiding this 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) |
There was a problem hiding this comment.
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
No description provided.