r/SpringBoot • • 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!

https://github.com/MiguelAntonioRS/Ecommerce-with-Spring

28 Upvotes

15 comments sorted by

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.

  1. To fetch all the addresses of a User, I get the current authenticated username using principal.getName() in the ProfileController and then pass it into the AddressService method getAllByUserUsername(String username) , it then calls findByUserUsername() in AddressRepository and returns the List. Is this the right way ?
  2. To open selected address's form-edit of a User, I get the current authenticated username using principal.getName() in the ProfileController (same as above). The addressId is obtained from \@RequestParam. The controller then pass both into the AddressService method getByIdAndUserUsername(Long AddressId, String username) , it then calls findByIdAndUserUsername() in AddressRepository and returns the Optional.

The obtained Address is then converted into a AddressProfileForm in the ProfileController (I hope this is the right place.??) and then given to the Model. I believe converting the Address into 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. The addressId is also available in the AddressProfileForm (if edit). The ProfileController checks for AddressId in the form:-

-> If its null, it calls the addAddressForUsername(String username, AddressProfileForm addressProfileForm) in AddressService. The AddressService then also fetches the User using UserService (Yes, the UserService is also present in the AddressService just for this purpose, is this right ??). It then creates a new Address object and passes to the save method in AddressRepository.

-> Else, it calls the updateAddressForUsername(String username, AddressProfileForm addressProfileForm) in AddressService. The AddressService then fetches the target Address using getByIdAndUserUsername(Long AddressId, String username) , replaces its fields with the new ones in DTO and then save it.

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.