Skip to content

Commit bd21a51

Browse files
authored
[conluz-109] Created endpoints to enable and disable supplies (#115)
1 parent 0ba2c4f commit bd21a51

File tree

14 files changed

+812
-0
lines changed

14 files changed

+812
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.lucoenergia.conluz.domain.admin.supply.disable;
2+
3+
import org.lucoenergia.conluz.domain.admin.supply.Supply;
4+
import org.lucoenergia.conluz.domain.shared.SupplyId;
5+
6+
public interface DisableSupplyRepository {
7+
8+
Supply disable(SupplyId id);
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.lucoenergia.conluz.domain.admin.supply.disable;
2+
3+
import org.lucoenergia.conluz.domain.admin.supply.Supply;
4+
import org.lucoenergia.conluz.domain.shared.SupplyId;
5+
6+
public interface DisableSupplyService {
7+
8+
Supply disable(SupplyId id);
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.lucoenergia.conluz.domain.admin.supply.enable;
2+
3+
import org.lucoenergia.conluz.domain.admin.supply.Supply;
4+
import org.lucoenergia.conluz.domain.shared.SupplyId;
5+
6+
public interface EnableSupplyRepository {
7+
8+
Supply enable(SupplyId id);
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.lucoenergia.conluz.domain.admin.supply.enable;
2+
3+
import org.lucoenergia.conluz.domain.admin.supply.Supply;
4+
import org.lucoenergia.conluz.domain.shared.SupplyId;
5+
6+
public interface EnableSupplyService {
7+
8+
Supply enable(SupplyId id);
9+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.lucoenergia.conluz.infrastructure.admin.supply.disable;
2+
3+
import io.swagger.v3.oas.annotations.Operation;
4+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
5+
import io.swagger.v3.oas.annotations.responses.ApiResponses;
6+
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
7+
import org.lucoenergia.conluz.domain.admin.supply.Supply;
8+
import org.lucoenergia.conluz.domain.admin.supply.disable.DisableSupplyService;
9+
import org.lucoenergia.conluz.domain.shared.SupplyId;
10+
import org.lucoenergia.conluz.infrastructure.admin.supply.SupplyResponse;
11+
import org.lucoenergia.conluz.infrastructure.shared.web.apidocs.ApiTag;
12+
import org.lucoenergia.conluz.infrastructure.shared.web.apidocs.response.*;
13+
import org.springframework.security.access.prepost.PreAuthorize;
14+
import org.springframework.web.bind.annotation.PathVariable;
15+
import org.springframework.web.bind.annotation.PostMapping;
16+
import org.springframework.web.bind.annotation.RequestMapping;
17+
import org.springframework.web.bind.annotation.RestController;
18+
19+
import java.util.UUID;
20+
21+
@RestController
22+
@RequestMapping("/api/v1")
23+
public class DisableSupplyController {
24+
25+
private final DisableSupplyService service;
26+
27+
public DisableSupplyController(DisableSupplyService service) {
28+
this.service = service;
29+
}
30+
31+
@PostMapping(path = "/supplies/{id}/disable")
32+
@Operation(
33+
summary = "Disables a supply by ID",
34+
description = """
35+
This endpoint disables a supply by its unique identifier.
36+
37+
Authentication via bearer token is required.
38+
Required Role: ADMIN
39+
40+
The operation is idempotent: disabling an already disabled supply will not fail and will return the current state.
41+
""",
42+
tags = ApiTag.SUPPLIES,
43+
operationId = "disableSupply",
44+
security = @SecurityRequirement(name = "bearerToken", scopes = {"ADMIN"})
45+
)
46+
@ApiResponses(value = {
47+
@ApiResponse(
48+
responseCode = "200",
49+
description = "Supply disabled successfully",
50+
useReturnTypeSchema = true
51+
)
52+
})
53+
@ForbiddenErrorResponse
54+
@UnauthorizedErrorResponse
55+
@BadRequestErrorResponse
56+
@InternalServerErrorResponse
57+
@NotFoundErrorResponse
58+
@PreAuthorize("hasRole('ADMIN')")
59+
public SupplyResponse disableSupply(@PathVariable("id") UUID supplyId) {
60+
Supply updated = service.disable(SupplyId.of(supplyId));
61+
return new SupplyResponse(updated);
62+
}
63+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.lucoenergia.conluz.infrastructure.admin.supply.disable;
2+
3+
import org.lucoenergia.conluz.domain.admin.supply.Supply;
4+
import org.lucoenergia.conluz.domain.admin.supply.SupplyNotFoundException;
5+
import org.lucoenergia.conluz.domain.admin.supply.disable.DisableSupplyRepository;
6+
import org.lucoenergia.conluz.domain.shared.SupplyId;
7+
import org.lucoenergia.conluz.infrastructure.admin.supply.SupplyEntity;
8+
import org.lucoenergia.conluz.infrastructure.admin.supply.SupplyEntityMapper;
9+
import org.lucoenergia.conluz.infrastructure.admin.supply.SupplyRepository;
10+
import org.springframework.stereotype.Repository;
11+
import org.springframework.transaction.annotation.Transactional;
12+
13+
import java.util.Optional;
14+
import java.util.UUID;
15+
16+
@Transactional
17+
@Repository
18+
public class DisableSupplyRepositoryDatabase implements DisableSupplyRepository {
19+
20+
private final SupplyRepository repository;
21+
private final SupplyEntityMapper mapper;
22+
23+
public DisableSupplyRepositoryDatabase(SupplyRepository repository, SupplyEntityMapper mapper) {
24+
this.repository = repository;
25+
this.mapper = mapper;
26+
}
27+
28+
@Override
29+
public Supply disable(SupplyId id) {
30+
UUID supplyUuid = id.getId();
31+
Optional<SupplyEntity> result = repository.findById(supplyUuid);
32+
if (result.isEmpty()) {
33+
throw new SupplyNotFoundException(id);
34+
}
35+
SupplyEntity entity = result.get();
36+
if (Boolean.FALSE.equals(entity.getEnabled())) {
37+
// idempotent: already disabled
38+
return mapper.map(entity);
39+
}
40+
entity.setEnabled(false);
41+
return mapper.map(repository.save(entity));
42+
}
43+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.lucoenergia.conluz.infrastructure.admin.supply.disable;
2+
3+
import org.lucoenergia.conluz.domain.admin.supply.Supply;
4+
import org.lucoenergia.conluz.domain.admin.supply.disable.DisableSupplyRepository;
5+
import org.lucoenergia.conluz.domain.admin.supply.disable.DisableSupplyService;
6+
import org.lucoenergia.conluz.domain.shared.SupplyId;
7+
import org.springframework.stereotype.Service;
8+
import org.springframework.transaction.annotation.Transactional;
9+
10+
@Transactional
11+
@Service
12+
public class DisableSupplyServiceImpl implements DisableSupplyService {
13+
14+
private final DisableSupplyRepository repository;
15+
16+
public DisableSupplyServiceImpl(DisableSupplyRepository repository) {
17+
this.repository = repository;
18+
}
19+
20+
@Override
21+
public Supply disable(SupplyId id) {
22+
return repository.disable(id);
23+
}
24+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.lucoenergia.conluz.infrastructure.admin.supply.enable;
2+
3+
import io.swagger.v3.oas.annotations.Operation;
4+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
5+
import io.swagger.v3.oas.annotations.responses.ApiResponses;
6+
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
7+
import org.lucoenergia.conluz.domain.admin.supply.Supply;
8+
import org.lucoenergia.conluz.domain.admin.supply.enable.EnableSupplyService;
9+
import org.lucoenergia.conluz.domain.shared.SupplyId;
10+
import org.lucoenergia.conluz.infrastructure.admin.supply.SupplyResponse;
11+
import org.lucoenergia.conluz.infrastructure.shared.web.apidocs.ApiTag;
12+
import org.lucoenergia.conluz.infrastructure.shared.web.apidocs.response.*;
13+
import org.springframework.security.access.prepost.PreAuthorize;
14+
import org.springframework.web.bind.annotation.PathVariable;
15+
import org.springframework.web.bind.annotation.PostMapping;
16+
import org.springframework.web.bind.annotation.RequestMapping;
17+
import org.springframework.web.bind.annotation.RestController;
18+
19+
import java.util.UUID;
20+
21+
@RestController
22+
@RequestMapping("/api/v1")
23+
public class EnableSupplyController {
24+
25+
private final EnableSupplyService service;
26+
27+
public EnableSupplyController(EnableSupplyService service) {
28+
this.service = service;
29+
}
30+
31+
@PostMapping(path = "/supplies/{id}/enable")
32+
@Operation(
33+
summary = "Enables a supply by ID",
34+
description = """
35+
This endpoint enables a supply by its unique identifier.
36+
37+
Authentication via bearer token is required.
38+
Required Role: ADMIN
39+
40+
The operation is idempotent: enabling an already enabled supply will not fail and will return the current state.
41+
""",
42+
tags = ApiTag.SUPPLIES,
43+
operationId = "enableSupply",
44+
security = @SecurityRequirement(name = "bearerToken", scopes = {"ADMIN"})
45+
)
46+
@ApiResponses(value = {
47+
@ApiResponse(
48+
responseCode = "200",
49+
description = "Supply enabled successfully",
50+
useReturnTypeSchema = true
51+
)
52+
})
53+
@ForbiddenErrorResponse
54+
@UnauthorizedErrorResponse
55+
@BadRequestErrorResponse
56+
@InternalServerErrorResponse
57+
@NotFoundErrorResponse
58+
@PreAuthorize("hasRole('ADMIN')")
59+
public SupplyResponse enableSupply(@PathVariable("id") UUID supplyId) {
60+
Supply updated = service.enable(SupplyId.of(supplyId));
61+
return new SupplyResponse(updated);
62+
}
63+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.lucoenergia.conluz.infrastructure.admin.supply.enable;
2+
3+
import org.lucoenergia.conluz.domain.admin.supply.Supply;
4+
import org.lucoenergia.conluz.domain.admin.supply.SupplyNotFoundException;
5+
import org.lucoenergia.conluz.domain.admin.supply.enable.EnableSupplyRepository;
6+
import org.lucoenergia.conluz.domain.shared.SupplyId;
7+
import org.lucoenergia.conluz.infrastructure.admin.supply.SupplyEntity;
8+
import org.lucoenergia.conluz.infrastructure.admin.supply.SupplyEntityMapper;
9+
import org.lucoenergia.conluz.infrastructure.admin.supply.SupplyRepository;
10+
import org.springframework.stereotype.Repository;
11+
import org.springframework.transaction.annotation.Transactional;
12+
13+
import java.util.Optional;
14+
import java.util.UUID;
15+
16+
@Transactional
17+
@Repository
18+
public class EnableSupplyRepositoryDatabase implements EnableSupplyRepository {
19+
20+
private final SupplyRepository repository;
21+
private final SupplyEntityMapper mapper;
22+
23+
public EnableSupplyRepositoryDatabase(SupplyRepository repository, SupplyEntityMapper mapper) {
24+
this.repository = repository;
25+
this.mapper = mapper;
26+
}
27+
28+
@Override
29+
public Supply enable(SupplyId id) {
30+
UUID supplyUuid = id.getId();
31+
Optional<SupplyEntity> result = repository.findById(supplyUuid);
32+
if (result.isEmpty()) {
33+
throw new SupplyNotFoundException(id);
34+
}
35+
SupplyEntity entity = result.get();
36+
if (Boolean.TRUE.equals(entity.getEnabled())) {
37+
// idempotent: already enabled
38+
return mapper.map(entity);
39+
}
40+
entity.setEnabled(true);
41+
return mapper.map(repository.save(entity));
42+
}
43+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.lucoenergia.conluz.infrastructure.admin.supply.enable;
2+
3+
import org.lucoenergia.conluz.domain.admin.supply.Supply;
4+
import org.lucoenergia.conluz.domain.admin.supply.enable.EnableSupplyRepository;
5+
import org.lucoenergia.conluz.domain.admin.supply.enable.EnableSupplyService;
6+
import org.lucoenergia.conluz.domain.shared.SupplyId;
7+
import org.springframework.stereotype.Service;
8+
import org.springframework.transaction.annotation.Transactional;
9+
10+
@Transactional
11+
@Service
12+
public class EnableSupplyServiceImpl implements EnableSupplyService {
13+
14+
private final EnableSupplyRepository repository;
15+
16+
public EnableSupplyServiceImpl(EnableSupplyRepository repository) {
17+
this.repository = repository;
18+
}
19+
20+
@Override
21+
public Supply enable(SupplyId id) {
22+
return repository.enable(id);
23+
}
24+
}

0 commit comments

Comments
 (0)