Skip to content

Commit

Permalink
[conluz-54] Renamed current id to code and added a new field id of ty… (
Browse files Browse the repository at this point in the history
#55)

* [conluz-54] Renamed current id to code and added a new field id of type UUID

* [conluz-54] Adapted OAS documentation to the adition of the new code field
  • Loading branch information
viktorKhan authored Mar 8, 2024
1 parent 7f37f07 commit 61548ce
Show file tree
Hide file tree
Showing 36 changed files with 317 additions and 111 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
package org.lucoenergia.conluz.domain.admin.supply;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import org.lucoenergia.conluz.domain.admin.user.User;
import org.lucoenergia.conluz.infrastructure.shared.uuid.ValidUUID;

import java.time.LocalDate;
import java.util.UUID;

public class Supply {

private final String id;
@NotNull
@ValidUUID
private final UUID id;
@NotBlank
private final String code;
@NotNull
private User user;
private final String name;
@NotBlank
private final String address;
@NotNull
private final Float partitionCoefficient;
@NotNull
private final Boolean enabled;
private final LocalDate validDateFrom;
private final String distributor;
Expand All @@ -19,6 +31,7 @@ public class Supply {

private Supply(Builder builder) {
this.id = builder.id;
this.code = builder.code;
this.user = builder.user;
this.name = builder.name;
this.address = builder.address;
Expand All @@ -31,7 +44,8 @@ private Supply(Builder builder) {
}

public static class Builder {
private String id;
private UUID id;
private String code;
private User user;
private String name;
private String address;
Expand All @@ -42,11 +56,16 @@ public static class Builder {
private String distributorCode;
private String pointType;

public Builder withId(String id) {
public Builder withId(UUID id) {
this.id = id;
return this;
}

public Builder withCode(String code) {
this.code = code;
return this;
}

public Builder withUser(User user) {
this.user = user;
return this;
Expand Down Expand Up @@ -97,9 +116,12 @@ public Supply build() {
}
}

public String getId() {
public UUID getId() {
return id;
}
public String getCode() {
return code;
}

public User getUser() {
return user;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.lucoenergia.conluz.domain.admin.supply;

import org.lucoenergia.conluz.domain.shared.SupplyCode;

public class SupplyAlreadyExistsException extends RuntimeException {

private final SupplyCode code;

public SupplyAlreadyExistsException(SupplyCode code) {
this.code = code;
}

public SupplyCode getUserId() {
return code;
}
}
14 changes: 14 additions & 0 deletions src/main/java/org/lucoenergia/conluz/domain/shared/SupplyCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.lucoenergia.conluz.domain.shared;

public class SupplyCode {

private final String code;

public SupplyCode(String code) {
this.code = code;
}

public String getCode() {
return code;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package org.lucoenergia.conluz.domain.shared;

import java.util.UUID;

public class SupplyId {

private final String id;
private final UUID id;

public SupplyId(String id) {
public SupplyId(UUID id) {
this.id = id;
}

public String getId() {
public UUID getId() {
return id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
import jakarta.persistence.ManyToOne;
import org.lucoenergia.conluz.infrastructure.admin.user.UserEntity;

import java.util.UUID;

@Entity(name = "supplies")
public class SupplyEntity {

@Id
private String id;
private UUID id;
private String code;
@ManyToOne(fetch = FetchType.LAZY)
private UserEntity user;
private String name;
Expand All @@ -22,22 +25,91 @@ public SupplyEntity() {
enabled = true;
}

public SupplyEntity(String id, String name, String address, Float partitionCoefficient, Boolean enabled) {
this.id = id;
this.name = name;
this.address = address;
this.partitionCoefficient = partitionCoefficient;
this.enabled = enabled;
public static class Builder {
private UUID id;
private String code;
private UserEntity user;
private String name;
private String address;
private Float partitionCoefficient;
private Boolean enabled;

public Builder withId(UUID id) {
this.id = id;
return this;
}

public Builder withCode(String code) {
this.code = code;
return this;
}

public Builder withUser(UserEntity user) {
this.user = user;
return this;
}

public Builder withName(String name) {
this.name = name;
return this;
}

public Builder withAddress(String address) {
this.address = address;
return this;
}

public Builder withPartitionCoefficient(Float partitionCoefficient) {
this.partitionCoefficient = partitionCoefficient;
return this;
}

public Builder withEnabled(Boolean enabled) {
this.enabled = enabled;
return this;
}

public SupplyEntity build() {
SupplyEntity entity = new SupplyEntity();
entity.id = this.id;
entity.code = this.code;
entity.user = this.user;
entity.name = this.name;
entity.address = this.address;
entity.partitionCoefficient = this.partitionCoefficient;
entity.enabled = this.enabled;
return entity;
}
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof SupplyEntity )) return false;
return code != null && code.equals(((SupplyEntity) o).getCode());
}

@Override
public int hashCode() {
return getClass().hashCode();
}

public String getId() {
public UUID getId() {
return id;
}

public void setId(String id) {
public void setId(UUID id) {
this.id = id;
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getName() {
return name;
}
Expand All @@ -50,6 +122,7 @@ public String getAddress() {
return address;
}


public void setAddress(String address) {
this.address = address;
}
Expand Down Expand Up @@ -77,16 +150,4 @@ public UserEntity getUser() {
public void setUser(UserEntity user) {
this.user = user;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof SupplyEntity )) return false;
return id != null && id.equals(((SupplyEntity) o).getId());
}

@Override
public int hashCode() {
return getClass().hashCode();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class SupplyEntityMapper extends BaseMapper<SupplyEntity, Supply> {
public Supply map(SupplyEntity entity) {
return new Supply.Builder()
.withId(entity.getId())
.withCode(entity.getCode())
.withAddress(entity.getAddress())
.withPartitionCoefficient(entity.getPartitionCoefficient())
.withEnabled(entity.getEnabled())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,12 @@

import org.springframework.data.jpa.repository.JpaRepository;

public interface SupplyRepository extends JpaRepository<SupplyEntity, String> {
import java.util.Optional;
import java.util.UUID;

public interface SupplyRepository extends JpaRepository<SupplyEntity, UUID> {

Optional<SupplyEntity> findByCode(String code);

int countByCode(String code);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import org.lucoenergia.conluz.domain.admin.supply.Supply;
import org.lucoenergia.conluz.infrastructure.admin.user.UserResponse;

import java.util.UUID;

public class SupplyResponse {

private final String id;
private final UUID id;
private final String code;
private final UserResponse user;
private final String name;
private final String address;
Expand All @@ -14,16 +17,20 @@ public class SupplyResponse {

public SupplyResponse(Supply supply) {
this.id = supply.getId();
this.code = supply.getCode();
this.name = supply.getName();
this.address = supply.getAddress();
this.partitionCoefficient = supply.getPartitionCoefficient();
this.enabled = supply.getEnabled();
this.user = supply.getUser() != null ? new UserResponse(supply.getUser()) : null;
}

public String getId() {
public UUID getId() {
return id;
}
public String getCode() {
return code;
}

public UserResponse getUser() {
return user;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class CreateSupplyAssembler implements Assembler<CreateSupplyBody, Supply
@Override
public Supply assemble(CreateSupplyBody body) {
return new Supply.Builder()
.withId(body.getId())
.withCode(body.getCode())
.withAddress(body.getAddress())
.withPartitionCoefficient(body.getPartitionCoefficient())
.withEnabled(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
public class CreateSupplyBody {

@NotEmpty
private String id;
private String code;
@NotEmpty
private UUID userId;
@NotEmpty
private String address;
@Positive
private Float partitionCoefficient;

public String getId() {
return id;
public String getCode() {
return code;
}

public void setId(String id) {
this.id = id;
public void setCode(String code) {
this.code = code;
}

public String getAddress() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public CreateSupplyController(CreateSupplyAssembler assembler, CreateSupplyServi
examples = @ExampleObject(
value = """
{
"id":"ES0033333333333333AA0A",
"id":"785de77b-8c22-4d2f-9d12-f172113f9aa4",
"code":"ES0033333333333333AA0A",
"user":{
"id":"e7ab39cd-9250-40a9-b829-f11f65aae27d",
"personalId":"rAtjrSXAU",
Expand Down
Loading

0 comments on commit 61548ce

Please sign in to comment.