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,504 +12,394 @@ 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;
|
private final TransferService transferService;
|
||||||
private final TransferService transferService;
|
private final MigrationService migrationService;
|
||||||
private final MigrationService migrationService;
|
|
||||||
|
|
||||||
public ApiController(UserService userService,
|
public ApiController(UserService userService,
|
||||||
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;
|
this.transferService = transferService;
|
||||||
this.transferService = transferService;
|
this.migrationService = migrationService;
|
||||||
this.migrationService = migrationService;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// ---- 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);
|
||||||
{
|
} catch (NumberFormatException e) {
|
||||||
id = Integer.parseInt(userId);
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
||||||
}
|
}
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
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());
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// ---- 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);
|
||||||
{
|
} catch (NumberFormatException e) {
|
||||||
id = Integer.parseInt(userId);
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
||||||
}
|
}
|
||||||
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"));
|
||||||
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"));
|
}
|
||||||
}
|
|
||||||
|
|
||||||
allowanceService.addHistory(id, request);
|
allowanceService.addHistory(id, request);
|
||||||
return ResponseEntity.ok(new MessageResponse("History updated successfully"));
|
return ResponseEntity.ok(new MessageResponse("History updated successfully"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@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);
|
||||||
{
|
} catch (NumberFormatException e) {
|
||||||
id = Integer.parseInt(userId);
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
||||||
}
|
}
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
|
||||||
}
|
|
||||||
|
|
||||||
List<HistoryDto> history = allowanceService.getHistory(id);
|
List<HistoryDto> history = allowanceService.getHistory(id);
|
||||||
return ResponseEntity.ok(history);
|
return ResponseEntity.ok(history);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- 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);
|
||||||
{
|
} catch (NumberFormatException e) {
|
||||||
id = Integer.parseInt(userId);
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
||||||
}
|
}
|
||||||
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"));
|
||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("User not found"));
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return ResponseEntity.ok(allowanceService.getUserAllowances(id));
|
return ResponseEntity.ok(allowanceService.getUserAllowances(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
@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);
|
||||||
{
|
} catch (NumberFormatException e) {
|
||||||
id = Integer.parseInt(userId);
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
||||||
}
|
}
|
||||||
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"));
|
||||||
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"));
|
}
|
||||||
}
|
|
||||||
|
|
||||||
int allowanceId = allowanceService.createAllowance(id, request);
|
int allowanceId = allowanceService.createAllowance(id, request);
|
||||||
return ResponseEntity.status(HttpStatus.CREATED).body(new IdResponse(allowanceId));
|
return ResponseEntity.status(HttpStatus.CREATED).body(new IdResponse(allowanceId));
|
||||||
}
|
}
|
||||||
|
|
||||||
@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);
|
||||||
{
|
} catch (NumberFormatException e) {
|
||||||
id = Integer.parseInt(userId);
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
||||||
}
|
}
|
||||||
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"));
|
||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("User not found"));
|
}
|
||||||
}
|
|
||||||
|
|
||||||
allowanceService.bulkUpdateAllowance(id, requests);
|
allowanceService.bulkUpdateAllowance(id, requests);
|
||||||
return ResponseEntity.ok(new MessageResponse("Allowance updated successfully"));
|
return ResponseEntity.ok(new MessageResponse("Allowance updated successfully"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@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);
|
||||||
{
|
} catch (NumberFormatException e) {
|
||||||
uid = Integer.parseInt(userId);
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
||||||
}
|
}
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
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) {
|
||||||
}
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid allowance ID"));
|
||||||
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"));
|
||||||
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);
|
||||||
{
|
} catch (NumberFormatException e) {
|
||||||
uid = Integer.parseInt(userId);
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
||||||
}
|
}
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
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) {
|
||||||
}
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid allowance ID"));
|
||||||
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"));
|
||||||
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"));
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@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);
|
||||||
{
|
} catch (NumberFormatException e) {
|
||||||
uid = Integer.parseInt(userId);
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
||||||
}
|
}
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
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) {
|
||||||
}
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid allowance ID"));
|
||||||
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"));
|
||||||
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);
|
||||||
{
|
} catch (NumberFormatException e) {
|
||||||
uid = Integer.parseInt(userId);
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
||||||
}
|
}
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
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) {
|
||||||
}
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid allowance ID"));
|
||||||
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"));
|
||||||
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"));
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@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);
|
||||||
{
|
} catch (NumberFormatException e) {
|
||||||
uid = Integer.parseInt(userId);
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
||||||
}
|
}
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
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) {
|
||||||
}
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid allowance ID"));
|
||||||
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"));
|
||||||
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"));
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// ---- 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"));
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int taskId = taskService.createTask(request);
|
int taskId = taskService.createTask(request);
|
||||||
return ResponseEntity.status(HttpStatus.CREATED).body(new IdResponse(taskId));
|
return ResponseEntity.status(HttpStatus.CREATED).body(new IdResponse(taskId));
|
||||||
}
|
}
|
||||||
|
|
||||||
@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);
|
||||||
{
|
} catch (NumberFormatException e) {
|
||||||
id = Integer.parseInt(taskId);
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid task ID"));
|
||||||
}
|
}
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
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);
|
||||||
{
|
} catch (NumberFormatException e) {
|
||||||
id = Integer.parseInt(taskId);
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid task ID"));
|
||||||
}
|
}
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
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"));
|
}
|
||||||
}
|
|
||||||
|
|
||||||
taskService.updateTask(id, request);
|
taskService.updateTask(id, request);
|
||||||
return ResponseEntity.ok(new MessageResponse("Task updated successfully"));
|
return ResponseEntity.ok(new MessageResponse("Task updated successfully"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@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);
|
||||||
{
|
} catch (NumberFormatException e) {
|
||||||
id = Integer.parseInt(taskId);
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid task ID"));
|
||||||
}
|
}
|
||||||
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"));
|
||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("Task not found"));
|
}
|
||||||
}
|
|
||||||
|
|
||||||
taskService.deleteTask(id);
|
taskService.deleteTask(id);
|
||||||
return ResponseEntity.ok(new MessageResponse("Task deleted successfully"));
|
return ResponseEntity.ok(new MessageResponse("Task deleted successfully"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@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);
|
||||||
{
|
} catch (NumberFormatException e) {
|
||||||
id = Integer.parseInt(taskId);
|
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid task ID"));
|
||||||
}
|
}
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
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"));
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// ---- 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 BAD_REQUEST -> ResponseEntity.badRequest().body(new ErrorResponse(result.message()));
|
||||||
case SUCCESS -> ResponseEntity.ok(new MessageResponse(result.message()));
|
case NOT_FOUND -> ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse(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()));
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---- 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,160 +15,135 @@ 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());
|
||||||
{
|
return "index";
|
||||||
model.addAttribute("users", userService.getUsers());
|
}
|
||||||
return "index";
|
return renderWithUser(model, currentUser);
|
||||||
}
|
}
|
||||||
return renderWithUser(model, currentUser);
|
|
||||||
}
|
|
||||||
|
|
||||||
@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.setMaxAge(3600);
|
||||||
Cookie cookie = new Cookie("user", user);
|
cookie.setHttpOnly(true);
|
||||||
cookie.setMaxAge(3600);
|
response.addCookie(cookie);
|
||||||
cookie.setHttpOnly(true);
|
}
|
||||||
response.addCookie(cookie);
|
return "redirect:/";
|
||||||
}
|
}
|
||||||
return "redirect:/";
|
|
||||||
}
|
|
||||||
|
|
||||||
@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";
|
}
|
||||||
}
|
|
||||||
|
|
||||||
CreateTaskRequest taskRequest = new CreateTaskRequest(name, reward, null,
|
CreateTaskRequest taskRequest = new CreateTaskRequest(name, reward, null,
|
||||||
(schedule != null && !schedule.isEmpty()) ? schedule : null);
|
(schedule != null && !schedule.isEmpty()) ? schedule : null);
|
||||||
taskService.createTask(taskRequest);
|
taskService.createTask(taskRequest);
|
||||||
return "redirect:/";
|
return "redirect:/";
|
||||||
}
|
}
|
||||||
|
|
||||||
@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:/";
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@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";
|
}
|
||||||
}
|
|
||||||
|
|
||||||
allowanceService.createAllowance(currentUser, new CreateAllowanceRequest(name, target, weight, ""));
|
allowanceService.createAllowance(currentUser, new CreateAllowanceRequest(name, target, weight, ""));
|
||||||
return "redirect:/";
|
return "redirect:/";
|
||||||
}
|
}
|
||||||
|
|
||||||
@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);
|
||||||
}
|
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();
|
||||||
Cookie[] cookies = request.getCookies();
|
if (cookies == null) {
|
||||||
if (cookies == null)
|
return null;
|
||||||
{
|
}
|
||||||
return null;
|
String userStr = Arrays.stream(cookies)
|
||||||
}
|
.filter(c -> "user".equals(c.getName()))
|
||||||
String userStr = Arrays.stream(cookies)
|
.map(Cookie::getValue)
|
||||||
.filter(c -> "user".equals(c.getName()))
|
.findFirst()
|
||||||
.map(Cookie::getValue)
|
.orElse(null);
|
||||||
.findFirst()
|
if (userStr == null) {
|
||||||
.orElse(null);
|
return null;
|
||||||
if (userStr == null)
|
}
|
||||||
{
|
try {
|
||||||
return null;
|
int userId = Integer.parseInt(userStr);
|
||||||
}
|
if (!userService.userExists(userId)) {
|
||||||
try
|
unsetUserCookie(response);
|
||||||
{
|
return null;
|
||||||
int userId = Integer.parseInt(userStr);
|
}
|
||||||
if (!userService.userExists(userId))
|
return userId;
|
||||||
{
|
} catch (NumberFormatException e) {
|
||||||
unsetUserCookie(response);
|
unsetUserCookie(response);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return userId;
|
}
|
||||||
}
|
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
unsetUserCookie(response);
|
|
||||||
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("/");
|
cookie.setHttpOnly(true);
|
||||||
cookie.setHttpOnly(true);
|
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));
|
model.addAttribute("tasks", taskService.getTasks());
|
||||||
model.addAttribute("tasks", taskService.getTasks());
|
model.addAttribute("history", allowanceService.getHistory(currentUser));
|
||||||
model.addAttribute("history", allowanceService.getHistory(currentUser));
|
return "index";
|
||||||
return "index";
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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) {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,19 +3,22 @@ package be.seeseepuff.allowanceplanner.dto;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public record MigrationDto(
|
public record MigrationDto(
|
||||||
List<MigrationUserDto> users,
|
List<MigrationUserDto> users,
|
||||||
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,96 +4,81 @@ 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;
|
|
||||||
|
|
||||||
@Column(name = "user_id", nullable = false)
|
@Column(name = "user_id", nullable = false)
|
||||||
private int userId;
|
private int userId;
|
||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
private long target;
|
private long target;
|
||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
private long balance = 0;
|
private long balance = 0;
|
||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
private double weight;
|
private double weight;
|
||||||
|
|
||||||
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,70 +4,59 @@ 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;
|
|
||||||
|
|
||||||
@Column(name = "user_id", nullable = false)
|
@Column(name = "user_id", nullable = false)
|
||||||
private int userId;
|
private int userId;
|
||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
private long timestamp;
|
private long timestamp;
|
||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
private long amount;
|
private long amount;
|
||||||
|
|
||||||
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,94 +4,79 @@ 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;
|
|
||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
private long reward;
|
private long reward;
|
||||||
|
|
||||||
private Integer assigned;
|
private Integer assigned;
|
||||||
|
|
||||||
private String schedule;
|
private String schedule;
|
||||||
|
|
||||||
private Long completed;
|
private Long completed;
|
||||||
|
|
||||||
@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,58 +4,49 @@ 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;
|
|
||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
private double weight = 10.0;
|
private double weight = 10.0;
|
||||||
|
|
||||||
@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,19 +9,18 @@ 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);
|
||||||
|
|
||||||
int countByIdAndUserId(int id, int userId);
|
int countByIdAndUserId(int id, int userId);
|
||||||
|
|
||||||
void deleteByIdAndUserId(int id, int userId);
|
void deleteByIdAndUserId(int id, int userId);
|
||||||
|
|
||||||
@Query("SELECT a FROM Allowance a WHERE a.userId = :userId AND a.weight > 0 ORDER BY (a.target - a.balance) ASC")
|
@Query("SELECT a FROM Allowance a WHERE a.userId = :userId AND a.weight > 0 ORDER BY (a.target - a.balance) ASC")
|
||||||
List<Allowance> findByUserIdWithPositiveWeightOrderByRemainingAsc(int userId);
|
List<Allowance> findByUserIdWithPositiveWeightOrderByRemainingAsc(int userId);
|
||||||
|
|
||||||
@Query("SELECT COALESCE(SUM(a.weight), 0) FROM Allowance a WHERE a.userId = :userId AND a.weight > 0")
|
@Query("SELECT COALESCE(SUM(a.weight), 0) FROM Allowance a WHERE a.userId = :userId AND a.weight > 0")
|
||||||
double sumPositiveWeights(int userId);
|
double sumPositiveWeights(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,9 +8,8 @@ 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,282 +17,242 @@ 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<>();
|
|
||||||
|
|
||||||
// Add the "rest" allowance (id=0)
|
// Add the "rest" allowance (id=0)
|
||||||
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)
|
||||||
{
|
.map(u -> new AllowanceDto(0, "", 0, u.getBalance() / 100.0, u.getWeight(), ""));
|
||||||
return userRepository.findById(userId)
|
}
|
||||||
.map(u -> new AllowanceDto(0, "", 0, u.getBalance() / 100.0, u.getWeight(), ""));
|
return allowanceRepository.findByIdAndUserId(allowanceId, userId)
|
||||||
}
|
.map(this::toDto);
|
||||||
return allowanceRepository.findByIdAndUserId(allowanceId, userId)
|
}
|
||||||
.map(this::toDto);
|
|
||||||
}
|
|
||||||
|
|
||||||
@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);
|
allowance.setName(request.name());
|
||||||
allowance.setName(request.name());
|
allowance.setTarget(Math.round(request.target() * 100.0));
|
||||||
allowance.setTarget(Math.round(request.target() * 100.0));
|
allowance.setWeight(request.weight());
|
||||||
allowance.setWeight(request.weight());
|
allowance.setColour(colour);
|
||||||
allowance.setColour(colour);
|
allowance = allowanceRepository.save(allowance);
|
||||||
allowance = allowanceRepository.save(allowance);
|
return allowance.getId();
|
||||||
return allowance.getId();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@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);
|
||||||
}
|
return true;
|
||||||
allowanceRepository.deleteByIdAndUserId(allowanceId, userId);
|
}
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@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.setWeight(request.weight());
|
||||||
User user = userRepository.findById(userId).orElseThrow();
|
userRepository.save(user);
|
||||||
user.setWeight(request.weight());
|
return true;
|
||||||
userRepository.save(user);
|
}
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
Optional<Allowance> opt = allowanceRepository.findByIdAndUserId(allowanceId, userId);
|
Optional<Allowance> opt = allowanceRepository.findByIdAndUserId(allowanceId, userId);
|
||||||
if (opt.isEmpty())
|
if (opt.isEmpty()) {
|
||||||
{
|
return false;
|
||||||
return false;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
int colour = ColourUtil.convertStringToColour(request.colour());
|
int colour = ColourUtil.convertStringToColour(request.colour());
|
||||||
Allowance allowance = opt.get();
|
Allowance allowance = opt.get();
|
||||||
allowance.setName(request.name());
|
allowance.setName(request.name());
|
||||||
allowance.setTarget(Math.round(request.target() * 100.0));
|
allowance.setTarget(Math.round(request.target() * 100.0));
|
||||||
allowance.setWeight(request.weight());
|
allowance.setWeight(request.weight());
|
||||||
allowance.setColour(colour);
|
allowance.setColour(colour);
|
||||||
allowanceRepository.save(allowance);
|
allowanceRepository.save(allowance);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@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) {
|
||||||
{
|
User user = userRepository.findById(userId).orElseThrow();
|
||||||
if (req.id() == 0)
|
user.setWeight(req.weight());
|
||||||
{
|
userRepository.save(user);
|
||||||
User user = userRepository.findById(userId).orElseThrow();
|
} else {
|
||||||
user.setWeight(req.weight());
|
allowanceRepository.findByIdAndUserId(req.id(), userId).ifPresent(a ->
|
||||||
userRepository.save(user);
|
{
|
||||||
}
|
a.setWeight(req.weight());
|
||||||
else
|
allowanceRepository.save(a);
|
||||||
{
|
});
|
||||||
allowanceRepository.findByIdAndUserId(req.id(), userId).ifPresent(a ->
|
}
|
||||||
{
|
}
|
||||||
a.setWeight(req.weight());
|
}
|
||||||
allowanceRepository.save(a);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@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;
|
|
||||||
}
|
|
||||||
|
|
||||||
Allowance allowance = opt.get();
|
Allowance allowance = opt.get();
|
||||||
long cost = allowance.getBalance();
|
long cost = allowance.getBalance();
|
||||||
String allowanceName = allowance.getName();
|
String allowanceName = allowance.getName();
|
||||||
|
|
||||||
// Delete the allowance
|
// Delete the allowance
|
||||||
allowanceRepository.delete(allowance);
|
allowanceRepository.delete(allowance);
|
||||||
|
|
||||||
// Add a history entry
|
// Add a history entry
|
||||||
History history = new History();
|
History history = new History();
|
||||||
history.setUserId(userId);
|
history.setUserId(userId);
|
||||||
history.setTimestamp(Instant.now().getEpochSecond());
|
history.setTimestamp(Instant.now().getEpochSecond());
|
||||||
history.setAmount(-cost);
|
history.setAmount(-cost);
|
||||||
history.setDescription("Allowance completed: " + allowanceName);
|
history.setDescription("Allowance completed: " + allowanceName);
|
||||||
historyRepository.save(history);
|
historyRepository.save(history);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@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
|
||||||
History history = new History();
|
History history = new History();
|
||||||
history.setUserId(userId);
|
history.setUserId(userId);
|
||||||
history.setTimestamp(Instant.now().getEpochSecond());
|
history.setTimestamp(Instant.now().getEpochSecond());
|
||||||
history.setAmount(remainingAmount);
|
history.setAmount(remainingAmount);
|
||||||
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();
|
||||||
{
|
if (remainingAmount > user.getBalance()) {
|
||||||
User user = userRepository.findById(userId).orElseThrow();
|
throw new IllegalArgumentException("cannot remove more than the current balance: " + 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);
|
||||||
User user = userRepository.findById(userId).orElseThrow();
|
} else if (remainingAmount < 0) {
|
||||||
user.setBalance(user.getBalance() + remainingAmount);
|
Allowance allowance = allowanceRepository.findByIdAndUserId(allowanceId, userId).orElse(null);
|
||||||
userRepository.save(user);
|
if (allowance == null) {
|
||||||
}
|
return false;
|
||||||
else if (remainingAmount < 0)
|
}
|
||||||
{
|
if (remainingAmount > allowance.getBalance()) {
|
||||||
Allowance allowance = allowanceRepository.findByIdAndUserId(allowanceId, userId).orElse(null);
|
throw new IllegalArgumentException("cannot remove more than the current allowance balance: " + allowance.getBalance());
|
||||||
if (allowance == null)
|
}
|
||||||
{
|
allowance.setBalance(allowance.getBalance() + remainingAmount);
|
||||||
return false;
|
allowanceRepository.save(allowance);
|
||||||
}
|
} else {
|
||||||
if (remainingAmount > allowance.getBalance())
|
Allowance allowance = allowanceRepository.findByIdAndUserId(allowanceId, userId).orElse(null);
|
||||||
{
|
if (allowance == null) {
|
||||||
throw new IllegalArgumentException("cannot remove more than the current allowance balance: " + allowance.getBalance());
|
return false;
|
||||||
}
|
}
|
||||||
allowance.setBalance(allowance.getBalance() + remainingAmount);
|
|
||||||
allowanceRepository.save(allowance);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Allowance allowance = allowanceRepository.findByIdAndUserId(allowanceId, userId).orElse(null);
|
|
||||||
if (allowance == null)
|
|
||||||
{
|
|
||||||
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();
|
|
||||||
|
|
||||||
double sumOfWeights = allowanceRepository.sumPositiveWeights(userId) + userWeight;
|
double sumOfWeights = allowanceRepository.sumPositiveWeights(userId) + userWeight;
|
||||||
|
|
||||||
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);
|
||||||
{
|
if (allowance.getBalance() + amount > allowance.getTarget()) {
|
||||||
int amount = (int) ((allowance.getWeight() / sumOfWeights) * remainingReward);
|
amount = (int) (allowance.getTarget() - allowance.getBalance());
|
||||||
if (allowance.getBalance() + amount > allowance.getTarget())
|
}
|
||||||
{
|
sumOfWeights -= allowance.getWeight();
|
||||||
amount = (int) (allowance.getTarget() - allowance.getBalance());
|
allowance.setBalance(allowance.getBalance() + amount);
|
||||||
}
|
allowanceRepository.save(allowance);
|
||||||
sumOfWeights -= allowance.getWeight();
|
remainingReward -= amount;
|
||||||
allowance.setBalance(allowance.getBalance() + amount);
|
}
|
||||||
allowanceRepository.save(allowance);
|
}
|
||||||
remainingReward -= amount;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add remaining to user's balance
|
// Add remaining to user's balance
|
||||||
user = userRepository.findById(userId).orElseThrow();
|
user = userRepository.findById(userId).orElseThrow();
|
||||||
user.setBalance(user.getBalance() + remainingReward);
|
user.setBalance(user.getBalance() + remainingReward);
|
||||||
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,
|
Instant.ofEpochSecond(h.getTimestamp()),
|
||||||
Instant.ofEpochSecond(h.getTimestamp()),
|
h.getDescription()))
|
||||||
h.getDescription()))
|
.toList();
|
||||||
.toList();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@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);
|
history.setTimestamp(Instant.now().getEpochSecond());
|
||||||
history.setTimestamp(Instant.now().getEpochSecond());
|
history.setAmount(amount);
|
||||||
history.setAmount(amount);
|
history.setDescription(request.description());
|
||||||
history.setDescription(request.description());
|
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(),
|
a.getTarget() / 100.0,
|
||||||
a.getTarget() / 100.0,
|
a.getBalance() / 100.0,
|
||||||
a.getBalance() / 100.0,
|
a.getWeight(),
|
||||||
a.getWeight(),
|
ColourUtil.convertColourToString(a.getColour()));
|
||||||
ColourUtil.convertColourToString(a.getColour()));
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,95 +10,88 @@ 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;
|
private final TaskRepository taskRepository;
|
||||||
private final TaskRepository taskRepository;
|
private final EntityManager entityManager;
|
||||||
private final EntityManager entityManager;
|
|
||||||
|
|
||||||
public MigrationService(UserRepository userRepository,
|
public MigrationService(UserRepository userRepository,
|
||||||
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;
|
this.taskRepository = taskRepository;
|
||||||
this.taskRepository = taskRepository;
|
this.entityManager = entityManager;
|
||||||
this.entityManager = entityManager;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@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();
|
allowanceRepository.deleteAll();
|
||||||
allowanceRepository.deleteAll();
|
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())
|
.setParameter("name", u.name())
|
||||||
.setParameter("name", u.name())
|
.setParameter("balance", u.balance())
|
||||||
.setParameter("balance", u.balance())
|
.setParameter("weight", u.weight())
|
||||||
.setParameter("weight", u.weight())
|
.executeUpdate();
|
||||||
.executeUpdate();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// 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())
|
.setParameter("userId", a.userId())
|
||||||
.setParameter("userId", a.userId())
|
.setParameter("name", a.name())
|
||||||
.setParameter("name", a.name())
|
.setParameter("target", a.target())
|
||||||
.setParameter("target", a.target())
|
.setParameter("balance", a.balance())
|
||||||
.setParameter("balance", a.balance())
|
.setParameter("weight", a.weight())
|
||||||
.setParameter("weight", a.weight())
|
.setParameter("colour", a.colour())
|
||||||
.setParameter("colour", a.colour())
|
.executeUpdate();
|
||||||
.executeUpdate();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// 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())
|
.setParameter("userId", h.userId())
|
||||||
.setParameter("userId", h.userId())
|
.setParameter("timestamp", h.timestamp())
|
||||||
.setParameter("timestamp", h.timestamp())
|
.setParameter("amount", h.amount())
|
||||||
.setParameter("amount", h.amount())
|
.setParameter("description", h.description())
|
||||||
.setParameter("description", h.description())
|
.executeUpdate();
|
||||||
.executeUpdate();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// 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())
|
.setParameter("name", t.name())
|
||||||
.setParameter("name", t.name())
|
.setParameter("reward", t.reward())
|
||||||
.setParameter("reward", t.reward())
|
.setParameter("assigned", t.assigned())
|
||||||
.setParameter("assigned", t.assigned())
|
.setParameter("schedule", t.schedule())
|
||||||
.setParameter("schedule", t.schedule())
|
.setParameter("completed", t.completed())
|
||||||
.setParameter("completed", t.completed())
|
.setParameter("nextRun", t.nextRun())
|
||||||
.setParameter("nextRun", t.nextRun())
|
.executeUpdate();
|
||||||
.executeUpdate();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Reset sequences so new inserts don't collide with migrated IDs
|
// Reset sequences so new inserts don't collide with migrated IDs
|
||||||
entityManager.createNativeQuery("SELECT setval('users_id_seq', COALESCE((SELECT MAX(id) FROM users), 0))").getSingleResult();
|
entityManager.createNativeQuery("SELECT setval('users_id_seq', COALESCE((SELECT MAX(id) FROM users), 0))").getSingleResult();
|
||||||
entityManager.createNativeQuery("SELECT setval('allowances_id_seq', COALESCE((SELECT MAX(id) FROM allowances), 0))").getSingleResult();
|
entityManager.createNativeQuery("SELECT setval('allowances_id_seq', COALESCE((SELECT MAX(id) FROM allowances), 0))").getSingleResult();
|
||||||
entityManager.createNativeQuery("SELECT setval('history_id_seq', COALESCE((SELECT MAX(id) FROM history), 0))").getSingleResult();
|
entityManager.createNativeQuery("SELECT setval('history_id_seq', COALESCE((SELECT MAX(id) FROM history), 0))").getSingleResult();
|
||||||
entityManager.createNativeQuery("SELECT setval('tasks_id_seq', COALESCE((SELECT MAX(id) FROM tasks), 0))").getSingleResult();
|
entityManager.createNativeQuery("SELECT setval('tasks_id_seq', COALESCE((SELECT MAX(id) FROM tasks), 0))").getSingleResult();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,118 +16,105 @@ 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;
|
private final AllowanceService allowanceService;
|
||||||
private final AllowanceService allowanceService;
|
|
||||||
|
|
||||||
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;
|
this.allowanceService = allowanceService;
|
||||||
this.allowanceService = allowanceService;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@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));
|
task.setAssigned(request.assigned());
|
||||||
task.setAssigned(request.assigned());
|
task = taskRepository.save(task);
|
||||||
task = taskRepository.save(task);
|
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.setName(request.name());
|
||||||
Task task = opt.get();
|
task.setReward(Math.round((request.reward() != null ? request.reward() : 0.0) * 100.0));
|
||||||
task.setName(request.name());
|
task.setAssigned(request.assigned());
|
||||||
task.setReward(Math.round((request.reward() != null ? request.reward() : 0.0) * 100.0));
|
taskRepository.save(task);
|
||||||
task.setAssigned(request.assigned());
|
return true;
|
||||||
taskRepository.save(task);
|
}
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
Task task = opt.get();
|
Task task = opt.get();
|
||||||
long reward = task.getReward();
|
long reward = task.getReward();
|
||||||
String rewardName = task.getName();
|
String rewardName = task.getName();
|
||||||
|
|
||||||
// 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());
|
history.setTimestamp(Instant.now().getEpochSecond());
|
||||||
history.setTimestamp(Instant.now().getEpochSecond());
|
history.setAmount(reward);
|
||||||
history.setAmount(reward);
|
history.setDescription("Task completed: " + rewardName);
|
||||||
history.setDescription("Task completed: " + rewardName);
|
historyRepository.save(history);
|
||||||
historyRepository.save(history);
|
|
||||||
|
|
||||||
// Distribute reward
|
// Distribute reward
|
||||||
allowanceService.addDistributedReward(user.getId(), (int) reward);
|
allowanceService.addDistributedReward(user.getId(), (int) reward);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mark task as completed
|
// Mark task as completed
|
||||||
task.setCompleted(Instant.now().getEpochSecond());
|
task.setCompleted(Instant.now().getEpochSecond());
|
||||||
taskRepository.save(task);
|
taskRepository.save(task);
|
||||||
|
|
||||||
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(),
|
t.getReward() / 100.0,
|
||||||
t.getReward() / 100.0,
|
t.getAssigned(),
|
||||||
t.getAssigned(),
|
t.getSchedule());
|
||||||
t.getSchedule());
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,94 +9,78 @@ 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");
|
}
|
||||||
}
|
|
||||||
|
|
||||||
from.setBalance(from.getBalance() - transfer);
|
from.setBalance(from.getBalance() - transfer);
|
||||||
to.setBalance(to.getBalance() + transfer);
|
to.setBalance(to.getBalance() + transfer);
|
||||||
allowanceRepository.save(from);
|
allowanceRepository.save(from);
|
||||||
allowanceRepository.save(to);
|
allowanceRepository.save(to);
|
||||||
|
|
||||||
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
|
return new TransferResult(Status.SUCCESS, "Transfer successful");
|
||||||
{
|
}
|
||||||
SUCCESS, BAD_REQUEST, NOT_FOUND
|
|
||||||
}
|
|
||||||
|
|
||||||
public static TransferResult success()
|
public static TransferResult badRequest(String message) {
|
||||||
{
|
return new TransferResult(Status.BAD_REQUEST, message);
|
||||||
return new TransferResult(Status.SUCCESS, "Transfer successful");
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public static TransferResult badRequest(String message)
|
public static TransferResult notFound() {
|
||||||
{
|
return new TransferResult(Status.NOT_FOUND, "Allowance not found");
|
||||||
return new TransferResult(Status.BAD_REQUEST, message);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public static TransferResult notFound()
|
public enum Status {
|
||||||
{
|
SUCCESS, BAD_REQUEST, NOT_FOUND
|
||||||
return new TransferResult(Status.NOT_FOUND, "Allowance 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,34 +9,29 @@ 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 ->
|
{
|
||||||
{
|
long totalAmount = userRepository.sumHistoryAmount(userId);
|
||||||
long totalAmount = userRepository.sumHistoryAmount(userId);
|
return new UserWithAllowanceDto(u.getId(), u.getName(), totalAmount / 100.0);
|
||||||
return new UserWithAllowanceDto(u.getId(), u.getName(), totalAmount / 100.0);
|
});
|
||||||
});
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public boolean userExists(int userId)
|
public boolean userExists(int userId) {
|
||||||
{
|
return userRepository.existsById(userId);
|
||||||
return userRepository.existsById(userId);
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,42 +1,33 @@
|
|||||||
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) == '#') {
|
||||||
}
|
colourStr = colourStr.substring(1);
|
||||||
if (colourStr.charAt(0) == '#')
|
}
|
||||||
{
|
if (colourStr.length() != 6 && colourStr.length() != 3) {
|
||||||
colourStr = colourStr.substring(1);
|
throw new IllegalArgumentException("colour must be a valid hex string");
|
||||||
}
|
}
|
||||||
if (colourStr.length() != 6 && colourStr.length() != 3)
|
int colour = Integer.parseInt(colourStr, 16);
|
||||||
{
|
if (colourStr.length() == 3) {
|
||||||
throw new IllegalArgumentException("colour must be a valid hex string");
|
int r = (colour & 0xF00) >> 8;
|
||||||
}
|
int g = (colour & 0x0F0) >> 4;
|
||||||
int colour = Integer.parseInt(colourStr, 16);
|
int b = (colour & 0x00F);
|
||||||
if (colourStr.length() == 3)
|
colour = (r << 20) | (g << 12) | (b << 4);
|
||||||
{
|
}
|
||||||
int r = (colour & 0xF00) >> 8;
|
return colour;
|
||||||
int g = (colour & 0x0F0) >> 4;
|
}
|
||||||
int b = (colour & 0x00F);
|
|
||||||
colour = (r << 20) | (g << 12) | (b << 4);
|
|
||||||
}
|
|
||||||
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>
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -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