-
Notifications
You must be signed in to change notification settings - Fork 0
customer #63
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
Merged
customer #63
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
058a831
Merge branch 'customer'
7MCristy f531de2
Merge branch 'main' of https://github.com/UdL-EPS-SoftArch/mycoffe-api
7MCristy 5d533ed
added Securityconfigurations for the custiomer and admin
7MCristy 0540516
id to the gmail
7MCristy 1e818ca
Added Customer Controler
7MCristy a4e0f1f
Merge branch 'main' of https://github.com/UdL-EPS-SoftArch/mycoffe-ap…
7MCristy 6f384de
fix: remove necessary CustomerController
rogargon 752580f
fix: use Id from extended user
rogargon 34beada
refactor: serialise Loyalty from class instance
rogargon 08329ba
fix: update loyalty tests
rogargon 75dcf7c
fix: error code for repeated customer username is 409
rogargon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/main/java/cat/udl/eps/softarch/demo/controller/CustomerController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package cat.udl.eps.softarch.demo.controller; | ||
|
|
||
| import cat.udl.eps.softarch.demo.domain.Customer; | ||
| import cat.udl.eps.softarch.demo.repository.CustomerRepository; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.util.Set; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/customers") | ||
| public class CustomerController { | ||
|
|
||
| private final CustomerRepository customerRepository; | ||
|
|
||
| public CustomerController(CustomerRepository customerRepository) { | ||
| this.customerRepository = customerRepository; | ||
| } | ||
|
|
||
| @PostMapping | ||
| public Customer register(@RequestBody Customer customer) { | ||
| customer.encodePassword(); // 🔐 CLAVE | ||
| customer.setRoles(Set.of("CUSTOMER")); // 🔑 CLAVE | ||
| return customerRepository.save(customer); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/test/java/cat/udl/eps/softarch/demo/steps/UpdateLoyaltyStepDefs.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package cat.udl.eps.softarch.demo.steps; | ||
|
|
||
| import cat.udl.eps.softarch.demo.domain.Business; | ||
| import cat.udl.eps.softarch.demo.domain.Customer; | ||
| import cat.udl.eps.softarch.demo.domain.Loyalty; | ||
| import cat.udl.eps.softarch.demo.repository.BusinessRepository; | ||
| import cat.udl.eps.softarch.demo.repository.CustomerRepository; | ||
| import cat.udl.eps.softarch.demo.repository.LoyaltyRepository; | ||
| import io.cucumber.java.en.And; | ||
| import io.cucumber.java.en.Given; | ||
| import io.cucumber.java.en.When; | ||
| import org.json.JSONObject; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.http.MediaType; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.List; | ||
|
|
||
| import static org.hamcrest.Matchers.is; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; | ||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
|
|
||
| public class UpdateLoyaltyStepDefs { | ||
|
|
||
| @Autowired | ||
| private StepDefs stepDefs; | ||
|
|
||
| @Autowired | ||
| private LoyaltyRepository loyaltyRepository; | ||
|
|
||
| @Autowired | ||
| private CustomerRepository customerRepository; | ||
|
|
||
| @Autowired | ||
| private BusinessRepository businessRepository; | ||
|
|
||
| private Long loyaltyId; | ||
|
|
||
| @When("I update the loyalty for customer {string} and business {Long} to have {int} points") | ||
| public void iUpdateTheLoyaltyForCustomerAndBusinessToHavePoints( | ||
| String customerUsername, String businessId, Integer newPoints) throws Exception { | ||
|
|
||
| Customer customer = customerRepository.findById(customerUsername).orElseThrow(); | ||
| Business business = businessRepository.findById(businessId).orElseThrow(); | ||
|
|
||
| List<Loyalty> loyalties = loyaltyRepository.findByCustomerAndBusiness(customer, business); | ||
| assertTrue(!loyalties.isEmpty(), "Loyalty should exist before updating"); | ||
| Loyalty loyalty = loyalties.get(0); | ||
| loyaltyId = loyalty.getId(); | ||
|
|
||
| JSONObject loyaltyUpdate = new JSONObject(); | ||
| loyaltyUpdate.put("accumulatedPoints", newPoints); | ||
|
|
||
| stepDefs.result = stepDefs.mockMvc.perform( | ||
| patch("/loyalties/{id}", loyaltyId) | ||
| .contentType(MediaType.APPLICATION_JSON) | ||
| .content(loyaltyUpdate.toString()) | ||
| .characterEncoding(StandardCharsets.UTF_8) | ||
| .accept(MediaType.APPLICATION_JSON) | ||
| .with(AuthenticationStepDefs.authenticate())) | ||
| .andDo(print()); | ||
| } | ||
|
|
||
| @When("I update the loyalty with id {long} to have {int} points") | ||
| public void iUpdateTheLoyaltyWithIdToHavePoints(Long id, Integer newPoints) throws Exception { | ||
| JSONObject loyaltyUpdate = new JSONObject(); | ||
| loyaltyUpdate.put("accumulatedPoints", newPoints); | ||
|
|
||
| stepDefs.result = stepDefs.mockMvc.perform( | ||
| patch("/loyalties/{id}", id) | ||
| .contentType(MediaType.APPLICATION_JSON) | ||
| .content(loyaltyUpdate.toString()) | ||
| .characterEncoding(StandardCharsets.UTF_8) | ||
| .accept(MediaType.APPLICATION_JSON) | ||
| .with(AuthenticationStepDefs.authenticate())) | ||
| .andDo(print()); | ||
| } | ||
|
|
||
| @And("The loyalty has {int} accumulated points") | ||
| public void theLoyaltyHasAccumulatedPoints(Integer expectedPoints) throws Exception { | ||
| // Verificamos que el último resultado contenga los puntos esperados | ||
| stepDefs.result.andExpect(jsonPath("$.accumulatedPoints", is(expectedPoints))); | ||
| } | ||
|
|
||
| @Given("There is no loyalty with id {long}") | ||
| public void thereIsNoLoyaltyWithId(Long id) { | ||
| assertFalse(loyaltyRepository.existsById(id), | ||
| "Loyalty with id \"" + id + "\" shouldn't exist"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| Feature: Update Loyalty | ||
| In order to manage loyalty program progress | ||
| As a business | ||
| I want to update existing loyalty records | ||
|
|
||
| Background: | ||
| Given There is a registered customer with username "customer1" and password "password" and email "customer1@example.com" | ||
| Given There is a registered business with id 1 and name "Test Business" and address "123 Main St" | ||
| And There is a loyalty for customer "customer1" and business 1 with 50 points | ||
|
|
||
|
|
||
| And I login as "customer1" with password "password" | ||
| And There is a loyalty for customer "customer1" and business 1 with 50 points | ||
|
|
||
| Scenario: Update loyalty points successfully | ||
| When I update the loyalty for customer "customer1" and business 1 to have 120 points | ||
| Then The response code is 200 | ||
| And The loyalty has 120 accumulated points | ||
|
|
||
| Scenario: Update loyalty points to a negative value fails | ||
| When I update the loyalty for customer "customer1" and business 1 to have -30 points | ||
| Then The response code is 400 | ||
|
|
||
| Scenario: Update non-existent loyalty fails | ||
| Given There is no loyalty with id 999 | ||
| When I update the loyalty with id 999 to have 200 points | ||
| Then The response code is 404 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 don't need to develop a custom CustomerController for Customer registration. The CustomerRepository already provides and enpoint at POST /customers
Check the approach followed to register users for similar functionality.
And if you need to customise something before or after creating a new customer, you can use the CustomerEventHandler.