-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[conluz-72] Implemented endpoint for updating supplies
- Loading branch information
1 parent
15ffa84
commit cdb6b2c
Showing
10 changed files
with
615 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/java/org/lucoenergia/conluz/domain/admin/supply/update/UpdateSupplyService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.lucoenergia.conluz.domain.admin.supply.update; | ||
|
||
import org.lucoenergia.conluz.domain.admin.supply.Supply; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class UpdateSupplyService { | ||
|
||
private final UpdateSupplyRepository repository; | ||
|
||
public UpdateSupplyService(UpdateSupplyRepository repository) { | ||
this.repository = repository; | ||
} | ||
|
||
public Supply update(Supply supply) { | ||
return repository.update(supply); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
...main/java/org/lucoenergia/conluz/infrastructure/admin/supply/update/UpdateSupplyBody.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package org.lucoenergia.conluz.infrastructure.admin.supply.update; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import jakarta.validation.constraints.NotEmpty; | ||
import jakarta.validation.constraints.Positive; | ||
import org.lucoenergia.conluz.domain.admin.supply.Supply; | ||
import org.lucoenergia.conluz.domain.admin.user.User; | ||
import org.lucoenergia.conluz.infrastructure.shared.time.DateConverter; | ||
|
||
import java.text.DateFormat; | ||
import java.util.UUID; | ||
|
||
@Schema(requiredProperties = { | ||
"code", "address", "partitionCoefficient" | ||
}) | ||
public class UpdateSupplyBody { | ||
|
||
@NotEmpty | ||
private String code; | ||
private String name; | ||
@NotEmpty | ||
private String address; | ||
@Positive | ||
private Float partitionCoefficient; | ||
private Boolean enabled; | ||
private String validDateFrom; | ||
private String distributor; | ||
private String distributorCode; | ||
private Integer pointType; | ||
private String shellyMac; | ||
private String shellyId; | ||
private String shellyMqttPrefix; | ||
|
||
|
||
public String getCode() { | ||
return code; | ||
} | ||
|
||
public void setCode(String code) { | ||
this.code = code; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getAddress() { | ||
return address; | ||
} | ||
|
||
public void setAddress(String address) { | ||
this.address = address; | ||
} | ||
|
||
public Float getPartitionCoefficient() { | ||
return partitionCoefficient; | ||
} | ||
|
||
public void setPartitionCoefficient(Float partitionCoefficient) { | ||
this.partitionCoefficient = partitionCoefficient; | ||
} | ||
|
||
public Boolean getEnabled() { | ||
return enabled; | ||
} | ||
|
||
public void setEnabled(Boolean enabled) { | ||
this.enabled = enabled; | ||
} | ||
|
||
public String getValidDateFrom() { | ||
return validDateFrom; | ||
} | ||
|
||
public void setValidDateFrom(String validDateFrom) { | ||
this.validDateFrom = validDateFrom; | ||
} | ||
|
||
public String getDistributor() { | ||
return distributor; | ||
} | ||
|
||
public void setDistributor(String distributor) { | ||
this.distributor = distributor; | ||
} | ||
|
||
public String getDistributorCode() { | ||
return distributorCode; | ||
} | ||
|
||
public void setDistributorCode(String distributorCode) { | ||
this.distributorCode = distributorCode; | ||
} | ||
|
||
public Integer getPointType() { | ||
return pointType; | ||
} | ||
|
||
public void setPointType(Integer pointType) { | ||
this.pointType = pointType; | ||
} | ||
|
||
public String getShellyMac() { | ||
return shellyMac; | ||
} | ||
|
||
public void setShellyMac(String shellyMac) { | ||
this.shellyMac = shellyMac; | ||
} | ||
|
||
public String getShellyId() { | ||
return shellyId; | ||
} | ||
|
||
public void setShellyId(String shellyId) { | ||
this.shellyId = shellyId; | ||
} | ||
|
||
public String getShellyMqttPrefix() { | ||
return shellyMqttPrefix; | ||
} | ||
|
||
public void setShellyMqttPrefix(String shellyMqttPrefix) { | ||
this.shellyMqttPrefix = shellyMqttPrefix; | ||
} | ||
|
||
public Supply mapToSupply(UUID supplyId) { | ||
Supply.Builder builder = new Supply.Builder(); | ||
builder.withId(supplyId) | ||
.withCode(code) | ||
.withName(name) | ||
.withAddress(address) | ||
.withPartitionCoefficient(partitionCoefficient) | ||
.withValidDateFrom(validDateFrom != null ? DateConverter.convertStringToLocalDate(validDateFrom, "yyyy-MM-dd") : null) | ||
.withDistributor(distributor) | ||
.withDistributorCode(distributorCode) | ||
.withPointType(pointType) | ||
.withShellyMac(shellyMac) | ||
.withShellyId(shellyId) | ||
.withShellyMqttPrefix(shellyMqttPrefix); | ||
return builder.build(); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...ava/org/lucoenergia/conluz/infrastructure/admin/supply/update/UpdateSupplyController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.lucoenergia.conluz.infrastructure.admin.supply.update; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import jakarta.validation.Valid; | ||
import org.lucoenergia.conluz.domain.admin.supply.update.UpdateSupplyService; | ||
import org.lucoenergia.conluz.infrastructure.admin.supply.SupplyResponse; | ||
import org.lucoenergia.conluz.infrastructure.shared.web.apidocs.ApiTag; | ||
import org.lucoenergia.conluz.infrastructure.shared.web.apidocs.response.*; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.UUID; | ||
|
||
/** | ||
* Updates an existing supply | ||
*/ | ||
@RestController | ||
@RequestMapping("/api/v1") | ||
@Validated | ||
public class UpdateSupplyController { | ||
|
||
private final UpdateSupplyService service; | ||
|
||
public UpdateSupplyController(UpdateSupplyService service) { | ||
this.service = service; | ||
} | ||
|
||
@PutMapping("/supplies/{id}") | ||
@Operation( | ||
summary = "Updates supply information", | ||
description = """ | ||
This endpoint enables the update of supply information by specifying the supply's unique identifier in the endpoint path. | ||
Clients send a request containing the updated supply details, and authentication, through an authentication token, is required for secure access. | ||
A successful update results in an HTTP status code of 200, indicating that the supply information has been successfully modified. In cases where the update encounters errors, the server responds with an appropriate error status code along with a descriptive error message to assist clients in addressing and resolving the issue. | ||
If you don't provide some of the optional parameters, they will be considered as null value so their values will be updated with a null value.""", | ||
tags = ApiTag.SUPPLIES, | ||
operationId = "updateSupply" | ||
) | ||
@ApiResponses(value = { | ||
@ApiResponse( | ||
responseCode = "200", | ||
description = "Supply successfully updated.", | ||
useReturnTypeSchema = true | ||
) | ||
}) | ||
@ForbiddenErrorResponse | ||
@UnauthorizedErrorResponse | ||
@BadRequestErrorResponse | ||
@InternalServerErrorResponse | ||
@NotFoundErrorResponse | ||
public SupplyResponse updateSupply(@PathVariable("id") UUID supplyId, @Valid @RequestBody UpdateSupplyBody body) { | ||
return new SupplyResponse(service.update(body.mapToSupply(supplyId))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
.../java/org/lucoenergia/conluz/infrastructure/admin/supply/update/UpdateSupplyBodyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.lucoenergia.conluz.infrastructure.admin.supply.update; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.lucoenergia.conluz.domain.admin.supply.Supply; | ||
|
||
import java.util.UUID; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class UpdateSupplyBodyTest { | ||
|
||
@Test | ||
void testMapToSupply() { | ||
// Arrange | ||
UUID supplyId = UUID.randomUUID(); | ||
UpdateSupplyBody updateSupplyBody = new UpdateSupplyBody(); | ||
updateSupplyBody.setCode("code"); | ||
updateSupplyBody.setName("name"); | ||
updateSupplyBody.setAddress("address"); | ||
updateSupplyBody.setPartitionCoefficient(0.5f); | ||
updateSupplyBody.setValidDateFrom("2022-01-01"); | ||
updateSupplyBody.setDistributor("distributor"); | ||
updateSupplyBody.setDistributorCode("distributorCode"); | ||
updateSupplyBody.setPointType(6); | ||
updateSupplyBody.setShellyMac("shellyMac"); | ||
updateSupplyBody.setShellyId("shellyId"); | ||
updateSupplyBody.setShellyMqttPrefix("shellyMqttPrefix"); | ||
|
||
// Act | ||
Supply supply = updateSupplyBody.mapToSupply(supplyId); | ||
|
||
// Assert | ||
assertEquals(supplyId, supply.getId()); | ||
assertEquals("code", supply.getCode()); | ||
assertEquals("name", supply.getName()); | ||
assertEquals("address", supply.getAddress()); | ||
assertEquals(0.5f, supply.getPartitionCoefficient()); | ||
assertEquals("2022-01-01", supply.getValidDateFrom().toString()); | ||
assertEquals("distributor", supply.getDistributor()); | ||
assertEquals("distributorCode", supply.getDistributorCode()); | ||
assertEquals(6, supply.getPointType()); | ||
assertEquals("shellyMac", supply.getShellyMac()); | ||
assertEquals("shellyId", supply.getShellyId()); | ||
assertEquals("shellyMqttPrefix", supply.getShellyMqttPrefix()); | ||
} | ||
} |
Oops, something went wrong.