Switch to string-based identifiers for all objects
This is needed to begin using UUIDs so that objects can be created client-side with unique identifiers that are less likely to collide with objects created from other clients simultaneously
This commit is contained in:
parent
377f2b61f7
commit
7237e12363
25 changed files with 107 additions and 98 deletions
|
@ -6,6 +6,7 @@ import org.springframework.security.core.context.SecurityContextHolder;
|
|||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.UUID;
|
||||
|
||||
public final class Utils {
|
||||
private static final int[] CALENDAR_FIELDS = new int[]{
|
||||
|
@ -40,4 +41,8 @@ public final class Utils {
|
|||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String randomId() {
|
||||
return UUID.randomUUID().toString().replace("-", "");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,11 +8,12 @@ import java.util.HashSet;
|
|||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import static com.wbrawner.budgetserver.Utils.randomId;
|
||||
|
||||
@Entity
|
||||
public class Budget {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
private String id = randomId();
|
||||
private String name;
|
||||
private String description;
|
||||
private String currencyCode;
|
||||
|
@ -35,11 +36,11 @@ public class Budget {
|
|||
this.currencyCode = currencyCode;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
package com.wbrawner.budgetserver.budget;
|
||||
|
||||
public class BudgetBalanceResponse {
|
||||
public final long id;
|
||||
public final String id;
|
||||
public final long balance;
|
||||
|
||||
public BudgetBalanceResponse(long id, long balance) {
|
||||
public BudgetBalanceResponse(String id, long balance) {
|
||||
this.id = id;
|
||||
this.balance = balance;
|
||||
}
|
||||
|
|
|
@ -77,7 +77,7 @@ public class BudgetController {
|
|||
|
||||
@GetMapping(value = "/{id}", produces = {MediaType.APPLICATION_JSON_VALUE})
|
||||
@ApiOperation(value = "getBudget", nickname = "getBudget", tags = {"Budgets"})
|
||||
public ResponseEntity<BudgetResponse> getBudget(@PathVariable long id) {
|
||||
public ResponseEntity<BudgetResponse> getBudget(@PathVariable String id) {
|
||||
return getBudgetWithPermission(id, Permission.READ, (budget) ->
|
||||
ResponseEntity.ok(new BudgetResponse(budget, userPermissionsRepository.findAllByBudget(budget, null)))
|
||||
);
|
||||
|
@ -85,7 +85,7 @@ public class BudgetController {
|
|||
|
||||
@GetMapping(value = "/{id}/balance", produces = {MediaType.APPLICATION_JSON_VALUE})
|
||||
@ApiOperation(value = "getBudgetBalance", nickname = "getBudgetBalance", tags = {"Budgets"})
|
||||
public ResponseEntity<BudgetBalanceResponse> getBudgetBalance(@PathVariable long id) {
|
||||
public ResponseEntity<BudgetBalanceResponse> getBudgetBalance(@PathVariable String id) {
|
||||
return getBudgetWithPermission(id, Permission.READ, (budget) -> {
|
||||
var balance = transactionRepository.sumBalanceByBudgetId(budget.getId(), getFirstOfMonth());
|
||||
return ResponseEntity.ok(new BudgetBalanceResponse(budget.getId(), balance));
|
||||
|
@ -125,7 +125,7 @@ public class BudgetController {
|
|||
|
||||
@PutMapping(value = "/{id}", consumes = {MediaType.APPLICATION_JSON_VALUE}, produces = {MediaType.APPLICATION_JSON_VALUE})
|
||||
@ApiOperation(value = "updateBudget", nickname = "updateBudget", tags = {"Budgets"})
|
||||
public ResponseEntity<BudgetResponse> updateBudget(@PathVariable long id, @RequestBody BudgetRequest request) {
|
||||
public ResponseEntity<BudgetResponse> updateBudget(@PathVariable String id, @RequestBody BudgetRequest request) {
|
||||
return getBudgetWithPermission(id, Permission.MANAGE, (budget) -> {
|
||||
if (request.name != null) {
|
||||
budget.setName(request.name);
|
||||
|
@ -157,7 +157,7 @@ public class BudgetController {
|
|||
|
||||
@DeleteMapping(value = "/{id}", produces = {MediaType.TEXT_PLAIN_VALUE})
|
||||
@ApiOperation(value = "deleteBudget", nickname = "deleteBudget", tags = {"Budgets"})
|
||||
public ResponseEntity<Void> deleteBudget(@PathVariable long id) {
|
||||
public ResponseEntity<Void> deleteBudget(@PathVariable String id) {
|
||||
return getBudgetWithPermission(id, Permission.MANAGE, (budget) -> {
|
||||
budgetRepository.delete(budget);
|
||||
return ResponseEntity.ok().build();
|
||||
|
@ -165,7 +165,7 @@ public class BudgetController {
|
|||
}
|
||||
|
||||
private <T> ResponseEntity<T> getBudgetWithPermission(
|
||||
long budgetId,
|
||||
String budgetId,
|
||||
Permission permission,
|
||||
Function<Budget, ResponseEntity<T>> callback
|
||||
) {
|
||||
|
|
|
@ -9,12 +9,12 @@ import java.util.Objects;
|
|||
import java.util.stream.Collectors;
|
||||
|
||||
public class BudgetResponse {
|
||||
public final long id;
|
||||
public final String id;
|
||||
public final String name;
|
||||
public final String description;
|
||||
private final List<UserPermissionResponse> users;
|
||||
|
||||
public BudgetResponse(long id, String name, String description, List<UserPermissionResponse> users) {
|
||||
public BudgetResponse(String id, String name, String description, List<UserPermissionResponse> users) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
|
|
|
@ -3,12 +3,14 @@ package com.wbrawner.budgetserver.category;
|
|||
import com.wbrawner.budgetserver.budget.Budget;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.UUID;
|
||||
|
||||
import static com.wbrawner.budgetserver.Utils.randomId;
|
||||
|
||||
@Entity
|
||||
public class Category implements Comparable<Category> {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private final Long id = null;
|
||||
private final String id = randomId();
|
||||
private String title;
|
||||
private String description;
|
||||
private long amount;
|
||||
|
@ -42,7 +44,7 @@ public class Category implements Comparable<Category> {
|
|||
return title.compareTo(other.title);
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
package com.wbrawner.budgetserver.category;
|
||||
|
||||
public class CategoryBalanceResponse {
|
||||
private final long id;
|
||||
private final String id;
|
||||
private final long balance;
|
||||
|
||||
public CategoryBalanceResponse(long id, long balance) {
|
||||
public CategoryBalanceResponse(String id, long balance) {
|
||||
this.id = id;
|
||||
this.balance = balance;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
|
|
|
@ -45,7 +45,7 @@ class CategoryController {
|
|||
@GetMapping(path = "", produces = {MediaType.APPLICATION_JSON_VALUE})
|
||||
@ApiOperation(value = "getCategories", nickname = "getCategories", tags = {"Categories"})
|
||||
ResponseEntity<List<CategoryResponse>> getCategories(
|
||||
@RequestParam(name = "budgetIds", required = false) List<Long> budgetIds,
|
||||
@RequestParam(name = "budgetIds", required = false) List<String> budgetIds,
|
||||
@RequestParam(name = "isExpense", required = false) Boolean isExpense,
|
||||
@RequestParam(name = "includeArchived", required = false) Boolean includeArchived,
|
||||
@RequestParam(name = "count", required = false) Integer count,
|
||||
|
@ -84,7 +84,7 @@ class CategoryController {
|
|||
|
||||
@GetMapping(path = "/{id}", produces = {MediaType.APPLICATION_JSON_VALUE})
|
||||
@ApiOperation(value = "getCategory", nickname = "getCategory", tags = {"Categories"})
|
||||
ResponseEntity<CategoryResponse> getCategory(@PathVariable Long id) {
|
||||
ResponseEntity<CategoryResponse> getCategory(@PathVariable String id) {
|
||||
var budgets = userPermissionsRepository.findAllByUser(getCurrentUser(), null)
|
||||
.stream()
|
||||
.map(UserPermission::getBudget)
|
||||
|
@ -96,7 +96,7 @@ class CategoryController {
|
|||
|
||||
@GetMapping(path = "/{id}/balance", produces = {MediaType.APPLICATION_JSON_VALUE})
|
||||
@ApiOperation(value = "getCategoryBalance", nickname = "getCategoryBalance", tags = {"Categories"})
|
||||
ResponseEntity<CategoryBalanceResponse> getCategoryBalance(@PathVariable Long id) {
|
||||
ResponseEntity<CategoryBalanceResponse> getCategoryBalance(@PathVariable String id) {
|
||||
var budgets = userPermissionsRepository.findAllByUser(getCurrentUser(), null)
|
||||
.stream()
|
||||
.map(UserPermission::getBudget)
|
||||
|
@ -132,7 +132,7 @@ class CategoryController {
|
|||
|
||||
@PutMapping(path = "/{id}", consumes = {MediaType.APPLICATION_JSON_VALUE}, produces = {MediaType.APPLICATION_JSON_VALUE})
|
||||
@ApiOperation(value = "updateCategory", nickname = "updateCategory", tags = {"Categories"})
|
||||
ResponseEntity<CategoryResponse> updateCategory(@PathVariable Long id, @RequestBody UpdateCategoryRequest request) {
|
||||
ResponseEntity<CategoryResponse> updateCategory(@PathVariable String id, @RequestBody UpdateCategoryRequest request) {
|
||||
var category = categoryRepository.findById(id).orElse(null);
|
||||
if (category == null) return ResponseEntity.notFound().build();
|
||||
var userPermission = userPermissionsRepository.findByUserAndBudget_Id(getCurrentUser(), category.getBudget().getId()).orElse(null);
|
||||
|
@ -160,7 +160,7 @@ class CategoryController {
|
|||
|
||||
@DeleteMapping(path = "/{id}", produces = {MediaType.TEXT_PLAIN_VALUE})
|
||||
@ApiOperation(value = "deleteCategory", nickname = "deleteCategory", tags = {"Categories"})
|
||||
ResponseEntity<Void> deleteCategory(@PathVariable Long id) {
|
||||
ResponseEntity<Void> deleteCategory(@PathVariable String id) {
|
||||
var category = categoryRepository.findById(id).orElse(null);
|
||||
if (category == null) return ResponseEntity.notFound().build();
|
||||
var userPermission = userPermissionsRepository.findByUserAndBudget_Id(getCurrentUser(), category.getBudget().getId()).orElse(null);
|
||||
|
|
|
@ -9,15 +9,15 @@ import org.springframework.data.repository.PagingAndSortingRepository;
|
|||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface CategoryRepository extends PagingAndSortingRepository<Category, Long> {
|
||||
public interface CategoryRepository extends PagingAndSortingRepository<Category, String> {
|
||||
List<Category> findAllByBudget(Budget budget, Pageable pageable);
|
||||
|
||||
@Query("SELECT c FROM Category c where c.budget IN (:budgets) AND (:expense IS NULL OR c.expense = :expense) AND (:archived IS NULL OR c.archived = :archived)")
|
||||
List<Category> findAllByBudgetIn(List<Budget> budgets, Boolean expense, Boolean archived, Pageable pageable);
|
||||
|
||||
Optional<Category> findByBudgetInAndId(List<Budget> budgets, Long id);
|
||||
Optional<Category> findByBudgetInAndId(List<Budget> budgets, String id);
|
||||
|
||||
Optional<Category> findByBudgetAndId(Budget budget, Long id);
|
||||
Optional<Category> findByBudgetAndId(Budget budget, String id);
|
||||
|
||||
List<Category> findAllByBudgetInAndIdIn(List<Budget> budgets, List<Long> ids, Pageable pageable);
|
||||
List<Category> findAllByBudgetInAndIdIn(List<Budget> budgets, List<String> ids, Pageable pageable);
|
||||
}
|
|
@ -3,11 +3,11 @@ package com.wbrawner.budgetserver.category;
|
|||
import java.util.Objects;
|
||||
|
||||
public class CategoryResponse {
|
||||
private final long id;
|
||||
private final String id;
|
||||
private final String title;
|
||||
private final String description;
|
||||
private final long amount;
|
||||
private final long budgetId;
|
||||
private final String budgetId;
|
||||
private final boolean expense;
|
||||
private final boolean archived;
|
||||
|
||||
|
@ -23,7 +23,7 @@ public class CategoryResponse {
|
|||
);
|
||||
}
|
||||
|
||||
public CategoryResponse(long id, String title, String description, long amount, long budgetId, boolean expense, boolean archived) {
|
||||
public CategoryResponse(String id, String title, String description, long amount, String budgetId, boolean expense, boolean archived) {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
this.description = description;
|
||||
|
@ -33,7 +33,7 @@ public class CategoryResponse {
|
|||
this.archived = archived;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
|
@ -49,7 +49,7 @@ public class CategoryResponse {
|
|||
return amount;
|
||||
}
|
||||
|
||||
public long getBudgetId() {
|
||||
public String getBudgetId() {
|
||||
return budgetId;
|
||||
}
|
||||
|
||||
|
|
|
@ -4,14 +4,14 @@ public class NewCategoryRequest {
|
|||
private final String title;
|
||||
private final String description;
|
||||
private final Long amount;
|
||||
private final Long budgetId;
|
||||
private final String budgetId;
|
||||
private final Boolean expense;
|
||||
|
||||
public NewCategoryRequest() {
|
||||
this(null, null, null, null, null);
|
||||
}
|
||||
|
||||
public NewCategoryRequest(String title, String description, Long amount, Long budgetId, Boolean expense) {
|
||||
public NewCategoryRequest(String title, String description, Long amount, String budgetId, Boolean expense) {
|
||||
this.title = title;
|
||||
this.description = description;
|
||||
this.amount = amount;
|
||||
|
@ -31,7 +31,7 @@ public class NewCategoryRequest {
|
|||
return amount;
|
||||
}
|
||||
|
||||
public Long getBudgetId() {
|
||||
public String getBudgetId() {
|
||||
return budgetId;
|
||||
}
|
||||
|
||||
|
|
|
@ -7,31 +7,30 @@ import java.util.Calendar;
|
|||
import java.util.GregorianCalendar;
|
||||
import java.util.UUID;
|
||||
|
||||
import static com.wbrawner.budgetserver.Utils.randomId;
|
||||
|
||||
@Entity
|
||||
public class PasswordResetRequest {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private final Long id;
|
||||
private final String id = randomId();
|
||||
@ManyToOne
|
||||
private final User user;
|
||||
private final Calendar date;
|
||||
private final String token;
|
||||
|
||||
public PasswordResetRequest() {
|
||||
this(null, null);
|
||||
this(null);
|
||||
}
|
||||
|
||||
public PasswordResetRequest(Long id, User user) {
|
||||
this(id, user, new GregorianCalendar(), UUID.randomUUID().toString().replace("-", ""));
|
||||
public PasswordResetRequest(User user) {
|
||||
this(user, new GregorianCalendar(), randomId());
|
||||
}
|
||||
|
||||
public PasswordResetRequest(
|
||||
Long id,
|
||||
User user,
|
||||
Calendar date,
|
||||
String token
|
||||
) {
|
||||
this.id = id;
|
||||
this.user = user;
|
||||
this.date = date;
|
||||
this.token = token;
|
||||
|
|
|
@ -5,14 +5,14 @@ import java.io.Serializable;
|
|||
|
||||
@Embeddable
|
||||
public class UserPermissionKey implements Serializable {
|
||||
private final Long budgetId;
|
||||
private final Long userId;
|
||||
private final String budgetId;
|
||||
private final String userId;
|
||||
|
||||
public UserPermissionKey() {
|
||||
this(0, 0);
|
||||
this(null, null);
|
||||
}
|
||||
|
||||
public UserPermissionKey(long budgetId, long userId) {
|
||||
public UserPermissionKey(String budgetId, String userId) {
|
||||
this.budgetId = budgetId;
|
||||
this.userId = userId;
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ import java.util.List;
|
|||
import java.util.Optional;
|
||||
|
||||
public interface UserPermissionRepository extends PagingAndSortingRepository<UserPermission, UserPermissionKey> {
|
||||
Optional<UserPermission> findByUserAndBudget_Id(User user, Long budgetId);
|
||||
Optional<UserPermission> findByUserAndBudget_Id(User user, String budgetId);
|
||||
|
||||
List<UserPermission> findAllByUser(User user, Pageable pageable);
|
||||
|
||||
|
@ -17,5 +17,5 @@ public interface UserPermissionRepository extends PagingAndSortingRepository<Use
|
|||
|
||||
List<UserPermission> findAllByUserAndBudget(User user, Budget budget, Pageable pageable);
|
||||
|
||||
List<UserPermission> findAllByUserAndBudget_IdIn(User user, List<Long> budgetIds, Pageable pageable);
|
||||
List<UserPermission> findAllByUserAndBudget_IdIn(User user, List<String> budgetIds, Pageable pageable);
|
||||
}
|
|
@ -1,19 +1,19 @@
|
|||
package com.wbrawner.budgetserver.permission;
|
||||
|
||||
public class UserPermissionRequest {
|
||||
private final Long user;
|
||||
private final String user;
|
||||
private final Permission permission;
|
||||
|
||||
public UserPermissionRequest() {
|
||||
this(0L, Permission.READ);
|
||||
this(null, Permission.READ);
|
||||
}
|
||||
|
||||
public UserPermissionRequest(Long user, Permission permission) {
|
||||
public UserPermissionRequest(String user, Permission permission) {
|
||||
this.user = user;
|
||||
this.permission = permission;
|
||||
}
|
||||
|
||||
public Long getUser() {
|
||||
public String getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
|
|
|
@ -5,15 +5,15 @@ class NewTransactionRequest {
|
|||
private final String description;
|
||||
private final String date;
|
||||
private final Long amount;
|
||||
private final Long categoryId;
|
||||
private final String categoryId;
|
||||
private final Boolean expense;
|
||||
private final Long budgetId;
|
||||
private final String budgetId;
|
||||
|
||||
NewTransactionRequest() {
|
||||
this(null, null, null, null, null, null, null);
|
||||
}
|
||||
|
||||
NewTransactionRequest(String title, String description, String date, Long amount, Long categoryId, Boolean expense, Long budgetId) {
|
||||
NewTransactionRequest(String title, String description, String date, Long amount, String categoryId, Boolean expense, String budgetId) {
|
||||
this.title = title;
|
||||
this.description = description;
|
||||
this.date = date;
|
||||
|
@ -39,7 +39,7 @@ class NewTransactionRequest {
|
|||
return amount;
|
||||
}
|
||||
|
||||
public Long getCategoryId() {
|
||||
public String getCategoryId() {
|
||||
return categoryId;
|
||||
}
|
||||
|
||||
|
@ -47,7 +47,7 @@ class NewTransactionRequest {
|
|||
return expense;
|
||||
}
|
||||
|
||||
public Long getBudgetId() {
|
||||
public String getBudgetId() {
|
||||
return budgetId;
|
||||
}
|
||||
}
|
|
@ -7,11 +7,12 @@ import com.wbrawner.budgetserver.user.User;
|
|||
import javax.persistence.*;
|
||||
import java.time.Instant;
|
||||
|
||||
import static com.wbrawner.budgetserver.Utils.randomId;
|
||||
|
||||
@Entity
|
||||
public class Transaction implements Comparable<Transaction> {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private final Long id = null;
|
||||
private final String id = randomId();
|
||||
@ManyToOne
|
||||
@JoinColumn(nullable = false)
|
||||
private final User createdBy;
|
||||
|
@ -48,7 +49,7 @@ public class Transaction implements Comparable<Transaction> {
|
|||
this.budget = budget;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
public String getId() {
|
||||
// This should only be set from Hibernate so it shouldn't actually be null ever
|
||||
//noinspection ConstantConditions
|
||||
return id;
|
||||
|
|
|
@ -47,8 +47,8 @@ public class TransactionController {
|
|||
@GetMapping(path = "", produces = {MediaType.APPLICATION_JSON_VALUE})
|
||||
@ApiOperation(value = "getTransactions", nickname = "getTransactions", tags = {"Transactions"})
|
||||
public ResponseEntity<List<TransactionResponse>> getTransactions(
|
||||
@RequestParam(value = "categoryIds", required = false) List<Long> categoryIds,
|
||||
@RequestParam(value = "budgetIds", required = false) List<Long> budgetIds,
|
||||
@RequestParam(value = "categoryIds", required = false) List<String> categoryIds,
|
||||
@RequestParam(value = "budgetIds", required = false) List<String> budgetIds,
|
||||
@RequestParam(value = "from", required = false) String from,
|
||||
@RequestParam(value = "to", required = false) String to,
|
||||
@RequestParam(value = "count", required = false) Integer count,
|
||||
|
@ -113,7 +113,7 @@ public class TransactionController {
|
|||
|
||||
@GetMapping(path = "/{id}", produces = {MediaType.APPLICATION_JSON_VALUE})
|
||||
@ApiOperation(value = "getTransaction", nickname = "getTransaction", tags = {"Transactions"})
|
||||
public ResponseEntity<TransactionResponse> getTransaction(@PathVariable Long id) {
|
||||
public ResponseEntity<TransactionResponse> getTransaction(@PathVariable String id) {
|
||||
var budgets = userPermissionsRepository.findAllByUser(getCurrentUser(), null)
|
||||
.stream()
|
||||
.map(UserPermission::getBudget)
|
||||
|
@ -153,7 +153,7 @@ public class TransactionController {
|
|||
|
||||
@PutMapping(path = "/{id}", consumes = {MediaType.APPLICATION_JSON_VALUE}, produces = {MediaType.APPLICATION_JSON_VALUE})
|
||||
@ApiOperation(value = "updateTransaction", nickname = "updateTransaction", tags = {"Transactions"})
|
||||
public ResponseEntity<Object> updateTransaction(@PathVariable Long id, @RequestBody UpdateTransactionRequest request) {
|
||||
public ResponseEntity<Object> updateTransaction(@PathVariable String id, @RequestBody UpdateTransactionRequest request) {
|
||||
var transaction = transactionRepository.findById(id).orElse(null);
|
||||
if (transaction == null) return ResponseEntity.notFound().build();
|
||||
var userPermission = userPermissionsRepository.findByUserAndBudget_Id(getCurrentUser(), transaction.getBudget().getId()).orElse(null);
|
||||
|
@ -199,7 +199,7 @@ public class TransactionController {
|
|||
|
||||
@DeleteMapping(path = "/{id}", produces = {MediaType.TEXT_PLAIN_VALUE})
|
||||
@ApiOperation(value = "deleteTransaction", nickname = "deleteTransaction", tags = {"Transactions"})
|
||||
public ResponseEntity<Void> deleteTransaction(@PathVariable Long id) {
|
||||
public ResponseEntity<Void> deleteTransaction(@PathVariable String id) {
|
||||
var transaction = transactionRepository.findById(id).orElse(null);
|
||||
if (transaction == null) return ResponseEntity.notFound().build();
|
||||
// Check that the transaction belongs to an budget that the user has access to before deleting it
|
||||
|
|
|
@ -11,8 +11,8 @@ import java.util.Date;
|
|||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface TransactionRepository extends PagingAndSortingRepository<Transaction, Long> {
|
||||
Optional<Transaction> findByIdAndBudgetIn(Long id, List<Budget> budgets);
|
||||
public interface TransactionRepository extends PagingAndSortingRepository<Transaction, String> {
|
||||
Optional<Transaction> findByIdAndBudgetIn(String id, List<Budget> budgets);
|
||||
|
||||
List<Transaction> findAllByBudgetInAndCategoryInAndDateGreaterThanAndDateLessThan(
|
||||
List<Budget> budgets,
|
||||
|
@ -28,11 +28,11 @@ public interface TransactionRepository extends PagingAndSortingRepository<Transa
|
|||
nativeQuery = true,
|
||||
value = "SELECT (COALESCE((SELECT SUM(amount) from transaction WHERE Budget_id = :BudgetId AND expense = 0 AND date > :start), 0)) - (COALESCE((SELECT SUM(amount) from transaction WHERE Budget_id = :BudgetId AND expense = 1 AND date > :date), 0));"
|
||||
)
|
||||
Long sumBalanceByBudgetId(Long BudgetId, Date start);
|
||||
Long sumBalanceByBudgetId(String BudgetId, Date start);
|
||||
|
||||
@Query(
|
||||
nativeQuery = true,
|
||||
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(Long categoryId, Date start);
|
||||
Long sumBalanceByCategoryId(String categoryId, Date start);
|
||||
}
|
|
@ -1,25 +1,25 @@
|
|||
package com.wbrawner.budgetserver.transaction;
|
||||
|
||||
class TransactionResponse {
|
||||
private final Long id;
|
||||
private final String id;
|
||||
private final String title;
|
||||
private final String description;
|
||||
private final String date;
|
||||
private final Long amount;
|
||||
private final Boolean expense;
|
||||
private final Long budgetId;
|
||||
private final Long categoryId;
|
||||
private final Long createdBy;
|
||||
private final String budgetId;
|
||||
private final String categoryId;
|
||||
private final String createdBy;
|
||||
|
||||
TransactionResponse(Long id,
|
||||
TransactionResponse(String id,
|
||||
String title,
|
||||
String description,
|
||||
String date,
|
||||
Long amount,
|
||||
Boolean expense,
|
||||
Long budgetId,
|
||||
Long categoryId,
|
||||
Long createdBy) {
|
||||
String budgetId,
|
||||
String categoryId,
|
||||
String createdBy) {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
this.description = description;
|
||||
|
@ -45,7 +45,7 @@ class TransactionResponse {
|
|||
);
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
|
@ -69,15 +69,15 @@ class TransactionResponse {
|
|||
return expense;
|
||||
}
|
||||
|
||||
public Long getBudgetId() {
|
||||
public String getBudgetId() {
|
||||
return budgetId;
|
||||
}
|
||||
|
||||
public Long getCategoryId() {
|
||||
public String getCategoryId() {
|
||||
return categoryId;
|
||||
}
|
||||
|
||||
public Long getCreatedBy() {
|
||||
public String getCreatedBy() {
|
||||
return createdBy;
|
||||
}
|
||||
}
|
|
@ -5,16 +5,16 @@ class UpdateTransactionRequest {
|
|||
private final String description;
|
||||
private final String date;
|
||||
private final Long amount;
|
||||
private final Long categoryId;
|
||||
private final String categoryId;
|
||||
private final Boolean expense;
|
||||
private final Long budgetId;
|
||||
private final Long createdBy;
|
||||
private final String budgetId;
|
||||
private final String createdBy;
|
||||
|
||||
UpdateTransactionRequest() {
|
||||
this(null, null, null, null, null, null, null, null);
|
||||
}
|
||||
|
||||
UpdateTransactionRequest(String title, String description, String date, Long amount, Long categoryId, Boolean expense, Long budgetId, Long createdBy) {
|
||||
UpdateTransactionRequest(String title, String description, String date, Long amount, String categoryId, Boolean expense, String budgetId, String createdBy) {
|
||||
this.title = title;
|
||||
this.description = description;
|
||||
this.date = date;
|
||||
|
@ -41,7 +41,7 @@ class UpdateTransactionRequest {
|
|||
return amount;
|
||||
}
|
||||
|
||||
public Long getCategoryId() {
|
||||
public String getCategoryId() {
|
||||
return categoryId;
|
||||
}
|
||||
|
||||
|
@ -49,11 +49,11 @@ class UpdateTransactionRequest {
|
|||
return expense;
|
||||
}
|
||||
|
||||
public Long getBudgetId() {
|
||||
public String getBudgetId() {
|
||||
return budgetId;
|
||||
}
|
||||
|
||||
public Long getCreatedBy() {
|
||||
public String getCreatedBy() {
|
||||
return createdBy;
|
||||
}
|
||||
}
|
|
@ -9,11 +9,12 @@ import java.util.Collection;
|
|||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static com.wbrawner.budgetserver.Utils.randomId;
|
||||
|
||||
@Entity
|
||||
public class User implements UserDetails {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private final Long id = null;
|
||||
private final String id = randomId();
|
||||
@Transient
|
||||
private final List<GrantedAuthority> authorities = Collections.singletonList(new SimpleGrantedAuthority("USER"));
|
||||
private String username;
|
||||
|
@ -30,7 +31,7 @@ public class User implements UserDetails {
|
|||
this.email = email;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
public String getId() {
|
||||
// This shouldn't ever need to be set manually, only through Hibernate
|
||||
//noinspection ConstantConditions
|
||||
return id;
|
||||
|
|
|
@ -102,7 +102,7 @@ public class UserController {
|
|||
|
||||
@GetMapping(path = "/{id}")
|
||||
@ApiOperation(value = "getUser", nickname = "getUser", tags = {"Users"})
|
||||
ResponseEntity<UserResponse> getUser(@PathVariable Long id) {
|
||||
ResponseEntity<UserResponse> getUser(@PathVariable String id) {
|
||||
var user = userRepository.findById(id).orElse(null);
|
||||
if (user == null) {
|
||||
return ResponseEntity.notFound().build();
|
||||
|
@ -152,7 +152,7 @@ public class UserController {
|
|||
|
||||
@DeleteMapping(path = "/{id}", produces = {MediaType.TEXT_PLAIN_VALUE})
|
||||
@ApiOperation(value = "deleteUser", nickname = "deleteUser", tags = {"Users"})
|
||||
ResponseEntity<Void> deleteUser(@PathVariable Long id) {
|
||||
ResponseEntity<Void> deleteUser(@PathVariable String id) {
|
||||
if (!getCurrentUser().getId().equals(id)) return ResponseEntity.status(403).build();
|
||||
userRepository.deleteById(id);
|
||||
return ResponseEntity.ok().build();
|
||||
|
|
|
@ -5,7 +5,7 @@ import org.springframework.data.repository.PagingAndSortingRepository;
|
|||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface UserRepository extends PagingAndSortingRepository<User, Long> {
|
||||
public interface UserRepository extends PagingAndSortingRepository<User, String> {
|
||||
Optional<User> findByUsername(String username);
|
||||
|
||||
Optional<User> findByUsernameAndPassword(String username, String password);
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package com.wbrawner.budgetserver.user;
|
||||
|
||||
public class UserResponse {
|
||||
private final long id;
|
||||
private final String id;
|
||||
private final String username;
|
||||
private final String email;
|
||||
|
||||
|
@ -9,13 +9,13 @@ public class UserResponse {
|
|||
this(user.getId(), user.getUsername(), user.getEmail());
|
||||
}
|
||||
|
||||
public UserResponse(long id, String username, String email) {
|
||||
public UserResponse(String id, String username, String email) {
|
||||
this.id = id;
|
||||
this.username = username;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue