-
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.
Merge pull request #1 from moudjames23/feat/product-type
add possibility to create a product type and update readme
- Loading branch information
Showing
6 changed files
with
194 additions
and
41 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
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
11 changes: 11 additions & 0 deletions
11
src/main/java/io/github/moudjames23/scan2dojo/dto/requests/ProductTypeRequest.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,11 @@ | ||
package io.github.moudjames23.scan2dojo.dto.requests; | ||
|
||
public record ProductTypeRequest ( | ||
String name, | ||
|
||
String description, | ||
|
||
boolean criticalProduct, | ||
|
||
boolean keyProduct | ||
){} |
26 changes: 26 additions & 0 deletions
26
src/main/java/io/github/moudjames23/scan2dojo/dto/responses/ProductTypeResponse.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,26 @@ | ||
package io.github.moudjames23.scan2dojo.dto.responses; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import java.io.Serializable; | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public record ProductTypeResponse ( | ||
|
||
int id, | ||
String name, | ||
|
||
String description, | ||
|
||
@JsonProperty("critical_product") | ||
boolean criticalProduct, | ||
|
||
@JsonProperty("key_product") | ||
boolean keyProduct | ||
)implements ResponseMessage, Serializable { | ||
@Override | ||
public String success() { | ||
return "Product type created \uD83C\uDF89\uD83C\uDF89\uD83C\uDF89 \nId: " + this.id+ "\nName: " +this.name; | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
src/main/java/io/github/moudjames23/scan2dojo/http/ProductType.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,64 @@ | ||
package io.github.moudjames23.scan2dojo.http; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.github.moudjames23.scan2dojo.dto.Configuration; | ||
import io.github.moudjames23.scan2dojo.dto.requests.ProductTypeRequest; | ||
import io.github.moudjames23.scan2dojo.enums.HttpMethod; | ||
import okhttp3.Headers; | ||
import okhttp3.MediaType; | ||
import okhttp3.RequestBody; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class ProductType implements Request{ | ||
|
||
private final Configuration config; | ||
private final ProductTypeRequest productTypeRequest; | ||
|
||
public ProductType(Configuration config, ProductTypeRequest productRequest) { | ||
this.config = config; | ||
this.productTypeRequest = productRequest; | ||
} | ||
|
||
|
||
@Override | ||
public HttpMethod getMethod() { | ||
return HttpMethod.POST; | ||
} | ||
|
||
@Override | ||
public Headers getHeaders() { | ||
return new Headers.Builder() | ||
.add(getAuthorization(), BEARER.concat(this.config.getApiKey())) | ||
.add("Content-Type", APPLICATION_JSON) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public String getUri() { | ||
return config.getEndpoint().concat("/api/v2/product_types/"); | ||
} | ||
|
||
@Override | ||
public RequestBody getBody() { | ||
RequestBody requestBody; | ||
|
||
Map<String, Object> data = new HashMap<>(); | ||
|
||
data.put("name", productTypeRequest.name()); | ||
data.put("description", productTypeRequest.description()); | ||
data.put("critical_product", productTypeRequest.criticalProduct()); | ||
data.put("key_product", productTypeRequest.keyProduct()); | ||
|
||
|
||
try { | ||
String json = new ObjectMapper().writeValueAsString(data); | ||
requestBody = RequestBody.create(json, MediaType.parse(APPLICATION_JSON)); | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException("Les données fournies sont incorrectes"); | ||
} | ||
return requestBody; | ||
} | ||
} |
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