This commit is contained in:
@@ -4,10 +4,8 @@ import org.springframework.boot.SpringApplication;
|
|||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class AllowancePlannerApplication
|
public class AllowancePlannerApplication {
|
||||||
{
|
static void main(String[] args) {
|
||||||
public static void main(String[] args)
|
|
||||||
{
|
|
||||||
SpringApplication.run(AllowancePlannerApplication.class, args);
|
SpringApplication.run(AllowancePlannerApplication.class, args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,7 @@
|
|||||||
package be.seeseepuff.allowanceplanner.controller;
|
package be.seeseepuff.allowanceplanner.controller;
|
||||||
|
|
||||||
import be.seeseepuff.allowanceplanner.dto.*;
|
import be.seeseepuff.allowanceplanner.dto.*;
|
||||||
import be.seeseepuff.allowanceplanner.service.AllowanceService;
|
import be.seeseepuff.allowanceplanner.service.*;
|
||||||
import be.seeseepuff.allowanceplanner.service.MigrationService;
|
|
||||||
import be.seeseepuff.allowanceplanner.service.TaskService;
|
|
||||||
import be.seeseepuff.allowanceplanner.service.TransferService;
|
|
||||||
import be.seeseepuff.allowanceplanner.service.UserService;
|
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
@@ -16,8 +12,7 @@ import java.util.Optional;
|
|||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api")
|
@RequestMapping("/api")
|
||||||
@CrossOrigin(origins = "*")
|
@CrossOrigin(origins = "*")
|
||||||
public class ApiController
|
public class ApiController {
|
||||||
{
|
|
||||||
private final UserService userService;
|
private final UserService userService;
|
||||||
private final AllowanceService allowanceService;
|
private final AllowanceService allowanceService;
|
||||||
private final TaskService taskService;
|
private final TaskService taskService;
|
||||||
@@ -28,8 +23,7 @@ public class ApiController
|
|||||||
AllowanceService allowanceService,
|
AllowanceService allowanceService,
|
||||||
TaskService taskService,
|
TaskService taskService,
|
||||||
TransferService transferService,
|
TransferService transferService,
|
||||||
MigrationService migrationService)
|
MigrationService migrationService) {
|
||||||
{
|
|
||||||
this.userService = userService;
|
this.userService = userService;
|
||||||
this.allowanceService = allowanceService;
|
this.allowanceService = allowanceService;
|
||||||
this.taskService = taskService;
|
this.taskService = taskService;
|
||||||
@@ -40,27 +34,21 @@ public class ApiController
|
|||||||
// ---- Users ----
|
// ---- Users ----
|
||||||
|
|
||||||
@GetMapping("/users")
|
@GetMapping("/users")
|
||||||
public List<UserDto> getUsers()
|
public List<UserDto> getUsers() {
|
||||||
{
|
|
||||||
return userService.getUsers();
|
return userService.getUsers();
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/user/{userId}")
|
@GetMapping("/user/{userId}")
|
||||||
public ResponseEntity<?> getUser(@PathVariable String userId)
|
public ResponseEntity<?> getUser(@PathVariable String userId) {
|
||||||
{
|
|
||||||
int id;
|
int id;
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
id = Integer.parseInt(userId);
|
id = Integer.parseInt(userId);
|
||||||
}
|
} catch (NumberFormatException e) {
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<UserWithAllowanceDto> user = userService.getUser(id);
|
Optional<UserWithAllowanceDto> user = userService.getUser(id);
|
||||||
if (user.isEmpty())
|
if (user.isEmpty()) {
|
||||||
{
|
|
||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("User not found"));
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("User not found"));
|
||||||
}
|
}
|
||||||
return ResponseEntity.ok(user.get());
|
return ResponseEntity.ok(user.get());
|
||||||
@@ -69,25 +57,19 @@ public class ApiController
|
|||||||
// ---- History ----
|
// ---- History ----
|
||||||
|
|
||||||
@PostMapping("/user/{userId}/history")
|
@PostMapping("/user/{userId}/history")
|
||||||
public ResponseEntity<?> postHistory(@PathVariable String userId, @RequestBody PostHistoryRequest request)
|
public ResponseEntity<?> postHistory(@PathVariable String userId, @RequestBody PostHistoryRequest request) {
|
||||||
{
|
|
||||||
int id;
|
int id;
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
id = Integer.parseInt(userId);
|
id = Integer.parseInt(userId);
|
||||||
}
|
} catch (NumberFormatException e) {
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request.description() == null || request.description().isEmpty())
|
if (request.description() == null || request.description().isEmpty()) {
|
||||||
{
|
|
||||||
return ResponseEntity.badRequest().body(new ErrorResponse("Description cannot be empty"));
|
return ResponseEntity.badRequest().body(new ErrorResponse("Description cannot be empty"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!userService.userExists(id))
|
if (!userService.userExists(id)) {
|
||||||
{
|
|
||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("User not found"));
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("User not found"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,15 +78,11 @@ public class ApiController
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/user/{userId}/history")
|
@GetMapping("/user/{userId}/history")
|
||||||
public ResponseEntity<?> getHistory(@PathVariable String userId)
|
public ResponseEntity<?> getHistory(@PathVariable String userId) {
|
||||||
{
|
|
||||||
int id;
|
int id;
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
id = Integer.parseInt(userId);
|
id = Integer.parseInt(userId);
|
||||||
}
|
} catch (NumberFormatException e) {
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,20 +93,15 @@ public class ApiController
|
|||||||
// ---- Allowances ----
|
// ---- Allowances ----
|
||||||
|
|
||||||
@GetMapping("/user/{userId}/allowance")
|
@GetMapping("/user/{userId}/allowance")
|
||||||
public ResponseEntity<?> getUserAllowance(@PathVariable String userId)
|
public ResponseEntity<?> getUserAllowance(@PathVariable String userId) {
|
||||||
{
|
|
||||||
int id;
|
int id;
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
id = Integer.parseInt(userId);
|
id = Integer.parseInt(userId);
|
||||||
}
|
} catch (NumberFormatException e) {
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!userService.userExists(id))
|
if (!userService.userExists(id)) {
|
||||||
{
|
|
||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("User not found"));
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("User not found"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,25 +110,19 @@ public class ApiController
|
|||||||
|
|
||||||
@PostMapping("/user/{userId}/allowance")
|
@PostMapping("/user/{userId}/allowance")
|
||||||
public ResponseEntity<?> createUserAllowance(@PathVariable String userId,
|
public ResponseEntity<?> createUserAllowance(@PathVariable String userId,
|
||||||
@RequestBody CreateAllowanceRequest request)
|
@RequestBody CreateAllowanceRequest request) {
|
||||||
{
|
|
||||||
int id;
|
int id;
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
id = Integer.parseInt(userId);
|
id = Integer.parseInt(userId);
|
||||||
}
|
} catch (NumberFormatException e) {
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request.name() == null || request.name().isEmpty())
|
if (request.name() == null || request.name().isEmpty()) {
|
||||||
{
|
|
||||||
return ResponseEntity.badRequest().body(new ErrorResponse("Allowance name cannot be empty"));
|
return ResponseEntity.badRequest().body(new ErrorResponse("Allowance name cannot be empty"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!userService.userExists(id))
|
if (!userService.userExists(id)) {
|
||||||
{
|
|
||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("User not found"));
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("User not found"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,20 +132,15 @@ public class ApiController
|
|||||||
|
|
||||||
@PutMapping("/user/{userId}/allowance")
|
@PutMapping("/user/{userId}/allowance")
|
||||||
public ResponseEntity<?> bulkPutUserAllowance(@PathVariable String userId,
|
public ResponseEntity<?> bulkPutUserAllowance(@PathVariable String userId,
|
||||||
@RequestBody List<BulkUpdateAllowanceRequest> requests)
|
@RequestBody List<BulkUpdateAllowanceRequest> requests) {
|
||||||
{
|
|
||||||
int id;
|
int id;
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
id = Integer.parseInt(userId);
|
id = Integer.parseInt(userId);
|
||||||
}
|
} catch (NumberFormatException e) {
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!userService.userExists(id))
|
if (!userService.userExists(id)) {
|
||||||
{
|
|
||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("User not found"));
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("User not found"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,77 +149,58 @@ public class ApiController
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/user/{userId}/allowance/{allowanceId}")
|
@GetMapping("/user/{userId}/allowance/{allowanceId}")
|
||||||
public ResponseEntity<?> getUserAllowanceById(@PathVariable String userId, @PathVariable String allowanceId)
|
public ResponseEntity<?> getUserAllowanceById(@PathVariable String userId, @PathVariable String allowanceId) {
|
||||||
{
|
|
||||||
int uid;
|
int uid;
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
uid = Integer.parseInt(userId);
|
uid = Integer.parseInt(userId);
|
||||||
}
|
} catch (NumberFormatException e) {
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
||||||
}
|
}
|
||||||
|
|
||||||
int aid;
|
int aid;
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
aid = Integer.parseInt(allowanceId);
|
aid = Integer.parseInt(allowanceId);
|
||||||
}
|
} catch (NumberFormatException e) {
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid allowance ID"));
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid allowance ID"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!userService.userExists(uid))
|
if (!userService.userExists(uid)) {
|
||||||
{
|
|
||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("User not found"));
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("User not found"));
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<AllowanceDto> allowance = allowanceService.getUserAllowanceById(uid, aid);
|
Optional<AllowanceDto> allowance = allowanceService.getUserAllowanceById(uid, aid);
|
||||||
if (allowance.isEmpty())
|
if (allowance.isEmpty()) {
|
||||||
{
|
|
||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("Allowance not found"));
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("Allowance not found"));
|
||||||
}
|
}
|
||||||
return ResponseEntity.ok(allowance.get());
|
return ResponseEntity.ok(allowance.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/user/{userId}/allowance/{allowanceId}")
|
@DeleteMapping("/user/{userId}/allowance/{allowanceId}")
|
||||||
public ResponseEntity<?> deleteUserAllowance(@PathVariable String userId, @PathVariable String allowanceId)
|
public ResponseEntity<?> deleteUserAllowance(@PathVariable String userId, @PathVariable String allowanceId) {
|
||||||
{
|
|
||||||
int uid;
|
int uid;
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
uid = Integer.parseInt(userId);
|
uid = Integer.parseInt(userId);
|
||||||
}
|
} catch (NumberFormatException e) {
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
||||||
}
|
}
|
||||||
|
|
||||||
int aid;
|
int aid;
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
aid = Integer.parseInt(allowanceId);
|
aid = Integer.parseInt(allowanceId);
|
||||||
}
|
} catch (NumberFormatException e) {
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid allowance ID"));
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid allowance ID"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (aid == 0)
|
if (aid == 0) {
|
||||||
{
|
|
||||||
return ResponseEntity.badRequest().body(new ErrorResponse("Allowance id zero cannot be deleted"));
|
return ResponseEntity.badRequest().body(new ErrorResponse("Allowance id zero cannot be deleted"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!userService.userExists(uid))
|
if (!userService.userExists(uid)) {
|
||||||
{
|
|
||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("User not found"));
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("User not found"));
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean deleted = allowanceService.deleteAllowance(uid, aid);
|
boolean deleted = allowanceService.deleteAllowance(uid, aid);
|
||||||
if (!deleted)
|
if (!deleted) {
|
||||||
{
|
|
||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("History not found"));
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("History not found"));
|
||||||
}
|
}
|
||||||
return ResponseEntity.ok(new MessageResponse("History deleted successfully"));
|
return ResponseEntity.ok(new MessageResponse("History deleted successfully"));
|
||||||
@@ -265,72 +208,54 @@ public class ApiController
|
|||||||
|
|
||||||
@PutMapping("/user/{userId}/allowance/{allowanceId}")
|
@PutMapping("/user/{userId}/allowance/{allowanceId}")
|
||||||
public ResponseEntity<?> putUserAllowance(@PathVariable String userId, @PathVariable String allowanceId,
|
public ResponseEntity<?> putUserAllowance(@PathVariable String userId, @PathVariable String allowanceId,
|
||||||
@RequestBody UpdateAllowanceRequest request)
|
@RequestBody UpdateAllowanceRequest request) {
|
||||||
{
|
|
||||||
int uid;
|
int uid;
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
uid = Integer.parseInt(userId);
|
uid = Integer.parseInt(userId);
|
||||||
}
|
} catch (NumberFormatException e) {
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
||||||
}
|
}
|
||||||
|
|
||||||
int aid;
|
int aid;
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
aid = Integer.parseInt(allowanceId);
|
aid = Integer.parseInt(allowanceId);
|
||||||
}
|
} catch (NumberFormatException e) {
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid allowance ID"));
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid allowance ID"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!userService.userExists(uid))
|
if (!userService.userExists(uid)) {
|
||||||
{
|
|
||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("User not found"));
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("User not found"));
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean updated = allowanceService.updateAllowance(uid, aid, request);
|
boolean updated = allowanceService.updateAllowance(uid, aid, request);
|
||||||
if (!updated)
|
if (!updated) {
|
||||||
{
|
|
||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("Allowance not found"));
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("Allowance not found"));
|
||||||
}
|
}
|
||||||
return ResponseEntity.ok(new MessageResponse("Allowance updated successfully"));
|
return ResponseEntity.ok(new MessageResponse("Allowance updated successfully"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/user/{userId}/allowance/{allowanceId}/complete")
|
@PostMapping("/user/{userId}/allowance/{allowanceId}/complete")
|
||||||
public ResponseEntity<?> completeAllowance(@PathVariable String userId, @PathVariable String allowanceId)
|
public ResponseEntity<?> completeAllowance(@PathVariable String userId, @PathVariable String allowanceId) {
|
||||||
{
|
|
||||||
int uid;
|
int uid;
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
uid = Integer.parseInt(userId);
|
uid = Integer.parseInt(userId);
|
||||||
}
|
} catch (NumberFormatException e) {
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
||||||
}
|
}
|
||||||
|
|
||||||
int aid;
|
int aid;
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
aid = Integer.parseInt(allowanceId);
|
aid = Integer.parseInt(allowanceId);
|
||||||
}
|
} catch (NumberFormatException e) {
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid allowance ID"));
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid allowance ID"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!userService.userExists(uid))
|
if (!userService.userExists(uid)) {
|
||||||
{
|
|
||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("User not found"));
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("User not found"));
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean completed = allowanceService.completeAllowance(uid, aid);
|
boolean completed = allowanceService.completeAllowance(uid, aid);
|
||||||
if (!completed)
|
if (!completed) {
|
||||||
{
|
|
||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("Allowance not found"));
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("Allowance not found"));
|
||||||
}
|
}
|
||||||
return ResponseEntity.ok(new MessageResponse("Allowance completed successfully"));
|
return ResponseEntity.ok(new MessageResponse("Allowance completed successfully"));
|
||||||
@@ -338,36 +263,27 @@ public class ApiController
|
|||||||
|
|
||||||
@PostMapping("/user/{userId}/allowance/{allowanceId}/add")
|
@PostMapping("/user/{userId}/allowance/{allowanceId}/add")
|
||||||
public ResponseEntity<?> addToAllowance(@PathVariable String userId, @PathVariable String allowanceId,
|
public ResponseEntity<?> addToAllowance(@PathVariable String userId, @PathVariable String allowanceId,
|
||||||
@RequestBody AddAllowanceAmountRequest request)
|
@RequestBody AddAllowanceAmountRequest request) {
|
||||||
{
|
|
||||||
int uid;
|
int uid;
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
uid = Integer.parseInt(userId);
|
uid = Integer.parseInt(userId);
|
||||||
}
|
} catch (NumberFormatException e) {
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
||||||
}
|
}
|
||||||
|
|
||||||
int aid;
|
int aid;
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
aid = Integer.parseInt(allowanceId);
|
aid = Integer.parseInt(allowanceId);
|
||||||
}
|
} catch (NumberFormatException e) {
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid allowance ID"));
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid allowance ID"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!userService.userExists(uid))
|
if (!userService.userExists(uid)) {
|
||||||
{
|
|
||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("User not found"));
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("User not found"));
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean result = allowanceService.addAllowanceAmount(uid, aid, request);
|
boolean result = allowanceService.addAllowanceAmount(uid, aid, request);
|
||||||
if (!result)
|
if (!result) {
|
||||||
{
|
|
||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("Allowance not found"));
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("Allowance not found"));
|
||||||
}
|
}
|
||||||
return ResponseEntity.ok(new MessageResponse("Allowance completed successfully"));
|
return ResponseEntity.ok(new MessageResponse("Allowance completed successfully"));
|
||||||
@@ -376,22 +292,17 @@ public class ApiController
|
|||||||
// ---- Tasks ----
|
// ---- Tasks ----
|
||||||
|
|
||||||
@PostMapping("/tasks")
|
@PostMapping("/tasks")
|
||||||
public ResponseEntity<?> createTask(@RequestBody CreateTaskRequest request)
|
public ResponseEntity<?> createTask(@RequestBody CreateTaskRequest request) {
|
||||||
{
|
if (request.name() == null || request.name().isEmpty()) {
|
||||||
if (request.name() == null || request.name().isEmpty())
|
|
||||||
{
|
|
||||||
return ResponseEntity.badRequest().body(new ErrorResponse("Task name cannot be empty"));
|
return ResponseEntity.badRequest().body(new ErrorResponse("Task name cannot be empty"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request.schedule() != null)
|
if (request.schedule() != null) {
|
||||||
{
|
|
||||||
return ResponseEntity.badRequest().body(new ErrorResponse("Schedules are not yet supported"));
|
return ResponseEntity.badRequest().body(new ErrorResponse("Schedules are not yet supported"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request.assigned() != null)
|
if (request.assigned() != null) {
|
||||||
{
|
if (!userService.userExists(request.assigned())) {
|
||||||
if (!userService.userExists(request.assigned()))
|
|
||||||
{
|
|
||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("User not found"));
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("User not found"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -401,48 +312,37 @@ public class ApiController
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/tasks")
|
@GetMapping("/tasks")
|
||||||
public List<TaskDto> getTasks()
|
public List<TaskDto> getTasks() {
|
||||||
{
|
|
||||||
return taskService.getTasks();
|
return taskService.getTasks();
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/task/{taskId}")
|
@GetMapping("/task/{taskId}")
|
||||||
public ResponseEntity<?> getTask(@PathVariable String taskId)
|
public ResponseEntity<?> getTask(@PathVariable String taskId) {
|
||||||
{
|
|
||||||
int id;
|
int id;
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
id = Integer.parseInt(taskId);
|
id = Integer.parseInt(taskId);
|
||||||
}
|
} catch (NumberFormatException e) {
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid task ID"));
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid task ID"));
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<TaskDto> task = taskService.getTask(id);
|
Optional<TaskDto> task = taskService.getTask(id);
|
||||||
if (task.isEmpty())
|
if (task.isEmpty()) {
|
||||||
{
|
|
||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("Task not found"));
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("Task not found"));
|
||||||
}
|
}
|
||||||
return ResponseEntity.ok(task.get());
|
return ResponseEntity.ok(task.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping("/task/{taskId}")
|
@PutMapping("/task/{taskId}")
|
||||||
public ResponseEntity<?> putTask(@PathVariable String taskId, @RequestBody CreateTaskRequest request)
|
public ResponseEntity<?> putTask(@PathVariable String taskId, @RequestBody CreateTaskRequest request) {
|
||||||
{
|
|
||||||
int id;
|
int id;
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
id = Integer.parseInt(taskId);
|
id = Integer.parseInt(taskId);
|
||||||
}
|
} catch (NumberFormatException e) {
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid task ID"));
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid task ID"));
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<TaskDto> existing = taskService.getTask(id);
|
Optional<TaskDto> existing = taskService.getTask(id);
|
||||||
if (existing.isEmpty())
|
if (existing.isEmpty()) {
|
||||||
{
|
|
||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("Task not found"));
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("Task not found"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -451,20 +351,15 @@ public class ApiController
|
|||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/task/{taskId}")
|
@DeleteMapping("/task/{taskId}")
|
||||||
public ResponseEntity<?> deleteTask(@PathVariable String taskId)
|
public ResponseEntity<?> deleteTask(@PathVariable String taskId) {
|
||||||
{
|
|
||||||
int id;
|
int id;
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
id = Integer.parseInt(taskId);
|
id = Integer.parseInt(taskId);
|
||||||
}
|
} catch (NumberFormatException e) {
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid task ID"));
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid task ID"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!taskService.hasTask(id))
|
if (!taskService.hasTask(id)) {
|
||||||
{
|
|
||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("Task not found"));
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("Task not found"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -473,21 +368,16 @@ public class ApiController
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/task/{taskId}/complete")
|
@PostMapping("/task/{taskId}/complete")
|
||||||
public ResponseEntity<?> completeTask(@PathVariable String taskId)
|
public ResponseEntity<?> completeTask(@PathVariable String taskId) {
|
||||||
{
|
|
||||||
int id;
|
int id;
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
id = Integer.parseInt(taskId);
|
id = Integer.parseInt(taskId);
|
||||||
}
|
} catch (NumberFormatException e) {
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid task ID"));
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid task ID"));
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean completed = taskService.completeTask(id);
|
boolean completed = taskService.completeTask(id);
|
||||||
if (!completed)
|
if (!completed) {
|
||||||
{
|
|
||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("Task not found"));
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("Task not found"));
|
||||||
}
|
}
|
||||||
return ResponseEntity.ok(new MessageResponse("Task completed successfully"));
|
return ResponseEntity.ok(new MessageResponse("Task completed successfully"));
|
||||||
@@ -496,23 +386,19 @@ public class ApiController
|
|||||||
// ---- Transfer ----
|
// ---- Transfer ----
|
||||||
|
|
||||||
@PostMapping("/transfer")
|
@PostMapping("/transfer")
|
||||||
public ResponseEntity<?> transfer(@RequestBody TransferRequest request)
|
public ResponseEntity<?> transfer(@RequestBody TransferRequest request) {
|
||||||
{
|
|
||||||
TransferService.TransferResult result = transferService.transfer(request);
|
TransferService.TransferResult result = transferService.transfer(request);
|
||||||
return switch (result.status())
|
return switch (result.status()) {
|
||||||
{
|
|
||||||
case SUCCESS -> ResponseEntity.ok(new MessageResponse(result.message()));
|
case SUCCESS -> ResponseEntity.ok(new MessageResponse(result.message()));
|
||||||
case BAD_REQUEST -> ResponseEntity.badRequest().body(new ErrorResponse(result.message()));
|
case BAD_REQUEST -> ResponseEntity.badRequest().body(new ErrorResponse(result.message()));
|
||||||
case NOT_FOUND ->
|
case NOT_FOUND -> ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse(result.message()));
|
||||||
ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse(result.message()));
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- Migration ----
|
// ---- Migration ----
|
||||||
|
|
||||||
@PostMapping("/import")
|
@PostMapping("/import")
|
||||||
public ResponseEntity<?> importData(@RequestBody MigrationDto data)
|
public ResponseEntity<?> importData(@RequestBody MigrationDto data) {
|
||||||
{
|
|
||||||
migrationService.importData(data);
|
migrationService.importData(data);
|
||||||
return ResponseEntity.ok(new MessageResponse("Import successful"));
|
return ResponseEntity.ok(new MessageResponse("Import successful"));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package be.seeseepuff.allowanceplanner.controller;
|
package be.seeseepuff.allowanceplanner.controller;
|
||||||
|
|
||||||
import be.seeseepuff.allowanceplanner.dto.*;
|
import be.seeseepuff.allowanceplanner.dto.CreateAllowanceRequest;
|
||||||
|
import be.seeseepuff.allowanceplanner.dto.CreateTaskRequest;
|
||||||
import be.seeseepuff.allowanceplanner.service.AllowanceService;
|
import be.seeseepuff.allowanceplanner.service.AllowanceService;
|
||||||
import be.seeseepuff.allowanceplanner.service.TaskService;
|
import be.seeseepuff.allowanceplanner.service.TaskService;
|
||||||
import be.seeseepuff.allowanceplanner.service.UserService;
|
import be.seeseepuff.allowanceplanner.service.UserService;
|
||||||
@@ -14,28 +15,23 @@ import org.springframework.web.bind.annotation.PostMapping;
|
|||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
public class WebController
|
public class WebController {
|
||||||
{
|
|
||||||
private final UserService userService;
|
private final UserService userService;
|
||||||
private final AllowanceService allowanceService;
|
private final AllowanceService allowanceService;
|
||||||
private final TaskService taskService;
|
private final TaskService taskService;
|
||||||
|
|
||||||
public WebController(UserService userService, AllowanceService allowanceService, TaskService taskService)
|
public WebController(UserService userService, AllowanceService allowanceService, TaskService taskService) {
|
||||||
{
|
|
||||||
this.userService = userService;
|
this.userService = userService;
|
||||||
this.allowanceService = allowanceService;
|
this.allowanceService = allowanceService;
|
||||||
this.taskService = taskService;
|
this.taskService = taskService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/")
|
@GetMapping("/")
|
||||||
public String index(HttpServletRequest request, HttpServletResponse response, Model model)
|
public String index(HttpServletRequest request, HttpServletResponse response, Model model) {
|
||||||
{
|
|
||||||
Integer currentUser = getCurrentUser(request, response);
|
Integer currentUser = getCurrentUser(request, response);
|
||||||
if (currentUser == null)
|
if (currentUser == null) {
|
||||||
{
|
|
||||||
model.addAttribute("users", userService.getUsers());
|
model.addAttribute("users", userService.getUsers());
|
||||||
return "index";
|
return "index";
|
||||||
}
|
}
|
||||||
@@ -43,10 +39,8 @@ public class WebController
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/login")
|
@GetMapping("/login")
|
||||||
public String login(@RequestParam(required = false) String user, HttpServletResponse response)
|
public String login(@RequestParam(required = false) String user, HttpServletResponse response) {
|
||||||
{
|
if (user != null && !user.isEmpty()) {
|
||||||
if (user != null && !user.isEmpty())
|
|
||||||
{
|
|
||||||
Cookie cookie = new Cookie("user", user);
|
Cookie cookie = new Cookie("user", user);
|
||||||
cookie.setMaxAge(3600);
|
cookie.setMaxAge(3600);
|
||||||
cookie.setHttpOnly(true);
|
cookie.setHttpOnly(true);
|
||||||
@@ -58,16 +52,13 @@ public class WebController
|
|||||||
@PostMapping("/createTask")
|
@PostMapping("/createTask")
|
||||||
public String createTask(@RequestParam String name, @RequestParam double reward,
|
public String createTask(@RequestParam String name, @RequestParam double reward,
|
||||||
@RequestParam(required = false) String schedule,
|
@RequestParam(required = false) String schedule,
|
||||||
HttpServletRequest request, HttpServletResponse response, Model model)
|
HttpServletRequest request, HttpServletResponse response, Model model) {
|
||||||
{
|
|
||||||
Integer currentUser = getCurrentUser(request, response);
|
Integer currentUser = getCurrentUser(request, response);
|
||||||
if (currentUser == null)
|
if (currentUser == null) {
|
||||||
{
|
|
||||||
return "redirect:/";
|
return "redirect:/";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (name.isEmpty() || reward <= 0)
|
if (name.isEmpty() || reward <= 0) {
|
||||||
{
|
|
||||||
model.addAttribute("error", "Invalid input");
|
model.addAttribute("error", "Invalid input");
|
||||||
return "index";
|
return "index";
|
||||||
}
|
}
|
||||||
@@ -79,8 +70,7 @@ public class WebController
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/completeTask")
|
@GetMapping("/completeTask")
|
||||||
public String completeTask(@RequestParam("task") int taskId)
|
public String completeTask(@RequestParam("task") int taskId) {
|
||||||
{
|
|
||||||
taskService.completeTask(taskId);
|
taskService.completeTask(taskId);
|
||||||
return "redirect:/";
|
return "redirect:/";
|
||||||
}
|
}
|
||||||
@@ -88,16 +78,13 @@ public class WebController
|
|||||||
@PostMapping("/createAllowance")
|
@PostMapping("/createAllowance")
|
||||||
public String createAllowance(@RequestParam String name, @RequestParam double target,
|
public String createAllowance(@RequestParam String name, @RequestParam double target,
|
||||||
@RequestParam double weight,
|
@RequestParam double weight,
|
||||||
HttpServletRequest request, HttpServletResponse response, Model model)
|
HttpServletRequest request, HttpServletResponse response, Model model) {
|
||||||
{
|
|
||||||
Integer currentUser = getCurrentUser(request, response);
|
Integer currentUser = getCurrentUser(request, response);
|
||||||
if (currentUser == null)
|
if (currentUser == null) {
|
||||||
{
|
|
||||||
return "redirect:/";
|
return "redirect:/";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (name.isEmpty() || target <= 0 || weight <= 0)
|
if (name.isEmpty() || target <= 0 || weight <= 0) {
|
||||||
{
|
|
||||||
model.addAttribute("error", "Invalid input");
|
model.addAttribute("error", "Invalid input");
|
||||||
return "index";
|
return "index";
|
||||||
}
|
}
|
||||||
@@ -108,22 +95,18 @@ public class WebController
|
|||||||
|
|
||||||
@GetMapping("/completeAllowance")
|
@GetMapping("/completeAllowance")
|
||||||
public String completeAllowance(@RequestParam("allowance") int allowanceId,
|
public String completeAllowance(@RequestParam("allowance") int allowanceId,
|
||||||
HttpServletRequest request, HttpServletResponse response)
|
HttpServletRequest request, HttpServletResponse response) {
|
||||||
{
|
|
||||||
Integer currentUser = getCurrentUser(request, response);
|
Integer currentUser = getCurrentUser(request, response);
|
||||||
if (currentUser == null)
|
if (currentUser == null) {
|
||||||
{
|
|
||||||
return "redirect:/";
|
return "redirect:/";
|
||||||
}
|
}
|
||||||
allowanceService.completeAllowance(currentUser, allowanceId);
|
allowanceService.completeAllowance(currentUser, allowanceId);
|
||||||
return "redirect:/";
|
return "redirect:/";
|
||||||
}
|
}
|
||||||
|
|
||||||
private Integer getCurrentUser(HttpServletRequest request, HttpServletResponse response)
|
private Integer getCurrentUser(HttpServletRequest request, HttpServletResponse response) {
|
||||||
{
|
|
||||||
Cookie[] cookies = request.getCookies();
|
Cookie[] cookies = request.getCookies();
|
||||||
if (cookies == null)
|
if (cookies == null) {
|
||||||
{
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
String userStr = Arrays.stream(cookies)
|
String userStr = Arrays.stream(cookies)
|
||||||
@@ -131,29 +114,23 @@ public class WebController
|
|||||||
.map(Cookie::getValue)
|
.map(Cookie::getValue)
|
||||||
.findFirst()
|
.findFirst()
|
||||||
.orElse(null);
|
.orElse(null);
|
||||||
if (userStr == null)
|
if (userStr == null) {
|
||||||
{
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
int userId = Integer.parseInt(userStr);
|
int userId = Integer.parseInt(userStr);
|
||||||
if (!userService.userExists(userId))
|
if (!userService.userExists(userId)) {
|
||||||
{
|
|
||||||
unsetUserCookie(response);
|
unsetUserCookie(response);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return userId;
|
return userId;
|
||||||
}
|
} catch (NumberFormatException e) {
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
unsetUserCookie(response);
|
unsetUserCookie(response);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void unsetUserCookie(HttpServletResponse response)
|
private void unsetUserCookie(HttpServletResponse response) {
|
||||||
{
|
|
||||||
Cookie cookie = new Cookie("user", "");
|
Cookie cookie = new Cookie("user", "");
|
||||||
cookie.setMaxAge(0);
|
cookie.setMaxAge(0);
|
||||||
cookie.setPath("/");
|
cookie.setPath("/");
|
||||||
@@ -161,8 +138,7 @@ public class WebController
|
|||||||
response.addCookie(cookie);
|
response.addCookie(cookie);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String renderWithUser(Model model, int currentUser)
|
private String renderWithUser(Model model, int currentUser) {
|
||||||
{
|
|
||||||
model.addAttribute("users", userService.getUsers());
|
model.addAttribute("users", userService.getUsers());
|
||||||
model.addAttribute("currentUser", currentUser);
|
model.addAttribute("currentUser", currentUser);
|
||||||
model.addAttribute("allowances", allowanceService.getUserAllowances(currentUser));
|
model.addAttribute("allowances", allowanceService.getUserAllowances(currentUser));
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
package be.seeseepuff.allowanceplanner.dto;
|
package be.seeseepuff.allowanceplanner.dto;
|
||||||
|
|
||||||
public record AddAllowanceAmountRequest(double amount, String description)
|
public record AddAllowanceAmountRequest(double amount, String description) {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,5 @@ package be.seeseepuff.allowanceplanner.dto;
|
|||||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
|
|
||||||
@JsonInclude(JsonInclude.Include.ALWAYS)
|
@JsonInclude(JsonInclude.Include.ALWAYS)
|
||||||
public record AllowanceDto(int id, String name, double target, double progress, double weight, String colour)
|
public record AllowanceDto(int id, String name, double target, double progress, double weight, String colour) {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
package be.seeseepuff.allowanceplanner.dto;
|
package be.seeseepuff.allowanceplanner.dto;
|
||||||
|
|
||||||
public record BulkUpdateAllowanceRequest(int id, double weight)
|
public record BulkUpdateAllowanceRequest(int id, double weight) {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
package be.seeseepuff.allowanceplanner.dto;
|
package be.seeseepuff.allowanceplanner.dto;
|
||||||
|
|
||||||
public record CreateAllowanceRequest(String name, double target, double weight, String colour)
|
public record CreateAllowanceRequest(String name, double target, double weight, String colour) {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,8 @@
|
|||||||
package be.seeseepuff.allowanceplanner.dto;
|
package be.seeseepuff.allowanceplanner.dto;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
|
||||||
|
|
||||||
public record CreateTaskRequest(
|
public record CreateTaskRequest(
|
||||||
String name,
|
String name,
|
||||||
Double reward,
|
Double reward,
|
||||||
Integer assigned,
|
Integer assigned,
|
||||||
String schedule)
|
String schedule) {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
package be.seeseepuff.allowanceplanner.dto;
|
package be.seeseepuff.allowanceplanner.dto;
|
||||||
|
|
||||||
public record ErrorResponse(String error)
|
public record ErrorResponse(String error) {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,5 @@ package be.seeseepuff.allowanceplanner.dto;
|
|||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
|
||||||
public record HistoryDto(double allowance, Instant timestamp, String description)
|
public record HistoryDto(double allowance, Instant timestamp, String description) {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
package be.seeseepuff.allowanceplanner.dto;
|
package be.seeseepuff.allowanceplanner.dto;
|
||||||
|
|
||||||
public record IdResponse(int id)
|
public record IdResponse(int id) {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
package be.seeseepuff.allowanceplanner.dto;
|
package be.seeseepuff.allowanceplanner.dto;
|
||||||
|
|
||||||
public record MessageResponse(String message)
|
public record MessageResponse(String message) {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,15 +7,18 @@ public record MigrationDto(
|
|||||||
List<MigrationAllowanceDto> allowances,
|
List<MigrationAllowanceDto> allowances,
|
||||||
List<MigrationHistoryDto> history,
|
List<MigrationHistoryDto> history,
|
||||||
List<MigrationTaskDto> tasks
|
List<MigrationTaskDto> tasks
|
||||||
)
|
) {
|
||||||
{
|
public record MigrationUserDto(int id, String name, long balance, double weight) {
|
||||||
public record MigrationUserDto(int id, String name, long balance, double weight) {}
|
}
|
||||||
|
|
||||||
public record MigrationAllowanceDto(int id, int userId, String name, long target, long balance, double weight,
|
public record MigrationAllowanceDto(int id, int userId, String name, long target, long balance, double weight,
|
||||||
Integer colour) {}
|
Integer colour) {
|
||||||
|
}
|
||||||
|
|
||||||
public record MigrationHistoryDto(int id, int userId, long timestamp, long amount, String description) {}
|
public record MigrationHistoryDto(int id, int userId, long timestamp, long amount, String description) {
|
||||||
|
}
|
||||||
|
|
||||||
public record MigrationTaskDto(int id, String name, long reward, Integer assigned, String schedule,
|
public record MigrationTaskDto(int id, String name, long reward, Integer assigned, String schedule,
|
||||||
Long completed, Long nextRun) {}
|
Long completed, Long nextRun) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
package be.seeseepuff.allowanceplanner.dto;
|
package be.seeseepuff.allowanceplanner.dto;
|
||||||
|
|
||||||
public record PostHistoryRequest(double allowance, String description)
|
public record PostHistoryRequest(double allowance, String description) {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,5 @@ package be.seeseepuff.allowanceplanner.dto;
|
|||||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
|
|
||||||
@JsonInclude(JsonInclude.Include.ALWAYS)
|
@JsonInclude(JsonInclude.Include.ALWAYS)
|
||||||
public record TaskDto(int id, String name, double reward, Integer assigned, String schedule)
|
public record TaskDto(int id, String name, double reward, Integer assigned, String schedule) {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
package be.seeseepuff.allowanceplanner.dto;
|
package be.seeseepuff.allowanceplanner.dto;
|
||||||
|
|
||||||
public record TransferRequest(int from, int to, double amount)
|
public record TransferRequest(int from, int to, double amount) {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
package be.seeseepuff.allowanceplanner.dto;
|
package be.seeseepuff.allowanceplanner.dto;
|
||||||
|
|
||||||
public record UpdateAllowanceRequest(String name, double target, double weight, String colour)
|
public record UpdateAllowanceRequest(String name, double target, double weight, String colour) {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
package be.seeseepuff.allowanceplanner.dto;
|
package be.seeseepuff.allowanceplanner.dto;
|
||||||
|
|
||||||
public record UserDto(int id, String name)
|
public record UserDto(int id, String name) {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
package be.seeseepuff.allowanceplanner.dto;
|
package be.seeseepuff.allowanceplanner.dto;
|
||||||
|
|
||||||
public record UserWithAllowanceDto(int id, String name, double allowance)
|
public record UserWithAllowanceDto(int id, String name, double allowance) {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,8 +4,7 @@ import jakarta.persistence.*;
|
|||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "allowances")
|
@Table(name = "allowances")
|
||||||
public class Allowance
|
public class Allowance {
|
||||||
{
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private int id;
|
private int id;
|
||||||
@@ -27,73 +26,59 @@ public class Allowance
|
|||||||
|
|
||||||
private Integer colour;
|
private Integer colour;
|
||||||
|
|
||||||
public int getId()
|
public int getId() {
|
||||||
{
|
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setId(int id)
|
public void setId(int id) {
|
||||||
{
|
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getUserId()
|
public int getUserId() {
|
||||||
{
|
|
||||||
return userId;
|
return userId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUserId(int userId)
|
public void setUserId(int userId) {
|
||||||
{
|
|
||||||
this.userId = userId;
|
this.userId = userId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName()
|
public String getName() {
|
||||||
{
|
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setName(String name)
|
public void setName(String name) {
|
||||||
{
|
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getTarget()
|
public long getTarget() {
|
||||||
{
|
|
||||||
return target;
|
return target;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setTarget(long target)
|
public void setTarget(long target) {
|
||||||
{
|
|
||||||
this.target = target;
|
this.target = target;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getBalance()
|
public long getBalance() {
|
||||||
{
|
|
||||||
return balance;
|
return balance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setBalance(long balance)
|
public void setBalance(long balance) {
|
||||||
{
|
|
||||||
this.balance = balance;
|
this.balance = balance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getWeight()
|
public double getWeight() {
|
||||||
{
|
|
||||||
return weight;
|
return weight;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setWeight(double weight)
|
public void setWeight(double weight) {
|
||||||
{
|
|
||||||
this.weight = weight;
|
this.weight = weight;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Integer getColour()
|
public Integer getColour() {
|
||||||
{
|
|
||||||
return colour;
|
return colour;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setColour(Integer colour)
|
public void setColour(Integer colour) {
|
||||||
{
|
|
||||||
this.colour = colour;
|
this.colour = colour;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,8 +4,7 @@ import jakarta.persistence.*;
|
|||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "history")
|
@Table(name = "history")
|
||||||
public class History
|
public class History {
|
||||||
{
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private int id;
|
private int id;
|
||||||
@@ -21,53 +20,43 @@ public class History
|
|||||||
|
|
||||||
private String description;
|
private String description;
|
||||||
|
|
||||||
public int getId()
|
public int getId() {
|
||||||
{
|
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setId(int id)
|
public void setId(int id) {
|
||||||
{
|
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getUserId()
|
public int getUserId() {
|
||||||
{
|
|
||||||
return userId;
|
return userId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUserId(int userId)
|
public void setUserId(int userId) {
|
||||||
{
|
|
||||||
this.userId = userId;
|
this.userId = userId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getTimestamp()
|
public long getTimestamp() {
|
||||||
{
|
|
||||||
return timestamp;
|
return timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setTimestamp(long timestamp)
|
public void setTimestamp(long timestamp) {
|
||||||
{
|
|
||||||
this.timestamp = timestamp;
|
this.timestamp = timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getAmount()
|
public long getAmount() {
|
||||||
{
|
|
||||||
return amount;
|
return amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAmount(long amount)
|
public void setAmount(long amount) {
|
||||||
{
|
|
||||||
this.amount = amount;
|
this.amount = amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDescription()
|
public String getDescription() {
|
||||||
{
|
|
||||||
return description;
|
return description;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDescription(String description)
|
public void setDescription(String description) {
|
||||||
{
|
|
||||||
this.description = description;
|
this.description = description;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,8 +4,7 @@ import jakarta.persistence.*;
|
|||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "tasks")
|
@Table(name = "tasks")
|
||||||
public class Task
|
public class Task {
|
||||||
{
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private int id;
|
private int id;
|
||||||
@@ -25,73 +24,59 @@ public class Task
|
|||||||
@Column(name = "next_run")
|
@Column(name = "next_run")
|
||||||
private Long nextRun;
|
private Long nextRun;
|
||||||
|
|
||||||
public int getId()
|
public int getId() {
|
||||||
{
|
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setId(int id)
|
public void setId(int id) {
|
||||||
{
|
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName()
|
public String getName() {
|
||||||
{
|
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setName(String name)
|
public void setName(String name) {
|
||||||
{
|
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getReward()
|
public long getReward() {
|
||||||
{
|
|
||||||
return reward;
|
return reward;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setReward(long reward)
|
public void setReward(long reward) {
|
||||||
{
|
|
||||||
this.reward = reward;
|
this.reward = reward;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Integer getAssigned()
|
public Integer getAssigned() {
|
||||||
{
|
|
||||||
return assigned;
|
return assigned;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAssigned(Integer assigned)
|
public void setAssigned(Integer assigned) {
|
||||||
{
|
|
||||||
this.assigned = assigned;
|
this.assigned = assigned;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSchedule()
|
public String getSchedule() {
|
||||||
{
|
|
||||||
return schedule;
|
return schedule;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSchedule(String schedule)
|
public void setSchedule(String schedule) {
|
||||||
{
|
|
||||||
this.schedule = schedule;
|
this.schedule = schedule;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Long getCompleted()
|
public Long getCompleted() {
|
||||||
{
|
|
||||||
return completed;
|
return completed;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCompleted(Long completed)
|
public void setCompleted(Long completed) {
|
||||||
{
|
|
||||||
this.completed = completed;
|
this.completed = completed;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Long getNextRun()
|
public Long getNextRun() {
|
||||||
{
|
|
||||||
return nextRun;
|
return nextRun;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setNextRun(Long nextRun)
|
public void setNextRun(Long nextRun) {
|
||||||
{
|
|
||||||
this.nextRun = nextRun;
|
this.nextRun = nextRun;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,8 +4,7 @@ import jakarta.persistence.*;
|
|||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "users")
|
@Table(name = "users")
|
||||||
public class User
|
public class User {
|
||||||
{
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private int id;
|
private int id;
|
||||||
@@ -19,43 +18,35 @@ public class User
|
|||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
private long balance = 0;
|
private long balance = 0;
|
||||||
|
|
||||||
public int getId()
|
public int getId() {
|
||||||
{
|
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setId(int id)
|
public void setId(int id) {
|
||||||
{
|
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName()
|
public String getName() {
|
||||||
{
|
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setName(String name)
|
public void setName(String name) {
|
||||||
{
|
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getWeight()
|
public double getWeight() {
|
||||||
{
|
|
||||||
return weight;
|
return weight;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setWeight(double weight)
|
public void setWeight(double weight) {
|
||||||
{
|
|
||||||
this.weight = weight;
|
this.weight = weight;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getBalance()
|
public long getBalance() {
|
||||||
{
|
|
||||||
return balance;
|
return balance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setBalance(long balance)
|
public void setBalance(long balance) {
|
||||||
{
|
|
||||||
this.balance = balance;
|
this.balance = balance;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,8 +9,7 @@ import java.util.List;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface AllowanceRepository extends JpaRepository<Allowance, Integer>
|
public interface AllowanceRepository extends JpaRepository<Allowance, Integer> {
|
||||||
{
|
|
||||||
List<Allowance> findByUserIdOrderByIdAsc(int userId);
|
List<Allowance> findByUserIdOrderByIdAsc(int userId);
|
||||||
|
|
||||||
Optional<Allowance> findByIdAndUserId(int id, int userId);
|
Optional<Allowance> findByIdAndUserId(int id, int userId);
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import org.springframework.stereotype.Repository;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface HistoryRepository extends JpaRepository<History, Integer>
|
public interface HistoryRepository extends JpaRepository<History, Integer> {
|
||||||
{
|
|
||||||
List<History> findByUserIdOrderByIdDesc(int userId);
|
List<History> findByUserIdOrderByIdDesc(int userId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,8 +8,7 @@ import java.util.List;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface TaskRepository extends JpaRepository<Task, Integer>
|
public interface TaskRepository extends JpaRepository<Task, Integer> {
|
||||||
{
|
|
||||||
List<Task> findByCompletedIsNull();
|
List<Task> findByCompletedIsNull();
|
||||||
|
|
||||||
Optional<Task> findByIdAndCompletedIsNull(int id);
|
Optional<Task> findByIdAndCompletedIsNull(int id);
|
||||||
|
|||||||
@@ -6,8 +6,7 @@ import org.springframework.data.jpa.repository.Query;
|
|||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface UserRepository extends JpaRepository<User, Integer>
|
public interface UserRepository extends JpaRepository<User, Integer> {
|
||||||
{
|
|
||||||
@Query("SELECT COALESCE(SUM(h.amount), 0) FROM History h WHERE h.userId = :userId")
|
@Query("SELECT COALESCE(SUM(h.amount), 0) FROM History h WHERE h.userId = :userId")
|
||||||
long sumHistoryAmount(int userId);
|
long sumHistoryAmount(int userId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,23 +17,20 @@ import java.util.List;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class AllowanceService
|
public class AllowanceService {
|
||||||
{
|
|
||||||
private final AllowanceRepository allowanceRepository;
|
private final AllowanceRepository allowanceRepository;
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
private final HistoryRepository historyRepository;
|
private final HistoryRepository historyRepository;
|
||||||
|
|
||||||
public AllowanceService(AllowanceRepository allowanceRepository,
|
public AllowanceService(AllowanceRepository allowanceRepository,
|
||||||
UserRepository userRepository,
|
UserRepository userRepository,
|
||||||
HistoryRepository historyRepository)
|
HistoryRepository historyRepository) {
|
||||||
{
|
|
||||||
this.allowanceRepository = allowanceRepository;
|
this.allowanceRepository = allowanceRepository;
|
||||||
this.userRepository = userRepository;
|
this.userRepository = userRepository;
|
||||||
this.historyRepository = historyRepository;
|
this.historyRepository = historyRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<AllowanceDto> getUserAllowances(int userId)
|
public List<AllowanceDto> getUserAllowances(int userId) {
|
||||||
{
|
|
||||||
User user = userRepository.findById(userId).orElseThrow();
|
User user = userRepository.findById(userId).orElseThrow();
|
||||||
List<AllowanceDto> result = new ArrayList<>();
|
List<AllowanceDto> result = new ArrayList<>();
|
||||||
|
|
||||||
@@ -41,17 +38,14 @@ public class AllowanceService
|
|||||||
result.add(new AllowanceDto(0, "", 0, user.getBalance() / 100.0, user.getWeight(), ""));
|
result.add(new AllowanceDto(0, "", 0, user.getBalance() / 100.0, user.getWeight(), ""));
|
||||||
|
|
||||||
// Add named allowances
|
// Add named allowances
|
||||||
for (Allowance a : allowanceRepository.findByUserIdOrderByIdAsc(userId))
|
for (Allowance a : allowanceRepository.findByUserIdOrderByIdAsc(userId)) {
|
||||||
{
|
|
||||||
result.add(toDto(a));
|
result.add(toDto(a));
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<AllowanceDto> getUserAllowanceById(int userId, int allowanceId)
|
public Optional<AllowanceDto> getUserAllowanceById(int userId, int allowanceId) {
|
||||||
{
|
if (allowanceId == 0) {
|
||||||
if (allowanceId == 0)
|
|
||||||
{
|
|
||||||
return userRepository.findById(userId)
|
return userRepository.findById(userId)
|
||||||
.map(u -> new AllowanceDto(0, "", 0, u.getBalance() / 100.0, u.getWeight(), ""));
|
.map(u -> new AllowanceDto(0, "", 0, u.getBalance() / 100.0, u.getWeight(), ""));
|
||||||
}
|
}
|
||||||
@@ -60,8 +54,7 @@ public class AllowanceService
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public int createAllowance(int userId, CreateAllowanceRequest request)
|
public int createAllowance(int userId, CreateAllowanceRequest request) {
|
||||||
{
|
|
||||||
int colour = ColourUtil.convertStringToColour(request.colour());
|
int colour = ColourUtil.convertStringToColour(request.colour());
|
||||||
Allowance allowance = new Allowance();
|
Allowance allowance = new Allowance();
|
||||||
allowance.setUserId(userId);
|
allowance.setUserId(userId);
|
||||||
@@ -74,11 +67,9 @@ public class AllowanceService
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public boolean deleteAllowance(int userId, int allowanceId)
|
public boolean deleteAllowance(int userId, int allowanceId) {
|
||||||
{
|
|
||||||
int count = allowanceRepository.countByIdAndUserId(allowanceId, userId);
|
int count = allowanceRepository.countByIdAndUserId(allowanceId, userId);
|
||||||
if (count == 0)
|
if (count == 0) {
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
allowanceRepository.deleteByIdAndUserId(allowanceId, userId);
|
allowanceRepository.deleteByIdAndUserId(allowanceId, userId);
|
||||||
@@ -86,10 +77,8 @@ public class AllowanceService
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public boolean updateAllowance(int userId, int allowanceId, UpdateAllowanceRequest request)
|
public boolean updateAllowance(int userId, int allowanceId, UpdateAllowanceRequest request) {
|
||||||
{
|
if (allowanceId == 0) {
|
||||||
if (allowanceId == 0)
|
|
||||||
{
|
|
||||||
User user = userRepository.findById(userId).orElseThrow();
|
User user = userRepository.findById(userId).orElseThrow();
|
||||||
user.setWeight(request.weight());
|
user.setWeight(request.weight());
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
@@ -97,8 +86,7 @@ public class AllowanceService
|
|||||||
}
|
}
|
||||||
|
|
||||||
Optional<Allowance> opt = allowanceRepository.findByIdAndUserId(allowanceId, userId);
|
Optional<Allowance> opt = allowanceRepository.findByIdAndUserId(allowanceId, userId);
|
||||||
if (opt.isEmpty())
|
if (opt.isEmpty()) {
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,18 +101,13 @@ public class AllowanceService
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void bulkUpdateAllowance(int userId, List<BulkUpdateAllowanceRequest> requests)
|
public void bulkUpdateAllowance(int userId, List<BulkUpdateAllowanceRequest> requests) {
|
||||||
{
|
for (BulkUpdateAllowanceRequest req : requests) {
|
||||||
for (BulkUpdateAllowanceRequest req : requests)
|
if (req.id() == 0) {
|
||||||
{
|
|
||||||
if (req.id() == 0)
|
|
||||||
{
|
|
||||||
User user = userRepository.findById(userId).orElseThrow();
|
User user = userRepository.findById(userId).orElseThrow();
|
||||||
user.setWeight(req.weight());
|
user.setWeight(req.weight());
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
allowanceRepository.findByIdAndUserId(req.id(), userId).ifPresent(a ->
|
allowanceRepository.findByIdAndUserId(req.id(), userId).ifPresent(a ->
|
||||||
{
|
{
|
||||||
a.setWeight(req.weight());
|
a.setWeight(req.weight());
|
||||||
@@ -135,11 +118,9 @@ public class AllowanceService
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public boolean completeAllowance(int userId, int allowanceId)
|
public boolean completeAllowance(int userId, int allowanceId) {
|
||||||
{
|
|
||||||
Optional<Allowance> opt = allowanceRepository.findByIdAndUserId(allowanceId, userId);
|
Optional<Allowance> opt = allowanceRepository.findByIdAndUserId(allowanceId, userId);
|
||||||
if (opt.isEmpty())
|
if (opt.isEmpty()) {
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,8 +143,7 @@ public class AllowanceService
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public boolean addAllowanceAmount(int userId, int allowanceId, AddAllowanceAmountRequest request)
|
public boolean addAllowanceAmount(int userId, int allowanceId, AddAllowanceAmountRequest request) {
|
||||||
{
|
|
||||||
long remainingAmount = Math.round(request.amount() * 100);
|
long remainingAmount = Math.round(request.amount() * 100);
|
||||||
|
|
||||||
// Insert history entry
|
// Insert history entry
|
||||||
@@ -174,65 +154,51 @@ public class AllowanceService
|
|||||||
history.setDescription(request.description());
|
history.setDescription(request.description());
|
||||||
historyRepository.save(history);
|
historyRepository.save(history);
|
||||||
|
|
||||||
if (allowanceId == 0)
|
if (allowanceId == 0) {
|
||||||
{
|
if (remainingAmount < 0) {
|
||||||
if (remainingAmount < 0)
|
|
||||||
{
|
|
||||||
User user = userRepository.findById(userId).orElseThrow();
|
User user = userRepository.findById(userId).orElseThrow();
|
||||||
if (remainingAmount > user.getBalance())
|
if (remainingAmount > user.getBalance()) {
|
||||||
{
|
|
||||||
throw new IllegalArgumentException("cannot remove more than the current balance: " + user.getBalance());
|
throw new IllegalArgumentException("cannot remove more than the current balance: " + user.getBalance());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
User user = userRepository.findById(userId).orElseThrow();
|
User user = userRepository.findById(userId).orElseThrow();
|
||||||
user.setBalance(user.getBalance() + remainingAmount);
|
user.setBalance(user.getBalance() + remainingAmount);
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
}
|
} else if (remainingAmount < 0) {
|
||||||
else if (remainingAmount < 0)
|
|
||||||
{
|
|
||||||
Allowance allowance = allowanceRepository.findByIdAndUserId(allowanceId, userId).orElse(null);
|
Allowance allowance = allowanceRepository.findByIdAndUserId(allowanceId, userId).orElse(null);
|
||||||
if (allowance == null)
|
if (allowance == null) {
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (remainingAmount > allowance.getBalance())
|
if (remainingAmount > allowance.getBalance()) {
|
||||||
{
|
|
||||||
throw new IllegalArgumentException("cannot remove more than the current allowance balance: " + allowance.getBalance());
|
throw new IllegalArgumentException("cannot remove more than the current allowance balance: " + allowance.getBalance());
|
||||||
}
|
}
|
||||||
allowance.setBalance(allowance.getBalance() + remainingAmount);
|
allowance.setBalance(allowance.getBalance() + remainingAmount);
|
||||||
allowanceRepository.save(allowance);
|
allowanceRepository.save(allowance);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
Allowance allowance = allowanceRepository.findByIdAndUserId(allowanceId, userId).orElse(null);
|
Allowance allowance = allowanceRepository.findByIdAndUserId(allowanceId, userId).orElse(null);
|
||||||
if (allowance == null)
|
if (allowance == null) {
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
long toAdd = remainingAmount;
|
long toAdd = remainingAmount;
|
||||||
if (allowance.getBalance() + toAdd > allowance.getTarget())
|
if (allowance.getBalance() + toAdd > allowance.getTarget()) {
|
||||||
{
|
|
||||||
toAdd = allowance.getTarget() - allowance.getBalance();
|
toAdd = allowance.getTarget() - allowance.getBalance();
|
||||||
}
|
}
|
||||||
remainingAmount -= toAdd;
|
remainingAmount -= toAdd;
|
||||||
|
|
||||||
if (toAdd > 0)
|
if (toAdd > 0) {
|
||||||
{
|
|
||||||
allowance.setBalance(allowance.getBalance() + toAdd);
|
allowance.setBalance(allowance.getBalance() + toAdd);
|
||||||
allowanceRepository.save(allowance);
|
allowanceRepository.save(allowance);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (remainingAmount > 0)
|
if (remainingAmount > 0) {
|
||||||
{
|
|
||||||
addDistributedReward(userId, (int) remainingAmount);
|
addDistributedReward(userId, (int) remainingAmount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addDistributedReward(int userId, int reward)
|
public void addDistributedReward(int userId, int reward) {
|
||||||
{
|
|
||||||
User user = userRepository.findById(userId).orElseThrow();
|
User user = userRepository.findById(userId).orElseThrow();
|
||||||
double userWeight = user.getWeight();
|
double userWeight = user.getWeight();
|
||||||
|
|
||||||
@@ -240,14 +206,11 @@ public class AllowanceService
|
|||||||
|
|
||||||
int remainingReward = reward;
|
int remainingReward = reward;
|
||||||
|
|
||||||
if (sumOfWeights > 0)
|
if (sumOfWeights > 0) {
|
||||||
{
|
|
||||||
List<Allowance> allowances = allowanceRepository.findByUserIdWithPositiveWeightOrderByRemainingAsc(userId);
|
List<Allowance> allowances = allowanceRepository.findByUserIdWithPositiveWeightOrderByRemainingAsc(userId);
|
||||||
for (Allowance allowance : allowances)
|
for (Allowance allowance : allowances) {
|
||||||
{
|
|
||||||
int amount = (int) ((allowance.getWeight() / sumOfWeights) * remainingReward);
|
int amount = (int) ((allowance.getWeight() / sumOfWeights) * remainingReward);
|
||||||
if (allowance.getBalance() + amount > allowance.getTarget())
|
if (allowance.getBalance() + amount > allowance.getTarget()) {
|
||||||
{
|
|
||||||
amount = (int) (allowance.getTarget() - allowance.getBalance());
|
amount = (int) (allowance.getTarget() - allowance.getBalance());
|
||||||
}
|
}
|
||||||
sumOfWeights -= allowance.getWeight();
|
sumOfWeights -= allowance.getWeight();
|
||||||
@@ -263,8 +226,7 @@ public class AllowanceService
|
|||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<HistoryDto> getHistory(int userId)
|
public List<HistoryDto> getHistory(int userId) {
|
||||||
{
|
|
||||||
return historyRepository.findByUserIdOrderByIdDesc(userId).stream()
|
return historyRepository.findByUserIdOrderByIdDesc(userId).stream()
|
||||||
.map(h -> new HistoryDto(
|
.map(h -> new HistoryDto(
|
||||||
h.getAmount() / 100.0,
|
h.getAmount() / 100.0,
|
||||||
@@ -274,8 +236,7 @@ public class AllowanceService
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void addHistory(int userId, PostHistoryRequest request)
|
public void addHistory(int userId, PostHistoryRequest request) {
|
||||||
{
|
|
||||||
long amount = Math.round(request.allowance() * 100.0);
|
long amount = Math.round(request.allowance() * 100.0);
|
||||||
History history = new History();
|
History history = new History();
|
||||||
history.setUserId(userId);
|
history.setUserId(userId);
|
||||||
@@ -285,8 +246,7 @@ public class AllowanceService
|
|||||||
historyRepository.save(history);
|
historyRepository.save(history);
|
||||||
}
|
}
|
||||||
|
|
||||||
private AllowanceDto toDto(Allowance a)
|
private AllowanceDto toDto(Allowance a) {
|
||||||
{
|
|
||||||
return new AllowanceDto(
|
return new AllowanceDto(
|
||||||
a.getId(),
|
a.getId(),
|
||||||
a.getName(),
|
a.getName(),
|
||||||
|
|||||||
@@ -10,8 +10,7 @@ import org.springframework.stereotype.Service;
|
|||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class MigrationService
|
public class MigrationService {
|
||||||
{
|
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
private final AllowanceRepository allowanceRepository;
|
private final AllowanceRepository allowanceRepository;
|
||||||
private final HistoryRepository historyRepository;
|
private final HistoryRepository historyRepository;
|
||||||
@@ -22,8 +21,7 @@ public class MigrationService
|
|||||||
AllowanceRepository allowanceRepository,
|
AllowanceRepository allowanceRepository,
|
||||||
HistoryRepository historyRepository,
|
HistoryRepository historyRepository,
|
||||||
TaskRepository taskRepository,
|
TaskRepository taskRepository,
|
||||||
EntityManager entityManager)
|
EntityManager entityManager) {
|
||||||
{
|
|
||||||
this.userRepository = userRepository;
|
this.userRepository = userRepository;
|
||||||
this.allowanceRepository = allowanceRepository;
|
this.allowanceRepository = allowanceRepository;
|
||||||
this.historyRepository = historyRepository;
|
this.historyRepository = historyRepository;
|
||||||
@@ -32,8 +30,7 @@ public class MigrationService
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void importData(MigrationDto data)
|
public void importData(MigrationDto data) {
|
||||||
{
|
|
||||||
// Delete in dependency order
|
// Delete in dependency order
|
||||||
taskRepository.deleteAll();
|
taskRepository.deleteAll();
|
||||||
historyRepository.deleteAll();
|
historyRepository.deleteAll();
|
||||||
@@ -41,8 +38,7 @@ public class MigrationService
|
|||||||
userRepository.deleteAll();
|
userRepository.deleteAll();
|
||||||
|
|
||||||
// Insert users with original IDs using native SQL to bypass auto-increment
|
// Insert users with original IDs using native SQL to bypass auto-increment
|
||||||
for (MigrationDto.MigrationUserDto u : data.users())
|
for (MigrationDto.MigrationUserDto u : data.users()) {
|
||||||
{
|
|
||||||
entityManager.createNativeQuery(
|
entityManager.createNativeQuery(
|
||||||
"INSERT INTO users (id, name, balance, weight) VALUES (:id, :name, :balance, :weight)")
|
"INSERT INTO users (id, name, balance, weight) VALUES (:id, :name, :balance, :weight)")
|
||||||
.setParameter("id", u.id())
|
.setParameter("id", u.id())
|
||||||
@@ -53,8 +49,7 @@ public class MigrationService
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Insert allowances with original IDs
|
// Insert allowances with original IDs
|
||||||
for (MigrationDto.MigrationAllowanceDto a : data.allowances())
|
for (MigrationDto.MigrationAllowanceDto a : data.allowances()) {
|
||||||
{
|
|
||||||
entityManager.createNativeQuery(
|
entityManager.createNativeQuery(
|
||||||
"INSERT INTO allowances (id, user_id, name, target, balance, weight, colour) VALUES (:id, :userId, :name, :target, :balance, :weight, :colour)")
|
"INSERT INTO allowances (id, user_id, name, target, balance, weight, colour) VALUES (:id, :userId, :name, :target, :balance, :weight, :colour)")
|
||||||
.setParameter("id", a.id())
|
.setParameter("id", a.id())
|
||||||
@@ -68,8 +63,7 @@ public class MigrationService
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Insert history with original IDs
|
// Insert history with original IDs
|
||||||
for (MigrationDto.MigrationHistoryDto h : data.history())
|
for (MigrationDto.MigrationHistoryDto h : data.history()) {
|
||||||
{
|
|
||||||
entityManager.createNativeQuery(
|
entityManager.createNativeQuery(
|
||||||
"INSERT INTO history (id, user_id, timestamp, amount, description) VALUES (:id, :userId, :timestamp, :amount, :description)")
|
"INSERT INTO history (id, user_id, timestamp, amount, description) VALUES (:id, :userId, :timestamp, :amount, :description)")
|
||||||
.setParameter("id", h.id())
|
.setParameter("id", h.id())
|
||||||
@@ -81,8 +75,7 @@ public class MigrationService
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Insert tasks with original IDs
|
// Insert tasks with original IDs
|
||||||
for (MigrationDto.MigrationTaskDto t : data.tasks())
|
for (MigrationDto.MigrationTaskDto t : data.tasks()) {
|
||||||
{
|
|
||||||
entityManager.createNativeQuery(
|
entityManager.createNativeQuery(
|
||||||
"INSERT INTO tasks (id, name, reward, assigned, schedule, completed, next_run) VALUES (:id, :name, :reward, :assigned, :schedule, :completed, :nextRun)")
|
"INSERT INTO tasks (id, name, reward, assigned, schedule, completed, next_run) VALUES (:id, :name, :reward, :assigned, :schedule, :completed, :nextRun)")
|
||||||
.setParameter("id", t.id())
|
.setParameter("id", t.id())
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package be.seeseepuff.allowanceplanner.service;
|
package be.seeseepuff.allowanceplanner.service;
|
||||||
|
|
||||||
import be.seeseepuff.allowanceplanner.dto.*;
|
import be.seeseepuff.allowanceplanner.dto.CreateTaskRequest;
|
||||||
|
import be.seeseepuff.allowanceplanner.dto.TaskDto;
|
||||||
import be.seeseepuff.allowanceplanner.entity.History;
|
import be.seeseepuff.allowanceplanner.entity.History;
|
||||||
import be.seeseepuff.allowanceplanner.entity.Task;
|
import be.seeseepuff.allowanceplanner.entity.Task;
|
||||||
import be.seeseepuff.allowanceplanner.entity.User;
|
import be.seeseepuff.allowanceplanner.entity.User;
|
||||||
@@ -15,8 +16,7 @@ import java.util.List;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class TaskService
|
public class TaskService {
|
||||||
{
|
|
||||||
private final TaskRepository taskRepository;
|
private final TaskRepository taskRepository;
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
private final HistoryRepository historyRepository;
|
private final HistoryRepository historyRepository;
|
||||||
@@ -25,8 +25,7 @@ public class TaskService
|
|||||||
public TaskService(TaskRepository taskRepository,
|
public TaskService(TaskRepository taskRepository,
|
||||||
UserRepository userRepository,
|
UserRepository userRepository,
|
||||||
HistoryRepository historyRepository,
|
HistoryRepository historyRepository,
|
||||||
AllowanceService allowanceService)
|
AllowanceService allowanceService) {
|
||||||
{
|
|
||||||
this.taskRepository = taskRepository;
|
this.taskRepository = taskRepository;
|
||||||
this.userRepository = userRepository;
|
this.userRepository = userRepository;
|
||||||
this.historyRepository = historyRepository;
|
this.historyRepository = historyRepository;
|
||||||
@@ -34,8 +33,7 @@ public class TaskService
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public int createTask(CreateTaskRequest request)
|
public int createTask(CreateTaskRequest request) {
|
||||||
{
|
|
||||||
Task task = new Task();
|
Task task = new Task();
|
||||||
task.setName(request.name());
|
task.setName(request.name());
|
||||||
task.setReward(Math.round((request.reward() != null ? request.reward() : 0.0) * 100.0));
|
task.setReward(Math.round((request.reward() != null ? request.reward() : 0.0) * 100.0));
|
||||||
@@ -44,25 +42,21 @@ public class TaskService
|
|||||||
return task.getId();
|
return task.getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<TaskDto> getTasks()
|
public List<TaskDto> getTasks() {
|
||||||
{
|
|
||||||
return taskRepository.findByCompletedIsNull().stream()
|
return taskRepository.findByCompletedIsNull().stream()
|
||||||
.map(this::toDto)
|
.map(this::toDto)
|
||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<TaskDto> getTask(int taskId)
|
public Optional<TaskDto> getTask(int taskId) {
|
||||||
{
|
|
||||||
return taskRepository.findByIdAndCompletedIsNull(taskId)
|
return taskRepository.findByIdAndCompletedIsNull(taskId)
|
||||||
.map(this::toDto);
|
.map(this::toDto);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public boolean updateTask(int taskId, CreateTaskRequest request)
|
public boolean updateTask(int taskId, CreateTaskRequest request) {
|
||||||
{
|
|
||||||
Optional<Task> opt = taskRepository.findByIdAndCompletedIsNull(taskId);
|
Optional<Task> opt = taskRepository.findByIdAndCompletedIsNull(taskId);
|
||||||
if (opt.isEmpty())
|
if (opt.isEmpty()) {
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
Task task = opt.get();
|
Task task = opt.get();
|
||||||
@@ -73,23 +67,19 @@ public class TaskService
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasTask(int taskId)
|
public boolean hasTask(int taskId) {
|
||||||
{
|
|
||||||
return taskRepository.existsById(taskId);
|
return taskRepository.existsById(taskId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void deleteTask(int taskId)
|
public void deleteTask(int taskId) {
|
||||||
{
|
|
||||||
taskRepository.deleteById(taskId);
|
taskRepository.deleteById(taskId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public boolean completeTask(int taskId)
|
public boolean completeTask(int taskId) {
|
||||||
{
|
|
||||||
Optional<Task> opt = taskRepository.findById(taskId);
|
Optional<Task> opt = taskRepository.findById(taskId);
|
||||||
if (opt.isEmpty())
|
if (opt.isEmpty()) {
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,8 +89,7 @@ public class TaskService
|
|||||||
|
|
||||||
// Give reward to all users
|
// Give reward to all users
|
||||||
List<User> users = userRepository.findAll();
|
List<User> users = userRepository.findAll();
|
||||||
for (User user : users)
|
for (User user : users) {
|
||||||
{
|
|
||||||
// Add history entry
|
// Add history entry
|
||||||
History history = new History();
|
History history = new History();
|
||||||
history.setUserId(user.getId());
|
history.setUserId(user.getId());
|
||||||
@@ -120,8 +109,7 @@ public class TaskService
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private TaskDto toDto(Task t)
|
private TaskDto toDto(Task t) {
|
||||||
{
|
|
||||||
return new TaskDto(
|
return new TaskDto(
|
||||||
t.getId(),
|
t.getId(),
|
||||||
t.getName(),
|
t.getName(),
|
||||||
|
|||||||
@@ -9,63 +9,52 @@ import org.springframework.transaction.annotation.Transactional;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class TransferService
|
public class TransferService {
|
||||||
{
|
|
||||||
private final AllowanceRepository allowanceRepository;
|
private final AllowanceRepository allowanceRepository;
|
||||||
|
|
||||||
public TransferService(AllowanceRepository allowanceRepository)
|
public TransferService(AllowanceRepository allowanceRepository) {
|
||||||
{
|
|
||||||
this.allowanceRepository = allowanceRepository;
|
this.allowanceRepository = allowanceRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public TransferResult transfer(TransferRequest request)
|
public TransferResult transfer(TransferRequest request) {
|
||||||
{
|
if (request.from() == request.to()) {
|
||||||
if (request.from() == request.to())
|
|
||||||
{
|
|
||||||
return TransferResult.success();
|
return TransferResult.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
int amountCents = (int) Math.round(request.amount() * 100.0);
|
int amountCents = (int) Math.round(request.amount() * 100.0);
|
||||||
if (amountCents <= 0)
|
if (amountCents <= 0) {
|
||||||
{
|
|
||||||
return TransferResult.badRequest("amount must be positive");
|
return TransferResult.badRequest("amount must be positive");
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<Allowance> fromOpt = allowanceRepository.findById(request.from());
|
Optional<Allowance> fromOpt = allowanceRepository.findById(request.from());
|
||||||
if (fromOpt.isEmpty())
|
if (fromOpt.isEmpty()) {
|
||||||
{
|
|
||||||
return TransferResult.notFound();
|
return TransferResult.notFound();
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<Allowance> toOpt = allowanceRepository.findById(request.to());
|
Optional<Allowance> toOpt = allowanceRepository.findById(request.to());
|
||||||
if (toOpt.isEmpty())
|
if (toOpt.isEmpty()) {
|
||||||
{
|
|
||||||
return TransferResult.notFound();
|
return TransferResult.notFound();
|
||||||
}
|
}
|
||||||
|
|
||||||
Allowance from = fromOpt.get();
|
Allowance from = fromOpt.get();
|
||||||
Allowance to = toOpt.get();
|
Allowance to = toOpt.get();
|
||||||
|
|
||||||
if (from.getUserId() != to.getUserId())
|
if (from.getUserId() != to.getUserId()) {
|
||||||
{
|
|
||||||
return TransferResult.badRequest("Allowances do not belong to the same user");
|
return TransferResult.badRequest("Allowances do not belong to the same user");
|
||||||
}
|
}
|
||||||
|
|
||||||
long remainingTo = to.getTarget() - to.getBalance();
|
long remainingTo = to.getTarget() - to.getBalance();
|
||||||
if (remainingTo <= 0)
|
if (remainingTo <= 0) {
|
||||||
{
|
|
||||||
return TransferResult.badRequest("target already reached");
|
return TransferResult.badRequest("target already reached");
|
||||||
}
|
}
|
||||||
|
|
||||||
int transfer = amountCents;
|
int transfer = amountCents;
|
||||||
if (transfer > remainingTo)
|
if (transfer > remainingTo) {
|
||||||
{
|
|
||||||
transfer = (int) remainingTo;
|
transfer = (int) remainingTo;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (from.getBalance() < transfer)
|
if (from.getBalance() < transfer) {
|
||||||
{
|
|
||||||
return TransferResult.badRequest("Insufficient funds in source allowance");
|
return TransferResult.badRequest("Insufficient funds in source allowance");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,26 +66,21 @@ public class TransferService
|
|||||||
return TransferResult.success();
|
return TransferResult.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
public record TransferResult(Status status, String message)
|
public record TransferResult(Status status, String message) {
|
||||||
{
|
public static TransferResult success() {
|
||||||
public enum Status
|
|
||||||
{
|
|
||||||
SUCCESS, BAD_REQUEST, NOT_FOUND
|
|
||||||
}
|
|
||||||
|
|
||||||
public static TransferResult success()
|
|
||||||
{
|
|
||||||
return new TransferResult(Status.SUCCESS, "Transfer successful");
|
return new TransferResult(Status.SUCCESS, "Transfer successful");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TransferResult badRequest(String message)
|
public static TransferResult badRequest(String message) {
|
||||||
{
|
|
||||||
return new TransferResult(Status.BAD_REQUEST, message);
|
return new TransferResult(Status.BAD_REQUEST, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TransferResult notFound()
|
public static TransferResult notFound() {
|
||||||
{
|
|
||||||
return new TransferResult(Status.NOT_FOUND, "Allowance not found");
|
return new TransferResult(Status.NOT_FOUND, "Allowance not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum Status {
|
||||||
|
SUCCESS, BAD_REQUEST, NOT_FOUND
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package be.seeseepuff.allowanceplanner.service;
|
package be.seeseepuff.allowanceplanner.service;
|
||||||
|
|
||||||
import be.seeseepuff.allowanceplanner.dto.*;
|
import be.seeseepuff.allowanceplanner.dto.UserDto;
|
||||||
import be.seeseepuff.allowanceplanner.entity.User;
|
import be.seeseepuff.allowanceplanner.dto.UserWithAllowanceDto;
|
||||||
import be.seeseepuff.allowanceplanner.repository.UserRepository;
|
import be.seeseepuff.allowanceplanner.repository.UserRepository;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@@ -9,24 +9,20 @@ import java.util.List;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class UserService
|
public class UserService {
|
||||||
{
|
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
|
|
||||||
public UserService(UserRepository userRepository)
|
public UserService(UserRepository userRepository) {
|
||||||
{
|
|
||||||
this.userRepository = userRepository;
|
this.userRepository = userRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<UserDto> getUsers()
|
public List<UserDto> getUsers() {
|
||||||
{
|
|
||||||
return userRepository.findAll().stream()
|
return userRepository.findAll().stream()
|
||||||
.map(u -> new UserDto(u.getId(), u.getName()))
|
.map(u -> new UserDto(u.getId(), u.getName()))
|
||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<UserWithAllowanceDto> getUser(int userId)
|
public Optional<UserWithAllowanceDto> getUser(int userId) {
|
||||||
{
|
|
||||||
return userRepository.findById(userId)
|
return userRepository.findById(userId)
|
||||||
.map(u ->
|
.map(u ->
|
||||||
{
|
{
|
||||||
@@ -35,8 +31,7 @@ public class UserService
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean userExists(int userId)
|
public boolean userExists(int userId) {
|
||||||
{
|
|
||||||
return userRepository.existsById(userId);
|
return userRepository.existsById(userId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,28 +1,21 @@
|
|||||||
package be.seeseepuff.allowanceplanner.util;
|
package be.seeseepuff.allowanceplanner.util;
|
||||||
|
|
||||||
public class ColourUtil
|
public class ColourUtil {
|
||||||
{
|
private ColourUtil() {
|
||||||
private ColourUtil()
|
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int convertStringToColour(String colourStr)
|
public static int convertStringToColour(String colourStr) {
|
||||||
{
|
if (colourStr == null || colourStr.isEmpty()) {
|
||||||
if (colourStr == null || colourStr.isEmpty())
|
|
||||||
{
|
|
||||||
return 0xFF0000; // Default colour
|
return 0xFF0000; // Default colour
|
||||||
}
|
}
|
||||||
if (colourStr.charAt(0) == '#')
|
if (colourStr.charAt(0) == '#') {
|
||||||
{
|
|
||||||
colourStr = colourStr.substring(1);
|
colourStr = colourStr.substring(1);
|
||||||
}
|
}
|
||||||
if (colourStr.length() != 6 && colourStr.length() != 3)
|
if (colourStr.length() != 6 && colourStr.length() != 3) {
|
||||||
{
|
|
||||||
throw new IllegalArgumentException("colour must be a valid hex string");
|
throw new IllegalArgumentException("colour must be a valid hex string");
|
||||||
}
|
}
|
||||||
int colour = Integer.parseInt(colourStr, 16);
|
int colour = Integer.parseInt(colourStr, 16);
|
||||||
if (colourStr.length() == 3)
|
if (colourStr.length() == 3) {
|
||||||
{
|
|
||||||
int r = (colour & 0xF00) >> 8;
|
int r = (colour & 0xF00) >> 8;
|
||||||
int g = (colour & 0x0F0) >> 4;
|
int g = (colour & 0x0F0) >> 4;
|
||||||
int b = (colour & 0x00F);
|
int b = (colour & 0x00F);
|
||||||
@@ -31,10 +24,8 @@ public class ColourUtil
|
|||||||
return colour;
|
return colour;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String convertColourToString(Integer colour)
|
public static String convertColourToString(Integer colour) {
|
||||||
{
|
if (colour == null) {
|
||||||
if (colour == null)
|
|
||||||
{
|
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
return String.format("#%06X", colour);
|
return String.format("#%06X", colour);
|
||||||
|
|||||||
@@ -1,12 +1,8 @@
|
|||||||
spring.application.name=allowance-planner
|
spring.application.name=allowance-planner
|
||||||
|
|
||||||
spring.datasource.url=jdbc:postgresql://localhost:5432/allowance_planner
|
spring.datasource.url=jdbc:postgresql://localhost:5432/allowance_planner
|
||||||
spring.datasource.username=postgres
|
spring.datasource.username=postgres
|
||||||
spring.datasource.password=postgres
|
spring.datasource.password=postgres
|
||||||
|
|
||||||
spring.jpa.hibernate.ddl-auto=validate
|
spring.jpa.hibernate.ddl-auto=validate
|
||||||
spring.jpa.open-in-view=false
|
spring.jpa.open-in-view=false
|
||||||
|
|
||||||
spring.flyway.enabled=true
|
spring.flyway.enabled=true
|
||||||
|
|
||||||
server.port=8080
|
server.port=8080
|
||||||
|
|||||||
@@ -20,8 +20,8 @@
|
|||||||
<h2>Users</h2>
|
<h2>Users</h2>
|
||||||
<span th:each="user : ${users}">
|
<span th:each="user : ${users}">
|
||||||
<strong th:if="${currentUser != null and currentUser == user.id()}" th:text="${user.name()}"></strong>
|
<strong th:if="${currentUser != null and currentUser == user.id()}" th:text="${user.name()}"></strong>
|
||||||
<a th:unless="${currentUser != null and currentUser == user.id()}"
|
<a th:href="@{/login(user=${user.id()})}"
|
||||||
th:href="@{/login(user=${user.id()})}" th:text="${user.name()}"></a>
|
th:text="${user.name()}" th:unless="${currentUser != null and currentUser == user.id()}"></a>
|
||||||
</span>
|
</span>
|
||||||
|
|
||||||
<div th:if="${currentUser != null and currentUser > 0}">
|
<div th:if="${currentUser != null and currentUser > 0}">
|
||||||
@@ -39,10 +39,10 @@
|
|||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
<tr>
|
<tr>
|
||||||
<td><label><input type="text" name="name" placeholder="Name"/></label></td>
|
<td><label><input name="name" placeholder="Name" type="text"/></label></td>
|
||||||
<td></td>
|
<td></td>
|
||||||
<td><label><input type="number" name="target" placeholder="Target"/></label></td>
|
<td><label><input name="target" placeholder="Target" type="number"/></label></td>
|
||||||
<td><label><input type="number" name="weight" placeholder="Weight"/></label></td>
|
<td><label><input name="weight" placeholder="Weight" type="number"/></label></td>
|
||||||
<td><input type="submit" value="Create"/></td>
|
<td><input type="submit" value="Create"/></td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr th:each="allowance : ${allowances}">
|
<tr th:each="allowance : ${allowances}">
|
||||||
@@ -66,7 +66,7 @@
|
|||||||
</form>
|
</form>
|
||||||
|
|
||||||
<h2>Tasks</h2>
|
<h2>Tasks</h2>
|
||||||
<form method="post" action="/createTask">
|
<form action="/createTask" method="post">
|
||||||
<table border="1">
|
<table border="1">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
@@ -91,10 +91,10 @@
|
|||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td><label><input type="text" name="name" placeholder="Name"/></label></td>
|
<td><label><input name="name" placeholder="Name" type="text"/></label></td>
|
||||||
<td></td>
|
<td></td>
|
||||||
<td><label><input type="number" name="reward" placeholder="Reward"/></label></td>
|
<td><label><input name="reward" placeholder="Reward" type="number"/></label></td>
|
||||||
<td><label><input type="text" name="schedule" placeholder="Schedule"/></label></td>
|
<td><label><input name="schedule" placeholder="Schedule" type="text"/></label></td>
|
||||||
<td><input type="submit" value="Create"/></td>
|
<td><input type="submit" value="Create"/></td>
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
|
|||||||
@@ -2,18 +2,17 @@ package be.seeseepuff.allowanceplanner;
|
|||||||
|
|
||||||
import io.restassured.RestAssured;
|
import io.restassured.RestAssured;
|
||||||
import io.restassured.http.ContentType;
|
import io.restassured.http.ContentType;
|
||||||
|
import org.flywaydb.core.Flyway;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||||
import org.springframework.test.context.DynamicPropertyRegistry;
|
import org.springframework.test.context.DynamicPropertyRegistry;
|
||||||
import org.springframework.test.context.DynamicPropertySource;
|
import org.springframework.test.context.DynamicPropertySource;
|
||||||
import org.testcontainers.containers.PostgreSQLContainer;
|
|
||||||
import org.testcontainers.junit.jupiter.Container;
|
import org.testcontainers.junit.jupiter.Container;
|
||||||
import org.testcontainers.junit.jupiter.Testcontainers;
|
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||||
|
import org.testcontainers.postgresql.PostgreSQLContainer;
|
||||||
import org.flywaydb.core.Flyway;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -24,12 +23,11 @@ import static org.hamcrest.Matchers.*;
|
|||||||
|
|
||||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||||
@Testcontainers
|
@Testcontainers
|
||||||
class ApiTest
|
class ApiTest {
|
||||||
{
|
|
||||||
private static final String TEST_HISTORY_NAME = "Test History";
|
private static final String TEST_HISTORY_NAME = "Test History";
|
||||||
|
|
||||||
@Container
|
@Container
|
||||||
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:17")
|
static PostgreSQLContainer postgres = new PostgreSQLContainer("postgres:18")
|
||||||
.withDatabaseName("allowance_planner_test")
|
.withDatabaseName("allowance_planner_test")
|
||||||
.withUsername("test")
|
.withUsername("test")
|
||||||
.withPassword("test");
|
.withPassword("test");
|
||||||
@@ -41,8 +39,7 @@ class ApiTest
|
|||||||
Flyway flyway;
|
Flyway flyway;
|
||||||
|
|
||||||
@DynamicPropertySource
|
@DynamicPropertySource
|
||||||
static void configureProperties(DynamicPropertyRegistry registry)
|
static void configureProperties(DynamicPropertyRegistry registry) {
|
||||||
{
|
|
||||||
registry.add("spring.datasource.url", postgres::getJdbcUrl);
|
registry.add("spring.datasource.url", postgres::getJdbcUrl);
|
||||||
registry.add("spring.datasource.username", postgres::getUsername);
|
registry.add("spring.datasource.username", postgres::getUsername);
|
||||||
registry.add("spring.datasource.password", postgres::getPassword);
|
registry.add("spring.datasource.password", postgres::getPassword);
|
||||||
@@ -50,8 +47,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp()
|
void setUp() {
|
||||||
{
|
|
||||||
RestAssured.port = port;
|
RestAssured.port = port;
|
||||||
RestAssured.basePath = "/api";
|
RestAssured.basePath = "/api";
|
||||||
RestAssured.config = RestAssured.config()
|
RestAssured.config = RestAssured.config()
|
||||||
@@ -66,8 +62,7 @@ class ApiTest
|
|||||||
// ---- User Tests ----
|
// ---- User Tests ----
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getUsers()
|
void getUsers() {
|
||||||
{
|
|
||||||
given()
|
given()
|
||||||
.when()
|
.when()
|
||||||
.get("/users")
|
.get("/users")
|
||||||
@@ -79,8 +74,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getUser()
|
void getUser() {
|
||||||
{
|
|
||||||
given()
|
given()
|
||||||
.when()
|
.when()
|
||||||
.get("/user/1")
|
.get("/user/1")
|
||||||
@@ -92,8 +86,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getUserUnknown()
|
void getUserUnknown() {
|
||||||
{
|
|
||||||
given()
|
given()
|
||||||
.when()
|
.when()
|
||||||
.get("/user/999")
|
.get("/user/999")
|
||||||
@@ -102,8 +95,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getUserBadId()
|
void getUserBadId() {
|
||||||
{
|
|
||||||
given()
|
given()
|
||||||
.when()
|
.when()
|
||||||
.get("/user/bad-id")
|
.get("/user/bad-id")
|
||||||
@@ -114,8 +106,7 @@ class ApiTest
|
|||||||
// ---- Allowance Tests ----
|
// ---- Allowance Tests ----
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getUserAllowanceWhenNoAllowancePresent()
|
void getUserAllowanceWhenNoAllowancePresent() {
|
||||||
{
|
|
||||||
given()
|
given()
|
||||||
.when()
|
.when()
|
||||||
.get("/user/1/allowance")
|
.get("/user/1/allowance")
|
||||||
@@ -126,8 +117,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getUserAllowance()
|
void getUserAllowance() {
|
||||||
{
|
|
||||||
createAllowance(1, TEST_HISTORY_NAME, 5000, 10);
|
createAllowance(1, TEST_HISTORY_NAME, 5000, 10);
|
||||||
|
|
||||||
given()
|
given()
|
||||||
@@ -144,8 +134,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getUserAllowanceNoUser()
|
void getUserAllowanceNoUser() {
|
||||||
{
|
|
||||||
given()
|
given()
|
||||||
.when()
|
.when()
|
||||||
.get("/user/999/allowance")
|
.get("/user/999/allowance")
|
||||||
@@ -154,8 +143,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getUserAllowanceBadId()
|
void getUserAllowanceBadId() {
|
||||||
{
|
|
||||||
given()
|
given()
|
||||||
.when()
|
.when()
|
||||||
.get("/user/bad-id/allowance")
|
.get("/user/bad-id/allowance")
|
||||||
@@ -164,8 +152,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void createUserAllowance()
|
void createUserAllowance() {
|
||||||
{
|
|
||||||
int allowanceId = createAllowance(1, TEST_HISTORY_NAME, 5000, 10);
|
int allowanceId = createAllowance(1, TEST_HISTORY_NAME, 5000, 10);
|
||||||
|
|
||||||
given()
|
given()
|
||||||
@@ -182,8 +169,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void createUserAllowanceNoUser()
|
void createUserAllowanceNoUser() {
|
||||||
{
|
|
||||||
given()
|
given()
|
||||||
.contentType(ContentType.JSON)
|
.contentType(ContentType.JSON)
|
||||||
.body(Map.of("name", TEST_HISTORY_NAME, "target", 5000, "weight", 10))
|
.body(Map.of("name", TEST_HISTORY_NAME, "target", 5000, "weight", 10))
|
||||||
@@ -194,8 +180,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void createUserAllowanceInvalidInput()
|
void createUserAllowanceInvalidInput() {
|
||||||
{
|
|
||||||
// Empty name
|
// Empty name
|
||||||
given()
|
given()
|
||||||
.contentType(ContentType.JSON)
|
.contentType(ContentType.JSON)
|
||||||
@@ -216,8 +201,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void createUserAllowanceBadId()
|
void createUserAllowanceBadId() {
|
||||||
{
|
|
||||||
given()
|
given()
|
||||||
.contentType(ContentType.JSON)
|
.contentType(ContentType.JSON)
|
||||||
.body(Map.of("name", TEST_HISTORY_NAME, "target", 5000, "weight", 10))
|
.body(Map.of("name", TEST_HISTORY_NAME, "target", 5000, "weight", 10))
|
||||||
@@ -228,8 +212,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void deleteUserAllowance()
|
void deleteUserAllowance() {
|
||||||
{
|
|
||||||
int allowanceId = createAllowance(1, TEST_HISTORY_NAME, 1000, 5);
|
int allowanceId = createAllowance(1, TEST_HISTORY_NAME, 1000, 5);
|
||||||
|
|
||||||
given()
|
given()
|
||||||
@@ -248,8 +231,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void deleteUserRestAllowance()
|
void deleteUserRestAllowance() {
|
||||||
{
|
|
||||||
given()
|
given()
|
||||||
.when()
|
.when()
|
||||||
.delete("/user/1/allowance/0")
|
.delete("/user/1/allowance/0")
|
||||||
@@ -258,8 +240,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void deleteUserAllowanceNotFound()
|
void deleteUserAllowanceNotFound() {
|
||||||
{
|
|
||||||
given()
|
given()
|
||||||
.when()
|
.when()
|
||||||
.delete("/user/1/allowance/999")
|
.delete("/user/1/allowance/999")
|
||||||
@@ -269,8 +250,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void deleteUserAllowanceInvalidId()
|
void deleteUserAllowanceInvalidId() {
|
||||||
{
|
|
||||||
given()
|
given()
|
||||||
.when()
|
.when()
|
||||||
.delete("/user/1/allowance/invalid-id")
|
.delete("/user/1/allowance/invalid-id")
|
||||||
@@ -282,8 +262,7 @@ class ApiTest
|
|||||||
// ---- Task Tests ----
|
// ---- Task Tests ----
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void createTask()
|
void createTask() {
|
||||||
{
|
|
||||||
// Without assigned user
|
// Without assigned user
|
||||||
int taskId = given()
|
int taskId = given()
|
||||||
.contentType(ContentType.JSON)
|
.contentType(ContentType.JSON)
|
||||||
@@ -319,8 +298,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void deleteTask()
|
void deleteTask() {
|
||||||
{
|
|
||||||
int taskId = createTestTask(100);
|
int taskId = createTestTask(100);
|
||||||
|
|
||||||
given().when().delete("/task/" + taskId).then().statusCode(200);
|
given().when().delete("/task/" + taskId).then().statusCode(200);
|
||||||
@@ -328,14 +306,12 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void deleteTaskNotFound()
|
void deleteTaskNotFound() {
|
||||||
{
|
|
||||||
given().when().delete("/task/1").then().statusCode(404);
|
given().when().delete("/task/1").then().statusCode(404);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void createTaskNoName()
|
void createTaskNoName() {
|
||||||
{
|
|
||||||
given()
|
given()
|
||||||
.contentType(ContentType.JSON)
|
.contentType(ContentType.JSON)
|
||||||
.body(Map.of("reward", 100))
|
.body(Map.of("reward", 100))
|
||||||
@@ -346,8 +322,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void createTaskInvalidAssignedUser()
|
void createTaskInvalidAssignedUser() {
|
||||||
{
|
|
||||||
given()
|
given()
|
||||||
.contentType(ContentType.JSON)
|
.contentType(ContentType.JSON)
|
||||||
.body(Map.of("name", "Test Task Invalid User", "reward", 100, "assigned", 999))
|
.body(Map.of("name", "Test Task Invalid User", "reward", 100, "assigned", 999))
|
||||||
@@ -359,8 +334,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void createTaskInvalidRequestBody()
|
void createTaskInvalidRequestBody() {
|
||||||
{
|
|
||||||
given()
|
given()
|
||||||
.contentType(ContentType.JSON)
|
.contentType(ContentType.JSON)
|
||||||
.body(Map.of("reward", 5000))
|
.body(Map.of("reward", 5000))
|
||||||
@@ -371,8 +345,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getTaskWhenNoTasks()
|
void getTaskWhenNoTasks() {
|
||||||
{
|
|
||||||
given()
|
given()
|
||||||
.when()
|
.when()
|
||||||
.get("/tasks")
|
.get("/tasks")
|
||||||
@@ -382,8 +355,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getTasksWhenTasks()
|
void getTasksWhenTasks() {
|
||||||
{
|
|
||||||
createTestTask(100);
|
createTestTask(100);
|
||||||
|
|
||||||
given()
|
given()
|
||||||
@@ -398,8 +370,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getTask()
|
void getTask() {
|
||||||
{
|
|
||||||
int taskId = createTestTask(100);
|
int taskId = createTestTask(100);
|
||||||
|
|
||||||
given()
|
given()
|
||||||
@@ -414,23 +385,20 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getTaskInvalidId()
|
void getTaskInvalidId() {
|
||||||
{
|
|
||||||
createTestTask(100);
|
createTestTask(100);
|
||||||
// Task ID won't be found since we use auto-increment and there's only one
|
// Task ID won't be found since we use auto-increment and there's only one
|
||||||
given().when().get("/task/99999").then().statusCode(404);
|
given().when().get("/task/99999").then().statusCode(404);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getTaskBadId()
|
void getTaskBadId() {
|
||||||
{
|
|
||||||
createTestTask(100);
|
createTestTask(100);
|
||||||
given().when().get("/task/invalid").then().statusCode(400);
|
given().when().get("/task/invalid").then().statusCode(400);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void putTaskModifiesTask()
|
void putTaskModifiesTask() {
|
||||||
{
|
|
||||||
int taskId = createTestTask(100);
|
int taskId = createTestTask(100);
|
||||||
|
|
||||||
given()
|
given()
|
||||||
@@ -452,8 +420,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void putTaskInvalidTaskId()
|
void putTaskInvalidTaskId() {
|
||||||
{
|
|
||||||
createTestTask(100);
|
createTestTask(100);
|
||||||
|
|
||||||
given()
|
given()
|
||||||
@@ -468,8 +435,7 @@ class ApiTest
|
|||||||
// ---- History Tests ----
|
// ---- History Tests ----
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void postHistory()
|
void postHistory() {
|
||||||
{
|
|
||||||
given()
|
given()
|
||||||
.contentType(ContentType.JSON)
|
.contentType(ContentType.JSON)
|
||||||
.body(Map.of("allowance", 100, "description", "Add a 100"))
|
.body(Map.of("allowance", 100, "description", "Add a 100"))
|
||||||
@@ -503,8 +469,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void postHistoryInvalidUserId()
|
void postHistoryInvalidUserId() {
|
||||||
{
|
|
||||||
given()
|
given()
|
||||||
.contentType(ContentType.JSON)
|
.contentType(ContentType.JSON)
|
||||||
.body(Map.of("allowance", 100, "description", "Good"))
|
.body(Map.of("allowance", 100, "description", "Good"))
|
||||||
@@ -515,8 +480,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void postHistoryInvalidDescription()
|
void postHistoryInvalidDescription() {
|
||||||
{
|
|
||||||
given()
|
given()
|
||||||
.contentType(ContentType.JSON)
|
.contentType(ContentType.JSON)
|
||||||
.body(Map.of("allowance", 100))
|
.body(Map.of("allowance", 100))
|
||||||
@@ -527,8 +491,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getHistory()
|
void getHistory() {
|
||||||
{
|
|
||||||
Instant before = Instant.now().minusSeconds(2);
|
Instant before = Instant.now().minusSeconds(2);
|
||||||
Instant after = Instant.now().plusSeconds(2);
|
Instant after = Instant.now().plusSeconds(2);
|
||||||
|
|
||||||
@@ -572,8 +535,7 @@ class ApiTest
|
|||||||
// ---- Allowance By ID Tests ----
|
// ---- Allowance By ID Tests ----
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getUserAllowanceById()
|
void getUserAllowanceById() {
|
||||||
{
|
|
||||||
int allowanceId = createAllowanceWithColour(1, TEST_HISTORY_NAME, 5000, 10, "#FF5733");
|
int allowanceId = createAllowanceWithColour(1, TEST_HISTORY_NAME, 5000, 10, "#FF5733");
|
||||||
|
|
||||||
given()
|
given()
|
||||||
@@ -599,32 +561,27 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getUserByAllowanceIdInvalidAllowance()
|
void getUserByAllowanceIdInvalidAllowance() {
|
||||||
{
|
|
||||||
given().when().get("/user/1/allowance/9999").then().statusCode(404);
|
given().when().get("/user/1/allowance/9999").then().statusCode(404);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getUserByAllowanceByIdInvalidUserId()
|
void getUserByAllowanceByIdInvalidUserId() {
|
||||||
{
|
|
||||||
given().when().get("/user/999/allowance/1").then().statusCode(404);
|
given().when().get("/user/999/allowance/1").then().statusCode(404);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getUserByAllowanceByIdBadUserId()
|
void getUserByAllowanceByIdBadUserId() {
|
||||||
{
|
|
||||||
given().when().get("/user/bad/allowance/1").then().statusCode(400);
|
given().when().get("/user/bad/allowance/1").then().statusCode(400);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getUserByAllowanceByIdBadAllowanceId()
|
void getUserByAllowanceByIdBadAllowanceId() {
|
||||||
{
|
|
||||||
given().when().get("/user/1/allowance/bad").then().statusCode(400);
|
given().when().get("/user/1/allowance/bad").then().statusCode(400);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void putAllowanceById()
|
void putAllowanceById() {
|
||||||
{
|
|
||||||
int allowanceId = createAllowanceWithColour(1, TEST_HISTORY_NAME, 5000, 10, "#FF5733");
|
int allowanceId = createAllowanceWithColour(1, TEST_HISTORY_NAME, 5000, 10, "#FF5733");
|
||||||
|
|
||||||
given()
|
given()
|
||||||
@@ -650,8 +607,7 @@ class ApiTest
|
|||||||
// ---- Complete Task Tests ----
|
// ---- Complete Task Tests ----
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void completeTask()
|
void completeTask() {
|
||||||
{
|
|
||||||
int taskId = createTestTask(101);
|
int taskId = createTestTask(101);
|
||||||
|
|
||||||
given().when().get("/tasks").then().statusCode(200).body("$.size()", is(1));
|
given().when().get("/tasks").then().statusCode(200).body("$.size()", is(1));
|
||||||
@@ -698,8 +654,7 @@ class ApiTest
|
|||||||
.body("[0].progress", closeTo(101.0, 0.01));
|
.body("[0].progress", closeTo(101.0, 0.01));
|
||||||
|
|
||||||
// Verify history for both users
|
// Verify history for both users
|
||||||
for (int userId = 1; userId <= 2; userId++)
|
for (int userId = 1; userId <= 2; userId++) {
|
||||||
{
|
|
||||||
given()
|
given()
|
||||||
.when()
|
.when()
|
||||||
.get("/user/" + userId + "/history")
|
.get("/user/" + userId + "/history")
|
||||||
@@ -711,8 +666,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void completeTaskWithNoWeights()
|
void completeTaskWithNoWeights() {
|
||||||
{
|
|
||||||
int taskId = createTestTask(101);
|
int taskId = createTestTask(101);
|
||||||
|
|
||||||
given().when().get("/tasks").then().statusCode(200).body("$.size()", is(1));
|
given().when().get("/tasks").then().statusCode(200).body("$.size()", is(1));
|
||||||
@@ -754,8 +708,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void completeTaskAllowanceWeightsSumTo0()
|
void completeTaskAllowanceWeightsSumTo0() {
|
||||||
{
|
|
||||||
int taskId = createTestTask(101);
|
int taskId = createTestTask(101);
|
||||||
|
|
||||||
given().when().get("/tasks").then().statusCode(200).body("$.size()", is(1));
|
given().when().get("/tasks").then().statusCode(200).body("$.size()", is(1));
|
||||||
@@ -788,16 +741,14 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void completeTaskInvalidId()
|
void completeTaskInvalidId() {
|
||||||
{
|
|
||||||
given().when().post("/task/999/complete").then().statusCode(404);
|
given().when().post("/task/999/complete").then().statusCode(404);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- Complete Allowance Tests ----
|
// ---- Complete Allowance Tests ----
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void completeAllowance()
|
void completeAllowance() {
|
||||||
{
|
|
||||||
createTestTask(100);
|
createTestTask(100);
|
||||||
createAllowance(1, "Test Allowance 1", 100, 50);
|
createAllowance(1, "Test Allowance 1", 100, 50);
|
||||||
|
|
||||||
@@ -842,22 +793,19 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void completeAllowanceInvalidUserId()
|
void completeAllowanceInvalidUserId() {
|
||||||
{
|
|
||||||
given().when().post("/user/999/allowance/1/complete").then().statusCode(404);
|
given().when().post("/user/999/allowance/1/complete").then().statusCode(404);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void completeAllowanceInvalidAllowanceId()
|
void completeAllowanceInvalidAllowanceId() {
|
||||||
{
|
|
||||||
given().when().post("/user/1/allowance/999/complete").then().statusCode(404);
|
given().when().post("/user/1/allowance/999/complete").then().statusCode(404);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- Bulk Update Tests ----
|
// ---- Bulk Update Tests ----
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void putBulkAllowance()
|
void putBulkAllowance() {
|
||||||
{
|
|
||||||
int id1 = createAllowance(1, "Test Allowance 1", 1000, 1);
|
int id1 = createAllowance(1, "Test Allowance 1", 1000, 1);
|
||||||
int id2 = createAllowance(1, "Test Allowance 2", 1000, 2);
|
int id2 = createAllowance(1, "Test Allowance 2", 1000, 2);
|
||||||
|
|
||||||
@@ -889,8 +837,7 @@ class ApiTest
|
|||||||
// ---- Add Allowance Amount Tests ----
|
// ---- Add Allowance Amount Tests ----
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void addAllowanceSimple()
|
void addAllowanceSimple() {
|
||||||
{
|
|
||||||
int allowanceId = createAllowance(1, "Test Allowance 1", 1000, 1);
|
int allowanceId = createAllowance(1, "Test Allowance 1", 1000, 1);
|
||||||
|
|
||||||
given()
|
given()
|
||||||
@@ -920,8 +867,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void addAllowanceWithSpillage()
|
void addAllowanceWithSpillage() {
|
||||||
{
|
|
||||||
int id1 = createAllowance(1, "Test Allowance 1", 5, 1);
|
int id1 = createAllowance(1, "Test Allowance 1", 5, 1);
|
||||||
int id2 = createAllowance(1, "Test Allowance 2", 5, 1);
|
int id2 = createAllowance(1, "Test Allowance 2", 5, 1);
|
||||||
given()
|
given()
|
||||||
@@ -963,8 +909,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void addAllowanceIdZero()
|
void addAllowanceIdZero() {
|
||||||
{
|
|
||||||
createAllowance(1, "Test Allowance 1", 1000, 1);
|
createAllowance(1, "Test Allowance 1", 1000, 1);
|
||||||
|
|
||||||
given()
|
given()
|
||||||
@@ -994,8 +939,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void subtractAllowanceSimple()
|
void subtractAllowanceSimple() {
|
||||||
{
|
|
||||||
int allowanceId = createAllowance(1, "Test Allowance 1", 1000, 1);
|
int allowanceId = createAllowance(1, "Test Allowance 1", 1000, 1);
|
||||||
|
|
||||||
given()
|
given()
|
||||||
@@ -1035,8 +979,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void subtractAllowanceIdZero()
|
void subtractAllowanceIdZero() {
|
||||||
{
|
|
||||||
createAllowance(1, "Test Allowance 1", 1000, 1);
|
createAllowance(1, "Test Allowance 1", 1000, 1);
|
||||||
|
|
||||||
given()
|
given()
|
||||||
@@ -1078,8 +1021,7 @@ class ApiTest
|
|||||||
// ---- Transfer Tests ----
|
// ---- Transfer Tests ----
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void transferSuccessful()
|
void transferSuccessful() {
|
||||||
{
|
|
||||||
int id1 = createAllowance(1, "From Allowance", 100, 1);
|
int id1 = createAllowance(1, "From Allowance", 100, 1);
|
||||||
int id2 = createAllowance(1, "To Allowance", 100, 1);
|
int id2 = createAllowance(1, "To Allowance", 100, 1);
|
||||||
|
|
||||||
@@ -1112,8 +1054,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void transferCapsAtTarget()
|
void transferCapsAtTarget() {
|
||||||
{
|
|
||||||
int id1 = createAllowance(1, "From Allowance", 100, 1);
|
int id1 = createAllowance(1, "From Allowance", 100, 1);
|
||||||
int id2 = createAllowance(1, "To Allowance", 5, 1);
|
int id2 = createAllowance(1, "To Allowance", 5, 1);
|
||||||
|
|
||||||
@@ -1145,8 +1086,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void transferDifferentUsersFails()
|
void transferDifferentUsersFails() {
|
||||||
{
|
|
||||||
int id1 = createAllowance(1, "User1 Allowance", 100, 1);
|
int id1 = createAllowance(1, "User1 Allowance", 100, 1);
|
||||||
|
|
||||||
// Create allowance for user 2
|
// Create allowance for user 2
|
||||||
@@ -1172,8 +1112,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void transferInsufficientFunds()
|
void transferInsufficientFunds() {
|
||||||
{
|
|
||||||
int id1 = createAllowance(1, "From Allowance", 100, 1);
|
int id1 = createAllowance(1, "From Allowance", 100, 1);
|
||||||
int id2 = createAllowance(1, "To Allowance", 100, 1);
|
int id2 = createAllowance(1, "To Allowance", 100, 1);
|
||||||
|
|
||||||
@@ -1188,8 +1127,7 @@ class ApiTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void transferNotFound()
|
void transferNotFound() {
|
||||||
{
|
|
||||||
given()
|
given()
|
||||||
.contentType(ContentType.JSON)
|
.contentType(ContentType.JSON)
|
||||||
.body(Map.of("from", 999, "to", 1000, "amount", 1))
|
.body(Map.of("from", 999, "to", 1000, "amount", 1))
|
||||||
@@ -1201,8 +1139,7 @@ class ApiTest
|
|||||||
|
|
||||||
// ---- Helpers ----
|
// ---- Helpers ----
|
||||||
|
|
||||||
private int createTestTask(int reward)
|
private int createTestTask(int reward) {
|
||||||
{
|
|
||||||
return given()
|
return given()
|
||||||
.contentType(ContentType.JSON)
|
.contentType(ContentType.JSON)
|
||||||
.body(Map.of("name", "Test Task", "reward", reward))
|
.body(Map.of("name", "Test Task", "reward", reward))
|
||||||
@@ -1214,8 +1151,7 @@ class ApiTest
|
|||||||
.path("id");
|
.path("id");
|
||||||
}
|
}
|
||||||
|
|
||||||
private int createAllowance(int userId, String name, double target, double weight)
|
private int createAllowance(int userId, String name, double target, double weight) {
|
||||||
{
|
|
||||||
return given()
|
return given()
|
||||||
.contentType(ContentType.JSON)
|
.contentType(ContentType.JSON)
|
||||||
.body(Map.of("name", name, "target", target, "weight", weight))
|
.body(Map.of("name", name, "target", target, "weight", weight))
|
||||||
@@ -1227,8 +1163,7 @@ class ApiTest
|
|||||||
.path("id");
|
.path("id");
|
||||||
}
|
}
|
||||||
|
|
||||||
private int createAllowanceWithColour(int userId, String name, double target, double weight, String colour)
|
private int createAllowanceWithColour(int userId, String name, double target, double weight, String colour) {
|
||||||
{
|
|
||||||
return given()
|
return given()
|
||||||
.contentType(ContentType.JSON)
|
.contentType(ContentType.JSON)
|
||||||
.body(Map.of("name", name, "target", target, "weight", weight, "colour", colour))
|
.body(Map.of("name", name, "target", target, "weight", weight, "colour", colour))
|
||||||
|
|||||||
@@ -5,29 +5,24 @@ import org.junit.jupiter.api.Test;
|
|||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
class ColourUtilTest
|
class ColourUtilTest {
|
||||||
{
|
|
||||||
@Test
|
@Test
|
||||||
void convertStringToColourWithSign()
|
void convertStringToColourWithSign() {
|
||||||
{
|
|
||||||
assertEquals(0x123456, ColourUtil.convertStringToColour("#123456"));
|
assertEquals(0x123456, ColourUtil.convertStringToColour("#123456"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void convertStringToColourWithoutSign()
|
void convertStringToColourWithoutSign() {
|
||||||
{
|
|
||||||
assertEquals(0x123456, ColourUtil.convertStringToColour("123456"));
|
assertEquals(0x123456, ColourUtil.convertStringToColour("123456"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void convertStringToColourWithSignThreeDigits()
|
void convertStringToColourWithSignThreeDigits() {
|
||||||
{
|
|
||||||
assertEquals(0xA0B0C0, ColourUtil.convertStringToColour("#ABC"));
|
assertEquals(0xA0B0C0, ColourUtil.convertStringToColour("#ABC"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void convertStringToColourWithoutSignThreeDigits()
|
void convertStringToColourWithoutSignThreeDigits() {
|
||||||
{
|
|
||||||
assertEquals(0xA0B0C0, ColourUtil.convertStringToColour("ABC"));
|
assertEquals(0xA0B0C0, ColourUtil.convertStringToColour("ABC"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user