-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
80 additions
and
35 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
...java/ai/dragon/controller/api/backendapi/repository/AbstractCrudBackendApiController.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 ai.dragon.controller.api.backendapi.repository; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.Field; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.util.ReflectionUtils; | ||
import org.springframework.web.server.ResponseStatusException; | ||
|
||
import ai.dragon.entity.IAbstractEntity; | ||
import ai.dragon.entity.SiloEntity; | ||
import ai.dragon.repository.AbstractRepository; | ||
|
||
abstract class AbstractCrudBackendApiController<T extends IAbstractEntity> { | ||
public T update(String uuid, Map<String, Object> fields, AbstractRepository<T> repository) { | ||
T entityToUpdate = repository.getByUuid(uuid); | ||
if (entityToUpdate == null) { | ||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Entity not found"); | ||
} | ||
|
||
fields.forEach((k, v) -> { | ||
Field field = ReflectionUtils.findField(SiloEntity.class, k); | ||
if (field != null) { | ||
field.setAccessible(true); | ||
ReflectionUtils.setField(field, entityToUpdate, v); | ||
} | ||
}); | ||
|
||
if (!uuid.equals(entityToUpdate.getUuid().toString())) { | ||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "UUID must not be changed."); | ||
} | ||
|
||
repository.save(entityToUpdate); | ||
|
||
return entityToUpdate; | ||
} | ||
|
||
public List<T> list(AbstractRepository<T> repository) { | ||
return repository.find().toList(); | ||
} | ||
|
||
public T create(AbstractRepository<T> repository) throws Exception { | ||
Class<T> clazz = repository.getGenericSuperclass(); | ||
Constructor<T> cons = clazz.getConstructor(); | ||
T entity = cons.newInstance(); | ||
repository.save(entity); | ||
return entity; | ||
} | ||
|
||
public T get(String uuid, AbstractRepository<T> repository) { | ||
return Optional.ofNullable(repository.getByUuid(uuid)) | ||
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Entity not found.")); | ||
} | ||
|
||
public void delete(String uuid, AbstractRepository<T> repository) { | ||
if (!repository.exists(uuid)) { | ||
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Entity not found."); | ||
} | ||
repository.delete(uuid); | ||
} | ||
} |
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
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