r/SpringBoot • u/AlarmOpening2062 • Mar 01 '26
Discussion E-commerce with Spring Boot
Hello everyone, I hope you're all doing well. I'm writing to ask for your support for this project I'm sharing here. Whether it's by submitting an issue, a PR, or giving a star, this is my first big project. Thank you all!
28
Upvotes
View all comments
Show parent comment
1
u/Status_Camel2859 Apr 27 '26
A bit late, I'm learning Spring too. But I often get confused where to put specific logic/lines of code.
User, I get the current authenticated username usingprincipal.getName()in the ProfileController and then pass it into the AddressService methodgetAllByUserUsername(String username), it then callsfindByUserUsername()in AddressRepository and returns theList. Is this the right way ?User, I get the current authenticated username usingprincipal.getName()in the ProfileController (same as above). The addressId is obtained from \@RequestParam. The controller then pass both into the AddressService methodgetByIdAndUserUsername(Long AddressId, String username), it then callsfindByIdAndUserUsername()in AddressRepository and returns theOptional.The obtained
Addressis then converted into aAddressProfileFormin the ProfileController (I hope this is the right place.??) and then given to the Model. I believe converting theAddressinto a DTO (Request/Response/Form object) couples the UI/Form design with the Business logic in Service and feels wrong, right ?3. To submit the above form-edit or a new-form, I get the current authenticated username (same as above). I get the Form using
\@TheModelAttribute. TheaddressIdis also available in theAddressProfileForm(if edit). The ProfileController checks forAddressIdin the form:--> If its null, it calls the
addAddressForUsername(String username, AddressProfileForm addressProfileForm)in AddressService. The AddressService then also fetches theUserusing UserService (Yes, the UserService is also present in the AddressService just for this purpose, is this right ??). It then creates a newAddressobject and passes to thesavemethod in AddressRepository.-> Else, it calls the
updateAddressForUsername(String username, AddressProfileForm addressProfileForm)in AddressService. The AddressService then fetches the targetAddressusinggetByIdAndUserUsername(Long AddressId, String username), replaces its fields with the new ones in DTO and thensaveit.My doubt is:
-> using DTOs in service tightly couples it with the UI/API DTOs (Request/Response/Form object) and makes it less reusable.
-> Using UserService in AddressService feels wrong, should I make a ProfileService/ProfileFacade to connect the two ? But then it also feels like making it unnecessarily complex.