Skip to content

Commit

Permalink
fix updateByid bug
Browse files Browse the repository at this point in the history
  • Loading branch information
PotatoWhite committed Jun 3, 2021
2 parents 08896b9 + 24d6095 commit 268e446
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 68 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jar.enabled(true)

allprojects {
group 'io.easywalk'
version '0.0.3.RELEASE'
version '0.0.4.RELEASE'
}

subprojects {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ private Throwable makeException(SimplyErrorResponse errorbody) {
Class exception = Class.forName(errorbody.getOriginalExceptionType());
Constructor[] constructors = exception.getDeclaredConstructors();
var exceptionClass = Arrays.stream(constructors)
.filter(constructor -> constructor.getParameterTypes().length == 1 && constructor.getParameterTypes()[0].equals(String.class))
.map(newException -> {
try {
return newException.newInstance(errorbody.getDescription());
} catch (Exception e) {
log.debug(e.getMessage());
}
return null;
})
.findFirst();
.filter(constructor -> constructor.getParameterTypes().length == 1 && constructor.getParameterTypes()[0].equals(String.class))
.map(newException -> {
try {
return newException.newInstance(errorbody.getDescription());
} catch (Exception e) {
log.debug(e.getMessage());
}
return null;
})
.findFirst();

return (Throwable) exceptionClass.orElseGet(() -> new Exception(errorbody.getDescription()));
} catch (ClassNotFoundException e) {
Expand All @@ -55,70 +55,70 @@ private Throwable makeException(SimplyErrorResponse errorbody) {
@Override
public T create(T createForm) throws Throwable {
return client.post()
.body(Mono.just(createForm), createForm.getClass())
.retrieve()
.onStatus(status -> !status.equals(HttpStatus.CREATED), clientResponse -> clientResponse.bodyToMono(SimplyErrorResponse.class)
.flatMap(errorbody -> Mono.error(makeException(errorbody))))
.bodyToMono(entityTypeClass).block();
.body(Mono.just(createForm), createForm.getClass())
.retrieve()
.onStatus(status -> !status.equals(HttpStatus.CREATED), clientResponse -> clientResponse.bodyToMono(SimplyErrorResponse.class)
.flatMap(errorbody -> Mono.error(makeException(errorbody))))
.bodyToMono(entityTypeClass).block();
}


public T replaceById(ID id, T replace) throws Throwable {
String path = "/" + id.toString();
return client.put()
.uri(path)
.body(Mono.just(replace), replace.getClass())
.retrieve()
.onStatus(status -> !status.equals(HttpStatus.OK), clientResponse -> clientResponse.bodyToMono(SimplyErrorResponse.class)
.flatMap(errorbody -> Mono.error(makeException(errorbody))))
.bodyToMono(entityTypeClass).block();
.uri(path)
.body(Mono.just(replace), replace.getClass())
.retrieve()
.onStatus(status -> !status.equals(HttpStatus.OK), clientResponse -> clientResponse.bodyToMono(SimplyErrorResponse.class)
.flatMap(errorbody -> Mono.error(makeException(errorbody))))
.bodyToMono(entityTypeClass).block();
}

@Override
public T updateById(ID id, Map<String, Object> fields) throws Throwable {
String path = "/" + id.toString();

return client.patch()
.uri(path)
.body(Mono.just(fields), fields.getClass())
.retrieve()
.onStatus(status -> !status.equals(HttpStatus.OK), clientResponse -> clientResponse.bodyToMono(SimplyErrorResponse.class)
.flatMap(errorbody -> Mono.error(makeException(errorbody))))
.bodyToMono(entityTypeClass).block();
.uri(path)
.body(Mono.just(fields), fields.getClass())
.retrieve()
.onStatus(status -> !status.equals(HttpStatus.OK), clientResponse -> clientResponse.bodyToMono(SimplyErrorResponse.class)
.flatMap(errorbody -> Mono.error(makeException(errorbody))))
.bodyToMono(entityTypeClass).block();
}

@Override
public T get(ID id) throws Throwable {
String path = "/" + id.toString();
return client.get()
.uri(path)
.retrieve()
.onStatus(status -> !status.equals(HttpStatus.OK), clientResponse -> clientResponse.bodyToMono(SimplyErrorResponse.class)
.flatMap(errorbody -> Mono.error(makeException(errorbody))))
.bodyToMono(entityTypeClass).block();
.uri(path)
.retrieve()
.onStatus(status -> !status.equals(HttpStatus.OK), clientResponse -> clientResponse.bodyToMono(SimplyErrorResponse.class)
.flatMap(errorbody -> Mono.error(makeException(errorbody))))
.bodyToMono(entityTypeClass).block();
}

@Override
public List<T> getAll() throws Throwable {
return client.get()
.retrieve()
.onStatus(status -> !status.equals(HttpStatus.OK), clientResponse -> clientResponse.bodyToMono(SimplyErrorResponse.class)
.flatMap(errorbody -> Mono.error(makeException(errorbody))))
.bodyToFlux(entityTypeClass)
.collect(Collectors.toList())
.block();
.retrieve()
.onStatus(status -> !status.equals(HttpStatus.OK), clientResponse -> clientResponse.bodyToMono(SimplyErrorResponse.class)
.flatMap(errorbody -> Mono.error(makeException(errorbody))))
.bodyToFlux(entityTypeClass)
.collect(Collectors.toList())
.block();
}

@Override
public void deleteById(ID id) throws Throwable {
String path = "/" + id.toString();
client.delete()
.uri(path)
.retrieve()
.onStatus(status -> !status.equals(HttpStatus.NO_CONTENT), clientResponse -> clientResponse.bodyToMono(SimplyErrorResponse.class)
.flatMap(errorbody -> Mono.error(makeException(errorbody))))
.bodyToMono(Void.class)
.block();
.uri(path)
.retrieve()
.onStatus(status -> !status.equals(HttpStatus.NO_CONTENT), clientResponse -> clientResponse.bodyToMono(SimplyErrorResponse.class)
.flatMap(errorbody -> Mono.error(makeException(errorbody))))
.bodyToMono(Void.class)
.block();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
package io.easywalk.simply.eventable.kafka;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import io.easywalk.simply.specification.serviceable.annotations.SimplyEntity;
import lombok.*;

import javax.annotation.PostConstruct;
import java.lang.annotation.Annotation;
import java.util.LinkedHashMap;
import java.util.Objects;

@Getter
@Setter
Expand All @@ -27,4 +21,5 @@ public class SimplyEventableMessage<T> {
@PostConstruct
private void init() {
payloadType = payload.getClass().getName();
}}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ private void createTopics() {
ConfigurableBeanFactory factory = (ConfigurableBeanFactory) beanFactory;

subTypesOf.stream()
.forEach(item -> {
this.topics.put(item, item.getName());
SimplyProducerService annotation = item.getAnnotation(SimplyProducerService.class);
NewTopic newTopic = new NewTopic(annotation.value(), numPartitions, numReplicas);
factory.registerSingleton(item.getName() + "topic", newTopic);
log.info("Topic created {} : {} : {}", item.getName(), numPartitions, numReplicas);
});
.forEach(item -> {
this.topics.put(item, item.getName());
SimplyProducerService annotation = item.getAnnotation(SimplyProducerService.class);
NewTopic newTopic = new NewTopic(annotation.value(), numPartitions, numReplicas);
factory.registerSingleton(item.getName() + "topic", newTopic);
log.info("Topic created {} : {} : {}", item.getName(), numPartitions, numReplicas);
});
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private static Class<?> getClass(Type type) {
Class<?> componentClass = getClass(componentType);
if (componentClass != null) {
return Array.newInstance(componentClass, 0)
.getClass();
.getClass();
} else {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import io.easywalk.simply.specification.eventable.annotations.SimplyProducer;
import io.easywalk.simply.specification.eventable.annotations.SimplyProducerId;
import io.easywalk.simply.specification.serviceable.annotations.SimplyEntity;
import io.easywalk.simply.utils.GsonTools;
import org.springframework.beans.BeanUtils;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.jpa.repository.JpaRepository;

import javax.persistence.EntityExistsException;
import javax.persistence.EntityNotFoundException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
Expand All @@ -36,14 +36,26 @@ public T create(T entity) throws EntityExistsException, DataIntegrityViolationEx
@Override
public T get(ID id) throws NoSuchElementException {
return repository.findById(id)
.orElseThrow(() -> new NoSuchElementException("entityid=" + id));
.orElseThrow(() -> new NoSuchElementException("entityid=" + id));
}

// update
@SimplyProducer("UPDATE")
@Override
public T updateById(@SimplyProducerId ID id, Map<String, Object> fields) throws Throwable {
return repository.save(GsonTools.merge(get(id), fields));

T target = get(id);

fields.forEach((key, value) -> {
try {
// fields 존재
target.getClass().getDeclaredMethod("set" + key.substring(0, 1).toUpperCase() + key.substring(1), value.getClass()).invoke(target, value);
} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
e.printStackTrace();
}
});

return repository.save(target);
}

// delete
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
public class GsonTools {

public static void extendJsonObject(JsonObject destinationObject, ConflictStrategy conflictResolutionStrategy, ArrayStrategy arrayStrategy, JsonObject... objs)
throws JsonObjectExtensionConflictException {
throws JsonObjectExtensionConflictException {
for (JsonObject obj : objs) {
extendJsonObject(destinationObject, obj, conflictResolutionStrategy, arrayStrategy);
}
}

private static void extendJsonObject(JsonObject leftObj, JsonObject rightObj, ConflictStrategy conflictStrategy, ArrayStrategy arrayStrategy)
throws JsonObjectExtensionConflictException {
throws JsonObjectExtensionConflictException {
for (Map.Entry<String, JsonElement> rightEntry : rightObj.entrySet()) {
String rightKey = rightEntry.getKey();
JsonElement rightVal = rightEntry.getValue();
Expand Down Expand Up @@ -50,7 +50,7 @@ private static void extendJsonObject(JsonObject leftObj, JsonObject rightObj, Co
}

private static void handleMergeConflict(String key, JsonObject leftObj, JsonElement leftVal, JsonElement rightVal, ConflictStrategy conflictStrategy)
throws JsonObjectExtensionConflictException {
throws JsonObjectExtensionConflictException {
{
switch (conflictStrategy) {
case PREFER_FIRST_OBJ:
Expand All @@ -76,9 +76,9 @@ private static void handleMergeConflict(String key, JsonObject leftObj, JsonElem
public static <T> T merge(T obj, Map<String, Object> fields) throws JsonObjectExtensionConflictException {
Gson gson = new GsonBuilder().create();
JsonObject target = gson.toJsonTree(obj)
.getAsJsonObject();
.getAsJsonObject();
JsonObject patch = gson.toJsonTree(fields)
.getAsJsonObject();
.getAsJsonObject();
extendJsonObject(target, PREFER_SECOND_OBJ, REPLACE, patch);

return (T) gson.fromJson(target.toString(), obj.getClass());
Expand Down

0 comments on commit 268e446

Please sign in to comment.