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