Update budgets routes to match Ktor
This commit is contained in:
parent
09ad68a528
commit
89b1b6ccc7
5 changed files with 25 additions and 10 deletions
|
@ -1,5 +1,6 @@
|
|||
package com.wbrawner.twigs.budget;
|
||||
|
||||
import com.wbrawner.twigs.category.CategoryRepository;
|
||||
import com.wbrawner.twigs.permission.Permission;
|
||||
import com.wbrawner.twigs.permission.UserPermission;
|
||||
import com.wbrawner.twigs.permission.UserPermissionRepository;
|
||||
|
@ -24,10 +25,11 @@ import java.util.stream.Collectors;
|
|||
import static com.wbrawner.twigs.Utils.getCurrentUser;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/budgets")
|
||||
@RequestMapping(value = "/api/budgets")
|
||||
@Transactional
|
||||
public class BudgetController {
|
||||
private final BudgetRepository budgetRepository;
|
||||
private final CategoryRepository categoryRepository;
|
||||
private final TransactionRepository transactionRepository;
|
||||
private final UserRepository userRepository;
|
||||
private final UserPermissionRepository userPermissionsRepository;
|
||||
|
@ -35,11 +37,13 @@ public class BudgetController {
|
|||
|
||||
public BudgetController(
|
||||
BudgetRepository budgetRepository,
|
||||
CategoryRepository categoryRepository,
|
||||
TransactionRepository transactionRepository,
|
||||
UserRepository userRepository,
|
||||
UserPermissionRepository userPermissionsRepository
|
||||
) {
|
||||
this.budgetRepository = budgetRepository;
|
||||
this.categoryRepository = categoryRepository;
|
||||
this.transactionRepository = transactionRepository;
|
||||
this.userRepository = userRepository;
|
||||
this.userPermissionsRepository = userPermissionsRepository;
|
||||
|
@ -150,6 +154,7 @@ public class BudgetController {
|
|||
produces = {MediaType.APPLICATION_JSON_VALUE}
|
||||
)
|
||||
public ResponseEntity<BudgetResponse> updateBudget(@PathVariable String id, @RequestBody BudgetRequest request) {
|
||||
// TODO: Make sure no changes in ownership are being attempted (except by the owner)
|
||||
return getBudgetWithPermission(id, Permission.MANAGE, (budget) -> {
|
||||
if (request.name != null) {
|
||||
budget.setName(request.name);
|
||||
|
@ -171,7 +176,8 @@ public class BudgetController {
|
|||
)
|
||||
))
|
||||
));
|
||||
} else {
|
||||
}
|
||||
if (users.isEmpty()) {
|
||||
users.addAll(userPermissionsRepository.findAllByBudget(budget, null));
|
||||
}
|
||||
|
||||
|
@ -179,11 +185,14 @@ public class BudgetController {
|
|||
});
|
||||
}
|
||||
|
||||
@DeleteMapping(value = "/{id}", produces = {MediaType.TEXT_PLAIN_VALUE})
|
||||
@DeleteMapping(value = "/{id}")
|
||||
public ResponseEntity<Void> deleteBudget(@PathVariable String id) {
|
||||
return getBudgetWithPermission(id, Permission.MANAGE, (budget) -> {
|
||||
categoryRepository.deleteAllByBudget(budget);
|
||||
transactionRepository.deleteAllByBudget(budget);
|
||||
userPermissionsRepository.deleteAllByBudget(budget);
|
||||
budgetRepository.delete(budget);
|
||||
return ResponseEntity.ok().build();
|
||||
return ResponseEntity.noContent().build();
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -21,4 +21,6 @@ public interface CategoryRepository extends CrudRepository<Category, String>,
|
|||
Optional<Category> findByBudgetAndId(Budget budget, String id);
|
||||
|
||||
List<Category> findAllByBudgetInAndIdIn(List<Budget> budgets, List<String> ids, Pageable pageable);
|
||||
|
||||
void deleteAllByBudget(Budget budget);
|
||||
}
|
|
@ -20,4 +20,6 @@ public interface UserPermissionRepository extends CrudRepository<UserPermission,
|
|||
List<UserPermission> findAllByUserAndBudget(User user, Budget budget, Pageable pageable);
|
||||
|
||||
List<UserPermission> findAllByUserAndBudget_IdIn(User user, List<String> budgetIds, Pageable pageable);
|
||||
|
||||
void deleteAllByBudget(Budget budget);
|
||||
}
|
|
@ -1,21 +1,21 @@
|
|||
package com.wbrawner.twigs.permission;
|
||||
|
||||
import com.wbrawner.twigs.user.UserResponse;
|
||||
import com.wbrawner.twigs.user.User;
|
||||
|
||||
public class UserPermissionResponse {
|
||||
private final UserResponse user;
|
||||
private final String user;
|
||||
private final Permission permission;
|
||||
|
||||
public UserPermissionResponse(UserPermission userPermission) {
|
||||
this(new UserResponse(userPermission.getUser()), userPermission.getPermission());
|
||||
this(userPermission.getUser(), userPermission.getPermission());
|
||||
}
|
||||
|
||||
public UserPermissionResponse(UserResponse userResponse, Permission permission) {
|
||||
this.user = userResponse;
|
||||
public UserPermissionResponse(User user, Permission permission) {
|
||||
this.user = user.getId();
|
||||
this.permission = permission;
|
||||
}
|
||||
|
||||
public UserResponse getUser() {
|
||||
public String getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
|
|
|
@ -44,4 +44,6 @@ public interface TransactionRepository extends CrudRepository<Transaction, Strin
|
|||
value = "SELECT (COALESCE((SELECT SUM(amount) from transaction WHERE category_id = :categoryId AND expense = 0 AND date > :start), 0)) - (COALESCE((SELECT SUM(amount) from transaction WHERE category_id = :categoryId AND expense = 1 AND date > :start), 0));"
|
||||
)
|
||||
Long sumBalanceByCategoryId(String categoryId, Date start);
|
||||
|
||||
void deleteAllByBudget(Budget budget);
|
||||
}
|
Loading…
Reference in a new issue