Skip to content

Refactored DTOs to use Java records #285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:

- name: Build and push
id: docker_build
uses: docker/build-push-action@v5
uses: docker/build-push-action@v6
with:
push: true
platforms: linux/amd64,linux/arm64
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/re-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build and push
uses: docker/build-push-action@v5
uses: docker/build-push-action@v6
with:
context: .
push: true
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build and push
uses: docker/build-push-action@v5
uses: docker/build-push-action@v6
with:
context: .
push: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,21 @@ public ResponseEntity<Object> verify() {
@PostMapping(value = "/create")
public ResponseEntity<ResponseRelayDTO> loadAccount(@RequestBody RequestRelayDTO request) {
try {
accountService.createAccount(request.getValue());
accountService.createAccount(request.value());
return ResponseEntity.ok(ResponseRelayDTO.create(true));
} catch (Exception e) {
return ResponseEntity.status(500).body(ResponseRelayDTO.create(false).withMessage(e.getMessage()));
return ResponseEntity.status(500).body(ResponseRelayDTO.fail(e.getMessage()));
}
}

@Operation(summary = "Remove existing account from Switcher AC")
@PostMapping(value = "/remove")
public ResponseEntity<ResponseRelayDTO> removeAccount(@RequestBody RequestRelayDTO request) {
try {
accountService.deleteAccount(request.getValue());
accountService.deleteAccount(request.value());
return ResponseEntity.ok(ResponseRelayDTO.create(true));
} catch (Exception e) {
return ResponseEntity.status(500).body(ResponseRelayDTO.create(false).withMessage(e.getMessage()));
return ResponseEntity.status(500).body(ResponseRelayDTO.fail(e.getMessage()));
}
}

Expand All @@ -80,18 +80,18 @@ public ResponseEntity<ResponseRelayDTO> limiter(@RequestParam String value) {

return ResponseEntity.ok(validatorFactory.runValidator(request));
} catch (ResponseStatusException e) {
return ResponseEntity.status(e.getStatusCode()).body(ResponseRelayDTO.create(false).withMessage(e.getMessage()));
return ResponseEntity.status(e.getStatusCode()).body(ResponseRelayDTO.fail(e.getMessage()));
}
}

@Operation(summary = "Perform account validation given input value")
@PostMapping(value = "/validate")
public ResponseEntity<Object> validate(@RequestBody RequestRelayDTO request) {
try {
var featureRequest = gson.fromJson(request.getPayload(), FeaturePayload.class);
var featureRequest = gson.fromJson(request.payload(), FeaturePayload.class);
return ResponseEntity.ok(validatorService.execute(featureRequest));
} catch (ResponseStatusException e) {
return ResponseEntity.status(e.getStatusCode()).body(ResponseRelayDTO.create(false).withMessage(e.getMessage()));
return ResponseEntity.status(e.getStatusCode()).body(ResponseRelayDTO.fail(e.getMessage()));
}
}

Expand Down
23 changes: 7 additions & 16 deletions src/main/java/com/github/switcherapi/ac/model/GitHubDetail.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,12 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

import lombok.Builder;
import lombok.Data;

