Add router CRUD API
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package com.litoralregas.vpnprovisioner.router.controller;
|
||||
|
||||
import com.litoralregas.vpnprovisioner.router.dto.CreateRouterRequest;
|
||||
import com.litoralregas.vpnprovisioner.router.dto.RouterResponse;
|
||||
import com.litoralregas.vpnprovisioner.router.dto.UpdateRouterRequest;
|
||||
import com.litoralregas.vpnprovisioner.router.service.RouterService;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/routers")
|
||||
public class RouterController {
|
||||
|
||||
private final RouterService routerService;
|
||||
|
||||
public RouterController(RouterService routerService) {
|
||||
this.routerService = routerService;
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public RouterResponse create(@Valid @RequestBody CreateRouterRequest request) {
|
||||
return routerService.create(request);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<RouterResponse> findAll() {
|
||||
return routerService.findAll();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public RouterResponse findById(@PathVariable UUID id) {
|
||||
return routerService.findById(id);
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public RouterResponse update(
|
||||
@PathVariable UUID id,
|
||||
@Valid @RequestBody UpdateRouterRequest request
|
||||
) {
|
||||
return routerService.update(id, request);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
@ResponseStatus(HttpStatus.NO_CONTENT)
|
||||
public void delete(@PathVariable UUID id) {
|
||||
routerService.delete(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.litoralregas.vpnprovisioner.router.dto;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
public record CreateRouterRequest(
|
||||
@NotBlank String name,
|
||||
String hardwareModel,
|
||||
String firmwareVersion
|
||||
) {
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.litoralregas.vpnprovisioner.router.dto;
|
||||
|
||||
import com.litoralregas.vpnprovisioner.router.entity.RouterStatus;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.UUID;
|
||||
|
||||
public record RouterResponse(
|
||||
UUID id,
|
||||
String name,
|
||||
String hardwareModel,
|
||||
String firmwareVersion,
|
||||
RouterStatus status,
|
||||
String vpnIp,
|
||||
Instant createdAt,
|
||||
Instant updatedAt
|
||||
) {
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.litoralregas.vpnprovisioner.router.dto;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
public record UpdateRouterRequest(
|
||||
@NotBlank String name,
|
||||
String hardwareModel,
|
||||
String firmwareVersion
|
||||
) {
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.litoralregas.vpnprovisioner.router.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.UUID;
|
||||
|
||||
@Entity
|
||||
@Table(name = "routers")
|
||||
public class Router {
|
||||
|
||||
@Id
|
||||
private UUID id;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String name;
|
||||
|
||||
private String hardwareModel;
|
||||
|
||||
private String firmwareVersion;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
@Column(nullable = false)
|
||||
private RouterStatus status;
|
||||
|
||||
private String vpnIp;
|
||||
|
||||
@Column(nullable = false)
|
||||
private Instant createdAt;
|
||||
|
||||
@Column(nullable = false)
|
||||
private Instant updatedAt;
|
||||
|
||||
protected Router() {
|
||||
}
|
||||
|
||||
public Router(String name, String hardwareModel, String firmwareVersion) {
|
||||
this.id = UUID.randomUUID();
|
||||
this.name = name;
|
||||
this.hardwareModel = hardwareModel;
|
||||
this.firmwareVersion = firmwareVersion;
|
||||
this.status = RouterStatus.PENDING;
|
||||
this.createdAt = Instant.now();
|
||||
this.updatedAt = Instant.now();
|
||||
}
|
||||
|
||||
@PreUpdate
|
||||
public void preUpdate() {
|
||||
this.updatedAt = Instant.now();
|
||||
}
|
||||
|
||||
public UUID getId() { return id; }
|
||||
public String getName() { return name; }
|
||||
public String getHardwareModel() { return hardwareModel; }
|
||||
public String getFirmwareVersion() { return firmwareVersion; }
|
||||
public RouterStatus getStatus() { return status; }
|
||||
public String getVpnIp() { return vpnIp; }
|
||||
public Instant getCreatedAt() { return createdAt; }
|
||||
public Instant getUpdatedAt() { return updatedAt; }
|
||||
|
||||
public void updateDetails(String name, String hardwareModel, String firmwareVersion) {
|
||||
this.name = name;
|
||||
this.hardwareModel = hardwareModel;
|
||||
this.firmwareVersion = firmwareVersion;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.litoralregas.vpnprovisioner.router.entity;
|
||||
|
||||
public enum RouterStatus {
|
||||
PENDING,
|
||||
CONFIGURED,
|
||||
PROVISIONING,
|
||||
PROVISIONED,
|
||||
FAILED,
|
||||
REMOVED
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.litoralregas.vpnprovisioner.router.repository;
|
||||
|
||||
import com.litoralregas.vpnprovisioner.router.entity.Router;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public interface RouterRepository extends JpaRepository<Router, UUID> {
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package com.litoralregas.vpnprovisioner.router.service;
|
||||
|
||||
import com.litoralregas.vpnprovisioner.router.dto.CreateRouterRequest;
|
||||
import com.litoralregas.vpnprovisioner.router.dto.RouterResponse;
|
||||
import com.litoralregas.vpnprovisioner.router.dto.UpdateRouterRequest;
|
||||
import com.litoralregas.vpnprovisioner.router.entity.Router;
|
||||
import com.litoralregas.vpnprovisioner.router.repository.RouterRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
public class RouterService {
|
||||
|
||||
private final RouterRepository routerRepository;
|
||||
|
||||
public RouterService(RouterRepository routerRepository) {
|
||||
this.routerRepository = routerRepository;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public RouterResponse create(CreateRouterRequest request) {
|
||||
Router router = new Router(
|
||||
request.name(),
|
||||
request.hardwareModel(),
|
||||
request.firmwareVersion()
|
||||
);
|
||||
|
||||
Router saved = routerRepository.save(router);
|
||||
return toResponse(saved);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<RouterResponse> findAll() {
|
||||
return routerRepository.findAll()
|
||||
.stream()
|
||||
.map(this::toResponse)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public RouterResponse findById(UUID id) {
|
||||
Router router = routerRepository.findById(id)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Router not found: " + id));
|
||||
|
||||
return toResponse(router);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public RouterResponse update(UUID id, UpdateRouterRequest request) {
|
||||
Router router = routerRepository.findById(id)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Router not found: " + id));
|
||||
|
||||
router.updateDetails(
|
||||
request.name(),
|
||||
request.hardwareModel(),
|
||||
request.firmwareVersion()
|
||||
);
|
||||
|
||||
return toResponse(router);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void delete(UUID id) {
|
||||
Router router = routerRepository.findById(id)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Router not found: " + id));
|
||||
|
||||
routerRepository.delete(router);
|
||||
}
|
||||
|
||||
private RouterResponse toResponse(Router router) {
|
||||
return new RouterResponse(
|
||||
router.getId(),
|
||||
router.getName(),
|
||||
router.getHardwareModel(),
|
||||
router.getFirmwareVersion(),
|
||||
router.getStatus(),
|
||||
router.getVpnIp(),
|
||||
router.getCreatedAt(),
|
||||
router.getUpdatedAt()
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user