feat: add PUT /{id}/default-split endpoint
This commit is contained in:
@@ -98,6 +98,18 @@ public class ExpenseListController {
|
||||
return new ResponseEntity<>(renamed, HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PutMapping("/{id}/default-split")
|
||||
public ResponseEntity<ExpenseList> updateDefaultSplit(@PathVariable("id") Long id,
|
||||
@RequestBody @Valid DefaultSplitRequest request, Authentication authentication) {
|
||||
AppUser user = authenticatedUserResolver.resolveCurrentUser(authentication);
|
||||
Optional<ExpenseList> listOpt = expenseListService.findById(id);
|
||||
if (listOpt.isEmpty())
|
||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||
assertMember(user, listOpt.get());
|
||||
ExpenseList updated = expenseListService.updateDefaultSplit(id, request.getPercentage());
|
||||
return new ResponseEntity<>(updated, HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/add")
|
||||
public ResponseEntity<Expense> addExpenseToList(
|
||||
@PathVariable("id") Long expenseListId,
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package de.zendric.app.xpensely_server.model;
|
||||
|
||||
import jakarta.validation.constraints.Max;
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class DefaultSplitRequest {
|
||||
|
||||
@NotNull(message = "Percentage is required")
|
||||
@Min(value = 0, message = "Percentage must be between 0 and 100")
|
||||
@Max(value = 100, message = "Percentage must be between 0 and 100")
|
||||
private Integer percentage;
|
||||
}
|
||||
@@ -50,6 +50,13 @@ public class ExpenseListService {
|
||||
return repository.save(list);
|
||||
}
|
||||
|
||||
public ExpenseList updateDefaultSplit(Long id, int percentage) {
|
||||
ExpenseList list = repository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFoundException("ExpenseList not found with id: " + id));
|
||||
list.setDefaultSplitPercentage(percentage);
|
||||
return repository.save(list);
|
||||
}
|
||||
|
||||
public Optional<ExpenseList> findById(Long id) {
|
||||
return repository.findById(id);
|
||||
}
|
||||
|
||||
+19
@@ -314,6 +314,25 @@ class ExpenseListControllerTest {
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
// --- Default split ---
|
||||
|
||||
@Test
|
||||
void updateDefaultSplit_setsPercentageForMember() throws Exception {
|
||||
AppUser owner = new AppUser(); owner.setId(1L);
|
||||
ExpenseList list = new ExpenseList(); list.setId(5L); list.setOwner(owner);
|
||||
|
||||
when(expenseListService.findById(5L)).thenReturn(Optional.of(list));
|
||||
when(authenticatedUserResolver.resolveCurrentUser(any())).thenReturn(owner);
|
||||
when(expenseListService.updateDefaultSplit(5L, 60)).thenReturn(list);
|
||||
|
||||
mockMvc.perform(put("/api/expenselist/5/default-split")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{\"percentage\":60}"))
|
||||
.andExpect(status().isOk());
|
||||
|
||||
org.mockito.Mockito.verify(expenseListService).updateDefaultSplit(5L, 60);
|
||||
}
|
||||
|
||||
// --- Duplicate-username invite guard ---
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user