@JsonIgnoreProperties(ignoreUnknown = true)
@Data
@Builder
public class GitHubDetail {

private String id;

private String name;

private String login;

@JsonProperty("avatar_url")
private String avatarUrl;

public record GitHubDetail(
String id,
String name,
String login,
@JsonProperty("avatar_url")
String avatarUrl
) {
}
14 changes: 4 additions & 10 deletions src/main/java/com/github/switcherapi/ac/model/dto/AccountDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import lombok.Data;

@JsonInclude(Include.NON_NULL)
@Data
public class AccountDTO {

private String id;

private String adminId;

private PlanDTO plan;

public record AccountDTO(
String id,
String adminId,
PlanDTO plan) {
}
12 changes: 3 additions & 9 deletions src/main/java/com/github/switcherapi/ac/model/dto/AdminDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

import lombok.Data;

@JsonInclude(Include.NON_NULL)
@Data
public class AdminDTO {

private String id;

private String gitHubId;

public record AdminDTO(
String id,
String gitHubId) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,9 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

import lombok.Data;

@JsonInclude(Include.NON_NULL)
@Data
public class GitHubAuthDTO {

private AdminDTO admin;

private String token;

private String refreshToken;

public record GitHubAuthDTO(
AdminDTO admin,
String token,
String refreshToken) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

@Builder
public record Metadata(
@JsonProperty("rate_limit")
@SerializedName("rate_limit")
int rateLimit
) {
@JsonProperty("rate_limit")
@SerializedName("rate_limit")
int rateLimit) {
}
12 changes: 7 additions & 5 deletions src/main/java/com/github/switcherapi/ac/model/dto/PlanDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.github.switcherapi.ac.model.domain.Plan;
import lombok.Generated;
import com.github.switcherapi.ac.model.domain.PlanAttribute;

import java.util.List;

@Generated
@JsonInclude(Include.NON_NULL)
public class PlanDTO extends Plan {

public record PlanDTO(
String id,
String name,
List<PlanAttribute> attributes) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

import lombok.Data;

@JsonInclude(Include.NON_NULL)
@Data
public class RequestRelayDTO {

private String value;

private String payload;

public record RequestRelayDTO(
String value,
String payload) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,23 @@

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Generated;
import lombok.NoArgsConstructor;

@Generated
@JsonInclude(Include.NON_NULL)
@Data
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ResponseRelayDTO {

private boolean result;

private String message;

private Metadata metadata;
public record ResponseRelayDTO(
boolean result,
String message,
Metadata metadata) {

public static ResponseRelayDTO create(boolean result) {
var response = new ResponseRelayDTO();
response.setResult(result);
return response;
return new ResponseRelayDTO(result, null, null);
}

public ResponseRelayDTO withMessage(String message) {
this.message = message;
return this;
public static ResponseRelayDTO fail(String message) {
return new ResponseRelayDTO(false, message, null);
}

public ResponseRelayDTO withMetadata(Metadata metadata) {
this.metadata = metadata;
return this;
public static ResponseRelayDTO success(Metadata metadata) {
return new ResponseRelayDTO(true, null, metadata);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@
import com.github.switcherapi.ac.model.dto.AccountDTO;
import com.github.switcherapi.ac.model.dto.PlanDTO;
import lombok.AccessLevel;
import lombok.Generated;
import lombok.NoArgsConstructor;

@Generated
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class AccountMapper {

public static AccountDTO createCopy(Account from) {
var to = DefaultMapper.createCopy(from, AccountDTO.class);
to.setPlan(DefaultMapper.createCopy(from.getPlan(), PlanDTO.class));
return to;
final var plan = new PlanDTO(from.getPlan().getId(), from.getPlan().getName(), from.getPlan().getAttributes());
return new AccountDTO(from.getId(), from.getAdminId(), plan);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.switcherapi.ac.model.mapper;

import com.github.switcherapi.client.exception.SwitcherException;
import lombok.Generated;
import lombok.experimental.UtilityClass;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
Expand All @@ -12,7 +11,6 @@
import java.util.ArrayList;
import java.util.Arrays;

@Generated
@UtilityClass
public class DefaultMapper {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,16 @@
import com.github.switcherapi.ac.model.domain.Admin;
import com.github.switcherapi.ac.model.dto.AdminDTO;
import com.github.switcherapi.ac.model.dto.GitHubAuthDTO;

import lombok.AccessLevel;
import lombok.Generated;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.tuple.Pair;

@Generated
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class GitHubAuthMapper {

public static GitHubAuthDTO createCopy(Admin from, Pair<String, String> tokens) {
var to = new GitHubAuthDTO();
to.setToken(tokens.getLeft());
to.setRefreshToken(tokens.getRight());
to.setAdmin(DefaultMapper.createCopy(from, AdminDTO.class));
return to;
final var admin = new AdminDTO(from.getId(), from.getGitHubId());
return new GitHubAuthDTO(admin, tokens.getLeft(), tokens.getRight());
}

}
Loading