Split API controller
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
package be.seeseepuff.allowanceplanner.controller;
|
||||
|
||||
import be.seeseepuff.allowanceplanner.dto.*;
|
||||
import be.seeseepuff.allowanceplanner.service.*;
|
||||
import be.seeseepuff.allowanceplanner.service.AllowanceService;
|
||||
import be.seeseepuff.allowanceplanner.service.UserService;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@@ -12,86 +13,15 @@ import java.util.Optional;
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
@CrossOrigin(origins = "*")
|
||||
public class ApiController {
|
||||
public class AllowanceController {
|
||||
private final UserService userService;
|
||||
private final AllowanceService allowanceService;
|
||||
private final TaskService taskService;
|
||||
private final TransferService transferService;
|
||||
private final MigrationService migrationService;
|
||||
|
||||
public ApiController(UserService userService,
|
||||
AllowanceService allowanceService,
|
||||
TaskService taskService,
|
||||
TransferService transferService,
|
||||
MigrationService migrationService) {
|
||||
public AllowanceController(UserService userService, AllowanceService allowanceService) {
|
||||
this.userService = userService;
|
||||
this.allowanceService = allowanceService;
|
||||
this.taskService = taskService;
|
||||
this.transferService = transferService;
|
||||
this.migrationService = migrationService;
|
||||
}
|
||||
|
||||
// ---- Users ----
|
||||
|
||||
@GetMapping("/users")
|
||||
public List<UserDto> getUsers() {
|
||||
return userService.getUsers();
|
||||
}
|
||||
|
||||
@GetMapping("/user/{userId}")
|
||||
public ResponseEntity<?> getUser(@PathVariable String userId) {
|
||||
int id;
|
||||
try {
|
||||
id = Integer.parseInt(userId);
|
||||
} catch (NumberFormatException e) {
|
||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
||||
}
|
||||
|
||||
Optional<UserWithAllowanceDto> user = userService.getUser(id);
|
||||
if (user.isEmpty()) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("User not found"));
|
||||
}
|
||||
return ResponseEntity.ok(user.get());
|
||||
}
|
||||
|
||||
// ---- History ----
|
||||
|
||||
@PostMapping("/user/{userId}/history")
|
||||
public ResponseEntity<?> postHistory(@PathVariable String userId, @RequestBody PostHistoryRequest request) {
|
||||
int id;
|
||||
try {
|
||||
id = Integer.parseInt(userId);
|
||||
} catch (NumberFormatException e) {
|
||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
||||
}
|
||||
|
||||
if (request.description() == null || request.description().isEmpty()) {
|
||||
return ResponseEntity.badRequest().body(new ErrorResponse("Description cannot be empty"));
|
||||
}
|
||||
|
||||
if (!userService.userExists(id)) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("User not found"));
|
||||
}
|
||||
|
||||
allowanceService.addHistory(id, request);
|
||||
return ResponseEntity.ok(new MessageResponse("History updated successfully"));
|
||||
}
|
||||
|
||||
@GetMapping("/user/{userId}/history")
|
||||
public ResponseEntity<?> getHistory(@PathVariable String userId) {
|
||||
int id;
|
||||
try {
|
||||
id = Integer.parseInt(userId);
|
||||
} catch (NumberFormatException e) {
|
||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
||||
}
|
||||
|
||||
List<HistoryDto> history = allowanceService.getHistory(id);
|
||||
return ResponseEntity.ok(history);
|
||||
}
|
||||
|
||||
// ---- Allowances ----
|
||||
|
||||
@GetMapping("/user/{userId}/allowance")
|
||||
public ResponseEntity<?> getUserAllowance(@PathVariable String userId) {
|
||||
int id;
|
||||
@@ -288,118 +218,4 @@ public class ApiController {
|
||||
}
|
||||
return ResponseEntity.ok(new MessageResponse("Allowance completed successfully"));
|
||||
}
|
||||
|
||||
// ---- Tasks ----
|
||||
|
||||
@PostMapping("/tasks")
|
||||
public ResponseEntity<?> createTask(@RequestBody CreateTaskRequest request) {
|
||||
if (request.name() == null || request.name().isEmpty()) {
|
||||
return ResponseEntity.badRequest().body(new ErrorResponse("Task name cannot be empty"));
|
||||
}
|
||||
|
||||
if (request.schedule() != null) {
|
||||
return ResponseEntity.badRequest().body(new ErrorResponse("Schedules are not yet supported"));
|
||||
}
|
||||
|
||||
if (request.assigned() != null) {
|
||||
if (!userService.userExists(request.assigned())) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("User not found"));
|
||||
}
|
||||
}
|
||||
|
||||
int taskId = taskService.createTask(request);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(new IdResponse(taskId));
|
||||
}
|
||||
|
||||
@GetMapping("/tasks")
|
||||
public List<TaskDto> getTasks() {
|
||||
return taskService.getTasks();
|
||||
}
|
||||
|
||||
@GetMapping("/task/{taskId}")
|
||||
public ResponseEntity<?> getTask(@PathVariable String taskId) {
|
||||
int id;
|
||||
try {
|
||||
id = Integer.parseInt(taskId);
|
||||
} catch (NumberFormatException e) {
|
||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid task ID"));
|
||||
}
|
||||
|
||||
Optional<TaskDto> task = taskService.getTask(id);
|
||||
if (task.isEmpty()) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("Task not found"));
|
||||
}
|
||||
return ResponseEntity.ok(task.get());
|
||||
}
|
||||
|
||||
@PutMapping("/task/{taskId}")
|
||||
public ResponseEntity<?> putTask(@PathVariable String taskId, @RequestBody CreateTaskRequest request) {
|
||||
int id;
|
||||
try {
|
||||
id = Integer.parseInt(taskId);
|
||||
} catch (NumberFormatException e) {
|
||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid task ID"));
|
||||
}
|
||||
|
||||
Optional<TaskDto> existing = taskService.getTask(id);
|
||||
if (existing.isEmpty()) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("Task not found"));
|
||||
}
|
||||
|
||||
taskService.updateTask(id, request);
|
||||
return ResponseEntity.ok(new MessageResponse("Task updated successfully"));
|
||||
}
|
||||
|
||||
@DeleteMapping("/task/{taskId}")
|
||||
public ResponseEntity<?> deleteTask(@PathVariable String taskId) {
|
||||
int id;
|
||||
try {
|
||||
id = Integer.parseInt(taskId);
|
||||
} catch (NumberFormatException e) {
|
||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid task ID"));
|
||||
}
|
||||
|
||||
if (!taskService.hasTask(id)) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("Task not found"));
|
||||
}
|
||||
|
||||
taskService.deleteTask(id);
|
||||
return ResponseEntity.ok(new MessageResponse("Task deleted successfully"));
|
||||
}
|
||||
|
||||
@PostMapping("/task/{taskId}/complete")
|
||||
public ResponseEntity<?> completeTask(@PathVariable String taskId) {
|
||||
int id;
|
||||
try {
|
||||
id = Integer.parseInt(taskId);
|
||||
} catch (NumberFormatException e) {
|
||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid task ID"));
|
||||
}
|
||||
|
||||
boolean completed = taskService.completeTask(id);
|
||||
if (!completed) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("Task not found"));
|
||||
}
|
||||
return ResponseEntity.ok(new MessageResponse("Task completed successfully"));
|
||||
}
|
||||
|
||||
// ---- Transfer ----
|
||||
|
||||
@PostMapping("/transfer")
|
||||
public ResponseEntity<?> transfer(@RequestBody TransferRequest request) {
|
||||
TransferService.TransferResult result = transferService.transfer(request);
|
||||
return switch (result.status()) {
|
||||
case SUCCESS -> ResponseEntity.ok(new MessageResponse(result.message()));
|
||||
case BAD_REQUEST -> ResponseEntity.badRequest().body(new ErrorResponse(result.message()));
|
||||
case NOT_FOUND -> ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse(result.message()));
|
||||
};
|
||||
}
|
||||
|
||||
// ---- Migration ----
|
||||
|
||||
@PostMapping("/import")
|
||||
public ResponseEntity<?> importData(@RequestBody MigrationDto data) {
|
||||
migrationService.importData(data);
|
||||
return ResponseEntity.ok(new MessageResponse("Import successful"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package be.seeseepuff.allowanceplanner.controller;
|
||||
|
||||
import be.seeseepuff.allowanceplanner.dto.*;
|
||||
import be.seeseepuff.allowanceplanner.service.AllowanceService;
|
||||
import be.seeseepuff.allowanceplanner.service.UserService;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
@CrossOrigin(origins = "*")
|
||||
public class HistoryController {
|
||||
private final UserService userService;
|
||||
private final AllowanceService allowanceService;
|
||||
|
||||
public HistoryController(UserService userService, AllowanceService allowanceService) {
|
||||
this.userService = userService;
|
||||
this.allowanceService = allowanceService;
|
||||
}
|
||||
|
||||
@PostMapping("/user/{userId}/history")
|
||||
public ResponseEntity<?> postHistory(@PathVariable String userId, @RequestBody PostHistoryRequest request) {
|
||||
int id;
|
||||
try {
|
||||
id = Integer.parseInt(userId);
|
||||
} catch (NumberFormatException e) {
|
||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
||||
}
|
||||
|
||||
if (request.description() == null || request.description().isEmpty()) {
|
||||
return ResponseEntity.badRequest().body(new ErrorResponse("Description cannot be empty"));
|
||||
}
|
||||
|
||||
if (!userService.userExists(id)) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("User not found"));
|
||||
}
|
||||
|
||||
allowanceService.addHistory(id, request);
|
||||
return ResponseEntity.ok(new MessageResponse("History updated successfully"));
|
||||
}
|
||||
|
||||
@GetMapping("/user/{userId}/history")
|
||||
public ResponseEntity<?> getHistory(@PathVariable String userId) {
|
||||
int id;
|
||||
try {
|
||||
id = Integer.parseInt(userId);
|
||||
} catch (NumberFormatException e) {
|
||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
||||
}
|
||||
|
||||
List<HistoryDto> history = allowanceService.getHistory(id);
|
||||
return ResponseEntity.ok(history);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package be.seeseepuff.allowanceplanner.controller;
|
||||
|
||||
import be.seeseepuff.allowanceplanner.dto.*;
|
||||
import be.seeseepuff.allowanceplanner.service.MigrationService;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
@CrossOrigin(origins = "*")
|
||||
public class MigrationController {
|
||||
private final MigrationService migrationService;
|
||||
|
||||
public MigrationController(MigrationService migrationService) {
|
||||
this.migrationService = migrationService;
|
||||
}
|
||||
|
||||
@PostMapping("/import")
|
||||
public ResponseEntity<?> importData(@RequestBody MigrationDto data) {
|
||||
migrationService.importData(data);
|
||||
return ResponseEntity.ok(new MessageResponse("Import successful"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package be.seeseepuff.allowanceplanner.controller;
|
||||
|
||||
import be.seeseepuff.allowanceplanner.dto.*;
|
||||
import be.seeseepuff.allowanceplanner.service.TaskService;
|
||||
import be.seeseepuff.allowanceplanner.service.UserService;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
@CrossOrigin(origins = "*")
|
||||
public class TaskController {
|
||||
private final UserService userService;
|
||||
private final TaskService taskService;
|
||||
|
||||
public TaskController(UserService userService, TaskService taskService) {
|
||||
this.userService = userService;
|
||||
this.taskService = taskService;
|
||||
}
|
||||
|
||||
@PostMapping("/tasks")
|
||||
public ResponseEntity<?> createTask(@RequestBody CreateTaskRequest request) {
|
||||
if (request.name() == null || request.name().isEmpty()) {
|
||||
return ResponseEntity.badRequest().body(new ErrorResponse("Task name cannot be empty"));
|
||||
}
|
||||
|
||||
if (request.schedule() != null) {
|
||||
return ResponseEntity.badRequest().body(new ErrorResponse("Schedules are not yet supported"));
|
||||
}
|
||||
|
||||
if (request.assigned() != null) {
|
||||
if (!userService.userExists(request.assigned())) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("User not found"));
|
||||
}
|
||||
}
|
||||
|
||||
int taskId = taskService.createTask(request);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(new IdResponse(taskId));
|
||||
}
|
||||
|
||||
@GetMapping("/tasks")
|
||||
public List<TaskDto> getTasks() {
|
||||
return taskService.getTasks();
|
||||
}
|
||||
|
||||
@GetMapping("/task/{taskId}")
|
||||
public ResponseEntity<?> getTask(@PathVariable String taskId) {
|
||||
int id;
|
||||
try {
|
||||
id = Integer.parseInt(taskId);
|
||||
} catch (NumberFormatException e) {
|
||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid task ID"));
|
||||
}
|
||||
|
||||
Optional<TaskDto> task = taskService.getTask(id);
|
||||
if (task.isEmpty()) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("Task not found"));
|
||||
}
|
||||
return ResponseEntity.ok(task.get());
|
||||
}
|
||||
|
||||
@PutMapping("/task/{taskId}")
|
||||
public ResponseEntity<?> putTask(@PathVariable String taskId, @RequestBody CreateTaskRequest request) {
|
||||
int id;
|
||||
try {
|
||||
id = Integer.parseInt(taskId);
|
||||
} catch (NumberFormatException e) {
|
||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid task ID"));
|
||||
}
|
||||
|
||||
Optional<TaskDto> existing = taskService.getTask(id);
|
||||
if (existing.isEmpty()) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("Task not found"));
|
||||
}
|
||||
|
||||
taskService.updateTask(id, request);
|
||||
return ResponseEntity.ok(new MessageResponse("Task updated successfully"));
|
||||
}
|
||||
|
||||
@DeleteMapping("/task/{taskId}")
|
||||
public ResponseEntity<?> deleteTask(@PathVariable String taskId) {
|
||||
int id;
|
||||
try {
|
||||
id = Integer.parseInt(taskId);
|
||||
} catch (NumberFormatException e) {
|
||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid task ID"));
|
||||
}
|
||||
|
||||
if (!taskService.hasTask(id)) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("Task not found"));
|
||||
}
|
||||
|
||||
taskService.deleteTask(id);
|
||||
return ResponseEntity.ok(new MessageResponse("Task deleted successfully"));
|
||||
}
|
||||
|
||||
@PostMapping("/task/{taskId}/complete")
|
||||
public ResponseEntity<?> completeTask(@PathVariable String taskId) {
|
||||
int id;
|
||||
try {
|
||||
id = Integer.parseInt(taskId);
|
||||
} catch (NumberFormatException e) {
|
||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid task ID"));
|
||||
}
|
||||
|
||||
boolean completed = taskService.completeTask(id);
|
||||
if (!completed) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("Task not found"));
|
||||
}
|
||||
return ResponseEntity.ok(new MessageResponse("Task completed successfully"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package be.seeseepuff.allowanceplanner.controller;
|
||||
|
||||
import be.seeseepuff.allowanceplanner.dto.*;
|
||||
import be.seeseepuff.allowanceplanner.service.TransferService;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
@CrossOrigin(origins = "*")
|
||||
public class TransferController {
|
||||
private final TransferService transferService;
|
||||
|
||||
public TransferController(TransferService transferService) {
|
||||
this.transferService = transferService;
|
||||
}
|
||||
|
||||
@PostMapping("/transfer")
|
||||
public ResponseEntity<?> transfer(@RequestBody TransferRequest request) {
|
||||
TransferService.TransferResult result = transferService.transfer(request);
|
||||
return switch (result.status()) {
|
||||
case SUCCESS -> ResponseEntity.ok(new MessageResponse(result.message()));
|
||||
case BAD_REQUEST -> ResponseEntity.badRequest().body(new ErrorResponse(result.message()));
|
||||
case NOT_FOUND -> ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse(result.message()));
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package be.seeseepuff.allowanceplanner.controller;
|
||||
|
||||
import be.seeseepuff.allowanceplanner.dto.*;
|
||||
import be.seeseepuff.allowanceplanner.service.UserService;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
@CrossOrigin(origins = "*")
|
||||
public class UserController {
|
||||
private final UserService userService;
|
||||
|
||||
public UserController(UserService userService) {
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
@GetMapping("/users")
|
||||
public List<UserDto> getUsers() {
|
||||
return userService.getUsers();
|
||||
}
|
||||
|
||||
@GetMapping("/user/{userId}")
|
||||
public ResponseEntity<?> getUser(@PathVariable String userId) {
|
||||
int id;
|
||||
try {
|
||||
id = Integer.parseInt(userId);
|
||||
} catch (NumberFormatException e) {
|
||||
return ResponseEntity.badRequest().body(new ErrorResponse("Invalid user ID"));
|
||||
}
|
||||
|
||||
Optional<UserWithAllowanceDto> user = userService.getUser(id);
|
||||
if (user.isEmpty()) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse("User not found"));
|
||||
}
|
||||
return ResponseEntity.ok(user.get());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user