Skip to content

Commit

Permalink
[conluz-72] Implemented endpoint for updating supplies
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorKhan committed May 19, 2024
1 parent 15ffa84 commit cdb6b2c
Show file tree
Hide file tree
Showing 10 changed files with 615 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@

public class Supply {

private static final Integer DEFAULT_POINT_TYPE = 5;
private static final String DEFAULT_DISTRIBUTOR_CODE = DistributorCode.E_DISTRIBUCION;

@NotNull
@ValidUUID
private UUID id;
Expand Down Expand Up @@ -197,11 +194,11 @@ public String getDistributor() {
}

public String getDistributorCode() {
return distributorCode != null ? distributorCode : DEFAULT_DISTRIBUTOR_CODE;
return distributorCode;
}

public Integer getPointType() {
return pointType != null ? pointType : DEFAULT_POINT_TYPE;
return pointType;
}

public void setAddress(String address) {
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public Supply map(SupplyEntity entity) {
return new Supply.Builder()
.withId(entity.getId())
.withCode(entity.getCode())
.withName(entity.getName())
.withAddress(entity.getAddress())
.withPartitionCoefficient(entity.getPartitionCoefficient())
.withEnabled(entity.getEnabled())
.withName(entity.getName())
.withUser(userEntityMapper.map(entity.getUser()))

.withValidDateFrom(entity.getValidDateFrom())
Expand Down
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();
}
}
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)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@ public Supply update(Supply supply) {
currentSupply.setName(supply.getName());
currentSupply.setAddress(supply.getAddress());
currentSupply.setPartitionCoefficient(supply.getPartitionCoefficient());
currentSupply.setEnabled(supply.getEnabled());

currentSupply.setValidDateFrom(supply.getValidDateFrom());
currentSupply.setDistributor(supply.getDistributor());
currentSupply.setDistributorCode(supply.getDistributorCode());
currentSupply.setPointType(supply.getPointType());

currentSupply.setShellyMac(supply.getShellyMac());
currentSupply.setShellyId(supply.getShellyId());
currentSupply.setShellyMqttPrefix(supply.getShellyMqttPrefix());

return mapper.map(repository.save(currentSupply));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
import java.util.UUID;

@Repository
public class UpdateUserRepositoryImpl implements UpdateUserRepository {
public class UpdateUserRepositoryDatabase implements UpdateUserRepository {

private final UserRepository repository;
private final UserEntityMapper mapper;

public UpdateUserRepositoryImpl(UserRepository repository, UserEntityMapper mapper) {
public UpdateUserRepositoryDatabase(UserRepository repository, UserEntityMapper mapper) {
this.repository = repository;
this.mapper = mapper;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ public OffsetDateTime convertInstantToOffsetDateTime(Instant instant) {
return instant.atOffset(zonedDateTime.getOffset());
}

public LocalDate convertStringToLocalDate(String dateString) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
public static LocalDate convertStringToLocalDate(String dateString) {
return convertStringToLocalDate(dateString, "yyyy/MM/dd");
}

public static LocalDate convertStringToLocalDate(String dateString, String pattern) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
return LocalDate.parse(dateString, formatter);
}

Expand Down
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());
}
}
Loading

0 comments on commit cdb6b2c

Please sign in to comment.