feat: add CreateExpenseListRequest DTO with validation to POST /create endpoint
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -58,9 +58,11 @@ public class ExpenseListController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/create")
|
@PostMapping("/create")
|
||||||
public ResponseEntity<ExpenseList> create(@RequestBody ExpenseList expenseList,
|
public ResponseEntity<ExpenseList> create(@RequestBody @Valid CreateExpenseListRequest request,
|
||||||
Authentication authentication) {
|
Authentication authentication) {
|
||||||
AppUser authenticatedUser = authenticatedUserResolver.resolveCurrentUser(authentication);
|
AppUser authenticatedUser = authenticatedUserResolver.resolveCurrentUser(authentication);
|
||||||
|
ExpenseList expenseList = new ExpenseList();
|
||||||
|
expenseList.setName(request.getName());
|
||||||
expenseList.setOwner(authenticatedUser);
|
expenseList.setOwner(authenticatedUser);
|
||||||
XpenselyStandardCategories standardCategories = categoryService.getDefaultCategories();
|
XpenselyStandardCategories standardCategories = categoryService.getDefaultCategories();
|
||||||
expenseList.setXpenselyStandardCategories(standardCategories);
|
expenseList.setXpenselyStandardCategories(standardCategories);
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package de.zendric.app.xpensely_server.model;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
import jakarta.validation.constraints.Size;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class CreateExpenseListRequest {
|
||||||
|
|
||||||
|
@NotBlank(message = "List name is required")
|
||||||
|
@Size(max = 100, message = "List name must not exceed 100 characters")
|
||||||
|
private String name;
|
||||||
|
}
|
||||||
+24
@@ -145,4 +145,28 @@ class ExpenseListControllerTest {
|
|||||||
.content("{\"name\":\"Groceries\"}"))
|
.content("{\"name\":\"Groceries\"}"))
|
||||||
.andExpect(status().isInternalServerError());
|
.andExpect(status().isInternalServerError());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void create_returns400_whenNameIsBlank() throws Exception {
|
||||||
|
AppUser user = new AppUser();
|
||||||
|
user.setId(1L);
|
||||||
|
when(authenticatedUserResolver.resolveCurrentUser(any())).thenReturn(user);
|
||||||
|
|
||||||
|
mockMvc.perform(post("/api/expenselist/create")
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.content("{\"name\":\"\"}"))
|
||||||
|
.andExpect(status().isBadRequest());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void create_returns400_whenBodyIsEmpty() throws Exception {
|
||||||
|
AppUser user = new AppUser();
|
||||||
|
user.setId(1L);
|
||||||
|
when(authenticatedUserResolver.resolveCurrentUser(any())).thenReturn(user);
|
||||||
|
|
||||||
|
mockMvc.perform(post("/api/expenselist/create")
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.content("{}"))
|
||||||
|
.andExpect(status().isBadRequest());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user